diff --git a/REQUIRE b/REQUIRE index 13aae8f..26112bf 100644 --- a/REQUIRE +++ b/REQUIRE @@ -5,3 +5,4 @@ JSON Highlights Mustache Documenter +YAML diff --git a/src/chunks.jl b/src/chunks.jl index 1a5aced..912a9f8 100644 --- a/src/chunks.jl +++ b/src/chunks.jl @@ -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 diff --git a/src/format.jl b/src/format.jl index c958051..39e439a 100644 --- a/src/format.jl +++ b/src/format.jl @@ -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"^---$(?
.+)^---$"ms, chunk.content) + chunk.content = lstrip(replace(chunk.content, r"^---$(?
.+)^---$"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) diff --git a/src/formatters.jl b/src/formatters.jl index 93b4ce4..139eb64 100644 --- a/src/formatters.jl +++ b/src/formatters.jl @@ -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=> "
",
@@ -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
                                           )
diff --git a/src/readers.jl b/src/readers.jl
index b623a62..35e5969 100644
--- a/src/readers.jl
+++ b/src/readers.jl
@@ -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"^---$(?
.+)^---$"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) diff --git a/src/run.jl b/src/run.jl index e45bfa9..6bb4284 100644 --- a/src/run.jl +++ b/src/run.jl @@ -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" diff --git a/templates/julia_html.txt b/templates/julia_html.txt index 4155b3a..2edd707 100644 --- a/templates/julia_html.txt +++ b/templates/julia_html.txt @@ -3,7 +3,7 @@ - {{{:title}}} + {{#:title}}{{:title}}{{/:title}} {{{ :header_script }}}