-
Notifications
You must be signed in to change notification settings - Fork 17
/
build.jl
410 lines (364 loc) · 11.9 KB
/
build.jl
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
const crossref_bin = string(pandoc_crossref_path)::String
const crossref = "--filter=$crossref_bin"
const include_lua_filter = joinpath(PROJECT_ROOT, "src", "include-codeblocks.lua")
const include_files = "--lua-filter=$include_lua_filter"
const citeproc = "--citeproc"
function pandoc_file(filename)
user_path = joinpath("pandoc", filename)
fallback_path = joinpath(PROJECT_ROOT, "defaults", filename)
isfile(user_path) ? user_path : fallback_path
end
function csl()
csl_path = pandoc_file("style.csl")
csl = "--csl=$csl_path"
end
extra_args = [
"--number-sections",
"--top-level-division=chapter"
]
function inputs(project)
H = config(project, "homepage_contents")::String
C = config(project, "contents")::Vector{String}
names = [H; C]
[joinpath("contents", "$name.md") for name in names]
end
"""
copy_extra_directory(dir)
Copy an extra directory such as "images" into build.
"""
function copy_extra_directory(dir)
if !isdir(dir)
error("Couldn't find `$(dir)` even though it was listed in `extra_directories`")
end
from = dir
to = joinpath(BUILD_DIR, dir)
cp(from, to; force=true)
end
"""
copy_extra_directories(project)
Copy the extra directories defined for `project`.
"""
function copy_extra_directories(project)
extra_directories = config(project, "extra_directories")::Vector
copy_extra_directory.(extra_directories)
end
function call_pandoc(args)::Tuple{Base.Process, String}
pandoc() do pandoc_bin
cmd = `$pandoc_bin $args`
stdout = IOBuffer()
p = run(pipeline(cmd; stdout))
out = String(take!(stdout))::String
return (p, out)
end
end
function copy_css()
filename = "style.css"
css_path = pandoc_file(filename)
cp(css_path, joinpath(BUILD_DIR, filename); force=true)
end
@memoize function is_mousetrap_enabled()::Bool
meta = default_metadata()::Dict
if "mousetrap" in keys(meta)
meta["mousetrap"]::Bool
else
false
end
end
@memoize function copy_mousetrap()
if is_mousetrap_enabled()
filename = "mousetrap.min.js"
from = pandoc_file(filename)
cp(from, joinpath(BUILD_DIR, filename); force=true)
end
end
@memoize function copy_juliamono()
filename = "JuliaMono-Regular.woff2"
from_path = joinpath(JULIAMONO_PATH, "webfonts", filename)
cp(from_path, joinpath(BUILD_DIR, filename); force=true)
end
function codeblock2output(s::AbstractString)
expr = s
# Hacky way to ignore code blocks with four spaces.
# Cleaner would be to improve the regex.
if endswith(s, "\n ```\n")
return s
end
expr = strip(expr)
# Remove start and end of code block.
expr = expr[7:end-4]
expr = strip(expr)
output_path = escape_expr(expr)
if isfile(output_path)
output = read(output_path, String)
# The indentation of the first line is already handled somewhere else
# (probably by the regex).
output = lstrip(output)
return output * '\n'
else
msg = """
Cannot find file at $output_path for $expr.
Did you run `gen()` when having loaded your module?
"""
return code_block(msg)
end
end
function inlinecodeblock2output(s::AbstractString)
expr = strip(s)
# Remove _jl prefix
expr = expr[4:end-1]
expr = strip(expr)
output_path = escape_expr(expr)
if isfile(output_path)
output = read(output_path, String)
output = strip(output)
return ' ' * output
else
msg = """
Cannot find file at $output_path for $expr.
Did you run `gen()` when having loaded your module?
"""
return code_block(msg)
end
end
"""
embed_output(text::String)
In a Markdown string containing `jl` code blocks, embed the output from the output path.
"""
function embed_output(text::String)
text = replace(text, CODEBLOCK_PATTERN => codeblock2output)
text = replace(text, INLINE_CODEBLOCK_PATTERN => inlinecodeblock2output)
return text
end
"""
write_input_markdown(project; skip_index=false)::String
Combine all the `contents/` files and embed the outputs into one Markdown file.
Return the path of the Markdown file.
"""
function write_input_markdown(project; skip_index=false)::String
files = inputs(project)
if skip_index
files = files[2:end]
end
texts = read.(files, String)
texts = embed_output.(texts)
text = join(texts, '\n')
dir = Books.GENERATED_DIR
if !isdir(dir)
@warn "$(dir) directory doesn't exist. Did you run `gen()`?"
mkpath(dir)
end
markdown_path = joinpath(dir, "input.md")
write(markdown_path, text)
return markdown_path
end
function verify_cross_references(h)
# For example, "<strong>¿sec:about?</strong>"
if contains(h, "<strong>¿")
rx = r"<strong>¿([^<]*)\?</strong>"
matches = collect(eachmatch(rx, h))
refs = ["- " * m[1] for m in matches]
missing_crossrefs = join(refs, "\n")
error("Output contains undefined cross-references:\n\n$missing_crossrefs\n")
end
end
function _pandoc_html(project::AbstractString, url_prefix::AbstractString; fail_on_error=false)
input_path = write_input_markdown(project)
copy_extra_directories(project)
html_template_path = pandoc_file("template.html")
template = "--template=$html_template_path"
output_filename = joinpath(BUILD_DIR, "index.html")
output = "--output=$output_filename"
metadata_path = combined_metadata_path(project)
metadata = "--metadata-file=$metadata_path"
copy_css()
copy_mousetrap()
copy_juliamono()
args = String[
input_path;
crossref;
citeproc;
"--mathjax";
# Using highlight.js instead of the Pandoc built-in highlighter.
"--no-highlight";
csl();
metadata;
template;
extra_args;
# The url_prefix is either "" or startswith '/'.
"--variable=url-prefix:$url_prefix";
# output
]
_, out = call_pandoc(args)
if fail_on_error
verify_cross_references(out)
end
return out
end
"""
ci_url_prefix(project)
Return the url prefix when `is_ci() == true`.
# Example
```jldoctest
julia> docs_dir = joinpath(pkgdir(Books), "docs");
julia> cd(docs_dir) do
Books.ci_url_prefix("default")
end
""
julia> cd(docs_dir) do
Books.ci_url_prefix("test")
end
"/Example.jl"
```
"""
function ci_url_prefix(project)
user_setting = config(project, "online_url_prefix")
if !startswith(user_setting, '/') && user_setting != ""
user_setting = '/' * user_setting
end
return user_setting
end
@memoize function highlight(url_prefix)
highlight_dir = joinpath(Artifacts.artifact"Highlight", "cdn-release-11.4.0")
highlight_name = "highlight.min.js"
highlight_path = joinpath(highlight_dir, "build", highlight_name)
cp(highlight_path, joinpath(BUILD_DIR, highlight_name); force=true)
julia_highlight_name = "julia.min.js"
julia_highlight_path = joinpath(highlight_dir, "build", "languages", julia_highlight_name)
cp(julia_highlight_path, joinpath(BUILD_DIR, julia_highlight_name); force=true)
style_name = "github.min.css"
style_path = joinpath(highlight_dir, "build", "styles", style_name)
cp(style_path, joinpath(BUILD_DIR, style_name); force=true)
# Don't add `url_prefix` to the stylesheet link. It will be handled by `fix_links`.
"""
<link rel="stylesheet" href="/$style_name">
<script src="$url_prefix/$highlight_name"></script>
<script src="$url_prefix/$julia_highlight_name"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll('pre').forEach((el) => {
hljs.highlightElement(el);
});
});
</script>
"""
end
function html(; project="default", extra_head="", fail_on_error=false, build_sitemap=false)
copy_extra_directories(project)
url_prefix = is_ci() ? ci_url_prefix(project)::String : ""
c = config(project, "contents")
if config(project, "highlight")::Bool
extra_head = extra_head * highlight(url_prefix)
end
h = _pandoc_html(project, url_prefix; fail_on_error)
if build_sitemap
sitemap(project, h)
end
return write_html_pages(url_prefix, h, extra_head)
end
"""
ignore_homepage(project, input_paths)
By default, the homepage is only shown to website visitors.
However, the user can show the homepage also to offline visitors via the configuration option.
"""
function ignore_homepage(project, input_paths)
c = config(project)
override::Bool = config(project, "include_homepage_outside_html")
return override ? input_paths : input_paths[2:end]
end
function juliamono_path()
artifact = Artifacts.artifact"JuliaMono"
dir = joinpath(artifact, "juliamono-$JULIAMONO_VERSION")
# The forward slash is required by LaTeX.
return dir * '/'
end
const JULIAMONO_PATH = juliamono_path()
function pdf(; project="default")
input_path = write_input_markdown(project; skip_index=true)
copy_extra_directories(project)
latex_template_path = pandoc_file("template.tex")
template = "--template=$latex_template_path"
file = config(project, "output_filename")
output_filename = joinpath(BUILD_DIR, "$file.pdf")
output = "--output=$output_filename"
metadata_path = combined_metadata_path(project)
metadata = "--metadata-file=$metadata_path"
output_block_filter = joinpath(PROJECT_ROOT, "src", "output-block.lua")
input_files = ignore_homepage(project, inputs(project))
listings_unicode_path = joinpath(PROJECT_ROOT, "defaults", "julia_listings_unicode.tex")
listings_path = joinpath(PROJECT_ROOT, "defaults", "julia_listings.tex")
build_info = today()
tectonic = joinpath(Artifacts.artifact"Tectonic", "tectonic")
pdf_engine = "--pdf-engine=$tectonic"
args = [
input_path;
crossref;
citeproc;
csl();
metadata;
template;
"--lua-filter=$output_block_filter";
"--listings";
pdf_engine;
# Print engine info. Extremely useful for debugging.
"--pdf-engine-opt=--print";
"--variable=listings-unicode-path:$listings_unicode_path";
"--variable=listings-path:$listings_path";
"--variable=build-info:$build_info";
extra_args
]
output_tex_filename = joinpath(BUILD_DIR, "$file.tex")
println("Wrote $output_tex_filename (for debugging purposes)")
tex_output = "--output=$output_tex_filename"
call_pandoc([args; tex_output])
out = call_pandoc([args; output])
if !isnothing(out)
println("Built $output_filename")
end
return nothing
end
function docx(; project="default")
input_path = write_input_markdown(project; skip_index=true)
file = config(project, "output_filename")
output_filename = joinpath(BUILD_DIR, "$file.docx")
output = "--output=$output_filename"
metadata_path = combined_metadata_path(project)
metadata = "--metadata-file=$metadata_path"
input_files = ignore_homepage(project, inputs(project))
args = [
input_path;
crossref;
citeproc;
csl();
metadata;
output
]
out = call_pandoc(args)
if !isnothing(out)
println("Built $output_filename")
end
nothing
end
"""
cleanup()
Remove generated files.
"""
function cleanup()
dirs = [Books.GENERATED_DIR, Books.BUILD_DIR]
for dir in dirs
rm(dir; force=true, recursive=true)
mkpath(dir)
end
end
function build_all(; project="default", extra_head="", fail_on_error=false)
mkpath(BUILD_DIR)
filename = "favicon.png"
from_path = joinpath("pandoc", filename)
if isfile(from_path)
cp(from_path, joinpath(BUILD_DIR, filename); force=true)
end
build_sitemap = true
html(; project, extra_head, fail_on_error, build_sitemap)
write_extra_html_files(project)
pdf(; project)
docx(; project)
end