summaryrefslogtreecommitdiff
path: root/docs/modules/theme/pages/create-theme.adoc
blob: 823fc3f8e7769d4c875602fe86dbd958aa6d7f87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
= Create a Theme
:page-aliases: extend-theme.adoc
:description: Create a PDF theme by extending a built-in theme or starting one from scratch.

There are two ways to create a PDF theme.
One way is to extend one of the built-in themes and gradually layer your customizations over it.
The other way is to develop a theme from scratch.
There are advantages to both approaches.

[#extend-default]
== Extend the default theme

One theme can extend another theme using the `extends` key at the top of the theme file.
When extending a theme, you only have to define the keys you want to override from the theme you're extending, which is loaded prior to loading the keys in your theme.
Thus, the quickest and simplest way to create a theme is to extend the default theme.

Let's try this out by drastically modifying the base font color.
That way, you can clearly tell when Asciidoctor PDF is using your theme.
Create a new file named [.path]_my-theme.yml_ and populate it with the following code:

.my-theme.yml
[,yaml]
----
extends: default
base:
  font-color: #FF0000
----

This approach is useful if you want to modify the behavior of the converter or the running content without otherwise changing the style.
For example, here's a theme that customizes the text in the running footer.

[,yaml]
----
extends: default
footer:
  recto:
    right:
      content: A recto page
  verso:
    left:
      content: A verso page
----

Learn more about how to customize the running content on the xref:add-running-content.adoc[] page.

== How the extends key works

The `extends` key accepts either a single value or an array of values.
Each value is interpreted as either a theme name or filename.

If the value matches the name of a built-in theme (e.g., `default`), that theme is used.
If the value is an absolute path, that theme file is used.
If the value begins with `./`, the value is resolved to a theme file relative to the current theme file.
Otherwise, the value is resolved just like the value of the `pdf-theme` attribute.
In this case, a relative path is resolved starting from the value of the `pdf-themesdir` attribute.

[#load-theme-more-than-once]
If the same theme appears multiple times in the theme hierarchy, it will only be loaded once by default.
You can force the theme to be loaded, even if it has already been loaded, by adding the `!important` keyword at the end of the value offset by a space.

Initially, the theme starts out empty.
Then, the theme file(s) referenced by the `extends` key are loaded in order.
Finally, the keys in the current file are loaded.
Each time a theme is loaded, the flattened keys are overlaid onto the keys from the previous theme.

If you don't want to extend any theme (not even the base theme), omit the `extends` key or assign the value `~` to the `extends` key.

== Extend the base theme

If you're trying to carefully tailor the appearance of the PDF, you might find that the default theme gets in the way too much.
In this case, it might be better to extend the base theme.

The base theme provides rudimentary styling so that the visual hierarchy and block and inline elements of the content are honored, but does very little to embellish it.

WARNING: If you extend the base theme or create a theme from scratch, we strongly recommend defining TrueType fonts and specifying them in the `base` and `codespan` categories.
Otherwise, Asciidoctor PDF will use built-in AFM fonts, which can result in missing functionality and warnings.

Here's an example of a theme that extends the base theme:

.my-theme.yml
[,yaml]
----
extends: base
page:
  layout: portrait
  margin: [0.75in, 1in, 0.75in, 1in]
  size: Letter
base:
  font-color: #333333
  font-family: Times-Roman
  font-size: 12
  line-height-length: 17
  line-height: $base-line-height-length / $base-font-size
role:
  removed:
    font-style: italic
    text-decoration: line-through
    text-decoration-color: #FF0000
heading:
  font-color: #262626
  font-size: 17
  font-style: bold
  line-height: 1.2
  margin-bottom: 10
link:
  font-color: #002FA7
list:
  indent: $base-font-size * 1.5
----

If you want to go even more barebones, then you can set the `extends` key to `~` (or omit it) so the converter does not load any theme before your own.

.my-theme.yml
[,yaml]
----
extends: ~
#...
----

If you do this, you should consult the {url-repo-root}/data/themes/base-theme.yml[base theme] to discover which keys you'll need to set to support the visual hierarchy and core block and inline elements.
You can also find the location of the [.path]_data/themes_ directory on your local disk by running the following command:

 $ gem contents asciidoctor-pdf --show-install-dir

////
[TIP]
====
Instead of creating a theme from scratch, another option is to download the {url-repo-root}/data/themes/default-theme.yml[default-theme.yml^] file from the source repository.
Save the file using a unique name (e.g., _custom-theme.yml_) and start hacking on it.

Alternatively, you can snag the file from your local installation using the following command:

 $ ASCIIDOCTOR_PDF_DIR=`gem contents asciidoctor-pdf --show-install-dir`;\
   cp "$ASCIIDOCTOR_PDF_DIR/data/themes/default-theme.yml" custom-theme.yml
====
////