@@ -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
6366end
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
7680end
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
8892const STYLE = joinpath (dirname (@__FILE__ ), " .." , " .." , " assets" , " latex" , " documenter.sty" )
8993const 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
9198function 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
559590end
560591
0 commit comments