Skip to content

Commit 6fbcc31

Browse files
BambOoxXfingolfin
authored andcommitted
Add option to use listings for code highlighting in LaTeX
1 parent 7b5cadc commit 6fbcc31

File tree

12 files changed

+5965
-32
lines changed

12 files changed

+5965
-32
lines changed

.typos.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ fo = "fo"
33
ket = "ket"
44
trun = "trun"
55
varius = "varius"
6+
7+
[files]
8+
extend-exclude = ["assets/latex/jlcode.sty"]

assets/latex/documenter.sty

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,7 @@
3131
%
3232

3333
% listings
34-
\usepackage{minted}
35-
\setminted{
36-
breaklines = true,
37-
fontsize = \small,
38-
frame = none,
39-
bgcolor = codeblock-background,
40-
rulecolor=codeblock-border,
41-
}
34+
4235
%
4336

4437
% tables

assets/latex/jlcode.sty

Lines changed: 2247 additions & 0 deletions
Large diffs are not rendered by default.

assets/latex/listings.sty

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
\usepackage{listings}
2+
\lstset{
3+
breaklines = true,
4+
% fontsize = \small,
5+
frame = none,
6+
backgroundcolor = codeblock-background,
7+
rulecolor= codeblock-border,
8+
}
9+
\usepackage{jlcode}

assets/latex/minted.sty

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
\usepackage{minted}
2+
\setminted{
3+
breaklines = true,
4+
fontsize = \small,
5+
frame = none,
6+
bgcolor = codeblock-background,
7+
rulecolor=codeblock-border,
8+
}

assets/latex/preamble.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
\documentclass[oneside]{memoir}
33

44
\usepackage{./documenter}
5+
\usepackage{./code_listings}
56
\usepackage{./custom}
67

78
%% Title Page

src/latex/LaTeXWriter.jl

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ struct LaTeX <: Documenter.Writer
5252
platform::String
5353
version::String
5454
tectonic::Union{Cmd, String, Nothing}
55+
code_listings::String
5556
function LaTeX(;
5657
platform = "native",
5758
version = get(ENV, "TRAVIS_TAG", ""),
58-
tectonic = nothing
59+
tectonic = nothing,
60+
code_listings = "minted"
5961
)
6062
platform ("native", "tectonic", "docker", "none") || throw(ArgumentError("unknown platform: $platform"))
61-
return new(platform, string(version), tectonic)
63+
code_listings ("minted", "listings") || throw(ArgumentError("unknown code formatting package: $platform"))
64+
return new(platform, string(version), tectonic, code_listings)
6265
end
6366
end
6467

@@ -73,8 +76,9 @@ mutable struct Context{I <: IO} <: IO
7376
depth::Int
7477
filename::String # currently active source file
7578
doc::Documenter.Document
79+
code_listings::String
7680
end
77-
Context(io, doc) = Context{typeof(io)}(io, false, Dict(), 1, "", doc)
81+
Context(io, doc, code_listings) = Context{typeof(io)}(io, false, Dict(), 1, "", doc, code_listings)
7882

7983
_print(c::Context, args...) = Base.print(c.io, args...)
8084
_println(c::Context, args...) = Base.println(c.io, args...)
@@ -87,6 +91,9 @@ _hash(x) = string(hash(x))
8791

8892
const STYLE = joinpath(dirname(@__FILE__), "..", "..", "assets", "latex", "documenter.sty")
8993
const DEFAULT_PREAMBLE_PATH = joinpath(dirname(@__FILE__), "..", "..", "assets", "latex", "preamble.tex")
94+
const JLCODE_PATH = joinpath(dirname(@__FILE__), "..", "..", "assets", "latex", "jlcode.sty")
95+
const LISTINGS_PATH = joinpath(dirname(@__FILE__), "..", "..", "assets", "latex", "listings.sty")
96+
const MINTED_PATH = joinpath(dirname(@__FILE__), "..", "..", "assets", "latex", "minted.sty")
9097

9198
function hastex()
9299
try
@@ -122,7 +129,7 @@ function render(doc::Documenter.Document, settings::LaTeX = LaTeX())
122129
cd(joinpath(path, "build")) do
123130
fileprefix = latex_fileprefix(doc, settings)
124131
open("$(fileprefix).tex", "w") do io
125-
context = Context(io, doc)
132+
context = Context(io, doc, settings.code_listings)
126133
writeheader(context, doc, settings)
127134
for (title, filename, depth) in files(doc.user.pages)
128135
context.filename = filename
@@ -147,6 +154,7 @@ function render(doc::Documenter.Document, settings::LaTeX = LaTeX())
147154
writefooter(context, doc)
148155
end
149156
cp(STYLE, "documenter.sty")
157+
settings.code_listings == "listings" && cp(JLCODE_PATH, "jlcode.sty")
150158

151159
# compile .tex
152160
status = compile_tex(doc, settings, fileprefix)
@@ -258,6 +266,9 @@ function writeheader(io::IO, doc::Documenter.Document, settings::LaTeX)
258266
custom = joinpath(doc.user.root, doc.user.source, "assets", "custom.sty")
259267
isfile(custom) ? cp(custom, "custom.sty"; force = true) : touch("custom.sty")
260268

269+
cp(settings.code_listings == "minted" ? MINTED_PATH : LISTINGS_PATH, "code_listings.sty")
270+
271+
261272
custom_preamble_file = joinpath(doc.user.root, doc.user.source, "assets", "preamble.tex")
262273
if isfile(custom_preamble_file)
263274
# copy custom preamble.
@@ -538,23 +549,43 @@ function latex(io::Context, node::Node, code::MarkdownAST.CodeBlock)
538549
text = IOBuffer(code.code)
539550
code_code = repr(MIME"text/plain"(), ANSIColoredPrinters.PlainTextPrinter(text))
540551
escape = '' code_code
541-
_print(io, "\n\\begin{minted}")
542-
if escape
543-
_print(io, "[escapeinside=\\#\\%")
544-
end
545-
if language == "text/plain"
546-
_print(io, escape ? "," : "[")
547-
# Special-case the formatting of code outputs from Julia.
548-
_println(io, "xleftmargin=-\\fboxsep,xrightmargin=-\\fboxsep,bgcolor=white,frame=single]{text}")
549-
else
550-
_println(io, escape ? "]{" : "{", language, "}")
551-
end
552-
if escape
553-
_print_code_escapes_minted(io, code_code)
554-
else
555-
_print(io, code_code)
552+
if io.code_listings == "minted"
553+
_print(io, "\n\\begin{minted}")
554+
if escape
555+
_print(io, "[escapeinside=\\#\\%")
556+
end
557+
if language == "text/plain"
558+
_print(io, escape ? "," : "[")
559+
# Special-case the formatting of code outputs from Julia.
560+
_println(io, "xleftmargin=-\\fboxsep,xrightmargin=-\\fboxsep,bgcolor=white,frame=single]{text}")
561+
else
562+
_println(io, escape ? "]{" : "{", language, "}")
563+
end
564+
if escape
565+
_print_code_escapes_minted(io, code_code)
566+
else
567+
_print(io, code_code)
568+
end
569+
_println(io, "\n\\end{minted}\n")
570+
elseif io.code_listings == "listings"
571+
_print(io, "\n\\begin{lstlisting}")
572+
_print(io, escape ? "[escapeinside=\\#\\%," : "[")
573+
if language == "text/plain"
574+
# _print(io, escape ? "," : "[")
575+
# Special-case the formatting of code outputs from Julia.
576+
_println(io, "]")
577+
elseif language == "jlcon"
578+
_println(io, "language=julia, style=jlcodestyle]")
579+
else
580+
_println(io, "]")
581+
end
582+
if escape
583+
_print_code_escapes_minted(io, code_code)
584+
else
585+
_print(io, code_code)
586+
end
587+
_println(io, "\n\\end{lstlisting}\n")
556588
end
557-
_println(io, "\n\\end{minted}\n")
558589
return
559590
end
560591

test/examples/make.jl

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ else
2121
"html", "html-meta-custom", "html-mathjax2-custom", "html-mathjax3", "html-mathjax3-custom",
2222
"html-local", "html-draft", "html-repo-git", "html-repo-nothing", "html-repo-error",
2323
"html-sizethreshold-defaults-fail", "html-sizethreshold-success", "html-sizethreshold-ignore-success", "html-sizethreshold-override-fail", "html-sizethreshold-ignore-success", "html-sizethreshold-ignore-fail",
24-
"latex_texonly", "latex_simple_texonly", "latex_showcase_texonly", "html-pagesonly",
24+
"latex_texonly", "latex_simple_texonly", "latex_listings_texonly", "latex_showcase_texonly", "html-pagesonly",
2525
]
2626
end
2727

@@ -727,7 +727,7 @@ end
727727

728728
examples_latex_texonly_doc = if "latex_texonly" in EXAMPLE_BUILDS
729729
@info("Building mock package docs: LaTeXWriter/latex_texonly")
730-
@quietly makedocs(
730+
makedocs(
731731
format = Documenter.LaTeX(platform = "none"),
732732
sitename = "Documenter LaTeX",
733733
root = examples_root,
@@ -773,6 +773,54 @@ else
773773
nothing
774774
end
775775

776+
examples_latex_listings_texonly_doc = if "latex_listings_texonly" in EXAMPLE_BUILDS
777+
@info("Building mock package docs: LaTeXWriter/latex_listings_texonly")
778+
makedocs(
779+
format = Documenter.LaTeX(platform = "none", code_listings = "listings"),
780+
sitename = "Documenter LaTeX",
781+
root = examples_root,
782+
build = "builds/latex_listings_texonly",
783+
pages = Any[
784+
"General" => [
785+
"index.md",
786+
"latex.md",
787+
"unicode.md",
788+
hide("hidden.md"),
789+
"example-output.md",
790+
"linenumbers.md",
791+
],
792+
# SVG images nor code blocks in footnotes are allowed in LaTeX
793+
# "Manual" => [
794+
# "man/tutorial.md",
795+
# "man/style.md",
796+
# ],
797+
hide(
798+
"Hidden Pages" => "hidden/index.md", Any[
799+
"Page X" => "hidden/x.md",
800+
"hidden/y.md",
801+
"hidden/z.md",
802+
]
803+
),
804+
"Library" => [
805+
"lib/functions.md",
806+
"lib/autodocs.md",
807+
],
808+
"Expandorder" => [
809+
"expandorder/00.md",
810+
"expandorder/01.md",
811+
"expandorder/AA.md",
812+
],
813+
],
814+
doctest = false,
815+
debug = true,
816+
warnonly = [:footnote, :cross_references, :example_block, :eval_block],
817+
)
818+
else
819+
@info "Skipping build: LaTeXWriter/latex_listings_texonly"
820+
@debug "Controlling variables:" EXAMPLE_BUILDS get(ENV, "DOCUMENTER_TEST_EXAMPLES", nothing)
821+
nothing
822+
end
823+
776824
examples_latex_simple_texonly_doc = if "latex_simple_texonly" in EXAMPLE_BUILDS
777825
@info("Building mock package docs: LaTeXWriter/latex_simple_texonly")
778826
@quietly makedocs(
@@ -849,6 +897,26 @@ else
849897
nothing
850898
end
851899

900+
examples_latex_showcase_doc = if "latex_showcase_listings" in EXAMPLE_BUILDS
901+
@info("Building mock package docs: LaTeXWriter/latex_showcase_listings")
902+
@quietly makedocs(
903+
format = Documenter.LaTeX(platform = "docker", version = v"1.2.3", code_listings = "listings"),
904+
sitename = "Documenter LaTeX Showcase",
905+
root = examples_root,
906+
build = "builds/latex_showcase_listingslatex_showcase",
907+
source = "src.latex_showcase",
908+
pages = ["Showcase" => ["showcase.md", "docstrings.md"]],
909+
remotes = Dict(@__DIR__() => (TestRemote(), "6ef16754bc5da93f67a4323fb204c5bd3e64f336")),
910+
doctest = false,
911+
debug = true,
912+
warnonly = [:docs_block, :cross_references],
913+
)
914+
else
915+
@info "Skipping build: LaTeXWriter/latex_showcase"
916+
@debug "Controlling variables:" EXAMPLE_BUILDS get(ENV, "DOCUMENTER_TEST_EXAMPLES", nothing)
917+
nothing
918+
end
919+
852920
examples_latex_showcase_texonly_doc = if "latex_showcase_texonly" in EXAMPLE_BUILDS
853921
@info("Building mock package docs: LaTeXWriter/latex_showcase_texonly")
854922
@quietly makedocs(

0 commit comments

Comments
 (0)