-
Notifications
You must be signed in to change notification settings - Fork 482
/
LaTeXWriter.jl
788 lines (684 loc) · 25.4 KB
/
LaTeXWriter.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
"""
A module for rendering `Document` objects to LaTeX and PDF.
# Keywords
[`LaTeXWriter`](@ref) uses the following additional keyword arguments that can be passed to
[`makedocs`](@ref Documenter.makedocs): `authors`, `sitename`.
**`sitename`** is the site's title displayed in the title bar and at the top of the
navigation menu. It goes into the `\\title` LaTeX command.
**`authors`** can be used to specify the authors of. It goes into the `\\author` LaTeX command.
"""
module LaTeXWriter
import ...Documenter: Documenter
"""
Documenter.LaTeX(; kwargs...)
Output format specifier that results in LaTeX/PDF output.
Used together with [`makedocs`](@ref Documenter.makedocs), e.g.
```julia
makedocs(
format = Documenter.LaTeX()
)
```
The `makedocs` argument `sitename` will be used for the `\\title` field in the tex document,
and if the build is for a release tag (i.e. when the `"TRAVIS_TAG"` environment variable is set)
the version number will be appended to the title.
The `makedocs` argument `authors` should also be specified, it will be used for the
`\\authors` field in the tex document.
# Keyword arguments
**`platform`** sets the platform where the tex-file is compiled, either `"native"` (default), `"docker"`,
or "none" which doesn't compile the tex.
See [Other Output Formats](@ref) for more information.
"""
struct LaTeX <: Documenter.Writer
platform::String
function LaTeX(; platform = "native")
platform ∈ ("native", "docker", "none") || throw(ArgumentError("unknown platform: $platform"))
return new(platform)
end
end
import ...Documenter:
Anchors,
Builder,
Documents,
Expanders,
Documenter,
Utilities,
Writers
import Markdown
import ANSIColoredPrinters
mutable struct Context{I <: IO} <: IO
io::I
in_header::Bool
footnotes::Dict{String, Int}
depth::Int
filename::String # currently active source file
end
Context(io) = Context{typeof(io)}(io, false, Dict(), 1, "")
_print(c::Context, args...) = Base.print(c.io, args...)
_println(c::Context, args...) = Base.println(c.io, args...)
const STYLE = joinpath(dirname(@__FILE__), "..", "..", "assets", "latex", "documenter.sty")
hastex() = (try; success(`latexmk -version`); catch; false; end)
const DOCUMENT_STRUCTURE = (
"part",
"chapter",
"section",
"subsection",
"subsubsection",
"paragraph",
"subparagraph",
)
# https://github.com/JuliaLang/julia/pull/32851
function mktempdir(args...; kwargs...)
if VERSION < v"1.3.0-alpha.112"
return Base.mktempdir(args...; kwargs...)
else
return Base.mktempdir(args...; cleanup=false, kwargs...)
end
end
function render(doc::Documents.Document, settings::LaTeX=LaTeX())
@info "LaTeXWriter: creating the LaTeX file."
Base.mktempdir() do path
cp(joinpath(doc.user.root, doc.user.build), joinpath(path, "build"))
cd(joinpath(path, "build")) do
name = doc.user.sitename
let tag = get(ENV, "TRAVIS_TAG", "")
if occursin(Base.VERSION_REGEX, tag)
v = VersionNumber(tag)
name *= "-$(v.major).$(v.minor).$(v.patch)"
end
end
name = replace(name, " " => "")
texfile = name * ".tex"
pdffile = name * ".pdf"
open(texfile, "w") do io
context = Context(io)
writeheader(context, doc)
for (title, filename, depth) in files(doc.user.pages)
context.filename = filename
empty!(context.footnotes)
if 1 <= depth <= length(DOCUMENT_STRUCTURE)
header_type = DOCUMENT_STRUCTURE[depth]
header_text = "\n\\$(header_type){$(title)}\n"
if isempty(filename)
_println(context, header_text)
else
path = normpath(filename)
page = doc.blueprint.pages[path]
if get(page.globals.meta, :IgnorePage, :none) !== :latex
context.depth = depth + (isempty(title) ? 0 : 1)
context.depth > depth && _println(context, header_text)
latex(context, page, doc)
end
end
end
end
writefooter(context, doc)
end
cp(STYLE, "documenter.sty")
# compile .tex
status = compile_tex(doc, settings, texfile)
# Debug: if DOCUMENTER_LATEX_DEBUG environment variable is set, copy the LaTeX
# source files over to a directory under doc.user.root.
if haskey(ENV, "DOCUMENTER_LATEX_DEBUG")
dst = isempty(ENV["DOCUMENTER_LATEX_DEBUG"]) ? mktempdir(doc.user.root) :
joinpath(doc.user.root, ENV["DOCUMENTER_LATEX_DEBUG"])
sources = cp(pwd(), dst, force=true)
@info "LaTeX sources copied for debugging to $(sources)"
end
# If the build was successful, copy the PDF or the LaTeX source to the .build directory
if status && (settings.platform != "none")
cp(pdffile, joinpath(doc.user.root, doc.user.build, pdffile); force = true)
elseif status && (settings.platform == "none")
cp(pwd(), joinpath(doc.user.root, doc.user.build); force = true)
else
error("Compiling the .tex file failed. See logs for more information.")
end
end
end
end
const DOCKER_IMAGE_TAG = "0.1"
function compile_tex(doc::Documents.Document, settings::LaTeX, texfile::String)
if settings.platform == "native"
Sys.which("latexmk") === nothing && (@error "LaTeXWriter: latexmk command not found."; return false)
@info "LaTeXWriter: using latexmk to compile tex."
try
piperun(`latexmk -f -interaction=nonstopmode -view=none -lualatex -shell-escape $texfile`)
return true
catch err
logs = cp(pwd(), mktempdir(); force=true)
@error "LaTeXWriter: failed to compile tex with latexmk. " *
"Logs and partial output can be found in $(Utilities.locrepr(logs))." exception = err
return false
end
elseif settings.platform == "docker"
Sys.which("docker") === nothing && (@error "LaTeXWriter: docker command not found."; return false)
@info "LaTeXWriter: using docker to compile tex."
script = """
mkdir /home/zeptodoctor/build
cd /home/zeptodoctor/build
cp -r /mnt/. .
latexmk -f -interaction=nonstopmode -view=none -lualatex -shell-escape $texfile
"""
try
piperun(`docker run -itd -u zeptodoctor --name latex-container -v $(pwd()):/mnt/ --rm juliadocs/documenter-latex:$(DOCKER_IMAGE_TAG)`)
piperun(`docker exec -u zeptodoctor latex-container bash -c $(script)`)
piperun(`docker cp latex-container:/home/zeptodoctor/build/. .`)
return true
catch err
logs = cp(pwd(), mktempdir(); force=true)
@error "LaTeXWriter: failed to compile tex with docker. " *
"Logs and partial output can be found in $(Utilities.locrepr(logs))." exception = err
return false
finally
try; piperun(`docker stop latex-container`); catch; end
end
elseif settings.platform == "none"
@info "Skipping compiling tex file."
return true
end
end
function piperun(cmd)
verbose = "--verbose" in ARGS || get(ENV, "DOCUMENTER_VERBOSE", "false") == "true"
run(pipeline(cmd, stdout = verbose ? stdout : "LaTeXWriter.stdout",
stderr = verbose ? stderr : "LaTeXWriter.stderr"))
end
function writeheader(io::IO, doc::Documents.Document)
custom = joinpath(doc.user.root, doc.user.source, "assets", "custom.sty")
isfile(custom) ? cp(custom, "custom.sty"; force = true) : touch("custom.sty")
preamble =
"""
\\documentclass[oneside]{memoir}
\\usepackage{./documenter}
\\usepackage{./custom}
%% Title Page
\\title{
{\\HUGE $(doc.user.sitename)}\\\\
{\\Large $(get(ENV, "TRAVIS_TAG", ""))}
}
\\author{$(doc.user.authors)}
%% TOC settings
% -- TOC depth
% value: [part, chapter, section, subsection,
% subsubsection, paragraph, subparagraph]
\\settocdepth{section} % show "part+chapter+section" in TOC
% -- TOC spacing
% ref: https://tex.stackexchange.com/questions/60317/toc-spacing-in-memoir
% doc: memoir/memman.pdf
% - Figure 9.2: Layout of a ToC
% - Table 9.3: Value of K in macros for styling entries
\\makeatletter
% {part} to {chaper}
\\setlength{\\cftbeforepartskip}{1.5em \\@plus \\p@}
% {chaper} to {chaper}
\\setlength{\\cftbeforechapterskip}{0.0em \\@plus \\p@}
% Chapter num to chapter title spacing (Figure 9.2@memman)
\\setlength{\\cftchapternumwidth}{2.5em \\@plus \\p@}
% indent before section number
\\setlength{\\cftsectionindent}{2.5em \\@plus \\p@}
\\makeatother
%% Main document begin
\\begin{document}
\\frontmatter
\\maketitle
\\clearpage
\\tableofcontents
\\mainmatter
"""
_println(io, preamble)
end
function writefooter(io::IO, doc::Documents.Document)
_println(io, "\n\\end{document}")
end
function latex(io::IO, page::Documents.Page, doc::Documents.Document)
for element in page.elements
latex(io, page.mapping[element], page, doc)
end
end
function latex(io::IO, vec::Vector, page, doc)
for each in vec
latex(io, each, page, doc)
end
end
function latex(io::IO, anchor::Anchors.Anchor, page, doc)
id = string(hash(string(anchor.id, "-", anchor.nth)))
_println(io, "\n\\hypertarget{", id, "}{}\n")
latex(io, anchor.object, page, doc)
end
## Documentation Nodes.
function latex(io::IO, node::Documents.DocsNodes, page, doc)
for node in node.nodes
latex(io, node, page, doc)
end
end
function latex(io::IO, node::Documents.DocsNode, page, doc)
id = string(hash(string(node.anchor.id)))
# Docstring header based on the name of the binding and it's category.
_println(io, "\\hypertarget{", id, "}{} ")
_print(io, "\\hyperlink{", id, "}{\\texttt{")
latexesc(io, string(node.object.binding))
_print(io, "}} ")
_println(io, " -- {", Utilities.doccat(node.object), ".}\n")
# # Body. May contain several concatenated docstrings.
_println(io, "\\begin{adjustwidth}{2em}{0pt}")
latexdoc(io, node.docstr, page, doc)
_println(io, "\n\\end{adjustwidth}")
end
function latexdoc(io::IO, md::Markdown.MD, page, doc)
if haskey(md.meta, :results)
# The `:results` field contains a vector of `Docs.DocStr` objects associated with
# each markdown object. The `DocStr` contains data such as file and line info that
# we need for generating correct scurce links.
for (markdown, result) in zip(md.content, md.meta[:results])
latex(io, Writers.MarkdownWriter.dropheaders(markdown), page, doc)
# When a source link is available then print the link.
url = Utilities.url(doc.internal.remote, doc.user.repo, result)
if url !== nothing
link = "\\href{$url}{\\texttt{source}}"
_println(io, "\n", link, "\n")
end
end
else
# Docstrings with no `:results` metadata won't contain source locations so we don't
# try to print them out. Just print the basic docstring.
render(io, mime, dropheaders(md), page, doc)
end
end
function latexdoc(io::IO, other, page, doc)
# TODO: properly support non-markdown docstrings at some point.
latex(io, other, page, doc)
end
## Index, Contents, and Eval Nodes.
function latex(io::IO, index::Documents.IndexNode, page, doc)
# Having an empty itemize block in LaTeX throws an error, so we bail early
# in that situation:
isempty(index.elements) && (_println(io); return)
_println(io, "\\begin{itemize}")
for (object, _, page, mod, cat) in index.elements
id = string(hash(string(Utilities.slugify(object))))
text = string(object.binding)
_print(io, "\\item \\hyperlink{")
_print(io, id, "}{\\texttt{")
latexesc(io, text)
_println(io, "}}")
end
_println(io, "\\end{itemize}\n")
end
function latex(io::IO, contents::Documents.ContentsNode, page, doc)
# Having an empty itemize block in LaTeX throws an error, so we bail early
# in that situation:
isempty(contents.elements) && (_println(io); return)
depth = 1
_println(io, "\\begin{itemize}")
for (count, path, anchor) in contents.elements
header = anchor.object
level = Utilities.header_level(header)
id = string(hash(string(anchor.id, "-", anchor.nth)))
# If we're changing depth, we need to make sure we always print the
# correct number of \begin{itemize} and \end{itemize} statements.
if level > depth
for k in 1:(level - depth)
# if we jump by more than one level deeper we need to put empty
# \items in -- otherwise LaTeX will complain
(k >= 2) && _println(io, "\\item ~")
_println(io, "\\begin{itemize}")
depth += 1
end
elseif level < depth
for _ in 1:(depth - level)
_println(io, "\\end{itemize}")
depth -= 1
end
end
# Print the corresponding \item statement
_print(io, "\\item \\hyperlink{", id, "}{")
latexinline(io, header.text)
_println(io, "}")
end
# print any remaining missing \end{itemize} statements
for _ = 1:depth; _println(io, "\\end{itemize}"); end
_println(io)
end
function latex(io::IO, node::Documents.EvalNode, page, doc)
node.result === nothing ? nothing : latex(io, node.result, page, doc)
end
# Select the "best" representation for LaTeX output.
using Base64: base64decode
function latex(io::IO, mo::Documents.MultiOutput)
foreach(x->Base.invokelatest(latex, io, x), mo.content)
end
function latex(io::IO, d::Dict{MIME,Any})
filename = String(rand('a':'z', 7))
if haskey(d, MIME"image/png"())
write("$(filename).png", base64decode(d[MIME"image/png"()]))
_println(io, """
\\begin{figure}[H]
\\centering
\\includegraphics[max width=\\linewidth]{$(filename)}
\\end{figure}
""")
elseif haskey(d, MIME"image/jpeg"())
write("$(filename).jpeg", base64decode(d[MIME"image/jpeg"()]))
_println(io, """
\\begin{figure}[H]
\\centering
\\includegraphics[max width=\\linewidth]{$(filename)}
\\end{figure}
""")
elseif haskey(d, MIME"text/latex"())
# If it has a latex MIME, just write it out directly.
_print(io, d[MIME"text/latex"()])
elseif haskey(d, MIME"text/markdown"())
latex(io, Markdown.parse(d[MIME"text/markdown"()]))
elseif haskey(d, MIME"text/plain"())
text = d[MIME"text/plain"()]
out = repr(MIME"text/plain"(), ANSIColoredPrinters.PlainTextPrinter(IOBuffer(text)))
latex(io, Markdown.Code(out))
else
error("this should never happen.")
end
return nothing
end
## Basic Nodes. AKA: any other content that hasn't been handled yet.
latex(io::IO, str::AbstractString, page, doc) = _print(io, str)
function latex(io::IO, other, page, doc)
_println(io)
latex(io, other)
_println(io)
end
latex(io::IO, md::Markdown.MD) = latex(io, md.content)
function latex(io::IO, content::Vector)
for c in content
latex(io, c)
end
end
function latex(io::IO, h::Markdown.Header{N}) where N
tag = DOCUMENT_STRUCTURE[min(io.depth + N - 1, length(DOCUMENT_STRUCTURE))]
_print(io, "\\", tag, "{")
io.in_header = true
latexinline(io, h.text)
io.in_header = false
_println(io, "}\n")
end
# Whitelisted lexers.
const LEXER = Set([
"julia",
"jlcon",
])
function latex(io::IO, code::Markdown.Code)
language = Utilities.codelang(code.language)
language = isempty(language) ? "none" :
(language == "julia-repl") ? "jlcon" : # the julia-repl is called "jlcon" in Pygments
language
text = IOBuffer(code.code)
code.code = repr(MIME"text/plain"(), ANSIColoredPrinters.PlainTextPrinter(text))
escape = '⊻' ∈ code.code
if language in LEXER
_print(io, "\n\\begin{minted}")
if escape
_print(io, "[escapeinside=\\#\\%]")
end
_println(io, "{", language, "}")
if escape
_print_code_escapes_minted(io, code.code)
else
_print(io, code.code)
end
_println(io, "\n\\end{minted}\n")
else
_print(io, "\n\\begin{lstlisting}")
if escape
_println(io, "[escapeinside=\\%\\%]")
_print_code_escapes_lstlisting(io, code.code)
else
_println(io)
_print(io, code.code)
end
_println(io, "\n\\end{lstlisting}\n")
end
end
function latex(io::IO, mcb::Documents.MultiCodeBlock)
latex(io, Documents.join_multiblock(mcb))
end
function _print_code_escapes_minted(io, s::AbstractString)
for ch in s
ch === '#' ? _print(io, "##%") :
ch === '%' ? _print(io, "#%%") : # Note: "#\\%%" results in pygmentize error...
ch === '⊻' ? _print(io, "#\\unicodeveebar%") :
_print(io, ch)
end
end
function _print_code_escapes_lstlisting(io, s::AbstractString)
for ch in s
ch === '%' ? _print(io, "%\\%%") :
ch === '⊻' ? _print(io, "%\\unicodeveebar%") :
_print(io, ch)
end
end
function latexinline(io::IO, code::Markdown.Code)
_print(io, "\\texttt{")
_print_code_escapes_inline(io, code.code)
_print(io, "}")
end
function _print_code_escapes_inline(io, s::AbstractString)
for ch in s
ch === '⊻' ? _print(io, "\\unicodeveebar{}") :
latexesc(io, ch)
end
end
function latex(io::IO, md::Markdown.Paragraph)
for md in md.content
latexinline(io, md)
end
_println(io, "\n")
end
function latex(io::IO, md::Markdown.BlockQuote)
wrapblock(io, "quote") do
latex(io, md.content)
end
end
function latex(io::IO, md::Markdown.Admonition)
wrapblock(io, "quote") do
wrapinline(io, "textbf") do
latexinline(io, md.title)
end
_println(io, "\n")
latex(io, md.content)
end
end
function latex(io::IO, f::Markdown.Footnote)
id = get(io.footnotes, f.id, 1)
_print(io, "\\footnotetext[", id, "]{")
latex(io, f.text)
_println(io, "}")
end
function latex(io::IO, md::Markdown.List)
# `\begin{itemize}` is used here for both ordered and unordered lists since providing
# custom starting numbers for enumerated lists is simpler to do by manually assigning
# each number to `\item` ourselves rather than using `\setcounter{enumi}{<start>}`.
#
# For an ordered list starting at 5 the following will be generated:
#
# \begin{itemize}
# \item[5. ] ...
# \item[6. ] ...
# ...
# \end{itemize}
#
pad = ndigits(md.ordered + length(md.items)) + 2
fmt = n -> (Markdown.isordered(md) ? "[$(rpad("$(n + md.ordered - 1).", pad))]" : "")
wrapblock(io, "itemize") do
for (n, item) in enumerate(md.items)
_print(io, "\\item$(fmt(n)) ")
latex(io, item)
n < length(md.items) && _println(io)
end
end
end
function latex(io::IO, hr::Markdown.HorizontalRule)
_println(io, "{\\rule{\\textwidth}{1pt}}")
end
# This (equation*, split) math env seems to be the only way to correctly render all the
# equations in the Julia manual. However, if the equation is already wrapped in
# align/align*, then there is no need to further wrap it (in fact, it will break).
function latex(io::IO, math::Markdown.LaTeX)
if occursin(r"^\\begin\{align\*?\}", math.formula)
_print(io, math.formula)
else
_print(io, "\\begin{equation*}\n\\begin{split}")
_print(io, math.formula)
_println(io, "\\end{split}\\end{equation*}")
end
end
function latex(io::IO, md::Markdown.Table)
_println(io, "\n\\begin{table}[h]")
_print(io, "\n\\begin{tabulary}{\\linewidth}")
_println(io, "{|", uppercase(join(md.align, '|')), "|}")
for (i, row) in enumerate(md.rows)
i === 1 && _println(io, "\\hline")
for (j, cell) in enumerate(row)
j === 1 || _print(io, " & ")
latexinline(io, cell)
end
_println(io, " \\\\")
_println(io, "\\hline")
end
_println(io, "\\end{tabulary}\n")
_println(io, "\\end{table}\n")
end
function latex(io::IO, raw::Documents.RawNode)
raw.name === :latex ? _println(io, "\n", raw.text, "\n") : nothing
end
# Inline Elements.
function latexinline(io::IO, md::Vector)
for c in md
latexinline(io, c)
end
end
function latexinline(io::IO, md::AbstractString)
latexesc(io, md)
end
function latexinline(io::IO, md::Markdown.Bold)
wrapinline(io, "textbf") do
latexinline(io, md.text)
end
end
function latexinline(io::IO, md::Markdown.Italic)
wrapinline(io, "emph") do
latexinline(io, md.text)
end
end
function latexinline(io::IO, md::Markdown.Image)
wrapblock(io, "figure") do
_println(io, "\\centering")
url = if Utilities.isabsurl(md.url)
@warn "images with absolute URLs not supported in LaTeX output in $(Utilities.locrepr(io.filename))" url = md.url
# We nevertheless output an \includegraphics with the URL. The LaTeX build will
# then give an error, indicating to the user that something wrong. Only the
# warning would be drowned by all the output from LaTeX.
md.url
elseif startswith(md.url, '/')
# URLs starting with a / are assumed to be relative to the document's root
normpath(lstrip(md.url, '/'))
else
normpath(joinpath(dirname(io.filename), md.url))
end
url = replace(url, "\\" => "/") # use / on Windows too.
wrapinline(io, "includegraphics[max width=\\linewidth]") do
_print(io, url)
end
_println(io)
wrapinline(io, "caption") do
latexinline(io, md.alt)
end
_println(io)
end
end
function latexinline(io::IO, f::Markdown.Footnote)
id = get!(io.footnotes, f.id, length(io.footnotes) + 1)
_print(io, "\\footnotemark[", id, "]")
end
function latexinline(io::IO, md::Markdown.Link)
if io.in_header
latexinline(io, md.text)
else
if occursin(".md#", md.url)
file, target = split(md.url, ".md#"; limit = 2)
id = string(hash(target))
wrapinline(io, "hyperlink") do
_print(io, id)
end
else
wrapinline(io, "href") do
latexesc(io, md.url)
end
end
_print(io, "{")
latexinline(io, md.text)
_print(io, "}")
end
end
function latexinline(io, math::Markdown.LaTeX)
# Handle MathJax and TeX inconsistency since the first wants `\LaTeX` wrapped
# in math delims, whereas actual TeX fails when that is done.
math.formula == "\\LaTeX" ? _print(io, math.formula) : _print(io, "\\(", math.formula, "\\)")
end
function latexinline(io, hr::Markdown.HorizontalRule)
_println(io, "\\rule{\\textwidth}{1pt}}")
end
# Metadata Nodes get dropped from the final output for every format but are needed throughout
# rest of the build and so we just leave them in place and print a blank line in their place.
latex(io::IO, node::Documents.MetaNode, page, doc) = _println(io, "\n")
# Utilities.
const _latexescape_chars = Dict{Char, AbstractString}(
'~' => "{\\textasciitilde}",
'^' => "{\\textasciicircum}",
'\\' => "{\\textbackslash}",
'\'' => "{\\textquotesingle}",
'"' => "{\\textquotedbl}",
)
for ch in "&%\$#_{}"
_latexescape_chars[ch] = "\\$ch"
end
latexesc(io, ch::AbstractChar) = _print(io, get(_latexescape_chars, ch, ch))
function latexesc(io, s::AbstractString)
for ch in s
latexesc(io, ch)
end
end
latexesc(s) = sprint(latexesc, s)
function wrapblock(f, io, env)
_println(io, "\\begin{", env, "}")
f()
_println(io, "\\end{", env, "}")
end
function wrapinline(f, io, cmd)
_print(io, "\\", cmd, "{")
f()
_print(io, "}")
end
function files!(out::Vector, v::Vector, depth)
for each in v
files!(out, each, depth + 1)
end
return out
end
# Tuples come from `hide(page)` with either
# (visible, nothing, page, children) or
# (visible, page.first, pages.second, children)
function files!(out::Vector, v::Tuple, depth)
files!(out, v[2] == nothing ? v[3] : v[2] => v[3], depth)
files!(out, v[4], depth)
end
files!(out, s::AbstractString, depth) = push!(out, ("", s, depth))
function files!(out, p::Pair{<:AbstractString,<:Any}, depth)
# Hack time. Because of Julia's typing, something like
# `"Introduction" => "index.md"` may get typed as a `Pair{String,Any}`!
if p[2] isa AbstractString
push!(out, (p.first, p.second, depth))
else
push!(out, (p.first, "", depth))
files!(out, p.second, depth)
end
return out
end
files(v::Vector) = files!(Tuple{String, String, Int}[], v, 0)
end