• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / 1404

11 Jan 2026 12:09AM UTC coverage: 76.724% (+0.04%) from 76.683%
1404

push

buildkite

web-flow
Add `@stm` "SyntaxTree match" macro (#60475)

62704 of 81727 relevant lines covered (76.72%)

23123244.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.04
/stdlib/Markdown/src/render/html.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
include("rich.jl")
4

5
# Utils
6

7

8
# Handle URI encoding, poorly. The code here was initially copied from
9
# Base.Filesystem, then modified. The original code implements RFC3986 Section
10
# 2.1 and 2.2.
11
#
12
# We changed encode_uri_component to not encode characters declared as
13
# "reserved" by RFC3986 Section 2.2 (i.e., those from the gen-delims and
14
# sub-delims sets). Instead the user is expected to percent encode these (or
15
# not) as needed -- the alternative would be implement full URI parsing, which
16
# is non-trivial. That said, it would be better to do that, by e.g. using the
17
# URIs.jl package, but that is not an option for us, as it is not a stdlib...
18
#
19
# As a special affordance, we deviate from the "reserved" list in one way: we
20
# do *not* exclude '[' and ']' from percent encoding, even though they are in
21
# the gen-delims set. They are only used to encode IPv6 literal addresses in
22
# the URI, which is (still) rare. But they do occur in query strings and
23
# indeed in the CommonMark spec tests.
24

25
percent_escape(s) = '%' * join(map(b -> uppercase(string(b, base=16)), codeunits(s)), '%')
3,771✔
26
encode_uri_component(s::AbstractString) = replace(s, r"[^A-Za-z0-9\-_.~/:?#@!$&'()*+,;=]+" => percent_escape)
2,028✔
27
encode_uri_component(s::Symbol) = encode_uri_component(string(s))
×
28

29
function withtag(f, io::IO, tag, attrs...)
43,851✔
30
    print(io, "<$tag")
45,057✔
31
    for (attr, value) in attrs
45,057✔
32
        print(io, " ", attr, "=\"")
3,003✔
33
        htmlesc(io, value)
3,165✔
34
        print(io, "\"")
3,003✔
35
    end
3,297✔
36
    f === nothing && return print(io, " />")
45,057✔
37

38
    print(io, ">")
43,581✔
39
    f()
45,942✔
40
    print(io, "</$tag>")
43,581✔
41
end
42

43
tag(io::IO, tag, attrs...) = withtag(nothing, io, tag, attrs...)
1,476✔
44

45
function htmlesc(io::IO, s::AbstractString)
46
    replace(io, s, '<'=>"&lt;", '>'=>"&gt;", '"'=>"&quot;", '&'=>"&amp;")
36,945✔
47
end
48
function htmlesc(io::IO, s::Symbol)
9✔
49
    htmlesc(io, string(s))
9✔
50
end
51
function htmlesc(io::IO, xs::Union{AbstractString,Symbol}...)
×
52
    for s in xs
×
53
        htmlesc(io, s)
×
54
    end
×
55
end
56
function htmlesc(s::Union{AbstractString,Symbol})
×
57
    sprint(htmlesc, s)
×
58
end
59

60
# Block elements
61

62
function html(io::IO, content::Vector)
24,129✔
63
    for md in content
24,129✔
64
        html(io, md)
32,454✔
65
        println(io)
32,454✔
66
    end
32,454✔
67
end
68

69
html(io::IO, md::MD) = html(io, md.content)
18,075✔
70

71
function html(io::IO, header::Header{l}) where l
1,554✔
72
    withtag(io, "h$l") do
1,554✔
73
        htmlinline(io, header.text)
1,554✔
74
    end
75
end
76

77
function html(io::IO, code′::Code)
2,061✔
78
    if code′.language == "styled"
2,061✔
79
        code′ = Code("", String(styled(code′.code)))
×
80
    end
81
    code = code′
2,061✔
82
    withtag(io, :pre) do
2,061✔
83
        maybe_lang = !isempty(code.language) ? Any[:class=>"language-$(code.language)"] : []
3,951✔
84
        withtag(io, :code, maybe_lang...) do
2,061✔
85
            htmlesc(io, code.code)
2,382✔
86
            !isempty(code.code) && println(io)
2,061✔
87
        end
88
    end
89
end
90

91
function html(io::IO, md::Paragraph)
21,483✔
92
    withtag(io, :p) do
21,483✔
93
        htmlinline(io, md.content)
21,483✔
94
    end
95
end
96

97
function html(io::IO, md::HTML)
1,512✔
98
    for line in md.content[1:end-1]
1,512✔
99
        println(io, line)
2,322✔
100
    end
2,322✔
101
    print(io, md.content[end])
1,512✔
102
end
103

104
function html(io::IO, md::BlockQuote)
1,551✔
105
    withtag(io, :blockquote) do
1,551✔
106
        println(io)
1,551✔
107
        html(io, md.content)
1,551✔
108
    end
109
end
110

111
function html(io::IO, f::Footnote)
12✔
112
    withtag(io, :div, :class => "footnote", :id => "footnote-$(f.id)") do
12✔
113
        withtag(io, :p, :class => "footnote-title") do
12✔
114
            print(io, f.id)
12✔
115
        end
116
        html(io, f.text)
12✔
117
    end
118
end
119

120
function html(io::IO, md::Admonition)
15✔
121
    withtag(io, :div, :class => "admonition $(md.category)") do
15✔
122
        withtag(io, :p, :class => "admonition-title") do
15✔
123
            print(io, md.title)
15✔
124
        end
125
        html(io, md.content)
15✔
126
    end
127
end
128

129
function html(io::IO, md::List)
3,108✔
130
    maybe_attr = md.ordered > 1 ? Any[:start => string(md.ordered)] : []
5,925✔
131
    withtag(io, isordered(md) ? :ol : :ul, maybe_attr...) do
3,108✔
132
        for item in md.items
3,108✔
133
            println(io)
4,476✔
134
            withtag(io, :li) do
8,952✔
135
                html(io, item)
4,476✔
136
            end
137
        end
4,476✔
138
        println(io)
3,108✔
139
    end
140
end
141

142
function html(io::IO, md::HorizontalRule)
1,041✔
143
    tag(io, :hr)
1,041✔
144
end
145

146
html(io::IO, x) = tohtml(io, x)
39✔
147

148
# Inline elements
149

150
function htmlinline(io::IO, content::Vector)
28,758✔
151
    for x in content
28,758✔
152
        htmlinline(io, x)
37,263✔
153
    end
37,263✔
154
end
155

156
function htmlinline(io::IO, code′::Code)
1,074✔
157
    if code′.language == "styled"
1,074✔
158
        code′ = Code("", String(styled(code′.code)))
×
159
    end
160
    code = code′
1,074✔
161
    withtag(io, :code) do
1,074✔
162
        htmlesc(io, code.code)
1,191✔
163
    end
164
end
165

166
function htmlinline(io::IO, md::Union{Symbol,AbstractString})
29,874✔
167
    htmlesc(io, md)
29,925✔
168
end
169

170
function htmlinline(io::IO, md::Bold)
1,296✔
171
    withtag(io, :strong) do
1,296✔
172
        htmlinline(io, md.text)
1,296✔
173
    end
174
end
175

176
function htmlinline(io::IO, md::Italic)
2,829✔
177
    withtag(io, :em) do
2,829✔
178
        htmlinline(io, md.text)
2,829✔
179
    end
180
end
181

182
function htmlinline(io::IO, md::Strikethrough)
18✔
183
    withtag(io, :s) do
18✔
184
        htmlinline(io, md.text)
18✔
185
    end
186
end
187

188
function htmlinline(io::IO, md::Image)
270✔
189
    tag(io, :img, :src=>encode_uri_component(md.url), :alt=>md.alt)
270✔
190
end
191

192

193
function htmlinline(io::IO, f::Footnote)
12✔
194
    withtag(io, :a, :href => "#footnote-$(f.id)", :class => "footnote") do
12✔
195
        print(io, "[", f.id, "]")
12✔
196
    end
197
end
198

199
function htmlinline(io::IO, link::Link)
1,758✔
200
    withtag(io, :a, :href=>encode_uri_component(link.url)) do
1,758✔
201
        htmlinline(io, link.text)
1,758✔
202
    end
203
end
204

205
function htmlinline(io::IO, br::LineBreak)
165✔
206
    tag(io, :br)
165✔
207
    println(io)
165✔
208
end
209

210
htmlinline(io::IO, x) = tohtml(io, x)
243✔
211

212
# API
213

214
export html
215

216
"""
217
    html([io::IO], md)
218

219
Output the contents of the Markdown object `md` in HTML format, either
220
writing to an (optional) `io` stream or returning a string.
221

222
One can alternatively use `show(io, "text/html", md)` or `repr("text/html", md)`, which
223
differ in that they wrap the output in a `<div class="markdown"> ... </div>` element.
224

225
# Examples
226
```jldoctest
227
julia> html(md"hello _world_")
228
"<p>hello <em>world</em></p>\\n"
229
```
230
"""
231
html(md) = sprint(html, md)
11,994✔
232

233
function show(io::IO, ::MIME"text/html", md::MD)
3✔
234
    withtag(io, :div, :class=>"markdown") do
3✔
235
        html(io, md)
3✔
236
    end
237
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc