diff --git a/src/format.jl b/src/format.jl index c958051..02aeefb 100644 --- a/src/format.jl +++ b/src/format.jl @@ -36,13 +36,17 @@ 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 +function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2HTML) + css = stylesheet(MIME("text/html")) title = get_title(doc) path, wsource = splitdir(abspath(doc.source)) wversion = string(Pkg.installed("Weave")) @@ -57,6 +61,20 @@ function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2HTML) title = title) end +function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2tex) + highlight = stylesheet(MIME("text/latex")) + title = get_title(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) +end + + function get_title(doc::WeaveDoc) if isa(doc.chunks[1], CodeChunk) return doc.source @@ -84,6 +102,20 @@ 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) + #TODO add space between paragraphs + return Base.Markdown.latex(m) +end function format_chunk(chunk::CodeChunk, formatdict, docformat) #Fill undefined options with format specific defaults @@ -169,6 +201,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/templates/julia_tex.txt b/templates/julia_tex.txt
new file mode 100644
index 0000000..ead6602
--- /dev/null
+++ b/templates/julia_tex.txt
@@ -0,0 +1,13 @@
+\documentclass{article}[12pt]
+\usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry}
+
+\title{ {{{ :title }}} }
+
+{{{ :highlight }}}
+
+\begin{document}
+\maketitle
+
+{{{ :body }}}
+
+\end{document}