fix incorrect folder name for julia-0.6.x
Former-commit-id: ef2c7401e0876f22d2f7762d182cfbcd5a7d9c70
This commit is contained in:
191
julia-0.6.3/share/julia/base/markdown/render/html.jl
Normal file
191
julia-0.6.3/share/julia/base/markdown/render/html.jl
Normal file
@@ -0,0 +1,191 @@
|
||||
# This file is a part of Julia. License is MIT: https://julialang.org/license
|
||||
|
||||
include("rich.jl")
|
||||
|
||||
# Utils
|
||||
|
||||
function withtag(f, io::IO, tag, attrs...)
|
||||
print(io, "<$tag")
|
||||
for (attr, value) in attrs
|
||||
print(io, " ")
|
||||
htmlesc(io, attr)
|
||||
print(io, "=\"")
|
||||
htmlesc(io, value)
|
||||
print(io, "\"")
|
||||
end
|
||||
f === nothing && return print(io, " />")
|
||||
|
||||
print(io, ">")
|
||||
f()
|
||||
print(io, "</$tag>")
|
||||
end
|
||||
|
||||
tag(io::IO, tag, attrs...) = withtag(nothing, io, tag, attrs...)
|
||||
|
||||
const _htmlescape_chars = Dict('<'=>"<", '>'=>">",
|
||||
'"'=>""", '&'=>"&",
|
||||
# ' '=>" ",
|
||||
)
|
||||
for ch in "'`!\$\%()=+{}[]"
|
||||
_htmlescape_chars[ch] = "&#$(Int(ch));"
|
||||
end
|
||||
|
||||
function htmlesc(io::IO, s::AbstractString)
|
||||
# s1 = replace(s, r"&(?!(\w+|\#\d+);)", "&")
|
||||
for ch in s
|
||||
print(io, get(_htmlescape_chars, ch, ch))
|
||||
end
|
||||
end
|
||||
function htmlesc(io::IO, s::Symbol)
|
||||
htmlesc(io, string(s))
|
||||
end
|
||||
function htmlesc(io::IO, xs::Union{AbstractString,Symbol}...)
|
||||
for s in xs
|
||||
htmlesc(io, s)
|
||||
end
|
||||
end
|
||||
function htmlesc(s::Union{AbstractString,Symbol})
|
||||
sprint(htmlesc, s)
|
||||
end
|
||||
|
||||
# Block elements
|
||||
|
||||
function html(io::IO, content::Vector)
|
||||
for md in content
|
||||
html(io, md)
|
||||
println(io)
|
||||
end
|
||||
end
|
||||
|
||||
html(io::IO, md::MD) = html(io, md.content)
|
||||
|
||||
function html{l}(io::IO, header::Header{l})
|
||||
withtag(io, "h$l") do
|
||||
htmlinline(io, header.text)
|
||||
end
|
||||
end
|
||||
|
||||
function html(io::IO, code::Code)
|
||||
withtag(io, :pre) do
|
||||
maybe_lang = !isempty(code.language) ? Any[:class=>"language-$(code.language)"] : []
|
||||
withtag(io, :code, maybe_lang...) do
|
||||
htmlesc(io, code.code)
|
||||
# TODO should print newline if this is longer than one line ?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function html(io::IO, md::Paragraph)
|
||||
withtag(io, :p) do
|
||||
htmlinline(io, md.content)
|
||||
end
|
||||
end
|
||||
|
||||
function html(io::IO, md::BlockQuote)
|
||||
withtag(io, :blockquote) do
|
||||
println(io)
|
||||
html(io, md.content)
|
||||
end
|
||||
end
|
||||
|
||||
function html(io::IO, f::Footnote)
|
||||
withtag(io, :div, :class => "footnote", :id => "footnote-$(f.id)") do
|
||||
withtag(io, :p, :class => "footnote-title") do
|
||||
print(io, f.id)
|
||||
end
|
||||
html(io, f.text)
|
||||
end
|
||||
end
|
||||
|
||||
function html(io::IO, md::Admonition)
|
||||
withtag(io, :div, :class => "admonition $(md.category)") do
|
||||
withtag(io, :p, :class => "admonition-title") do
|
||||
print(io, md.title)
|
||||
end
|
||||
html(io, md.content)
|
||||
end
|
||||
end
|
||||
|
||||
function html(io::IO, md::List)
|
||||
maybe_attr = md.ordered > 1 ? Any[:start => string(md.ordered)] : []
|
||||
withtag(io, isordered(md) ? :ol : :ul, maybe_attr...) do
|
||||
for item in md.items
|
||||
println(io)
|
||||
withtag(io, :li) do
|
||||
html(io, item)
|
||||
end
|
||||
end
|
||||
println(io)
|
||||
end
|
||||
end
|
||||
|
||||
function html(io::IO, md::HorizontalRule)
|
||||
tag(io, :hr)
|
||||
end
|
||||
|
||||
html(io::IO, x) = tohtml(io, x)
|
||||
|
||||
# Inline elements
|
||||
|
||||
function htmlinline(io::IO, content::Vector)
|
||||
for x in content
|
||||
htmlinline(io, x)
|
||||
end
|
||||
end
|
||||
|
||||
function htmlinline(io::IO, code::Code)
|
||||
withtag(io, :code) do
|
||||
htmlesc(io, code.code)
|
||||
end
|
||||
end
|
||||
|
||||
function htmlinline(io::IO, md::Union{Symbol,AbstractString})
|
||||
htmlesc(io, md)
|
||||
end
|
||||
|
||||
function htmlinline(io::IO, md::Bold)
|
||||
withtag(io, :strong) do
|
||||
htmlinline(io, md.text)
|
||||
end
|
||||
end
|
||||
|
||||
function htmlinline(io::IO, md::Italic)
|
||||
withtag(io, :em) do
|
||||
htmlinline(io, md.text)
|
||||
end
|
||||
end
|
||||
|
||||
function htmlinline(io::IO, md::Image)
|
||||
tag(io, :img, :src=>md.url, :alt=>md.alt)
|
||||
end
|
||||
|
||||
|
||||
function htmlinline(io::IO, f::Footnote)
|
||||
withtag(io, :a, :href => "#footnote-$(f.id)", :class => "footnote") do
|
||||
print(io, "[", f.id, "]")
|
||||
end
|
||||
end
|
||||
|
||||
function htmlinline(io::IO, link::Link)
|
||||
withtag(io, :a, :href=>link.url) do
|
||||
htmlinline(io, link.text)
|
||||
end
|
||||
end
|
||||
|
||||
function htmlinline(io::IO, br::LineBreak)
|
||||
tag(io, :br)
|
||||
end
|
||||
|
||||
htmlinline(io::IO, x) = tohtml(io, x)
|
||||
|
||||
# API
|
||||
|
||||
export html
|
||||
|
||||
html(md) = sprint(html, md)
|
||||
|
||||
function show(io::IO, ::MIME"text/html", md::MD)
|
||||
withtag(io, :div, :class=>"markdown") do
|
||||
html(io, md)
|
||||
end
|
||||
end
|
||||
172
julia-0.6.3/share/julia/base/markdown/render/latex.jl
Normal file
172
julia-0.6.3/share/julia/base/markdown/render/latex.jl
Normal file
@@ -0,0 +1,172 @@
|
||||
# This file is a part of Julia. License is MIT: https://julialang.org/license
|
||||
|
||||
export latex
|
||||
|
||||
function wrapblock(f, io, env)
|
||||
println(io, "\\begin{", env, "}")
|
||||
f()
|
||||
println(io, "\\end{", env, "}")
|
||||
end
|
||||
|
||||
function wrapinline(f, io, cmd)
|
||||
print(io, "\\", cmd, "{")
|
||||
f()
|
||||
print(io, "}")
|
||||
end
|
||||
|
||||
# Block elements
|
||||
|
||||
latex(io::IO, md::MD) = latex(io, md.content)
|
||||
|
||||
function latex(io::IO, content::Vector)
|
||||
for c in content
|
||||
latex(io, c)
|
||||
end
|
||||
end
|
||||
|
||||
function latex{l}(io::IO, header::Header{l})
|
||||
tag = l < 4 ? "sub"^(l-1) * "section" : "sub"^(l-4) * "paragraph"
|
||||
wrapinline(io, tag) do
|
||||
latexinline(io, header.text)
|
||||
end
|
||||
println(io)
|
||||
end
|
||||
|
||||
function latex(io::IO, code::Code)
|
||||
wrapblock(io, "verbatim") do
|
||||
# TODO latex escape
|
||||
println(io, code.code)
|
||||
end
|
||||
end
|
||||
|
||||
function latexinline(io::IO, code::Code)
|
||||
wrapinline(io, "texttt") do
|
||||
print(io, latexesc(code.code))
|
||||
end
|
||||
end
|
||||
|
||||
function latex(io::IO, md::Paragraph)
|
||||
for md in md.content
|
||||
latexinline(io, md)
|
||||
end
|
||||
println(io)
|
||||
println(io)
|
||||
end
|
||||
|
||||
function latex(io::IO, md::BlockQuote)
|
||||
wrapblock(io, "quote") do
|
||||
latex(io, md.content)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function latex(io::IO, f::Footnote)
|
||||
print(io, "\\footnotetext[", f.id, "]{")
|
||||
latex(io, f.text)
|
||||
println(io, "}")
|
||||
end
|
||||
|
||||
function latex(io::IO, md::Admonition)
|
||||
wrapblock(io, "quote") do
|
||||
wrapinline(io, "textbf") do
|
||||
print(io, md.category)
|
||||
end
|
||||
println(io, "\n\n", md.title, "\n")
|
||||
latex(io, md.content)
|
||||
end
|
||||
end
|
||||
|
||||
function latex(io::IO, md::List)
|
||||
# `\begin{itemize}` is used here for both ordered and unordered lists since providing
|
||||
# custom starting numbers for enumerated lists is simpler to do by manually assigning
|
||||
# each number to `\item` ourselves rather than using `\setcounter{enumi}{<start>}`.
|
||||
#
|
||||
# For an ordered list starting at 5 the following will be generated:
|
||||
#
|
||||
# \begin{itemize}
|
||||
# \item[5. ] ...
|
||||
# \item[6. ] ...
|
||||
# ...
|
||||
# \end{itemize}
|
||||
#
|
||||
pad = ndigits(md.ordered + length(md.items)) + 2
|
||||
fmt = n -> (isordered(md) ? "[$(rpad("$(n + md.ordered - 1).", pad))]" : "")
|
||||
wrapblock(io, "itemize") do
|
||||
for (n, item) in enumerate(md.items)
|
||||
print(io, "\\item$(fmt(n)) ")
|
||||
latex(io, item)
|
||||
n < length(md.items) && println(io)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function show(io::IO, ::MIME"text/latex", md::HorizontalRule)
|
||||
println(io, "\\rule{\\textwidth}{1pt}")
|
||||
end
|
||||
|
||||
# Inline elements
|
||||
|
||||
function latexinline(io::IO, md::Vector)
|
||||
for c in md
|
||||
latexinline(io, c)
|
||||
end
|
||||
end
|
||||
|
||||
function latexinline(io::IO, md::AbstractString)
|
||||
latexesc(io, md)
|
||||
end
|
||||
|
||||
function latexinline(io::IO, md::Bold)
|
||||
wrapinline(io, "textbf") do
|
||||
latexinline(io, md.text)
|
||||
end
|
||||
end
|
||||
|
||||
function latexinline(io::IO, md::Italic)
|
||||
wrapinline(io, "emph") do
|
||||
latexinline(io, md.text)
|
||||
end
|
||||
end
|
||||
|
||||
function latexinline(io::IO, md::Image)
|
||||
wrapblock(io, "figure") do
|
||||
println(io, "\\centering")
|
||||
wrapinline(io, "includegraphics") do
|
||||
print(io, md.url)
|
||||
end
|
||||
println(io)
|
||||
wrapinline(io, "caption") do
|
||||
latexinline(io, md.alt)
|
||||
end
|
||||
println(io)
|
||||
end
|
||||
end
|
||||
|
||||
latexinline(io::IO, f::Footnote) = print(io, "\\footnotemark[", f.id, "]")
|
||||
|
||||
function latexinline(io::IO, md::Link)
|
||||
wrapinline(io, "href") do
|
||||
print(io, md.url)
|
||||
end
|
||||
print(io, "{")
|
||||
latexinline(io, md.text)
|
||||
print(io, "}")
|
||||
end
|
||||
|
||||
const _latexescape_chars = Dict{Char, AbstractString}(
|
||||
'~'=>"{\\sim}", '^'=>"\\^{}", '\\'=>"{\\textbackslash}")
|
||||
for ch in "&%\$#_{}"
|
||||
_latexescape_chars[ch] = "\\$ch"
|
||||
end
|
||||
|
||||
function latexesc(io, s::AbstractString)
|
||||
for ch in s
|
||||
print(io, get(_latexescape_chars, ch, ch))
|
||||
end
|
||||
end
|
||||
|
||||
latex(md) = sprint(latex, md)
|
||||
latexinline(md) = sprint(latexinline, md)
|
||||
latexesc(s) = sprint(latexesc, s)
|
||||
|
||||
show(io::IO, ::MIME"text/latex", md::MD) = latex(io, md)
|
||||
140
julia-0.6.3/share/julia/base/markdown/render/plain.jl
Normal file
140
julia-0.6.3/share/julia/base/markdown/render/plain.jl
Normal file
@@ -0,0 +1,140 @@
|
||||
# This file is a part of Julia. License is MIT: https://julialang.org/license
|
||||
|
||||
plain(x) = sprint(plain, x)
|
||||
|
||||
function plain(io::IO, content::Vector)
|
||||
isempty(content) && return
|
||||
for md in content[1:end-1]
|
||||
plain(io, md)
|
||||
println(io)
|
||||
end
|
||||
plain(io, content[end])
|
||||
end
|
||||
|
||||
plain(io::IO, md::MD) = plain(io, md.content)
|
||||
|
||||
function plain(io::IO, header::Header{l}) where l
|
||||
print(io, "#"^l*" ")
|
||||
plaininline(io, header.text)
|
||||
println(io)
|
||||
end
|
||||
|
||||
function plain(io::IO, code::Code)
|
||||
# If the code includes a fenced block this will break parsing,
|
||||
# so it must be enclosed by a longer ````-sequence.
|
||||
n = mapreduce(length, max, 2, matchall(r"^`+"m, code.code)) + 1
|
||||
println(io, "`" ^ n, code.language)
|
||||
println(io, code.code)
|
||||
println(io, "`" ^ n)
|
||||
end
|
||||
|
||||
function plain(io::IO, p::Paragraph)
|
||||
plaininline(io, p.content)
|
||||
println(io)
|
||||
end
|
||||
|
||||
function plain(io::IO, list::List)
|
||||
for (i, item) in enumerate(list.items)
|
||||
print(io, isordered(list) ? "$(i + list.ordered - 1). " : " * ")
|
||||
lines = split(rstrip(sprint(plain, item)), "\n")
|
||||
for (n, line) in enumerate(lines)
|
||||
print(io, (n == 1 || isempty(line)) ? "" : " ", line)
|
||||
n < length(lines) && println(io)
|
||||
end
|
||||
println(io)
|
||||
end
|
||||
end
|
||||
|
||||
function plain(io::IO, q::BlockQuote)
|
||||
s = sprint(plain, q.content)
|
||||
for line in split(rstrip(s), "\n")
|
||||
println(io, isempty(line) ? ">" : "> ", line)
|
||||
end
|
||||
println(io)
|
||||
end
|
||||
|
||||
function plain(io::IO, f::Footnote)
|
||||
print(io, "[^", f.id, "]:")
|
||||
s = sprint(plain, f.text)
|
||||
lines = split(rstrip(s), "\n")
|
||||
# Single line footnotes are printed on the same line as their label
|
||||
# rather than taking up an additional line.
|
||||
if length(lines) == 1
|
||||
println(io, " ", lines[1])
|
||||
else
|
||||
println(io)
|
||||
for line in lines
|
||||
println(io, isempty(line) ? "" : " ", line)
|
||||
end
|
||||
println(io)
|
||||
end
|
||||
end
|
||||
|
||||
function plain(io::IO, md::Admonition)
|
||||
s = sprint(plain, md.content)
|
||||
title = md.title == ucfirst(md.category) ? "" : " \"$(md.title)\""
|
||||
println(io, "!!! ", md.category, title)
|
||||
for line in split(rstrip(s), "\n")
|
||||
println(io, isempty(line) ? "" : " ", line)
|
||||
end
|
||||
println(io)
|
||||
end
|
||||
|
||||
function plain(io::IO, md::HorizontalRule)
|
||||
println(io, "-" ^ 3)
|
||||
end
|
||||
|
||||
function plain(io::IO, l::LaTeX)
|
||||
println(io, '$', '$')
|
||||
println(io, l.formula)
|
||||
println(io, '$', '$')
|
||||
end
|
||||
|
||||
function plain(io::IO, md)
|
||||
show(io, MIME"text/plain"(), md)
|
||||
println(io)
|
||||
end
|
||||
|
||||
# Inline elements
|
||||
|
||||
plaininline(x) = sprint(plaininline, x)
|
||||
|
||||
function plaininline(io::IO, md...)
|
||||
for el in md
|
||||
plaininline(io, el)
|
||||
end
|
||||
end
|
||||
|
||||
plaininline(io::IO, md::Vector) = !isempty(md) && plaininline(io, md...)
|
||||
|
||||
plaininline(io::IO, f::Footnote) = print(io, "[^", f.id, "]")
|
||||
|
||||
plaininline(io::IO, link::Link) = plaininline(io, "[", link.text, "](", link.url, ")")
|
||||
|
||||
plaininline(io::IO, md::Image) = plaininline(io, "")
|
||||
|
||||
plaininline(io::IO, s::AbstractString) = print(io, s)
|
||||
|
||||
plaininline(io::IO, md::Bold) = plaininline(io, "**", md.text, "**")
|
||||
|
||||
plaininline(io::IO, md::Italic) = plaininline(io, "*", md.text, "*")
|
||||
|
||||
function plaininline(io::IO, md::Code)
|
||||
if contains(md.code, "`")
|
||||
n = maximum(length(m) for m in matchall(r"(`+)", md.code))
|
||||
s = "`"^((iseven(n) ? 1 : 2) + n)
|
||||
print(io, s, Base.startswith(md.code, "`") ? " " : "")
|
||||
print(io, md.code, endswith(md.code, "`") ? " " : "", s)
|
||||
else
|
||||
print(io, "`", md.code, "`")
|
||||
end
|
||||
end
|
||||
|
||||
plaininline(io::IO, br::LineBreak) = println(io)
|
||||
|
||||
plaininline(io::IO, x) = show(io, MIME"text/plain"(), x)
|
||||
|
||||
# show
|
||||
|
||||
Base.show(io::IO, md::MD) = plain(io, md)
|
||||
Base.show(io::IO, ::MIME"text/markdown", md::MD) = plain(io, md)
|
||||
30
julia-0.6.3/share/julia/base/markdown/render/rich.jl
Normal file
30
julia-0.6.3/share/julia/base/markdown/render/rich.jl
Normal file
@@ -0,0 +1,30 @@
|
||||
# This file is a part of Julia. License is MIT: https://julialang.org/license
|
||||
|
||||
function tohtml(io::IO, m::MIME"text/html", x)
|
||||
show(io, m, x)
|
||||
end
|
||||
|
||||
function tohtml(io::IO, m::MIME"text/plain", x)
|
||||
htmlesc(io, sprint(show, m, x))
|
||||
end
|
||||
|
||||
function tohtml(io::IO, m::MIME"image/png", img)
|
||||
print(io, """<img src="data:image/png;base64,""")
|
||||
print(io, stringmime(m, img))
|
||||
print(io, "\" />")
|
||||
end
|
||||
|
||||
function tohtml(io::IO, m::MIME"image/svg+xml", img)
|
||||
show(io, m, img)
|
||||
end
|
||||
|
||||
# Display infrastructure
|
||||
|
||||
function bestmime(val)
|
||||
for mime in ("text/html", "image/svg+xml", "image/png", "text/plain")
|
||||
mimewritable(mime, val) && return MIME(Symbol(mime))
|
||||
end
|
||||
error("Cannot render $val to Markdown.")
|
||||
end
|
||||
|
||||
tohtml(io::IO, x) = tohtml(io, bestmime(x), x)
|
||||
145
julia-0.6.3/share/julia/base/markdown/render/rst.jl
Normal file
145
julia-0.6.3/share/julia/base/markdown/render/rst.jl
Normal file
@@ -0,0 +1,145 @@
|
||||
# This file is a part of Julia. License is MIT: https://julialang.org/license
|
||||
|
||||
rst(x) = sprint(rst, x)
|
||||
|
||||
function rst(io::IO, content::Vector)
|
||||
isempty(content) && return
|
||||
for md in content[1:end-1]
|
||||
rst(io, md)
|
||||
println(io)
|
||||
end
|
||||
rst(io, content[end])
|
||||
end
|
||||
|
||||
rst(io::IO, md::MD) = rst(io, md.content)
|
||||
|
||||
function rst{l}(io::IO, header::Header{l})
|
||||
s = rstinline(header.text)
|
||||
println(io, s)
|
||||
println(io, string("*=-~:.^"[l])^length(s))
|
||||
println(io)
|
||||
end
|
||||
|
||||
function rst(io::IO, code::Code)
|
||||
if code.language == "jldoctest"
|
||||
println(io, ".. doctest::\n")
|
||||
elseif code.language != "rst"
|
||||
println(io, ".. code-block:: julia\n")
|
||||
end
|
||||
for l in lines(code.code)
|
||||
println(io, " ", l)
|
||||
end
|
||||
end
|
||||
|
||||
function rst(io::IO, p::Paragraph)
|
||||
rstinline(io, p.content)
|
||||
println(io)
|
||||
end
|
||||
|
||||
function rst(io::IO, list::List)
|
||||
for (i, item) in enumerate(list.items)
|
||||
bullet = isordered(list) ? "$(i + list.ordered - 1). " : "* "
|
||||
print(io, bullet)
|
||||
lines = split(rstrip(sprint(rst, item)), '\n')
|
||||
for (n, line) in enumerate(lines)
|
||||
print(io, (n == 1 || isempty(line)) ? "" : " "^length(bullet), line)
|
||||
n < length(lines) && println(io)
|
||||
end
|
||||
println(io)
|
||||
end
|
||||
end
|
||||
|
||||
function rst(io::IO, q::BlockQuote)
|
||||
s = sprint(rst, q.content)
|
||||
for line in split(rstrip(s), "\n")
|
||||
println(io, " ", line)
|
||||
end
|
||||
println(io)
|
||||
end
|
||||
|
||||
function rst(io::IO, f::Footnote)
|
||||
print(io, ".. [", f.id, "]")
|
||||
s = sprint(rst, f.text)
|
||||
lines = split(rstrip(s), "\n")
|
||||
# Single line footnotes are printed on the same line as their label
|
||||
# rather than taking up an additional line.
|
||||
if length(lines) == 1
|
||||
println(io, " ", lines[1])
|
||||
else
|
||||
println(io)
|
||||
for line in lines
|
||||
println(io, isempty(line) ? "" : " ", rstrip(line))
|
||||
end
|
||||
println(io)
|
||||
end
|
||||
end
|
||||
|
||||
function rst(io::IO, md::Admonition)
|
||||
s = sprint(rst, md.content)
|
||||
title = md.title == ucfirst(md.category) ? "" : md.title
|
||||
println(io, ".. ", md.category, "::", isempty(title) ? "" : " $title")
|
||||
for line in split(rstrip(s), "\n")
|
||||
println(io, isempty(line) ? "" : " ", line)
|
||||
end
|
||||
println(io)
|
||||
end
|
||||
|
||||
function rst(io::IO, md::HorizontalRule)
|
||||
println(io, "–" ^ 5)
|
||||
end
|
||||
|
||||
function rst(io::IO, l::LaTeX)
|
||||
println(io, ".. math::\n")
|
||||
for l in lines(l.formula)
|
||||
println(io, " ", l)
|
||||
end
|
||||
end
|
||||
|
||||
rst(io::IO, md) = show(io, "text/rst", md)
|
||||
|
||||
# Inline elements
|
||||
|
||||
rstinline(x) = sprint(rstinline, x)
|
||||
|
||||
function rstinline(io::IO, md...)
|
||||
wasCode = false
|
||||
for el in md
|
||||
wasCode && isa(el, AbstractString) && !Base.startswith(el, " ") && print(io, "\\ ")
|
||||
wasCode = (isa(el, Code) || isa(el, LaTeX) || isa(el, Link)) && (wasCode = true)
|
||||
rstinline(io, el)
|
||||
end
|
||||
end
|
||||
|
||||
rstinline(io::IO, md::Vector) = !isempty(md) && rstinline(io, md...)
|
||||
|
||||
# rstinline(io::IO, md::Image) = rstinline(io, ".. image:: ", md.url)
|
||||
|
||||
function rstinline(io::IO, md::Link)
|
||||
if ismatch(r":(func|obj|ref|exc|class|const|data):`\.*", md.url)
|
||||
rstinline(io, md.url)
|
||||
else
|
||||
rstinline(io, "`", md.text, " <", md.url, ">`_")
|
||||
end
|
||||
end
|
||||
|
||||
rstinline(io::IO, f::Footnote) = print(io, "[", f.id, "]_")
|
||||
|
||||
rstescape(s) = replace(s, "\\", "\\\\")
|
||||
|
||||
rstinline(io::IO, s::AbstractString) = print(io, rstescape(s))
|
||||
|
||||
rstinline(io::IO, md::Bold) = rstinline(io, "**", md.text, "**")
|
||||
|
||||
rstinline(io::IO, md::Italic) = rstinline(io, "*", md.text, "*")
|
||||
|
||||
rstinline(io::IO, md::Code) = print(io, "``", md.code, "``")
|
||||
|
||||
rstinline(io::IO, br::LineBreak) = println(io)
|
||||
|
||||
rstinline(io::IO, l::LaTeX) = print(io, ":math:`", l.formula, "`")
|
||||
|
||||
rstinline(io::IO, x) = show(io, MIME"text/rst"(), x)
|
||||
|
||||
# show
|
||||
|
||||
Base.show(io::IO, ::MIME"text/rst", md::MD) = rst(io, md)
|
||||
@@ -0,0 +1,107 @@
|
||||
# This file is a part of Julia. License is MIT: https://julialang.org/license
|
||||
|
||||
# Styles
|
||||
|
||||
const text_formats = Dict(
|
||||
:black => ("\e[30m", "\e[39m"),
|
||||
:red => ("\e[31m", "\e[39m"),
|
||||
:green => ("\e[32m", "\e[39m"),
|
||||
:yellow => ("\e[33m", "\e[39m"),
|
||||
:blue => ("\e[34m", "\e[39m"),
|
||||
:magenta => ("\e[35m", "\e[39m"),
|
||||
:cyan => ("\e[36m", "\e[39m"),
|
||||
:white => ("\e[37m", "\e[39m"),
|
||||
:reset => ("\e[0m", "\e[0m"),
|
||||
:bold => ("\e[1m", "\e[22m"),
|
||||
:underline => ("\e[4m", "\e[24m"),
|
||||
:blink => ("\e[5m", "\e[25m"),
|
||||
:negative => ("\e[7m", "\e[27m"))
|
||||
|
||||
function with_output_format(f::Function, formats::Vector{Symbol}, io::IO, args...)
|
||||
Base.have_color && for format in formats
|
||||
haskey(text_formats, format) &&
|
||||
print(io, text_formats[format][1])
|
||||
end
|
||||
try f(io, args...)
|
||||
finally
|
||||
Base.have_color && for format in formats
|
||||
haskey(text_formats, format) &&
|
||||
print(io, text_formats[format][2])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
with_output_format(f::Function, format::Symbol, args...) =
|
||||
with_output_format(f, [format], args...)
|
||||
|
||||
with_output_format(format, f::Function, args...) =
|
||||
with_output_format(f, format, args...)
|
||||
|
||||
function print_with_format(format, io::IO, x)
|
||||
with_output_format(format, io) do io
|
||||
print(io, x)
|
||||
end
|
||||
end
|
||||
|
||||
function println_with_format(format, io::IO, x)
|
||||
print_with_format(format, io, x)
|
||||
println(io)
|
||||
end
|
||||
|
||||
# Wrapping
|
||||
|
||||
function ansi_length(s)
|
||||
replace(s, r"\e\[[0-9]+m", "") |> length
|
||||
end
|
||||
|
||||
words(s) = split(s, " ")
|
||||
lines(s) = split(s, "\n")
|
||||
|
||||
# This could really be more efficient
|
||||
function wrapped_lines(s::AbstractString; width = 80, i = 0)
|
||||
if ismatch(r"\n", s)
|
||||
return vcat(map(s->wrapped_lines(s, width = width, i = i), split(s, "\n"))...)
|
||||
end
|
||||
ws = words(s)
|
||||
lines = AbstractString[ws[1]]
|
||||
i += ws[1] |> ansi_length
|
||||
for word in ws[2:end]
|
||||
word_length = ansi_length(word)
|
||||
if i + word_length + 1 > width
|
||||
i = word_length
|
||||
push!(lines, word)
|
||||
else
|
||||
i += word_length + 1
|
||||
lines[end] *= " " * word
|
||||
end
|
||||
end
|
||||
return lines
|
||||
end
|
||||
|
||||
wrapped_lines(f::Function, args...; width = 80, i = 0) =
|
||||
wrapped_lines(sprint(f, args...), width = width, i = 0)
|
||||
|
||||
function print_wrapped(io::IO, s...; width = 80, pre = "", i = 0)
|
||||
lines = wrapped_lines(s..., width = width, i = i)
|
||||
println(io, lines[1])
|
||||
for line in lines[2:end]
|
||||
println(io, pre, line)
|
||||
end
|
||||
length(lines), length(pre) + ansi_length(lines[end])
|
||||
end
|
||||
|
||||
print_wrapped(f::Function, io::IO, args...; kws...) = print_wrapped(io, f, args...; kws...)
|
||||
|
||||
function print_centred(io::IO, s...; columns = 80, width = columns)
|
||||
lines = wrapped_lines(s..., width = width)
|
||||
for line in lines
|
||||
print(io, " "^(div(columns-ansi_length(line), 2)))
|
||||
println(io, line)
|
||||
end
|
||||
length(lines), length(pre) + length(lines[end])
|
||||
end
|
||||
|
||||
function centred(s, columns)
|
||||
pad = div(columns - ansi_length(s), 2)
|
||||
" "^pad * s
|
||||
end
|
||||
148
julia-0.6.3/share/julia/base/markdown/render/terminal/render.jl
Normal file
148
julia-0.6.3/share/julia/base/markdown/render/terminal/render.jl
Normal file
@@ -0,0 +1,148 @@
|
||||
# This file is a part of Julia. License is MIT: https://julialang.org/license
|
||||
|
||||
include("formatting.jl")
|
||||
|
||||
const margin = 2
|
||||
cols(io) = displaysize(io)[2]
|
||||
|
||||
function term(io::IO, content::Vector, cols)
|
||||
isempty(content) && return
|
||||
for md in content[1:end-1]
|
||||
term(io, md, cols)
|
||||
println(io)
|
||||
end
|
||||
term(io, content[end], cols)
|
||||
end
|
||||
|
||||
term(io::IO, md::MD, columns = cols(io)) = term(io, md.content, columns)
|
||||
|
||||
function term(io::IO, md::Paragraph, columns)
|
||||
print(io, " "^margin)
|
||||
print_wrapped(io, width = columns-2margin, pre = " "^margin) do io
|
||||
terminline(io, md.content)
|
||||
end
|
||||
end
|
||||
|
||||
function term(io::IO, md::BlockQuote, columns)
|
||||
s = sprint(term, md.content, columns - 10)
|
||||
for line in split(rstrip(s), "\n")
|
||||
println(io, " "^margin, "|", line)
|
||||
end
|
||||
end
|
||||
|
||||
function term(io::IO, md::Admonition, columns)
|
||||
print(io, " "^margin, "| ")
|
||||
with_output_format(:bold, print, io, isempty(md.title) ? md.category : md.title)
|
||||
println(io, "\n", " "^margin, "|")
|
||||
s = sprint(term, md.content, columns - 10)
|
||||
for line in split(rstrip(s), "\n")
|
||||
println(io, " "^margin, "|", line)
|
||||
end
|
||||
end
|
||||
|
||||
function term(io::IO, f::Footnote, columns)
|
||||
print(io, " "^margin, "| ")
|
||||
with_output_format(:bold, print, io, "[^$(f.id)]")
|
||||
println(io, "\n", " "^margin, "|")
|
||||
s = sprint(term, f.text, columns - 10)
|
||||
for line in split(rstrip(s), "\n")
|
||||
println(io, " "^margin, "|", line)
|
||||
end
|
||||
end
|
||||
|
||||
function term(io::IO, md::List, columns)
|
||||
for (i, point) in enumerate(md.items)
|
||||
print(io, " "^2margin, isordered(md) ? "$(i + md.ordered - 1). " : "• ")
|
||||
print_wrapped(io, width = columns-(4margin+2), pre = " "^(2margin+2),
|
||||
i = 2margin+2) do io
|
||||
term(io, point, columns - 10)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function _term_header(io::IO, md, char, columns)
|
||||
text = terminline(md.text)
|
||||
with_output_format(:bold, io) do io
|
||||
print(io, " "^(2margin), " ")
|
||||
line_no, lastline_width = print_wrapped(io, text,
|
||||
width=columns - 4margin; pre=" ")
|
||||
line_width = min(1 + lastline_width, columns)
|
||||
if line_no > 1
|
||||
line_width = max(line_width, div(columns, 3))
|
||||
end
|
||||
char != ' ' && println(io, " "^(2margin), string(char) ^ line_width)
|
||||
end
|
||||
end
|
||||
|
||||
const _header_underlines = collect("≡=–-⋅ ")
|
||||
# TODO settle on another option with unicode e.g. "≡=≃–∼⋅" ?
|
||||
|
||||
function term{l}(io::IO, md::Header{l}, columns)
|
||||
underline = _header_underlines[l]
|
||||
_term_header(io, md, underline, columns)
|
||||
end
|
||||
|
||||
function term(io::IO, md::Code, columns)
|
||||
with_output_format(:cyan, io) do io
|
||||
for line in lines(md.code)
|
||||
print(io, " "^margin)
|
||||
println(io, line)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function term(io::IO, br::LineBreak, columns)
|
||||
println(io)
|
||||
end
|
||||
|
||||
function term(io::IO, br::HorizontalRule, columns)
|
||||
println(io, " " ^ margin, "-" ^ (columns - 2margin))
|
||||
end
|
||||
|
||||
term(io::IO, x, _) = show(io, MIME"text/plain"(), x)
|
||||
|
||||
# Inline Content
|
||||
|
||||
terminline(md) = sprint(terminline, md)
|
||||
|
||||
function terminline(io::IO, content::Vector)
|
||||
for md in content
|
||||
terminline(io, md)
|
||||
end
|
||||
end
|
||||
|
||||
function terminline(io::IO, md::AbstractString)
|
||||
print(io, replace(md, r"[\s\t\n]+", " "))
|
||||
end
|
||||
|
||||
function terminline(io::IO, md::Bold)
|
||||
with_output_format(:bold, terminline, io, md.text)
|
||||
end
|
||||
|
||||
function terminline(io::IO, md::Italic)
|
||||
with_output_format(:underline, terminline, io, md.text)
|
||||
end
|
||||
|
||||
function terminline(io::IO, md::LineBreak)
|
||||
println(io)
|
||||
end
|
||||
|
||||
function terminline(io::IO, md::Image)
|
||||
terminline(io, "(Image: $(md.alt))")
|
||||
end
|
||||
|
||||
terminline(io::IO, f::Footnote) = with_output_format(:bold, terminline, io, "[^$(f.id)]")
|
||||
|
||||
function terminline(io::IO, md::Link)
|
||||
terminline(io, md.text)
|
||||
end
|
||||
|
||||
function terminline(io::IO, code::Code)
|
||||
print_with_format(:cyan, io, code.code)
|
||||
end
|
||||
|
||||
terminline(io::IO, x) = show(io, MIME"text/plain"(), x)
|
||||
|
||||
# Show in terminal
|
||||
|
||||
Base.display(d::Base.REPL.REPLDisplay, md::MD) = term(Base.REPL.outstream(d.repl), md)
|
||||
Reference in New Issue
Block a user