-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwriter.jl
227 lines (202 loc) · 8.42 KB
/
writer.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import Documenter: Anchor, Builder, Expanders, Documenter, Document
import Documenter: DocsNodes, DocsNode, IndexNode, ContentsNode, EvalNode, MetaNode, RawNode
import Documenter: MultiCodeBlock, MultiOutput
import ANSIColoredPrinters
# import Markdown as MarkdownStdlib
module _Markdown
import Markdown
end
const MarkdownStdlib = _Markdown.Markdown
struct MarkdownVitepress <: Documenter.Writer
end
# return the same file with the extension changed to .md
mdext(f) = string(splitext(f)[1], ".md")
function render(doc::Document, settings::MarkdownVitepress=MarkdownVitepress())
@info "DocumenterMarkdownVitepress: rendering MarkdownVitepress pages."
copy_assets(doc)
mime = MIME"text/plain"()
for (src, page) in doc.blueprint.pages
open(mdext(page.build), "w") do io
for elem in page.elements
node = page.mapping[elem]
render(io, mime, node, page, doc)
end
end
end
end
function copy_assets(doc::Document)
@debug "copying assets to build directory."
assets = ASSETS
if isdir(assets)
builddir = joinpath(doc.user.build, "assets")
isdir(builddir) || mkdir(builddir)
for each in readdir(assets)
src = joinpath(assets, each)
dst = joinpath(builddir, each)
ispath(dst) && @warn "DocumenterMarkdownVitepress: overwriting '$dst'."
cp(src, dst; force=true)
end
else
@warn "DocumenterMarkdownVitepress: no assets directory found."
end
end
function render(io::IO, mime::MIME"text/plain", vec::Vector, page, doc)
for each in vec
render(io, mime, each, page, doc)
end
end
function render(io::IO, mime::MIME"text/plain", anchor::Anchor, page, doc)
println(io, "\n<a id='", lstrip(anchor_fragment(anchor), '#'), "'></a>")
return render(io, mime, anchor.object, page, doc)
end
## Documentation Nodes.
function render(io::IO, mime::MIME"text/plain", node::DocsNodes, page, doc)
for node in node.nodes
render(io, mime, node, page, doc)
end
end
function render(io::IO, mime::MIME"text/plain", node::DocsNode, page, doc)
# Docstring header based on the name of the binding and it's category.
println(io,
"<div style='border-width:1px; border-style:solid; border-color:black; padding: 1em; border-radius: 25px;'>")
anchor = "<a id='$(node.anchor.id)' href='#$(node.anchor.id)'>#</a>"
header = " <b><u>$(node.object.binding)</u></b> — <i>$(doccat(node.object))</i>."
println(io, anchor, header, "\n\n")
# Body. May contain several concatenated docstrings.
renderdoc(io, mime, node.docstr, page, doc)
return println(io, "</div>\n<br>")
end
function renderdoc(io::IO, mime::MIME"text/plain", md::MarkdownStdlib.MD, page, doc)
if haskey(md.meta, :results)
# The `:results` field contains a vector of `Docs.DocStr` objects associated with
# each markdown object. The `DocStr` contains data such as file and line info that
# we need for generating correct source links.
for (markdown, result) in zip(md.content, md.meta[:results])
render(io, mime, dropheaders(markdown), page, doc)
# When a source link is available then print the link.
url = url(doc.internal.remote, doc.user.repo, result)
if url !== nothing
link = "<a target='_blank' href='$url' class='documenter-source'>source</a><br>"
println(io, "\n", link, "\n")
end
end
else
# Docstrings with no `:results` metadata won't contain source locations so we don't
# try to print them out. Just print the basic docstring.
render(io, mime, dropheaders(md), page, doc)
end
end
function renderdoc(io::IO, mime::MIME"text/plain", other, page, doc)
# TODO: properly support non-markdown docstrings at some point.
return render(io, mime, other, page, doc)
end
## Index, Contents, and Eval Nodes.
function render(io::IO, ::MIME"text/plain", index::IndexNode, page, doc)
for (object, _, page, mod, cat) in index.elements
page = mdext(page)
url = string("#", slugify(object))
println(io, "- [`", object.binding, "`](", url, ")")
end
return println(io)
end
function render(io::IO, ::MIME"text/plain", contents::ContentsNode, page, doc)
for (count, path, anchor) in contents.elements
path = mdext(path)
header = anchor.object
url = string(path, anchor_fragment(anchor))
link = MarkdownStdlib.Link(header.text, url)
level = header_level(header)
print(io, " "^(level - 1), "- ")
linkfix = ".md#"
println(io, replace(MarkdownStdlib.plaininline(link), linkfix => "#"))
end
return println(io)
end
function render(io::IO, mime::MIME"text/plain", node::EvalNode, page, doc)
return node.result === nothing ? nothing : render(io, mime, node.result, page, doc)
end
function render(io::IO, mime::MIME"text/plain", mcb::MultiCodeBlock, page, doc)
return render(io, mime, join_multiblock(mcb), page, doc)
end
# Select the "best" representation for Markdown output.
using Base64: base64decode
function render(io::IO, mime::MIME"text/plain", d::MultiOutput, page, doc)
return foreach(x -> Base.invokelatest(render, io, mime, x, page, doc), d.content)
end
function render(io::IO, mime::MIME"text/plain", d::Dict{MIME, Any}, page, doc)
filename = String(rand('a':'z', 7))
if haskey(d, MIME"text/markdown"())
println(io, d[MIME"text/markdown"()])
elseif haskey(d, MIME"text/html"())
println(io, d[MIME"text/html"()])
elseif haskey(d, MIME"image/svg+xml"())
# NOTE: It seems that we can't simply save the SVG images as a file and include them
# as browsers seem to need to have the xmlns attribute set in the <svg> tag if you
# want to include it with <img>. However, setting that attribute is up to the code
# creating the SVG image.
println(io, d[MIME"image/svg+xml"()])
elseif haskey(d, MIME"image/png"())
write(joinpath(dirname(page.build), "$(filename).png"),
base64decode(d[MIME"image/png"()]))
println(io,
"""
.png)
""")
elseif haskey(d, MIME"image/webp"())
write(joinpath(dirname(page.build), "$(filename).webp"),
base64decode(d[MIME"image/webp"()]))
println(io,
"""
.webp)
""")
elseif haskey(d, MIME"image/jpeg"())
write(joinpath(dirname(page.build), "$(filename).jpeg"),
base64decode(d[MIME"image/jpeg"()]))
println(io,
"""
.jpeg)
""")
elseif haskey(d, MIME"image/gif"())
write(joinpath(dirname(page.build), "$(filename).gif"),
base64decode(d[MIME"image/gif"()]))
println(io,
"""
.gif)
""")
elseif haskey(d, MIME"text/plain"())
text = d[MIME"text/plain"()]
out = repr(MIME"text/plain"(), ANSIColoredPrinters.PlainTextPrinter(IOBuffer(text)))
render(io, mime, MarkdownStdlib.Code(out), page, doc)
else
error("this should never happen.")
end
return nothing
end
## Basic Nodes. AKA: any other content that hasn't been handled yet.
function render(io::IO, ::MIME"text/plain", other, page, doc)
println(io)
linkfix = ".md#"
return println(io, replace(MarkdownStdlib.plain(other), linkfix => "#"))
end
render(io::IO, ::MIME"text/plain", str::AbstractString, page, doc) = print(io, str)
# Metadata Nodes get dropped from the final output for every format but are needed throughout
# rest of the build and so we just leave them in place and print a blank line in their place.
render(io::IO, ::MIME"text/plain", node::MetaNode, page, doc) = println(io, "\n")
function render(io::IO, ::MIME"text/plain", raw::RawNode, page, doc)
return raw.name === :html ? println(io, raw.text, "\n") : nothing
end
## Markdown Utilities.
# Remove all header nodes from a markdown object and replace them with bold font.
# Only for use in `text/plain` output, since we'll use some css to make these less obtrusive
# in the HTML rendering instead of using this hack.
function dropheaders(md::MarkdownStdlib.MD)
out = MarkdownStdlib.MD()
out.meta = md.meta
out.content = map(dropheaders, md.content)
return out
end
function dropheaders(h::MarkdownStdlib.Header)
return MarkdownStdlib.Paragraph([MarkdownStdlib.Bold(h.text)])
end
dropheaders(v::Vector) = map(dropheaders, v)
dropheaders(other) = other