mirror of https://github.com/mpastell/Weave.jl
reorder functions again
parent
c6b23d4541
commit
4b12f3da99
|
@ -3,6 +3,15 @@
|
|||
|
||||
abstract type HTMLFormat <: WeaveFormat end
|
||||
|
||||
format_code(code, docformat::HTMLFormat) =
|
||||
highlight_code(MIME("text/html"), code, docformat.highlight_theme)
|
||||
|
||||
format_termchunk(chunk, docformat::HTMLFormat) =
|
||||
should_render(chunk) ? highlight_term(MIME("text/html"), chunk.output, docformat.highlight_theme) : ""
|
||||
|
||||
# Julia markdown
|
||||
# --------------
|
||||
|
||||
Base.@kwdef mutable struct JMarkdown2HTML <: HTMLFormat
|
||||
description = "Julia markdown to html"
|
||||
extension = "html"
|
||||
|
@ -38,25 +47,6 @@ get_html_template(x) = get_template(x)
|
|||
get_stylesheet(::Nothing) = get_stylesheet(normpath(STYLESHEET_DIR, "skeleton.css"))
|
||||
get_stylesheet(path::AbstractString) = read(path, String)
|
||||
|
||||
Base.@kwdef mutable struct Pandoc2HTML <: HTMLFormat
|
||||
description = "Markdown to HTML (requires Pandoc 2)"
|
||||
extension = "md"
|
||||
codestart = "\n"
|
||||
codeend = "\n"
|
||||
termstart = codestart
|
||||
termend = codeend
|
||||
outputstart = "\n"
|
||||
outputend = "\n"
|
||||
mimetypes = ["image/png", "image/svg+xml", "image/jpg",
|
||||
"text/html", "text/markdown", "text/plain"]
|
||||
fig_ext = ".png"
|
||||
out_width = nothing
|
||||
out_height = nothing
|
||||
fig_pos = nothing
|
||||
fig_env = nothing
|
||||
end
|
||||
register_format!("pandoc2html", Pandoc2HTML())
|
||||
|
||||
# very similar to tex version of function
|
||||
function format_chunk(chunk::DocChunk, docformat::JMarkdown2HTML)
|
||||
out = IOBuffer()
|
||||
|
@ -79,14 +69,6 @@ end
|
|||
|
||||
format_output(result, docformat::JMarkdown2HTML) = Markdown.htmlesc(result)
|
||||
|
||||
format_code(code, docformat::HTMLFormat) =
|
||||
highlight_code(MIME("text/html"), code, docformat.highlight_theme)
|
||||
|
||||
format_termchunk(chunk, docformat::HTMLFormat) =
|
||||
should_render(chunk) ? highlight_term(MIME("text/html"), chunk.output, docformat.highlight_theme) : ""
|
||||
|
||||
formatfigures(chunk, docformat::Pandoc2HTML) = formatfigures(chunk, Pandoc())
|
||||
|
||||
function formatfigures(chunk, docformat::JMarkdown2HTML)
|
||||
fignames = chunk.figures
|
||||
caption = chunk.options[:fig_cap]
|
||||
|
@ -142,3 +124,27 @@ function render_doc(docformat::JMarkdown2HTML, body, doc; css = nothing)
|
|||
[Pair(Symbol(k), v) for (k, v) in doc.header]...,
|
||||
)
|
||||
end
|
||||
|
||||
# Pandoc
|
||||
# ------
|
||||
|
||||
Base.@kwdef mutable struct Pandoc2HTML <: HTMLFormat
|
||||
description = "Markdown to HTML (requires Pandoc 2)"
|
||||
extension = "md"
|
||||
codestart = "\n"
|
||||
codeend = "\n"
|
||||
termstart = codestart
|
||||
termend = codeend
|
||||
outputstart = "\n"
|
||||
outputend = "\n"
|
||||
mimetypes = ["image/png", "image/svg+xml", "image/jpg",
|
||||
"text/html", "text/markdown", "text/plain"]
|
||||
fig_ext = ".png"
|
||||
out_width = nothing
|
||||
out_height = nothing
|
||||
fig_pos = nothing
|
||||
fig_env = nothing
|
||||
end
|
||||
register_format!("pandoc2html", Pandoc2HTML())
|
||||
|
||||
formatfigures(chunk, docformat::Pandoc2HTML) = formatfigures(chunk, Pandoc())
|
||||
|
|
|
@ -1,10 +1,127 @@
|
|||
# PDF and Tex
|
||||
# -----------
|
||||
# Tex
|
||||
# ---
|
||||
|
||||
abstract type TexFormat <: WeaveFormat end
|
||||
|
||||
set_rendering_options!(docformat::TexFormat; keep_unicode = false, kwargs...) = docformat.keep_unicode |= keep_unicode
|
||||
|
||||
function formatfigures(chunk, docformat::TexFormat)
|
||||
fignames = chunk.figures
|
||||
caption = chunk.options[:fig_cap]
|
||||
width = chunk.options[:out_width]
|
||||
height = chunk.options[:out_height]
|
||||
f_pos = chunk.options[:fig_pos]
|
||||
f_env = chunk.options[:fig_env]
|
||||
result = ""
|
||||
figstring = ""
|
||||
|
||||
if f_env == nothing && caption != nothing
|
||||
f_env = "figure"
|
||||
end
|
||||
|
||||
(f_pos == nothing) && (f_pos = "!h")
|
||||
# Set size
|
||||
attribs = ""
|
||||
width == nothing || (attribs = "width=$(md_length_to_latex(width,"\\linewidth"))")
|
||||
(attribs != "" && height != nothing) && (attribs *= ",")
|
||||
height == nothing || (attribs *= "height=$(md_length_to_latex(height,"\\paperheight"))")
|
||||
|
||||
if f_env != nothing
|
||||
result *= "\\begin{$f_env}"
|
||||
(f_pos != "") && (result *= "[$f_pos]")
|
||||
result *= "\n"
|
||||
end
|
||||
|
||||
for fig in fignames
|
||||
if splitext(fig)[2] == ".tex" # Tikz figures
|
||||
figstring *= "\\resizebox{$width}{!}{\\input{$fig}}\n"
|
||||
else
|
||||
if isempty(attribs)
|
||||
figstring *= "\\includegraphics{$fig}\n"
|
||||
else
|
||||
figstring *= "\\includegraphics[$attribs]{$fig}\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Figure environment
|
||||
if caption != nothing
|
||||
result *= string("\\center\n", "$figstring", "\\caption{$caption}\n")
|
||||
else
|
||||
result *= figstring
|
||||
end
|
||||
|
||||
if chunk.options[:label] != nothing && f_env != nothing
|
||||
label = chunk.options[:label]
|
||||
result *= "\\label{fig:$label}\n"
|
||||
end
|
||||
|
||||
if f_env != nothing
|
||||
result *= "\\end{$f_env}\n"
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
function md_length_to_latex(def, reference)
|
||||
if occursin("%", def)
|
||||
_def = tryparse(Float64, replace(def, "%" => ""))
|
||||
_def == nothing && return def
|
||||
perc = round(_def / 100, digits = 2)
|
||||
return "$perc$reference"
|
||||
end
|
||||
return def
|
||||
end
|
||||
|
||||
# plain Tex
|
||||
# ---------
|
||||
|
||||
Base.@kwdef mutable struct Tex <: TexFormat
|
||||
description = "Latex with custom code environments"
|
||||
extension = "tex"
|
||||
codestart = "\\begin{juliacode}"
|
||||
codeend = "\\end{juliacode}"
|
||||
termstart = "\\begin{juliaterm}"
|
||||
termend = "\\end{juliaterm}"
|
||||
outputstart = "\\begin{juliaout}"
|
||||
outputend = "\\end{juliaout}"
|
||||
mimetypes = ["application/pdf", "image/png", "text/latex", "text/plain"]
|
||||
fig_ext = ".pdf"
|
||||
out_width = "\\linewidth"
|
||||
out_height = nothing
|
||||
fig_pos = "htpb"
|
||||
fig_env = "figure"
|
||||
# specials
|
||||
keep_unicode = false
|
||||
end
|
||||
register_format!("tex", Tex())
|
||||
|
||||
# minted Tex
|
||||
# ----------
|
||||
|
||||
Base.@kwdef mutable struct TexMinted <: TexFormat
|
||||
description = "Latex using minted for highlighting"
|
||||
extension = "tex"
|
||||
codestart = "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}"
|
||||
codeend = "\\end{minted}"
|
||||
termstart = "\\begin{minted}[fontsize=\\footnotesize, xleftmargin=0.5em, mathescape]{jlcon}"
|
||||
termend = "\\end{minted}"
|
||||
outputstart = "\\begin{minted}[fontsize=\\small, xleftmargin=0.5em, mathescape, frame = leftline]{text}"
|
||||
outputend = "\\end{minted}"
|
||||
mimetypes = ["application/pdf", "image/png", "text/latex", "text/plain"]
|
||||
fig_ext = ".pdf"
|
||||
out_width = "\\linewidth"
|
||||
out_height = nothing
|
||||
fig_pos = "htpb"
|
||||
fig_env = "figure"
|
||||
# specials
|
||||
keep_unicode = false
|
||||
end
|
||||
register_format!("texminted", TexMinted())
|
||||
|
||||
# Tex (directly to PDF)
|
||||
# ---------------------
|
||||
|
||||
Base.@kwdef mutable struct JMarkdown2tex <: TexFormat
|
||||
description = "Julia markdown to latex"
|
||||
extension = "tex"
|
||||
|
@ -47,46 +164,6 @@ function render_doc(docformat::JMarkdown2tex, body, doc)
|
|||
)
|
||||
end
|
||||
|
||||
Base.@kwdef mutable struct Tex <: TexFormat
|
||||
description = "Latex with custom code environments"
|
||||
extension = "tex"
|
||||
codestart = "\\begin{juliacode}"
|
||||
codeend = "\\end{juliacode}"
|
||||
termstart = "\\begin{juliaterm}"
|
||||
termend = "\\end{juliaterm}"
|
||||
outputstart = "\\begin{juliaout}"
|
||||
outputend = "\\end{juliaout}"
|
||||
mimetypes = ["application/pdf", "image/png", "text/latex", "text/plain"]
|
||||
fig_ext = ".pdf"
|
||||
out_width = "\\linewidth"
|
||||
out_height = nothing
|
||||
fig_pos = "htpb"
|
||||
fig_env = "figure"
|
||||
# specials
|
||||
keep_unicode = false
|
||||
end
|
||||
register_format!("tex", Tex())
|
||||
|
||||
Base.@kwdef mutable struct TexMinted <: TexFormat
|
||||
description = "Latex using minted for highlighting"
|
||||
extension = "tex"
|
||||
codestart = "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}"
|
||||
codeend = "\\end{minted}"
|
||||
termstart = "\\begin{minted}[fontsize=\\footnotesize, xleftmargin=0.5em, mathescape]{jlcon}"
|
||||
termend = "\\end{minted}"
|
||||
outputstart = "\\begin{minted}[fontsize=\\small, xleftmargin=0.5em, mathescape, frame = leftline]{text}"
|
||||
outputend = "\\end{minted}"
|
||||
mimetypes = ["application/pdf", "image/png", "text/latex", "text/plain"]
|
||||
fig_ext = ".pdf"
|
||||
out_width = "\\linewidth"
|
||||
out_height = nothing
|
||||
fig_pos = "htpb"
|
||||
fig_env = "figure"
|
||||
# specials
|
||||
keep_unicode = false
|
||||
end
|
||||
register_format!("texminted", TexMinted())
|
||||
|
||||
# very similar to export to html
|
||||
function format_chunk(chunk::DocChunk, docformat::JMarkdown2tex)
|
||||
out = IOBuffer()
|
||||
|
@ -155,71 +232,3 @@ function texify(s)
|
|||
s
|
||||
end
|
||||
end
|
||||
|
||||
function formatfigures(chunk, docformat::TexFormat)
|
||||
fignames = chunk.figures
|
||||
caption = chunk.options[:fig_cap]
|
||||
width = chunk.options[:out_width]
|
||||
height = chunk.options[:out_height]
|
||||
f_pos = chunk.options[:fig_pos]
|
||||
f_env = chunk.options[:fig_env]
|
||||
result = ""
|
||||
figstring = ""
|
||||
|
||||
if f_env == nothing && caption != nothing
|
||||
f_env = "figure"
|
||||
end
|
||||
|
||||
(f_pos == nothing) && (f_pos = "!h")
|
||||
# Set size
|
||||
attribs = ""
|
||||
width == nothing || (attribs = "width=$(md_length_to_latex(width,"\\linewidth"))")
|
||||
(attribs != "" && height != nothing) && (attribs *= ",")
|
||||
height == nothing || (attribs *= "height=$(md_length_to_latex(height,"\\paperheight"))")
|
||||
|
||||
if f_env != nothing
|
||||
result *= "\\begin{$f_env}"
|
||||
(f_pos != "") && (result *= "[$f_pos]")
|
||||
result *= "\n"
|
||||
end
|
||||
|
||||
for fig in fignames
|
||||
if splitext(fig)[2] == ".tex" # Tikz figures
|
||||
figstring *= "\\resizebox{$width}{!}{\\input{$fig}}\n"
|
||||
else
|
||||
if isempty(attribs)
|
||||
figstring *= "\\includegraphics{$fig}\n"
|
||||
else
|
||||
figstring *= "\\includegraphics[$attribs]{$fig}\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Figure environment
|
||||
if caption != nothing
|
||||
result *= string("\\center\n", "$figstring", "\\caption{$caption}\n")
|
||||
else
|
||||
result *= figstring
|
||||
end
|
||||
|
||||
if chunk.options[:label] != nothing && f_env != nothing
|
||||
label = chunk.options[:label]
|
||||
result *= "\\label{fig:$label}\n"
|
||||
end
|
||||
|
||||
if f_env != nothing
|
||||
result *= "\\end{$f_env}\n"
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
function md_length_to_latex(def, reference)
|
||||
if occursin("%", def)
|
||||
_def = tryparse(Float64, replace(def, "%" => ""))
|
||||
_def == nothing && return def
|
||||
perc = round(_def / 100, digits = 2)
|
||||
return "$perc$reference"
|
||||
end
|
||||
return def
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue