-
Notifications
You must be signed in to change notification settings - Fork 1
/
markdown.jl
136 lines (123 loc) · 5.26 KB
/
markdown.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
@use "github.com/jkroso/DOM.jl" => DOM @dom @css_str ["html.jl"] ["latex.jl"] add_attr
@use "github.com/jkroso/Prospects.jl" flat
@use LaTeXStrings: LaTeXString
@use Markdown
@use Atom
renderMD(v::Vector) = @dom[:div map(renderMD, v)...]
renderMD(s::AbstractString) = @dom[:p parse(MIME("text/html"), s)]
renderMD(p::Markdown.Paragraph) = @dom[:p map(renderMDinline, flat(p.content))...]
renderMD(b::Markdown.BlockQuote) = @dom[:blockquote map(renderMD, flat(b.content))...]
renderMD(l::Markdown.LaTeX) = @dom[:latex class="latex block" css"""
display: flex
align-items: center
flex-direction: column
margin: 2em 0
""" block=true LaTeXString(l.formula)]
renderMD(l::Markdown.Link) = @dom[:a href=l.url l.text]
renderMD(::Markdown.HorizontalRule) = @dom[:hr]
renderMD(h::Markdown.Header{l}) where l =
DOM.Container{Symbol(:h, l)}(DOM.Attrs(), map(renderMDinline, flat(h.text)))
"highlights using Atoms own highlighter when possible"
highlight(src, language) = begin
if haskey(ENV, "ATOM_HOME")
grammer = isempty(language) ? "text.plain" : "source.$language"
Atom.@rpc highlight((src=src, grammer=grammer, block=true))
elseif isempty(language)
"<pre>$src</pre>"
else
read(pipeline(IOBuffer(src), `pygmentize -f html -O "noclasses" -l $language`), String)
end
end
renderMD(c::Markdown.Code) = begin
html = highlight(c.code, c.language)
dom = parse(MIME("text/html"), html)
add_attr(dom, :class, css"""
display: flex
flex-direction: column
border-radius: 5px
font: 1em SourceCodePro-light
padding: 0.6em
margin: 0
""")
end
renderMD(f::Markdown.Footnote) =
@dom[:div class="footnote" id="footnote-$(f.id)"
[:p class="footnote-title" f.id]
renderMD(f.text)]
renderMD(md::Markdown.Admonition) =
@dom[:div class="admonition $(md.category)"
[:p class="admonition-title $(md.category == "warning" ? "icon-alert" : "icon-info")" md.title]
renderMD(md.content)]
renderMD(md::Markdown.List) =
DOM.Container{Markdown.isordered(md) ? :ol : :ul}(
DOM.Attrs(:start=>md.ordered > 1 ? string(md.ordered) : ""),
map(renderListItem, md.items))
renderListItem(v::Vector) = @dom[:li map(renderMDinline, v)...]
renderListItem(item) = @dom[:li renderMDinline(item)]
renderListItem(item::Markdown.Paragraph) = begin
content = item.content
first, rest = content[1], content[2:end]
m = first isa AbstractString ? match(r"^ *\[(x| )\] (.*)", first) : nothing
if m != nothing
@dom[:li class="task" css"""
list-style: none
> input[type="checkbox"]
height: 1em
margin: 0 0.5em 0 -2em
"""
[:input type="checkbox" checked=m.captures[1] == "x"]
[:label renderMDinline(m.captures[2])]
map(renderMDinline, rest)...]
else
@dom[:li map(renderMDinline, content)...]
end
end
renderMD(md::Markdown.Table) = begin
align = map(md.align) do s
s == :c && return "center"
s == :r && return "right"
s == :l && return "left"
end
@dom[:table css"""
border-collapse: collapse
border-spacing: 0
empty-cells: show
border: 1px solid #cbcbcb
> thead
background-color: #e0e0e0
color: #000
vertical-align: bottom
> thead > tr > th, > tbody > tr > td
font-size: inherit
margin: 0
overflow: visible
padding: 0.5em 1em
border-width: 0 0 1px 0
> tbody > tr:last-child > td
border-bottom-width: 0
"""
[:thead
[:tr (@dom[:th align=align[i] renderMDinline(column)]
for (i, column) in enumerate(md.rows[1]))...]]
[:tbody
map(md.rows[2:end]) do row
@dom[:tr (@dom[:td align=align[i] renderMDinline(column)]
for (i, column) in enumerate(row))...]
end...]]
end
renderMDinline(x) = renderMD(x)
renderMDinline(v::Vector) =
length(v) == 1 ? renderMDinline(v[1]) : @dom[:span map(renderMDinline, v)...]
renderMDinline(md::Union{Symbol,AbstractString}) = parse(MIME("text/html"), string(md))
renderMDinline(md::Markdown.Bold) = @dom[:b renderMDinline(md.text)]
renderMDinline(md::Markdown.Italic) = @dom[:em renderMDinline(md.text)]
renderMDinline(md::Markdown.Image) = @dom[:img src=md.url alt=md.alt]
renderMDinline(l::Markdown.Link) = @dom[:a href=l.url renderMDinline(l.text)]
renderMDinline(::Markdown.LineBreak) = @dom[:br]
renderMDinline(p::Markdown.Paragraph) = renderMD(p)
renderMDinline(f::Markdown.Footnote) =
@dom[:a href="#footnote-$(f.id)" class="footnote" [:span "[$(f.id)]"]]
renderMDinline(code::Markdown.Code) =
@dom[:code class="inline" block=false code.code]
renderMDinline(md::Markdown.LaTeX) =
@dom[:latex class="latex inline" block=false LaTeXString(md.formula)]