Merge branch 'md2tex'. Closes #63

pull/66/head
Matti Pastell 2016-12-15 14:08:50 +02:00
commit c11b390533
11 changed files with 140 additions and 29 deletions

View File

@ -5,3 +5,4 @@ JSON
Highlights
Mustache
Documenter
YAML

View File

@ -8,10 +8,11 @@ type WeaveDoc
format
doctype::AbstractString
header_script::String
function WeaveDoc(source, chunks)
header::Dict
function WeaveDoc(source, chunks, header)
path, fname = splitdir(abspath(source))
basename = splitext(fname)[1]
new(source, basename, path, chunks, "", nothing, "", "")
new(source, basename, path, chunks, "", nothing, "", "", header)
end
end

View File

@ -15,6 +15,11 @@ function format(doc::WeaveDoc)
docformat.formatdict[:cwd] = doc.cwd #pass wd to figure formatters
#strip header
if isa(doc.chunks[1], DocChunk)
doc.chunks[1] = strip_header(doc.chunks[1])
end
for chunk in copy(doc.chunks)
result = format_chunk(chunk, formatdict, docformat)
push!(formatted, result)
@ -36,14 +41,18 @@ function render_doc(formatted, doc::WeaveDoc, format)
return formatted
end
function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2HTML)
function stylesheet(m::MIME)
buf = PipeBuffer()
Highlights.stylesheet(buf, MIME("text/css"))
Highlights.stylesheet(buf, m)
flush(buf)
css = readstring(buf)
style = readstring(buf)
close(buf)
return style
end
title = get_title(doc)
function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2HTML)
css = stylesheet(MIME("text/html"))
title, author, date = get_titleblock(doc)
path, wsource = splitdir(abspath(doc.source))
wversion = string(Pkg.installed("Weave"))
wtime = string(Date(now()))
@ -54,24 +63,37 @@ function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2HTML)
return Mustache.render(template, themecss = theme_css,
highlightcss = css, body = formatted, header_script = doc.header_script,
source = wsource, wtime = wtime, wversion = wversion,
title = title)
title = title, author = author, date = date)
end
function get_title(doc::WeaveDoc)
if isa(doc.chunks[1], CodeChunk)
return doc.source
function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2tex)
highlight = stylesheet(MIME("text/latex"))
title, author, date = get_titleblock(doc)
path, wsource = splitdir(abspath(doc.source))
wversion = string(Pkg.installed("Weave"))
wtime = string(Date(now()))
template = Mustache.template_from_file(joinpath(dirname(@__FILE__), "../templates/julia_tex.txt"))
return Mustache.render(template, body = formatted,
highlight = highlight,
title = title, author = author, date = date)
end
function get_titleblock(doc::WeaveDoc)
title = get!(doc.header, "title", false)
author = get!(doc.header, "author", false)
date = get!(doc.header, "date", false)
return title, author, date
end
function strip_header(chunk::DocChunk)
if ismatch(r"^---$(?<header>.+)^---$"ms, chunk.content)
chunk.content = lstrip(replace(chunk.content, r"^---$(?<header>.+)^---$"ms, ""))
end
isempty(doc.chunks[1].content) && return doc.source
m = Base.Markdown.parse(doc.chunks[1].content)
if isa(m.content[1], Base.Markdown.Header)
title = m.content[1].text[1]
else
title = doc.source
end
return title
return chunk
end
function format_chunk(chunk::DocChunk, formatdict, docformat)
@ -84,6 +106,19 @@ function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
return string(Documenter.Writers.HTMLWriter.mdconvert(m))
end
function Base.Markdown.latex(io::IO, md::Base.Markdown.Paragraph)
println(io)
for md in md.content
Base.Markdown.latexinline(io, md)
end
println(io)
end
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
m = Base.Markdown.parse(chunk.content)
return Base.Markdown.latex(m)
end
function format_chunk(chunk::CodeChunk, formatdict, docformat)
#Fill undefined options with format specific defaults
@ -169,6 +204,15 @@ function format_code(result::AbstractString, docformat)
return result
end
function format_code(result::AbstractString, docformat::JMarkdown2tex)
buf = PipeBuffer()
Highlights.highlight(buf, MIME("text/latex"), strip(result), Highlights.Lexers.JuliaLexer)
flush(buf)
highlighted = readstring(buf)
close(buf)
return highlighted
end
function format_code(result::AbstractString, docformat::JMarkdown2HTML)
buf = PipeBuffer()
Highlights.highlight(buf, MIME("text/html"), strip(result), Highlights.Lexers.JuliaLexer)

View File

@ -105,7 +105,7 @@ type JMarkdown2HTML
formatdict::Dict{Symbol,Any}
end
const md2html = JMarkdown2HTML("Julia markdown", Dict{Symbol,Any}(
const md2html = JMarkdown2HTML("Julia markdown to html", Dict{Symbol,Any}(
:codestart => "\n",
:codeend=> "\n",
:outputstart=> "<pre class=\"hljl\">",
@ -114,6 +114,24 @@ const md2html = JMarkdown2HTML("Julia markdown", Dict{Symbol,Any}(
:extension=> "html",
:doctype=> "md2html"))
#Julia markdown
type JMarkdown2tex
description::AbstractString
formatdict::Dict{Symbol,Any}
end
const md2tex = JMarkdown2tex("Julia markdown to latex", Dict{Symbol,Any}(
:codestart => "",
:codeend=> "",
:outputstart=> "\\begin{lstlisting}",
:outputend=> "\\end{lstlisting}\n",
:fig_ext=> ".pdf",
:extension=> "tex",
:mimetypes => ["application/pdf", "image/png", "image/jpg",
"text/latex", "text/plain"],
:doctype=> "md2tex"))
type MultiMarkdown
description::AbstractString
formatdict::Dict{Symbol,Any}
@ -419,5 +437,6 @@ const formats = Dict{AbstractString, Any}("tex" => tex,
"multimarkdown" => multimarkdown,
"rst" => rst,
"asciidoc" => adoc,
"md2html" => md2html
"md2html" => md2html,
"md2tex" => md2tex
)

View File

@ -1,4 +1,4 @@
import JSON
import JSON, YAML
pushopt(options::Dict,expr::Expr) = Base.Meta.isexpr(expr,:(=)) && (options[expr.args[1]] = expr.args[2])
@ -48,7 +48,23 @@ function read_doc(source::AbstractString, format=:auto)
format == :auto && (format = detect_informat(source))
document = readstring(source)
parsed = parse_doc(document, format)
doc = WeaveDoc(source, parsed)
header = parse_header(parsed[1])
doc = WeaveDoc(source, parsed, header)
return doc
end
function parse_header(chunk::CodeChunk)
return Dict()
end
function parse_header(chunk::DocChunk)
m = match(r"^---$(?<header>.+)^---$"ms, chunk.content)
if m !== nothing
header = YAML.load(string(m[:header]))
else
header = Dict()
end
return header
end
function parse_doc(document::AbstractString, format="noweb"::AbstractString)

View File

@ -281,7 +281,7 @@ function init_plotting(plotlib)
else
l_plotlib = lowercase(plotlib)
rcParams[:chunk_defaults][:fig] = true
if l_plotlib == "winston"
if l_plotlib == "winston"
eval(parse("""include("$srcdir/winston.jl")"""))
rcParams[:plotlib] = "Winston"
elseif l_plotlib == "pyplot"

View File

@ -3,7 +3,7 @@
<HEAD>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>{{{:title}}}</title>
{{#:title}}<title>{{:title}}</title>{{/:title}}
{{{ :header_script }}}
<script type="text/x-mathjax-config">
@ -24,6 +24,12 @@
<div class = "row">
<div class = "col-md-12 twelve columns">
<div class="title">
{{#:title}}<h1 class="title">{{:title}}</h1>{{/:title}}
{{#:author}}<h5>{{{:author}}}</h5>{{/:author}}
{{#:date}}<h5>{{{:date}}}</h5>{{/:date}}
</div>
{{{ :body }}}

24
templates/julia_tex.txt Normal file
View File

@ -0,0 +1,24 @@
\documentclass{article}[12pt]
\usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry}
{{#:title}}
\title{ {{{ :title }}} }
{{/:title}}
{{#:author}}
\author{ {{{ :author }}} }
{{/:author}}
{{#:date}}
\date{ {{{ :date }}} }
{{/:date}}
{{{ :highlight }}}
\begin{document}
{{#:title}}\maketitle{{/:title}}
{{{ :body }}}
\end{document}

View File

@ -528,3 +528,4 @@ code {
h1.title {margin-top : 20px}
img {max-width : 100%%}
div.title {text-align: center;}

View File

@ -111,5 +111,6 @@ m = Base.Markdown.parse("**Some Markdown**")
m
\end{juliacode}
\textbf{Some Markdown}

View File

@ -21,8 +21,6 @@ f = Weave.format_chunk(dchunk, docformat.formatdict, docformat)
parsed = Weave.read_doc("documents/chunk_options.noweb")
doc = Weave.run(parsed, doctype = "md2html")
title = Weave.get_title(doc)
@test title == "documents/chunk_options.noweb"
c_check = "<pre class='hljl'>\n<span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-ni'>12</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>10</span><span class='hljl-p'>]</span><span class='hljl-t'>\n</span><span class='hljl-nf'>println</span><span class='hljl-p'>(</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span>\n</pre>\n"
c = Weave.format_code(doc.chunks[4].content, doc.format)