/
jocco.jl
331 lines (296 loc) · 10.9 KB
/
jocco.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# __Jocco__ is a [Julia] port of [Docco], the quick-and-dirty,
# hundred-line-long, literate-programming-style documentation generator. It
# produces HTML that displays your comments alongside your code. Comments are
# passed through [Pandoc], and code is syntax highlighted with [Pygments].
# This page is the result of running Jocco against its own source file:
#
# julia jocco.jl jocco.jl
#
# and the HTML file is generated in the `docs` directory.
#
# Using [Pandoc] allows us to have math inline $x=y$ or in display mode
# $$
# \begin{aligned}
# \nabla \times \vec{\mathbf{B}} -\, \frac1c\,
# \frac{\partial\vec{\mathbf{E}}}{\partial t} &=
# \frac{4\pi}{c}\vec{\mathbf{j}} \\
# \nabla \cdot \vec{\mathbf{E}} &= 4 \pi \rho \\
# \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\,
# \frac{\partial\vec{\mathbf{B}}}{\partial t} &= \vec{\mathbf{0}} \\
# \nabla \cdot \vec{\mathbf{B}} &= 0
# \end{aligned}
# $$
# if you wish. This uses the [MathJax] Content Distribution Network script to
# turn $\LaTeX$ source into rendered output and thus an internet connection is
# required. [MathJax] may be installed locally if offline access is desired.
#
# @Knuth:1984:LP might be something we should read when building a literate
# programming tool. We can also reference this in a note.[^1]
#
# This [Julia] port of [Docco] is roughly structured the same as the [Lua]
# port [Locco]. Its source is released in the public domain and is available
# on [GitHub](http://github.com/lcw/jocco).
#
# We use comments to separate the different chunks of code so that they can all
# be processed together with [Pygments] and the HTML can be split up after.
# Likewise, we use level five headers to separate the chunks of documentation.
const code_sep = "# CUT HERE\n"
const code_sep_html = "<span class=\"c\"># CUT HERE</span>\n"
const docs_sep = "\n##### CUT HERE\n\n"
const docs_sep_html = r"<h5 id=\"cut-here.*\">CUT HERE</h5>\n"
# For now we leave the HTML template hard coded.
const header = "<!DOCTYPE html>
<html>
<head>
<title>%title%</title>
<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">
<link rel=\"stylesheet\" media=\"all\" href=\"jocco.css\" />
<script type=\"text/javascript\"
src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\">
</script>
</head>
<body>
<div id=\"container\">
<div id=\"background\"></div>
<table cellpadding=\"0\" cellspacing=\"0\">
<thead>
<tr>
<th class=\"docs\">
<h1>
%title%
</h1>
</th>
<th class=\"code\">
</th>
</tr>
</thead>
<tbody>
"
const table_entry = "
<tr id=\"section-%index%\">
<td class=\"docs\">
<div class=\"pilwrap\">
<a class=\"pilcrow\" href=\"#section-%index%\">¶</a>
</div>
%docs_html%
</td>
<td class=\"code\">
<div class=\"highlight\"><pre>%code_html%
</pre></div>
</td>
</tr>
"
const footer = "
</tbody>
</table>
</div>
</body>
</html>"
#
# This function splits the `source` text into an array of documentation
# sections and code sections.
#
# ----------------------------------------------------------------------------
# Parameters:
# ----------- ----------------------------------------------------------------
# `source` An `ASCIIString`{.julia} of the document source to be parsed.
# ----------------------------------------------------------------------------
#
# ----------------------------------------------------------------------------
# Returns:
# --------- ----------------------------------------------------------------
# `code` An array of the code sections.
#
# `docs` An array of the documentation sections.
# ----------------------------------------------------------------------------
#
function parse_source(source)
code, docs = ASCIIString[], ASCIIString[]
f = open(source)
has_code = false
code_text, docs_text = "", ""
for line in readlines(f)
line = chomp(line)
m = match(r"^\s*(?:#\s(.*?)\s*$|$)", line)
if m == nothing
m = match(r"^\s*#()$", line)
end
if m == nothing || m.captures == (nothing,)
has_code = true
code_text = "$code_text$line\n"
else
if has_code
code = push(code, code_text)
docs = push(docs, docs_text)
has_code = false
code_text, docs_text = "", ""
end
(doc_line,) = m.captures
docs_text = "$docs_text$doc_line\n"
end
end
code = push(code, code_text)
docs = push(docs, docs_text)
close(f)
code, docs
end
# This function is common to the code and documentation highlighting. It is
# used to join text segments using `sep_in` to be processed by `cmd` as one
# file and then split back into sections using `sep_out`.
#
# ----------------------------------------------------------------------------
# Parameters:
# ------------ ---------------------------------------------------------------
# `text_array` An array of text segments to be highlighted as one document.
#
# `sep_in` An `ASCIIString`{.julia} which is inserted between text
# segments.
#
# `sep_out` An `ASCIIString`{.julia} searched for as a `--- CUT HERE ---`
# line to split the sections. This string is removed from the
# returned from returned segments.
#
# `cmd` The joined text will be written to this `Cmd`{.julia} and the
# text which will be split and returned will also be read from
# this `Cmd`{.julia}.
# ----------------------------------------------------------------------------
#
# ----------------------------------------------------------------------------
# Returns:
# --------- ----------------------------------------------------------------
# An array of highlighted text with a corresponding entry for each
# passed in segment.
# ----------------------------------------------------------------------------
#
function highlight(text_array, sep_in, sep_out, cmd)
write_stream = fdio(write_to(cmd).fd, true)
read_stream = fdio(read_from(cmd).fd, true)
spawn(cmd)
write(write_stream, join(text_array, sep_in))
close(write_stream)
text_out = readall(read_stream)
close(read_stream)
wait(cmd)
split(text_out, sep_out)
end
# This highlights the code using `pygmentize`. Here we assume the code is
# written in [Julia]. We remove the `<div>`{.html} and `<pre>`{.html} tags
# first and last segments so that each segment can be wrapped in their own
# tags using the template.
function highlight_code(code)
cmd = `pygmentize -l julia -f html -O encoding=utf8`
code = highlight(code, code_sep, code_sep_html, cmd)
if length(code) > 0
code[1] = replace(code[1], "<div class=\"highlight\"><pre>", "")
code[length(code)] = replace(code[length(code)], "</pre></div>", "")
end
code
end
# This returns an array of file names in the directory `dir` with the
# extension `wanted_ext`.
function get_files_with_extension(dir, wanted_ext)
files = split(chomp(readall(`ls $dir`)), "\n")
ext_files = Array(ASCIIString, 1, 0)
for f in files
filename = file_path(dir, f)
pathname, filebase, ext = fileparts(filename)
if(ext == wanted_ext)
ext_files = [ext_files filename]
end
end
ext_files
end
# This joins an argument `arg` array `vals` such that `args` is returned as:
#
# args = [arg, vals[1], arg, vals[2], arg, vals[3], ...]
#
function join_arg_vals(arg, vals)
args = Array(ASCIIString, 1, 2*length(vals))
args[1:2:end] = arg
args[2:2:end] = vals
args
end
# Here the documentation is passed through [Pandoc] using its extension of
# markdown to generate the HTML. BibTeX files that are stored in the
# `docs` directory are passed into [Pandoc] through with the `--bibliography`
# argument. Like wise [Citation Style Language](http://citationstyles.org/)
# (CSL) files found in the `docs` directory are passed into [Pandoc] with the
# `--csl` argument.
#
# Further any files with the extension `.hs` in the `docs` directory are
# considered [Pandoc]
# [scripting](http://johnmacfarlane.net/pandoc/scripting.html) filters which
# read and write [Pandoc] AST in [JSON] format. In the Jocco docs directory
# there are two filters such filters. The first `doiLinks.hs` adds a
# hyperlink to DOI citation entries. The second `pygments.hs` from [Matti
# Pastell](https://bitbucket.org/mpastell/pandoc-filters/) uses [Pygments] to
# highlight code blocks. This way we can have highlighted Julia code on the
# documentation side too like this:
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.julia .numberLines}
# function foo(bar)
# bar
# end
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
function highlight_docs(docs, path)
bib_files = get_files_with_extension(path, ".bib")
csl_files = get_files_with_extension(path, ".csl")
pan_files = get_files_with_extension(path, ".hs")
bib_args = join_arg_vals("--bibliography", bib_files)
csl_args = join_arg_vals("--csl", csl_files)
pan_args = ["-S" bib_args csl_args "-f" "markdown" "-t" "json"]
cmd = `pandoc $pan_args`
for p in pan_files
cmd = cmd | `runhaskell $p`
end
cmd = cmd | `pandoc -S --mathjax -f json -t html`
docs = highlight(docs, docs_sep, docs_sep_html, cmd)
end
# Here the generated code and documentation is substituted into the templates
# and written to the HTML file.
function generate_html(source, path, file, code, docs, jump_to)
outfile = file_path(path, replace(file, r"jl$", "html"))
f = open(outfile, "w")
h = replace(header, r"%title%", source)
write(f, h)
assert(length(code)==length(docs))
for i = 1:length(code)
t = replace(table_entry, r"%index%", i)
t = replace(t, r"%docs_html%", docs[i])
t = replace(t, r"%code_html%", code[i])
write(f, t)
end
write(f, footer)
close(f)
println("$file --> $outfile")
end
function generate_documentation(source, path, file, jump_to)
code, docs = parse_source(source)
code, docs = highlight_code(code), highlight_docs(docs, path)
generate_html(source, path, file, code, docs, jump_to)
end
# Documentation is generated in the `docs` directory for all of the files pass
# in as arguments to this program.
function main()
jump_to = ""
for source in ARGS
file = chomp(readall(`basename $source`))
path = file_path(chomp(readall(`dirname $source`)), "docs")
run(`mkdir -p $path`)
generate_documentation(source, path, file, jump_to)
end
end
main()
# ## References
#
# [^1]: A citation without locators [@Knuth:1984:LP].
#
# [Docco]: http://jashkenas.github.com/docco/
# [Pandoc]: http://johnmacfarlane.net/pandoc/
# [JSON]: http://www.json.org/
# [Julia]: http://julialang.org/
# [Lua]: http://lua.org/
# [Locco]: http://rgieseke.github.com/locco/
# [MathJax]: http://www.mathjax.org/
# [Pygments]: http://pygments.org/