/
MarkdownWriter.jl
174 lines (146 loc) · 5.87 KB
/
MarkdownWriter.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
"""
A module for rendering `Document` objects to markdown.
"""
module MarkdownWriter
import ...Documenter:
Anchors,
Builder,
Documents,
Expanders,
Formats,
Documenter,
Utilities
# import Markdown as MarkdownStdlib
module _Markdown
import Markdown
end
const MarkdownStdlib = _Markdown.Markdown
struct Markdown <: Documenter.Plugin
end
function render(doc::Documents.Document, settings::Markdown=Markdown())
copy_assets(doc)
mime = Formats.mimetype(:markdown)
for (src, page) in doc.internal.pages
open(Formats.extension(:markdown, 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::Documents.Document)
@debug "copying assets to build directory."
assets = joinpath(doc.internal.assets, "mkdocs")
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 "Documenter: overwriting '$dst'."
cp(src, dst; force = true)
end
else
error("assets directory '$(abspath(assets))' is missing.")
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::Anchors.Anchor, page, doc)
println(io, "\n<a id='", anchor.id, "-", anchor.nth, "'></a>")
render(io, mime, anchor.object, page, doc)
end
## Documentation Nodes.
function render(io::IO, mime::MIME"text/plain", node::Documents.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::Documents.DocsNode, page, doc)
# Docstring header based on the name of the binding and it's category.
anchor = "<a id='$(node.anchor.id)' href='#$(node.anchor.id)'>#</a>"
header = "**`$(node.object.binding)`** — *$(Utilities.doccat(node.object))*."
println(io, anchor, "\n", header, "\n\n")
# Body. May contain several concatenated docstrings.
renderdoc(io, mime, node.docstr, page, doc)
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 = Utilities.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.
render(io, mime, other, page, doc)
end
## Index, Contents, and Eval Nodes.
function render(io::IO, ::MIME"text/plain", index::Documents.IndexNode, page, doc)
for (object, _, page, mod, cat) in index.elements
page = Formats.extension(:markdown, page)
url = string(page, "#", Utilities.slugify(object))
println(io, "- [`", object.binding, "`](", url, ")")
end
println(io)
end
function render(io::IO, ::MIME"text/plain", contents::Documents.ContentsNode, page, doc)
for (count, path, anchor) in contents.elements
path = Formats.extension(:markdown, path)
header = anchor.object
url = string(path, '#', anchor.id, '-', anchor.nth)
link = MarkdownStdlib.Link(header.text, url)
level = Utilities.header_level(header)
print(io, " "^(level - 1), "- ")
MarkdownStdlib.plaininline(io, link)
println(io)
end
println(io)
end
function render(io::IO, mime::MIME"text/plain", node::Documents.EvalNode, page, doc)
node.result === nothing ? nothing : render(io, mime, node.result, page, doc)
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)
MarkdownStdlib.plain(io, other)
println(io)
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::Documents.MetaNode, page, doc) = println(io, "\n")
function render(io::IO, ::MIME"text/plain", raw::Documents.RawNode, page, doc)
raw.name === :html ? println(io, "\n", 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)
out
end
dropheaders(h::MarkdownStdlib.Header) = MarkdownStdlib.Paragraph([MarkdownStdlib.Bold(h.text)])
dropheaders(v::Vector) = map(dropheaders, v)
dropheaders(other) = other
end