forked from jashkenas/docco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
385 lines (297 loc) · 50.2 KB
/
index.html
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html> <html> <head> <title>docco.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="resources/docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <ul class="sections"> <li id="title"> <div class="annotation"> <h1>docco.coffee</h1> </div> </li> <li id="section-1"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p><strong>Docco</strong> is a 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
<a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a>, and code is
passed through <a href="http://pygments.org/">Pygments</a> syntax highlighting, if it
is present on the system.
This page is the result of running Docco against its own source file.</p>
<p>If you install Docco, you can run it from the command-line:</p>
<pre><code>docco src/*.coffee
</code></pre>
<p>...will generate an HTML documentation page for each of the named source files,
with a menu linking to the other pages, saving it into a <code>docs</code> folder.</p>
<p>The <a href="https://github.com/jashkenas/docco">source for Docco</a> is available on GitHub,
and released under the MIT license.</p>
<p>To install Docco, first make sure you have <a href="http://nodejs.org/">Node.js</a>,
<a href="http://pygments.org/">Pygments</a> (install the latest dev version of Pygments
from <a href="https://bitbucket.org/birkenfeld/pygments-main">its Mercurial repo</a>), and
<a href="http://coffeescript.org/">CoffeeScript</a>. Then, with NPM:</p>
<pre><code>sudo npm install -g docco
</code></pre>
<p>Docco can be used to process CoffeeScript, JavaScript, Ruby, Python, or TeX files.
Only single-line comments are processed -- block comments are ignored.</p>
<h3>Partners in Crime:</h3>
<ul>
<li><p>If <strong>Node.js</strong> doesn't run on your platform, or you'd prefer a more
convenient package, get <a href="https://github.com/rtomayko">Ryan Tomayko</a>'s
<a href="http://rtomayko.github.com/rocco/rocco.html">Rocco</a>, the Ruby port that's
available as a gem. </p></li>
<li><p>If you're writing shell scripts, try
<a href="http://rtomayko.github.com/shocco/">Shocco</a>, a port for the <strong>POSIX shell</strong>,
also by Mr. Tomayko.</p></li>
<li><p>If Python's more your speed, take a look at
<a href="https://github.com/fitzgen">Nick Fitzgerald</a>'s <a href="http://fitzgen.github.com/pycco/">Pycco</a>. </p></li>
<li><p>For <strong>Clojure</strong> fans, <a href="http://blog.fogus.me/">Fogus</a>'s
<a href="http://fogus.me/fun/marginalia/">Marginalia</a> is a bit of a departure from
"quick-and-dirty", but it'll get the job done.</p></li>
<li><p><strong>Lua</strong> enthusiasts can get their fix with
<a href="https://github.com/rgieseke">Robert Gieseke</a>'s <a href="http://rgieseke.github.com/locco/">Locco</a>.</p></li>
<li><p>And if you happen to be a <strong>.NET</strong>
aficionado, check out <a href="https://github.com/dontangg">Don Wilson</a>'s
<a href="http://dontangg.github.com/nocco/">Nocco</a>.</p></li>
</ul>
</div> </li> <li id="section-2"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div>
<h3>Main Documentation Generation Functions</h3>
</div> </li> <li id="section-3"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div>
<p>Generate the documentation for a source file by reading it in, splitting it
up into comment/code sections, highlighting them for the appropriate language,
and merging them into an HTML template.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">generateDocumentation = </span><span class="nf">(source, config, callback) -></span>
<span class="nx">fs</span><span class="p">.</span><span class="nx">readFile</span> <span class="nx">source</span><span class="p">,</span> <span class="nf">(error, buffer) -></span>
<span class="k">throw</span> <span class="nx">error</span> <span class="k">if</span> <span class="nx">error</span>
<span class="nv">code = </span><span class="nx">buffer</span><span class="p">.</span><span class="nx">toString</span><span class="p">()</span>
<span class="nv">sections = </span><span class="nx">parse</span> <span class="nx">source</span><span class="p">,</span> <span class="nx">code</span>
<span class="nx">highlight</span> <span class="nx">source</span><span class="p">,</span> <span class="nx">sections</span><span class="p">,</span> <span class="nf">-></span>
<span class="nx">generateHtml</span> <span class="nx">source</span><span class="p">,</span> <span class="nx">sections</span><span class="p">,</span> <span class="nx">config</span>
<span class="nx">callback</span><span class="p">()</span></pre></div></div> </li> <li id="section-4"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div>
<p>Given a string of source code, parse out each comment and the code that
follows it, and create an individual <strong>section</strong> for it.
Sections take the form:</p>
<pre><code>{
docsText: ...
docsHtml: ...
codeText: ...
codeHtml: ...
}
</code></pre>
</div> <div class="content"><div class="highlight"><pre><span class="nv">parse = </span><span class="nf">(source, code) -></span>
<span class="nv">lines = </span><span class="nx">code</span><span class="p">.</span><span class="nx">split</span> <span class="s">'\n'</span>
<span class="nv">sections = </span><span class="p">[]</span>
<span class="nv">language = </span><span class="nx">getLanguage</span> <span class="nx">source</span>
<span class="nv">hasCode = docsText = codeText = </span><span class="s">''</span>
<span class="nv">save = </span><span class="nf">(docsText, codeText) -></span>
<span class="nx">sections</span><span class="p">.</span><span class="nx">push</span> <span class="p">{</span><span class="nx">docsText</span><span class="p">,</span> <span class="nx">codeText</span><span class="p">}</span>
<span class="k">for</span> <span class="nx">line</span> <span class="k">in</span> <span class="nx">lines</span>
<span class="k">if</span> <span class="nx">line</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">language</span><span class="p">.</span><span class="nx">commentMatcher</span><span class="p">)</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">line</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">language</span><span class="p">.</span><span class="nx">commentFilter</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">hasCode</span>
<span class="nx">save</span> <span class="nx">docsText</span><span class="p">,</span> <span class="nx">codeText</span>
<span class="nv">hasCode = docsText = codeText = </span><span class="s">''</span>
<span class="nx">docsText</span> <span class="o">+=</span> <span class="nx">line</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">language</span><span class="p">.</span><span class="nx">commentMatcher</span><span class="p">,</span> <span class="s">''</span><span class="p">)</span> <span class="o">+</span> <span class="s">'\n'</span>
<span class="k">else</span>
<span class="nv">hasCode = </span><span class="kc">yes</span>
<span class="nx">codeText</span> <span class="o">+=</span> <span class="nx">line</span> <span class="o">+</span> <span class="s">'\n'</span>
<span class="nx">save</span> <span class="nx">docsText</span><span class="p">,</span> <span class="nx">codeText</span>
<span class="nx">sections</span></pre></div></div> </li> <li id="section-5"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div>
<p>Highlights parsed sections of code, using <strong>Pygments</strong> over stdio,
and runs the text of their corresponding comments through <strong>Markdown</strong>, using
<a href="http://attacklab.net/showdown/">Showdown.js</a>. If Pygments is not present
on the system, output the code in plain text.</p>
<p>We process all sections with single calls to Pygments and Showdown, by
inserting marker comments between them, and then splitting the result
string wherever the marker occurs.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">highlight = </span><span class="nf">(source, sections, callback) -></span>
<span class="nv">language = </span><span class="nx">getLanguage</span> <span class="nx">source</span>
<span class="nv">pygments = </span><span class="nx">spawn</span> <span class="s">'pygmentize'</span><span class="p">,</span> <span class="p">[</span>
<span class="s">'-l'</span><span class="p">,</span> <span class="nx">language</span><span class="p">.</span><span class="nx">name</span><span class="p">,</span>
<span class="s">'-f'</span><span class="p">,</span> <span class="s">'html'</span><span class="p">,</span>
<span class="s">'-O'</span><span class="p">,</span> <span class="s">'encoding=utf-8,tabsize=2'</span>
<span class="p">]</span>
<span class="nv">output = </span><span class="s">''</span>
<span class="nv">code = </span><span class="p">(</span><span class="nx">section</span><span class="p">.</span><span class="nx">codeText</span> <span class="k">for</span> <span class="nx">section</span> <span class="k">in</span> <span class="nx">sections</span><span class="p">).</span><span class="nx">join</span> <span class="nx">language</span><span class="p">.</span><span class="nx">codeSplitText</span>
<span class="nv">docs = </span><span class="p">(</span><span class="nx">section</span><span class="p">.</span><span class="nx">docsText</span> <span class="k">for</span> <span class="nx">section</span> <span class="k">in</span> <span class="nx">sections</span><span class="p">).</span><span class="nx">join</span> <span class="nx">language</span><span class="p">.</span><span class="nx">docsSplitText</span>
<span class="nx">pygments</span><span class="p">.</span><span class="nx">stderr</span><span class="p">.</span><span class="nx">on</span> <span class="s">'data'</span><span class="p">,</span> <span class="nf">-></span>
<span class="nx">pygments</span><span class="p">.</span><span class="nx">stdin</span><span class="p">.</span><span class="nx">on</span> <span class="s">'error'</span><span class="p">,</span> <span class="nf">-></span>
<span class="nx">pygments</span><span class="p">.</span><span class="nx">stdout</span><span class="p">.</span><span class="nx">on</span> <span class="s">'data'</span><span class="p">,</span> <span class="nf">(result) -></span>
<span class="nx">output</span> <span class="o">+=</span> <span class="nx">result</span> <span class="k">if</span> <span class="nx">result</span>
<span class="nx">pygments</span><span class="p">.</span><span class="nx">on</span> <span class="s">'exit'</span><span class="p">,</span> <span class="nf">-></span>
<span class="nv">output = </span><span class="nx">output</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">highlightStart</span><span class="p">,</span> <span class="s">''</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="nx">highlightEnd</span><span class="p">,</span> <span class="s">''</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">output</span> <span class="o">is</span> <span class="s">''</span>
<span class="nv">codeFragments = </span><span class="p">(</span><span class="nx">htmlEscape</span> <span class="nx">section</span><span class="p">.</span><span class="nx">codeText</span> <span class="k">for</span> <span class="nx">section</span> <span class="k">in</span> <span class="nx">sections</span><span class="p">)</span>
<span class="k">else</span>
<span class="nv">codeFragments = </span><span class="nx">output</span><span class="p">.</span><span class="nx">split</span> <span class="nx">language</span><span class="p">.</span><span class="nx">codeSplitHtml</span>
<span class="nv">docsFragments = </span><span class="nx">showdown</span><span class="p">.</span><span class="nx">makeHtml</span><span class="p">(</span><span class="nx">docs</span><span class="p">).</span><span class="nx">split</span> <span class="nx">language</span><span class="p">.</span><span class="nx">docsSplitHtml</span>
<span class="k">for</span> <span class="nx">section</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">sections</span>
<span class="nv">section.codeHtml = </span><span class="nx">highlightStart</span> <span class="o">+</span> <span class="nx">codeFragments</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">+</span> <span class="nx">highlightEnd</span>
<span class="nv">section.docsHtml = </span><span class="nx">docsFragments</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span>
<span class="nx">callback</span><span class="p">()</span>
<span class="k">if</span> <span class="nx">pygments</span><span class="p">.</span><span class="nx">stdin</span><span class="p">.</span><span class="nx">writable</span>
<span class="nx">pygments</span><span class="p">.</span><span class="nx">stdin</span><span class="p">.</span><span class="nx">write</span> <span class="nx">code</span>
<span class="nx">pygments</span><span class="p">.</span><span class="nx">stdin</span><span class="p">.</span><span class="nx">end</span><span class="p">()</span>
</pre></div></div> </li> <li id="section-6"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div>
<p>Escape an html string, to produce valid non-highlighted output when pygments
is not present on the system.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">htmlEscape = </span><span class="nf">(string) -></span>
<span class="nx">string</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/&/g</span><span class="p">,</span> <span class="s">'&amp;'</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/</g</span><span class="p">,</span> <span class="s">'&lt;'</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/>/g</span><span class="p">,</span> <span class="s">'&gt;'</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/"/g</span><span class="p">,</span> <span class="s">'&quot;'</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/'/g</span><span class="p">,</span> <span class="s">'&#x27;'</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\//g</span><span class="p">,</span><span class="s">'&#x2F;'</span><span class="p">)</span></pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div>
<p>Once all of the code is finished highlighting, we can generate the HTML file by
passing the completed sections into the template, and then writing the file to
the specified output path.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">generateHtml = </span><span class="nf">(source, sections, config) -></span>
<span class="nv">destination = </span><span class="nf">(filepath) -></span>
<span class="nx">path</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">config</span><span class="p">.</span><span class="nx">output</span><span class="p">,</span> <span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">filepath</span><span class="p">,</span> <span class="nx">path</span><span class="p">.</span><span class="nx">extname</span><span class="p">(</span><span class="nx">filepath</span><span class="p">))</span> <span class="o">+</span> <span class="s">'.html'</span><span class="p">)</span>
<span class="nv">title = </span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span> <span class="nx">source</span>
<span class="nv">dest = </span><span class="nx">destination</span> <span class="nx">source</span>
<span class="nv">html = </span><span class="nx">config</span><span class="p">.</span><span class="nx">doccoTemplate</span> <span class="p">{</span>
<span class="nv">title : </span><span class="nx">title</span><span class="p">,</span>
<span class="nv">sections : </span><span class="nx">sections</span><span class="p">,</span>
<span class="nv">sources : </span><span class="nx">config</span><span class="p">.</span><span class="nx">sources</span><span class="p">,</span>
<span class="nv">path : </span><span class="nx">path</span><span class="p">,</span>
<span class="nv">destination: </span><span class="nx">destination</span>
<span class="nv">css : </span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">config</span><span class="p">.</span><span class="nx">css</span><span class="p">)</span>
<span class="p">}</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">"docco: </span><span class="si">#{</span><span class="nx">source</span><span class="si">}</span><span class="s"> -> </span><span class="si">#{</span><span class="nx">dest</span><span class="si">}</span><span class="s">"</span>
<span class="nx">fs</span><span class="p">.</span><span class="nx">writeFileSync</span> <span class="nx">dest</span><span class="p">,</span> <span class="nx">html</span></pre></div></div> </li> <li id="section-8"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div>
<h3>Helpers & Setup</h3>
</div> </li> <li id="section-9"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div>
<p>Require our external dependencies, including <strong>Showdown.js</strong>
(the JavaScript implementation of Markdown).</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">fs = </span><span class="nx">require</span> <span class="s">'fs'</span>
<span class="nv">path = </span><span class="nx">require</span> <span class="s">'path'</span>
<span class="nv">showdown = </span><span class="nx">require</span><span class="p">(</span><span class="s">'./../vendor/showdown'</span><span class="p">).</span><span class="nx">Showdown</span>
<span class="p">{</span><span class="nx">spawn</span><span class="p">,</span> <span class="nx">exec</span><span class="p">}</span> <span class="o">=</span> <span class="nx">require</span> <span class="s">'child_process'</span>
<span class="nv">commander = </span><span class="nx">require</span> <span class="s">'commander'</span></pre></div></div> </li> <li id="section-10"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div>
<p>Read resource file and return its content.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">getResource = </span><span class="nf">(name) -></span>
<span class="nv">fullPath = </span><span class="nx">path</span><span class="p">.</span><span class="nx">join</span> <span class="nx">__dirname</span><span class="p">,</span> <span class="s">'..'</span><span class="p">,</span> <span class="s">'resources'</span><span class="p">,</span> <span class="nx">name</span>
<span class="nx">fs</span><span class="p">.</span><span class="nx">readFileSync</span><span class="p">(</span><span class="nx">fullPath</span><span class="p">).</span><span class="nx">toString</span><span class="p">()</span></pre></div></div> </li> <li id="section-11"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div>
<p>Languages are stored in JSON format in the file <code>resources/languages.json</code>
Each item maps the file extension to the name of the Pygments lexer and the
symbol that indicates a comment. To add a new language, modify the file.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">languages = </span><span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span> <span class="nx">getResource</span> <span class="s">'languages.json'</span></pre></div></div> </li> <li id="section-12"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div>
<p>Build out the appropriate matchers and delimiters for each language.</p>
</div> <div class="content"><div class="highlight"><pre><span class="k">for</span> <span class="nx">ext</span><span class="p">,</span> <span class="nx">l</span> <span class="k">of</span> <span class="nx">languages</span></pre></div></div> </li> <li id="section-13"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div>
<p>Does the line begin with a comment?</p>
</div> <div class="content"><div class="highlight"><pre> <span class="nv">l.commentMatcher = </span><span class="sr">///^\s*</span><span class="si">#{</span><span class="nx">l</span><span class="p">.</span><span class="nx">symbol</span><span class="si">}</span><span class="sr">\s?///</span></pre></div></div> </li> <li id="section-14"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div>
<p>Ignore <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)">hashbangs</a>
and interpolations...</p>
</div> <div class="content"><div class="highlight"><pre> <span class="nv">l.commentFilter = </span><span class="sr">/(^#![/]|^\s*#\{)/</span></pre></div></div> </li> <li id="section-15"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div>
<p>The dividing token we feed into Pygments, to delimit the boundaries between
sections.</p>
</div> <div class="content"><div class="highlight"><pre> <span class="nv">l.codeSplitText = </span><span class="s">"\n</span><span class="si">#{</span><span class="nx">l</span><span class="p">.</span><span class="nx">symbol</span><span class="si">}</span><span class="s">DIVIDER\n"</span></pre></div></div> </li> <li id="section-16"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div>
<p>The mirror of <code>codeSplitText</code> that we expect Pygments to return. We can split
on this to recover the original sections.
Note: the class is "c" for Python and "c1" for the other languages</p>
</div> <div class="content"><div class="highlight"><pre> <span class="nv">l.codeSplitHtml = </span><span class="sr">///\n*<span\sclass="c1?"></span><span class="si">#{</span><span class="nx">l</span><span class="p">.</span><span class="nx">symbol</span><span class="si">}</span><span class="sr">DIVIDER<\/span>\n*///</span></pre></div></div> </li> <li id="section-17"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div>
<p>The dividing token we feed into Showdown, to delimit the boundaries between
sections.</p>
</div> <div class="content"><div class="highlight"><pre> <span class="nv">l.docsSplitText = </span><span class="s">"\n</span><span class="err">#</span><span class="si">#{</span><span class="nx">l</span><span class="p">.</span><span class="nx">name</span><span class="si">}</span><span class="s">DOCDIVIDER\n"</span></pre></div></div> </li> <li id="section-18"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div>
<p>The mirror of <code>docsSplitText</code> that we expect Showdown to return. We can split
on this to recover the original sections.</p>
</div> <div class="content"><div class="highlight"><pre> <span class="nv">l.docsSplitHtml = </span><span class="sr">///<h1></span><span class="si">#{</span><span class="nx">l</span><span class="p">.</span><span class="nx">name</span><span class="si">}</span><span class="sr">DOCDIVIDER</h1>///</span></pre></div></div> </li> <li id="section-19"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div>
<p>Get the current language we're documenting, based on the extension.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">getLanguage = </span><span class="nf">(source) -></span> <span class="nx">languages</span><span class="p">[</span><span class="nx">path</span><span class="p">.</span><span class="nx">extname</span><span class="p">(</span><span class="nx">source</span><span class="p">)]</span></pre></div></div> </li> <li id="section-20"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</a> </div>
<p>Ensure that the destination directory exists.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">ensureDirectory = </span><span class="nf">(dir, cb, made=null) -></span>
<span class="nv">mode = </span><span class="nb">parseInt</span> <span class="s">'0777'</span><span class="p">,</span> <span class="mi">8</span>
<span class="nx">fs</span><span class="p">.</span><span class="nx">mkdir</span> <span class="nx">dir</span><span class="p">,</span> <span class="nx">mode</span><span class="p">,</span> <span class="nf">(er) -></span>
<span class="k">return</span> <span class="nx">cb</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">made</span> <span class="o">||</span> <span class="nx">dir</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">er</span>
<span class="k">if</span> <span class="nx">er</span><span class="p">.</span><span class="nx">code</span> <span class="o">==</span> <span class="s">'ENOENT'</span>
<span class="k">return</span> <span class="nx">ensureDirectory</span> <span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span><span class="p">(</span><span class="nx">dir</span><span class="p">),</span> <span class="nf">(er, made) -></span>
<span class="k">if</span> <span class="nx">er</span> <span class="k">then</span> <span class="nx">cb</span> <span class="nx">er</span><span class="p">,</span> <span class="nx">made</span> <span class="k">else</span> <span class="nx">ensureDirectory</span> <span class="nx">dir</span><span class="p">,</span> <span class="nx">cb</span><span class="p">,</span> <span class="nx">made</span>
<span class="nx">cb</span> <span class="nx">er</span><span class="p">,</span> <span class="nx">made</span></pre></div></div> </li> <li id="section-21"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">¶</a> </div>
<p>Micro-templating, originally by John Resig, borrowed by way of
<a href="http://documentcloud.github.com/underscore/">Underscore.js</a>.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">template = </span><span class="nf">(str) -></span>
<span class="k">new</span> <span class="nb">Function</span> <span class="s">'obj'</span><span class="p">,</span>
<span class="s">'var p=[],print=function(){p.push.apply(p,arguments);};'</span> <span class="o">+</span>
<span class="s">'with(obj){p.push(\''</span> <span class="o">+</span>
<span class="nx">str</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/[\r\t\n]/g</span><span class="p">,</span> <span class="s">" "</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/'(?=[^<]*%>)/g</span><span class="p">,</span><span class="s">"\t"</span><span class="p">)</span>
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s">"'"</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s">"\\'"</span><span class="p">)</span>
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s">"\t"</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s">"'"</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/<%=(.+?)%>/g</span><span class="p">,</span> <span class="s">"',$1,'"</span><span class="p">)</span>
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s">'<%'</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s">"');"</span><span class="p">)</span>
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s">'%>'</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s">"p.push('"</span><span class="p">)</span> <span class="o">+</span>
<span class="s">"');}return p.join('');"</span></pre></div></div> </li> <li id="section-22"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div>
<p>The start of each Pygments highlight block.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">highlightStart = </span><span class="s">'<div class="highlight"><pre>'</span></pre></div></div> </li> <li id="section-23"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div>
<p>The end of each Pygments highlight block.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">highlightEnd = </span><span class="s">'</pre></div>'</span></pre></div></div> </li> <li id="section-24"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">¶</a> </div>
<p>Extract the docco version from <code>package.json</code></p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">version = </span><span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">fs</span><span class="p">.</span><span class="nx">readFileSync</span><span class="p">(</span><span class="s">"</span><span class="si">#{</span><span class="nx">__dirname</span><span class="si">}</span><span class="s">/../package.json"</span><span class="p">)).</span><span class="nx">version</span></pre></div></div> </li> <li id="section-25"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">¶</a> </div>
<p>Default configuration options.</p>
</div> <div class="content"><div class="highlight"><pre><span class="nv">defaults =</span>
<span class="nv">template: </span><span class="s">"</span><span class="si">#{</span><span class="nx">__dirname</span><span class="si">}</span><span class="s">/../resources/docco.jst"</span>
<span class="nv">css : </span><span class="s">"</span><span class="si">#{</span><span class="nx">__dirname</span><span class="si">}</span><span class="s">/../resources/resources/docco.css"</span>
<span class="nv">output : </span><span class="s">"docs/"</span></pre></div></div> </li> <li id="section-26"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">¶</a> </div>
<h3>Run from Commandline</h3>
</div> </li> <li id="section-27"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">¶</a> </div>
<p>Run Docco from a set of command line arguments. </p>
<ol>
<li>Parse command line using <a href="https://github.com/visionmedia/commander.js">Commander JS</a>.</li>
<li>Document sources, or print the usage help if none are specified.</li>
</ol>
</div> <div class="content"><div class="highlight"><pre><span class="nv">run = </span><span class="nf">(args=process.argv) -></span>
<span class="nx">commander</span><span class="p">.</span><span class="nx">version</span><span class="p">(</span><span class="nx">version</span><span class="p">)</span>
<span class="p">.</span><span class="nx">usage</span><span class="p">(</span><span class="s">"[options] <filePattern ...>"</span><span class="p">)</span>
<span class="p">.</span><span class="nx">option</span><span class="p">(</span><span class="s">"-c, --css [file]"</span><span class="p">,</span><span class="s">"use a custom css file"</span><span class="p">,</span><span class="nx">defaults</span><span class="p">.</span><span class="nx">css</span><span class="p">)</span>
<span class="p">.</span><span class="nx">option</span><span class="p">(</span><span class="s">"-o, --output [path]"</span><span class="p">,</span><span class="s">"use a custom output path"</span><span class="p">,</span><span class="nx">defaults</span><span class="p">.</span><span class="nx">output</span><span class="p">)</span>
<span class="p">.</span><span class="nx">option</span><span class="p">(</span><span class="s">"-t, --template [file]"</span><span class="p">,</span><span class="s">"use a custom .jst template"</span><span class="p">,</span><span class="nx">defaults</span><span class="p">.</span><span class="nx">template</span><span class="p">)</span>
<span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">args</span><span class="p">)</span>
<span class="p">.</span><span class="nv">name = </span><span class="s">"docco"</span>
<span class="k">if</span> <span class="nx">commander</span><span class="p">.</span><span class="nx">args</span><span class="p">.</span><span class="nx">length</span>
<span class="nb">document</span><span class="p">(</span><span class="nx">commander</span><span class="p">.</span><span class="nx">args</span><span class="p">.</span><span class="nx">slice</span><span class="p">(),</span><span class="nx">commander</span><span class="p">)</span>
<span class="k">else</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="nx">commander</span><span class="p">.</span><span class="nx">helpInformation</span><span class="p">()</span></pre></div></div> </li> <li id="section-28"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">¶</a> </div>
<h3>Document Sources</h3>
</div> </li> <li id="section-29"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">¶</a> </div>
<p>Run Docco over a list of <code>sources</code> with the given <code>options</code>.</p>
<ol>
<li>Construct config to use by taking <code>defaults</code> first, then merging in <code>options</code></li>
<li>Generate the resolved source list, filtering out unknown types.</li>
<li>Load the specified template and css files.</li>
<li>Ensure the output path is created, write out the CSS file,
document each source, and invoke the completion callback if it is specified.</li>
</ol>
</div> <div class="content"><div class="highlight"><pre><span class="nb">document</span> <span class="o">=</span> <span class="nf">(sources, options = {}, callback = null) -></span>
<span class="nv">config = </span><span class="p">{}</span>
<span class="nx">config</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">defaults</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span><span class="nx">value</span> <span class="k">of</span> <span class="nx">defaults</span>
<span class="nx">config</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">value</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span><span class="nx">value</span> <span class="k">of</span> <span class="nx">options</span> <span class="k">if</span> <span class="nx">key</span> <span class="k">of</span> <span class="nx">defaults</span>
<span class="nv">resolved = </span><span class="p">[]</span>
<span class="nv">resolved = </span><span class="nx">resolved</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">resolveSource</span><span class="p">(</span><span class="nx">src</span><span class="p">))</span> <span class="k">for</span> <span class="nx">src</span> <span class="k">in</span> <span class="nx">sources</span>
<span class="nv">config.sources = </span><span class="nx">resolved</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="nf">(source) -></span> <span class="nx">getLanguage</span> <span class="nx">source</span><span class="p">).</span><span class="nx">sort</span><span class="p">()</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">"docco: skipped unknown type (</span><span class="si">#{</span><span class="nx">m</span><span class="si">}</span><span class="s">)"</span> <span class="k">for</span> <span class="nx">m</span> <span class="k">in</span> <span class="nx">resolved</span> <span class="k">when</span> <span class="nx">m</span> <span class="o">not</span> <span class="k">in</span> <span class="nx">config</span><span class="p">.</span><span class="nx">sources</span>
<span class="nv">config.doccoTemplate = </span><span class="nx">template</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">readFileSync</span><span class="p">(</span><span class="nx">config</span><span class="p">.</span><span class="nx">template</span><span class="p">).</span><span class="nx">toString</span><span class="p">()</span>
<span class="nv">doccoStyles = </span><span class="nx">fs</span><span class="p">.</span><span class="nx">readFileSync</span><span class="p">(</span><span class="nx">config</span><span class="p">.</span><span class="nx">css</span><span class="p">).</span><span class="nx">toString</span><span class="p">()</span>
<span class="nx">ensureDirectory</span> <span class="nx">config</span><span class="p">.</span><span class="nx">output</span><span class="p">,</span> <span class="nf">-></span>
<span class="nx">fs</span><span class="p">.</span><span class="nx">writeFileSync</span> <span class="nx">path</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">config</span><span class="p">.</span><span class="nx">output</span><span class="p">,</span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">config</span><span class="p">.</span><span class="nx">css</span><span class="p">)),</span> <span class="nx">doccoStyles</span>
<span class="nv">files = </span><span class="nx">config</span><span class="p">.</span><span class="nx">sources</span><span class="p">.</span><span class="nx">slice</span><span class="p">()</span>
<span class="nv">nextFile = </span><span class="nf">-></span>
<span class="nx">callback</span><span class="p">()</span> <span class="k">if</span> <span class="nx">callback</span><span class="o">?</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">files</span><span class="p">.</span><span class="nx">length</span>
<span class="nx">generateDocumentation</span> <span class="nx">files</span><span class="p">.</span><span class="nx">shift</span><span class="p">(),</span> <span class="nx">config</span><span class="p">,</span> <span class="nx">nextFile</span> <span class="k">if</span> <span class="nx">files</span><span class="p">.</span><span class="nx">length</span>
<span class="nx">nextFile</span><span class="p">()</span></pre></div></div> </li> <li id="section-30"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">¶</a> </div>
<h3>Resolve Wildcard Source Inputs</h3>
</div> </li> <li id="section-31"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">¶</a> </div>
<p>Resolve a wildcard <code>source</code> input to the files it matches.</p>
<ol>
<li>If the input contains no wildcard characters, just return it.</li>
<li>Convert the wildcard match to a regular expression, and return
an array of files in the path that match it.</li>
</ol>
</div> <div class="content"><div class="highlight"><pre><span class="nv">resolveSource = </span><span class="nf">(source) -></span>
<span class="k">return</span> <span class="nx">source</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">source</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/([\*\?])/</span><span class="p">)</span>
<span class="nv">regex_str = </span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">source</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\./g</span><span class="p">,</span> <span class="s">"\\$&"</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\*/</span><span class="p">,</span><span class="s">".*"</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\?/</span><span class="p">,</span><span class="s">"."</span><span class="p">)</span>
<span class="nv">regex = </span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s">'^('</span> <span class="o">+</span> <span class="nx">regex_str</span> <span class="o">+</span> <span class="s">')$'</span><span class="p">)</span>
<span class="nv">file_path = </span><span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span><span class="p">(</span><span class="nx">source</span><span class="p">)</span>
<span class="nv">files = </span><span class="nx">fs</span><span class="p">.</span><span class="nx">readdirSync</span> <span class="nx">file_path</span>
<span class="k">return</span> <span class="p">(</span><span class="nx">path</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">file_path</span><span class="p">,</span><span class="nx">file</span><span class="p">)</span> <span class="k">for</span> <span class="nx">file</span> <span class="k">in</span> <span class="nx">files</span> <span class="k">when</span> <span class="nx">file</span><span class="p">.</span><span class="nx">match</span> <span class="nx">regex</span><span class="p">)</span></pre></div></div> </li> <li id="section-32"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">¶</a> </div>
<h3>Exports</h3>
</div> </li> <li id="section-33"> <div class="annotation"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</a> </div>
<p>Information about docco, and functions for programatic usage.</p> </div> <div class="content"><div class="highlight"><pre><span class="nx">exports</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">value</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">value</span> <span class="k">of</span> <span class="p">{</span>
<span class="nv">run : </span><span class="nx">run</span>
<span class="nb">document</span> <span class="o">:</span> <span class="nb">document</span>
<span class="nv">parse : </span><span class="nx">parse</span>
<span class="nv">resolveSource : </span><span class="nx">resolveSource</span>
<span class="nv">version : </span><span class="nx">version</span>
<span class="nv">defaults : </span><span class="nx">defaults</span>
<span class="nv">languages : </span><span class="nx">languages</span>
<span class="nv">ensureDirectory: </span><span class="nx">ensureDirectory</span>
<span class="p">}</span>
</pre></div></div> </li> </ul> </div> </body> </html>