Embed images to html documents

pull/66/head
Matti Pastell 2016-12-13 15:30:23 +02:00
parent 9b170349d1
commit ded6ce2c2a
3 changed files with 34 additions and 19 deletions

View File

@ -1,8 +1,8 @@
% Intro to Weave.jl with Gadfly # Intro to Weave.jl with Gadfly
% Matti Pastell
% 20th April 2016
# Introduction Matti Pastell, 13th December 2016
## Introduction
This a sample [Julia](http://julialang.org/) noweb document that can This a sample [Julia](http://julialang.org/) noweb document that can
be executed using [Weave.jl](https://github.com/mpastell/Weave.jl). be executed using [Weave.jl](https://github.com/mpastell/Weave.jl).
@ -31,7 +31,7 @@ weave(Pkg.dir("Weave","examples","gadfly_md_sample.jmd"), informat="markdown",
*The documents will be written to the Julia working directory when you *The documents will be written to the Julia working directory when you
use the `out_path = :pwd`.* use the `out_path = :pwd`.*
# Capturing code ## Capturing code
The basic code chunk will be run with default options and the code and The basic code chunk will be run with default options and the code and
output will be captured. output will be captured.
@ -60,11 +60,11 @@ syntax from the source document):
plot(y = cumsum(randn(1000, 1)), Geom.line) plot(y = cumsum(randn(1000, 1)), Geom.line)
``` ```
# Whats next ## Whats next
Read the documentation: Read the documentation:
- stable: <http://mpastell.github.io/Weave.jl/stable/> - stable: [http://mpastell.github.io/Weave.jl/stable/](http://mpastell.github.io/Weave.jl/stable/)
- latest: <http://mpastell.github.io/Weave.jl/latest/> - latest: [http://mpastell.github.io/Weave.jl/latest/](http://mpastell.github.io/Weave.jl/latest/)
See other examples in: <https://github.com/mpastell/Weave.jl/tree/master/examples> See other examples in the [Github repo](https://github.com/mpastell/Weave.jl/tree/master/examples)

View File

@ -13,6 +13,7 @@ function format(doc::WeaveDoc)
get!(formatdict, :fig_pos, nothing) get!(formatdict, :fig_pos, nothing)
get!(formatdict, :fig_env, nothing) get!(formatdict, :fig_env, nothing)
docformat.formatdict[:cwd] = doc.cwd #pass wd to figure formatters
for chunk in copy(doc.chunks) for chunk in copy(doc.chunks)
result = format_chunk(chunk, formatdict, docformat) result = format_chunk(chunk, formatdict, docformat)
@ -76,6 +77,8 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat)
chunk.content = indent(chunk.content, formatdict[:indent]) chunk.content = indent(chunk.content, formatdict[:indent])
end end
chunk.content = format_code(chunk.content, docformat)
if !chunk.options[:eval] if !chunk.options[:eval]
if chunk.options[:echo] if chunk.options[:echo]
result = "$(formatdict[:codestart])$(chunk.content)\n$(formatdict[:codeend])" result = "$(formatdict[:codestart])$(chunk.content)\n$(formatdict[:codeend])"
@ -92,8 +95,7 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat)
if chunk.options[:echo] if chunk.options[:echo]
#Convert to output format and highlight (html, tex...) if needed #Convert to output format and highlight (html, tex...) if needed
formatted_code = format_code(chunk.content, docformat) result = "$(formatdict[:codestart])$(chunk.content)\n$(formatdict[:codeend])\n"
result = "$(formatdict[:codestart])$(formatted_code)\n$(formatdict[:codeend])\n"
else else
result = "" result = ""
end end
@ -127,7 +129,6 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat)
end end
end end
return result return result
end end

View File

@ -119,6 +119,19 @@ type MultiMarkdown
formatdict::Dict{Symbol,Any} formatdict::Dict{Symbol,Any}
end end
function img_to_base64(fig, ext, cwd)
f = open(joinpath(cwd, fig), "r")
raw = read(f)
close(f)
if ext == ".png"
return "data:image/png;base64," * stringmime(MIME("image/png"), raw)
elseif ext == ".svg"
return "data:image/svg+xml;base64," * stringmime(MIME("image/svg+xml"), raw)
else
return(fig)
end
end
function formatfigures(chunk, docformat::JMarkdown2HTML) function formatfigures(chunk, docformat::JMarkdown2HTML)
fignames = chunk.figures fignames = chunk.figures
caption = chunk.options[:fig_cap] caption = chunk.options[:fig_cap]
@ -135,11 +148,16 @@ function formatfigures(chunk, docformat::JMarkdown2HTML)
(attribs != "" && height != nothing ) && (attribs *= ",") (attribs != "" && height != nothing ) && (attribs *= ",")
height == nothing || (attribs *= " height=\"$height\" ") height == nothing || (attribs *= " height=\"$height\" ")
if f_env != nothing if caption != nothing
result *= """<figure>\n""" result *= """<figure>\n"""
end end
for fig = fignames for fig = fignames
ext = splitext(fig)[2]
if ext == ".png" || ext == ".svg"
fig = img_to_base64(fig, ext, docformat.formatdict[:cwd])
end
figstring *= """<img src="$fig" $attribs />\n""" figstring *= """<img src="$fig" $attribs />\n"""
end end
@ -151,17 +169,13 @@ function formatfigures(chunk, docformat::JMarkdown2HTML)
""" """
end end
if f_env != nothing if caption != nothing
result *= "</figure>\n" result *= "</figure>\n"
end end
return result return result
end end
const multimarkdown = MultiMarkdown("MultiMarkdown", const multimarkdown = MultiMarkdown("MultiMarkdown",
Dict{Symbol,Any}( Dict{Symbol,Any}(
:codestart => "````julia", :codestart => "````julia",
@ -320,7 +334,7 @@ end
function formatfigures(chunk, docformat::MultiMarkdown) function formatfigures(chunk, docformat::MultiMarkdown)
fignames = chunk.figures fignames = chunk.figures
caption = chunk.options[:fig_cap] caption = chunk.options[:fig_cap]
result = ""J result = ""
figstring = "" figstring = ""
if chunk.options[:out_width] == nothing if chunk.options[:out_width] == nothing