forked from GerHobbelt/docco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1253 lines (905 loc) · 57 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Docco</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/parallel/docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<h1 id="docco">Docco</h1>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p><strong>Docco</strong> is a quick-and-dirty documentation generator, written in
<a href="http://coffeescript.org/#literate">Literate CoffeeScript</a>.
It produces an HTML document that displays your comments intermingled with your
code. All prose is passed through
<a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a>, and code is
passed through <a href="http://highlightjs.org/">Highlight.js</a> syntax highlighting.
This page is the result of running Docco against its own
<a href="https://github.com/jashkenas/docco/blob/master/docco.litcoffee">source file</a>.</p>
<ol>
<li><p>Install Docco with <strong>npm</strong>: <code>sudo npm install -g docco</code></p>
</li>
<li><p>Run it against your code: <code>docco src/*.coffee</code></p>
</li>
</ol>
<p>There is no “Step 3”. This will generate an HTML page for each of the named
source files, with a menu linking to the other pages, saving the whole mess
into a <code>docs</code> folder (configurable).</p>
<p>The <a href="http://github.com/jashkenas/docco">Docco source</a> is available on GitHub,
and is released under the <a href="http://opensource.org/licenses/MIT">MIT license</a>.</p>
<p>Docco can be used to process code written in any programming language. If it
doesn’t handle your favorite yet, feel free to
<a href="https://github.com/jashkenas/docco/blob/master/resources/languages.json">add it to the list</a>.
Finally, the <a href="http://coffeescript.org/#literate">“literate” style</a> of <em>any</em>
language is also supported — just tack an <code>.md</code> extension on the end:
<code>.coffee.md</code>, <code>.py.md</code>, and so on. Also get usable source code by adding the
<code>--source</code> option while specifying a directory for the files.</p>
<p>By default only single-line comments are processed, block comments may be included
by passing the <code>-b</code> flag to Docco.</p>
<h2 id="partners-in-crime-">Partners in Crime:</h2>
</div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<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="http://github.com/rtomayko">Ryan Tomayko</a>‘s
<a href="http://rtomayko.github.io/rocco/rocco.html">Rocco</a>, the <strong>Ruby</strong> port that’s
available as a gem. (<strong>WARNING</strong>: project seems currently broken and apparently abandoned.)</p>
</li>
<li><p>If you’re writing shell scripts, try
<a href="http://rtomayko.github.io/shocco/">Shocco</a>, a port for the <strong>POSIX shell</strong>,
also by Mr. Tomayko. (<strong>WARNING</strong>: project seems currently broken and apparently abandoned.)</p>
</li>
<li><p>If <strong>Python</strong> is more your speed, take a look at
<a href="http://github.com/fitzgen">Nick Fitzgerald</a>‘s <a href="https://pycco-docs.github.io/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>There’s a <strong>Go</strong> port called <a href="http://nikhilm.github.io/gocco/">Gocco</a>,
written by <a href="https://github.com/nikhilm">Nikhil Marathe</a>.</p>
</li>
<li><p>For all you <strong>PHP</strong> buffs out there, Fredi Bach’s
<a href="http://jquery-jkit.com/sourcemakeup/">sourceMakeup</a> (we’ll let the faux pas
with respect to our naming scheme slide), should do the trick nicely.</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.io/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.io/nocco/">Nocco</a>.</p>
</li>
<li><p>Going further afield from the quick-and-dirty, <a href="http://nevir.github.io/groc/">Groc</a>
is a <strong>CoffeeScript</strong> fork of Docco that adds a searchable table of contents,
and aims to gracefully handle large projects with complex hierarchies of code.</p>
</li>
</ul>
<p>Note that not all ports will support all Docco features … yet.</p>
<h2 id="main-documentation-generation-functions">Main Documentation Generation Functions</h2>
</div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Generate the documentation for our configured source file by copying over static
assets, reading all the source files in, splitting them up into prose+code
sections, highlighting each file in the appropriate language, printing them
out in an HTML template, and writing plain code files where instructed.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">document</span> = <span class="hljs-params">(options = {}, user_callback)</span> -></span>
config = configure options
source_infos = []
fs.mkdirsSync config.output
fs.mkdirsSync config.source <span class="hljs-keyword">if</span> config.source
<span class="hljs-function">
<span class="hljs-title">callback</span> = <span class="hljs-params">(error)</span> -></span>
<span class="hljs-keyword">if</span> error
user_callback error <span class="hljs-keyword">if</span> user_callback
<span class="hljs-keyword">throw</span> error
<span class="hljs-keyword">if</span> user_callback
user_callback <span class="hljs-literal">null</span>, { source_infos, config }
<span class="hljs-function">
<span class="hljs-title">copyAsset</span> = <span class="hljs-params">(file, callback)</span> -></span>
<span class="hljs-keyword">return</span> callback() <span class="hljs-keyword">unless</span> fs.existsSync file
fs.copy file, path.join(config.output, path.basename(file)), callback
<span class="hljs-function"> <span class="hljs-title">complete</span> = -></span>
copyAsset config.css, <span class="hljs-function"><span class="hljs-params">(error)</span> -></span>
<span class="hljs-keyword">return</span> callback error <span class="hljs-keyword">if</span> error
<span class="hljs-keyword">return</span> copyAsset config.public, callback
files = config.sources.slice()
<span class="hljs-function">
<span class="hljs-title">nextFile</span> = -></span>
source = files.shift()
fs.readFile source, <span class="hljs-function"><span class="hljs-params">(error, buffer)</span> -></span>
<span class="hljs-keyword">return</span> callback error <span class="hljs-keyword">if</span> error
code = buffer.toString()
sections = parse source, code, config
format source, sections, config</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>The <strong>title</strong> of the file is either the first heading in the prose, or the
name of the source file.</p>
</div>
<div class="content"><div class='highlight'><pre> firstSection = _.find sections, <span class="hljs-function"><span class="hljs-params">(section)</span> -></span>
section.docsText.length > <span class="hljs-number">0</span>
first = marked.lexer(firstSection.docsText)[<span class="hljs-number">0</span>] <span class="hljs-keyword">if</span> firstSection
hasTitle = first <span class="hljs-keyword">and</span> first.type <span class="hljs-keyword">is</span> <span class="hljs-string">'heading'</span> <span class="hljs-keyword">and</span> first.depth <span class="hljs-keyword">is</span> <span class="hljs-number">1</span>
title = <span class="hljs-keyword">if</span> hasTitle <span class="hljs-keyword">then</span> first.text <span class="hljs-keyword">else</span> path.basename source
source_infos.push({
source: source,
hasTitle: hasTitle,
title: title,
sections: sections
})
<span class="hljs-keyword">if</span> files.length <span class="hljs-keyword">then</span> nextFile() <span class="hljs-keyword">else</span> outputFiles()</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>When we have finished all preparations (such as extracting a title for each file),
we produce all output files.</p>
<p>We have collected all titles before outputting the individual files to give the
template access to all sources’ titles for rendering, e.g. when the template
needs to produce a TOC with each file.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"> <span class="hljs-title">outputFiles</span> = -></span>
<span class="hljs-keyword">for</span> info, i <span class="hljs-keyword">in</span> source_infos
write info.source, i, source_infos, config
outputCode info.source, info.sections, i, source_infos, config
complete()</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Start processing all sources and producing the corresponding files for each:</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> files.length <span class="hljs-keyword">then</span> nextFile() <span class="hljs-keyword">else</span> outputFiles()</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Given a string of source code, <strong>parse</strong> out each block of prose and the code that
follows it — by detecting which is which, line by line — and then create an
individual <strong>section</strong> for it. Each section is an object with <code>docsText</code> and
<code>codeText</code> properties, and eventually <code>docsHtml</code> and <code>codeHtml</code> as well.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">parse</span> = <span class="hljs-params">(source, code, config = {})</span> -></span>
lines = code.split <span class="hljs-string">'\n'</span>
sections = []
lang = getLanguage source, config
hasCode = docsText = codeText = <span class="hljs-string">''</span>
param = <span class="hljs-string">''</span>
in_block = <span class="hljs-number">0</span>
ignore_this_block = <span class="hljs-number">0</span>
<span class="hljs-function">
<span class="hljs-title">save</span> = -></span>
sections.push {docsText, codeText}
hasCode = docsText = codeText = <span class="hljs-string">''</span></pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Our quick-and-dirty implementation of the literate programming style. Simply
invert the prose and code relationship on a per-line basis, and then continue as
normal below.</p>
<p>Note: “Literate markdown” is an exception here as it’s basically the reverse.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> lang.literate <span class="hljs-keyword">and</span> lang.name != <span class="hljs-string">'markdown'</span>
isText = maybeCode = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">for</span> line, i <span class="hljs-keyword">in</span> lines
lines[i] = <span class="hljs-keyword">if</span> maybeCode <span class="hljs-keyword">and</span> match = <span class="hljs-regexp">/^([ ]{4}|[ ]{0,3}\t)/</span>.exec line
isText = <span class="hljs-literal">no</span>
line[match[<span class="hljs-number">0</span>].length..]
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> maybeCode = <span class="hljs-regexp">/^\s*$/</span>.test line
<span class="hljs-keyword">if</span> isText <span class="hljs-keyword">then</span> lang.symbol <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>
<span class="hljs-keyword">else</span>
isText = <span class="hljs-literal">yes</span>
lang.symbol + <span class="hljs-string">' '</span> + line</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Iterate over the source lines, and separate out single/block
comments from code chunks.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> lines
<span class="hljs-keyword">if</span> in_block
++in_block
raw_line = line</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>If we’re not in a block comment, and find a match for the start
of one, eat the tokens, and note that we’re now in a block.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> in_block <span class="hljs-keyword">and</span> config.blocks <span class="hljs-keyword">and</span> lang.blocks <span class="hljs-keyword">and</span> line.match(lang.commentEnter)
line = line.replace(lang.commentEnter, <span class="hljs-string">''</span>)</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Make sure this is a comment that we actually want to process; if not, treat it as code</p>
</div>
<div class="content"><div class='highlight'><pre> in_block = <span class="hljs-number">1</span>
<span class="hljs-keyword">if</span> lang.commentIgnore <span class="hljs-keyword">and</span> line.match(lang.commentIgnore)
ignore_this_block = <span class="hljs-number">1</span></pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Process the line, marking it as docs if we’re in a block comment,
or we find a single-line comment marker.</p>
</div>
<div class="content"><div class='highlight'><pre> single = (<span class="hljs-keyword">not</span> in_block <span class="hljs-keyword">and</span> lang.commentMatcher <span class="hljs-keyword">and</span> line.match(lang.commentMatcher) <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> line.match(lang.commentFilter))</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>If there’s a single comment, and we’re not in a block, eat the
comment token.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> single
line = line.replace(lang.commentMatcher, <span class="hljs-string">''</span>)</pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>Make sure this is a comment that we actually want to process; if not, treat it as code</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> lang.commentIgnore <span class="hljs-keyword">and</span> line.match(lang.commentIgnore)
ignore_this_block = <span class="hljs-number">1</span></pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>Prepare the line further when it is (part of) a comment line.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> in_block <span class="hljs-keyword">or</span> single</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p>If we’re in a block comment and we find the end of it in the line, eat
the end token, and note that we’re no longer in the block.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> in_block <span class="hljs-keyword">and</span> line.match(lang.commentExit)
line = line.replace(lang.commentExit, <span class="hljs-string">''</span>)
in_block = <span class="hljs-number">-1</span></pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p>If we’re in a block comment and are processing comment line 2 or further, eat the
optional comment prefix (for C style comments, that would generally be
a single ‘*’, for example).</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> in_block > <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> lang.commentNext
line = line.replace(lang.commentNext, <span class="hljs-string">''</span>);</pre></div></div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>If we happen upon a JavaDoc <code>@param</code> parameter, then process that item.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> lang.commentParam
param = line.match(lang.commentParam);
<span class="hljs-keyword">if</span> param
line = line.replace(param[<span class="hljs-number">0</span>], <span class="hljs-string">'\n'</span> + <span class="hljs-string">'<b>'</span> + param[<span class="hljs-number">1</span>] + <span class="hljs-string">'</b>'</span>);
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> ignore_this_block <span class="hljs-keyword">and</span> (in_block <span class="hljs-keyword">or</span> single)</pre></div></div>
</li>
<li id="section-20">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<p>If we have code text, and we’re entering a comment, store off
the current docs and code, then start a new section.</p>
</div>
<div class="content"><div class='highlight'><pre> save() <span class="hljs-keyword">if</span> hasCode
docsText += line + <span class="hljs-string">'\n'</span>
save() <span class="hljs-keyword">if</span> <span class="hljs-regexp">/^(---+|===+)$/</span>.test line <span class="hljs-keyword">or</span> in_block == <span class="hljs-number">-1</span>
<span class="hljs-keyword">else</span>
hasCode = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">if</span> config.indent
oldLen = <span class="hljs-number">0</span>
<span class="hljs-keyword">while</span> oldLen != line.length
oldLen = line.length
line = line.replace(<span class="hljs-regexp">/^(\x20*)\t/</span>, <span class="hljs-string">'$1'</span> + config.indent)
codeText += line + <span class="hljs-string">'\n'</span></pre></div></div>
</li>
<li id="section-21">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-21">¶</a>
</div>
<p>Reset <code>in_block</code> when we have reached the end of the comment block.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> in_block == <span class="hljs-number">-1</span>
in_block = <span class="hljs-number">0</span></pre></div></div>
</li>
<li id="section-22">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-22">¶</a>
</div>
<p>Reset <code>ignore_this_block</code> when we have reached the end of the comment block or single comment line.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> in_block
ignore_this_block = <span class="hljs-number">0</span></pre></div></div>
</li>
<li id="section-23">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-23">¶</a>
</div>
<p>Save the final section, if any, and return the sections array.</p>
</div>
<div class="content"><div class='highlight'><pre> save()
sections</pre></div></div>
</li>
<li id="section-24">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-24">¶</a>
</div>
<p>To <strong>format</strong> and highlight the now-parsed sections of code, we use <strong>Highlight.js</strong>
over stdio, and run the text of their corresponding comments through
<strong>Markdown</strong>, using <a href="https://github.com/chjj/marked">Marked</a>.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">format</span> = <span class="hljs-params">(source, sections, config)</span> -></span>
language = getLanguage source, config</pre></div></div>
</li>
<li id="section-25">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-25">¶</a>
</div>
<p>Pass any user defined options to Marked if specified via command line option,
otherwise revert use the default configuration.</p>
</div>
<div class="content"><div class='highlight'><pre> markedOptions = config.marked_options
marked.setOptions markedOptions</pre></div></div>
</li>
<li id="section-26">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-26">¶</a>
</div>
<p>Tell Marked how to highlight code blocks within comments, treating that code
as either the language specified in the code block or the language of the file
if not specified.</p>
</div>
<div class="content"><div class='highlight'><pre> marked.setOptions {
highlight: <span class="hljs-function"><span class="hljs-params">(code, lang)</span> -></span>
lang <span class="hljs-keyword">or</span>= language.name
<span class="hljs-keyword">if</span> highlightjs.getLanguage(lang)
highlightjs.highlight(lang, code).value
<span class="hljs-keyword">else</span>
<span class="hljs-built_in">console</span>.warn <span class="hljs-string">"docco: couldn't highlight code block with unknown language '<span class="hljs-subst">#{lang}</span>' in <span class="hljs-subst">#{source}</span>"</span>
code
}</pre></div></div>
</li>
<li id="section-27">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-27">¶</a>
</div>
<p>Also instruct Marked to run a preliminary scan of all the chunks first, where we want to
collect all link references. Only in the second run will we then require Marked to produce
the final HTML rendered output.</p>
<p>We have to execute this 2-phase process to ensure that any input which includes MarkDown
link references is processed properly: without the initial scan any link reference defined
later (near the end of the input document) will be unknown to document chunks near the top
of the input document.</p>
</div>
<div class="content"><div class='highlight'><pre> marked.setOptions {
linksCollector: {}
execPrepPhaseOnly: <span class="hljs-literal">true</span>
}</pre></div></div>
</li>
<li id="section-28">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-28">¶</a>
</div>
<p>Process each chunk (phase 1):</p>
<ul>
<li>both the code and text blocks are stripped of trailing empty lines</li>
<li>the code block is marked up by highlighted to show a nice HTML rendition of the code</li>
<li>the text block is fed to Marked for an initial scan</li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> section, i <span class="hljs-keyword">in</span> sections
<span class="hljs-keyword">if</span> language.name == <span class="hljs-string">'markdown'</span>
<span class="hljs-keyword">if</span> language.literate
code = section.codeText
section.codeText = code = code.replace(<span class="hljs-regexp">/\s+$/</span>, <span class="hljs-string">''</span>)
section.codeHtml = marked(code)
doc = section.docsText
section.docsText = doc = doc.replace(<span class="hljs-regexp">/\s+$/</span>, <span class="hljs-string">''</span>)
marked(doc)
<span class="hljs-keyword">else</span>
section.codeHtml = <span class="hljs-string">''</span>
code = section.codeText
section.codeText = code = code.replace(<span class="hljs-regexp">/\s+$/</span>, <span class="hljs-string">''</span>)
marked(code)
<span class="hljs-keyword">else</span>
code = section.codeText
section.codeText = code = code.replace(<span class="hljs-regexp">/\s+$/</span>, <span class="hljs-string">''</span>)
<span class="hljs-keyword">try</span>
code = highlightjs.highlight(language.name, code).value
<span class="hljs-keyword">catch</span> err
<span class="hljs-keyword">throw</span> err <span class="hljs-keyword">unless</span> config.ignore
code = section.codeText
section.codeHtml = <span class="hljs-string">"<div class='highlight'><pre><span class="hljs-subst">#{code}</span></pre></div>"</span>
doc = section.docsText
section.docsText = doc = doc.replace(<span class="hljs-regexp">/\s+$/</span>, <span class="hljs-string">''</span>)
marked(doc)</pre></div></div>
</li>
<li id="section-29">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-29">¶</a>
</div>
<p>Process each chunk (phase 2):</p>
<ul>
<li>the text block is fed to Marked to turn it into HTML</li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> marked.setOptions {
execPrepPhaseOnly: <span class="hljs-literal">false</span>
}
<span class="hljs-keyword">for</span> section, i <span class="hljs-keyword">in</span> sections
<span class="hljs-keyword">if</span> language.name == <span class="hljs-string">'markdown'</span>
<span class="hljs-keyword">if</span> language.literate
doc = section.docsText
section.docsHtml = marked(doc)
<span class="hljs-keyword">else</span>
code = section.codeText
section.docsHtml = marked(code)
<span class="hljs-keyword">else</span>
doc = section.docsText
section.docsHtml = marked(doc)</pre></div></div>
</li>
<li id="section-30">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-30">¶</a>
</div>
<p>Once all of the code has finished highlighting, we can <strong>write</strong> the resulting
documentation file by passing the completed HTML sections into the template,
and rendering it to the specified output path.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">write</span> = <span class="hljs-params">(source, title_idx, source_infos, config)</span> -></span>
<span class="hljs-function">
<span class="hljs-title">destination</span> = <span class="hljs-params">(file)</span> -></span>
make_destination config.output, config.separator, file, <span class="hljs-string">'.html'</span>, config
destfile = destination source
<span class="hljs-function">
<span class="hljs-title">relative</span> = <span class="hljs-params">(srcfile)</span> -></span>
to = path.dirname(path.resolve(srcfile))
dstfile = destination srcfile
from = path.dirname(path.resolve(dstfile))
path.join(path.relative(from, to), path.basename(srcfile))
css = <span class="hljs-keyword">if</span> config.css <span class="hljs-keyword">then</span> relative path.join(config.output, path.basename(config.css)) <span class="hljs-keyword">else</span> <span class="hljs-literal">null</span>
html = config.template {
sources: config.sources
titles: source_infos.map (info) ->
info.title
css
title: source_infos[title_idx].title
hasTitle: source_infos[title_idx].hasTitle
sections: source_infos[title_idx].sections
path
destination
relative
language: getLanguage source, config
}
<span class="hljs-built_in">console</span>.log <span class="hljs-string">"docco: <span class="hljs-subst">#{source}</span> -> <span class="hljs-subst">#{destfile}</span>"</span>
fs.mkdirsSync path.dirname(destfile)
fs.writeFileSync destfile, html
source_infos[title_idx].destDocFile = destfile</pre></div></div>
</li>
<li id="section-31">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-31">¶</a>
</div>
<p>Print out the consolidated code sections parsed from the source file in to another
file. No documentation will be included in the new file.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">outputCode</span> = <span class="hljs-params">(source, sections, title_idx, source_infos, config)</span> -></span>
lang = getLanguage source, config
<span class="hljs-keyword">if</span> config.source
destfile = make_destination config.source, config.separator, source, lang.source, config
code = _.pluck(sections, <span class="hljs-string">'codeText'</span>).join <span class="hljs-string">'\n'</span>
code = code.trim().replace <span class="hljs-regexp">/(\n{2,})/g</span>, <span class="hljs-string">'\n\n'</span>
<span class="hljs-built_in">console</span>.log <span class="hljs-string">"docco: <span class="hljs-subst">#{source}</span> -> <span class="hljs-subst">#{destfile}</span>"</span>
fs.mkdirsSync path.dirname(destfile)
fs.writeFileSync destfile, code
source_infos[title_idx].destCodeFile = destfile</pre></div></div>
</li>
<li id="section-32">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-32">¶</a>
</div>
<h2 id="helper-functions">Helper Functions</h2>
</div>
</li>
<li id="section-33">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-33">¶</a>
</div>
<p>To help us produce decent file names and paths for all inputs, we define a few helper functions:</p>
<p>It should not matter if we are running on Windows, Unix or some other platform: we unify all paths
to a single UNIXy format which can be processed easily everywhere (Windows accepts both native and
UNIX path separators).</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">normalize</span> = <span class="hljs-params">(path)</span> -></span>
path.replace(<span class="hljs-regexp">/[\\\/]/g</span>, <span class="hljs-string">'/'</span>)</pre></div></div>
</li>
<li id="section-34">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-34">¶</a>
</div>
<p>We construct a suitable filename/path for each document by prepending it with the specified
relative path while using the separator specified on the command line. (The default separator (‘-‘
dash) is used to flatten the directory tree when we process a directory tree all at once.)</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">qualifiedName</span> = <span class="hljs-params">(file, separator, extension, config)</span> -></span>
cwd = <span class="hljs-keyword">if</span> config <span class="hljs-keyword">and</span> config.cwd <span class="hljs-keyword">then</span> config.cwd <span class="hljs-keyword">else</span> process.cwd()
file = normalize(file)
nameParts = path.dirname(file).replace(normalize(cwd), <span class="hljs-string">''</span>).split(<span class="hljs-string">'/'</span>)
nameParts.shift() <span class="hljs-keyword">while</span> nameParts[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">''</span> <span class="hljs-keyword">or</span> nameParts[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">'.'</span> <span class="hljs-keyword">or</span> nameParts[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">'..'</span>
nameParts.push(path.basename(file, path.extname(file)))
nameParts.join(separator) + extension
<span class="hljs-function">
<span class="hljs-title">make_destination</span> = <span class="hljs-params">(basepath, separator, file, extension, config)</span> -></span>
path.join basepath, qualifiedName(file, separator, extension, config)</pre></div></div>
</li>
<li id="section-35">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-35">¶</a>
</div>
<h2 id="configuration">Configuration</h2>
</div>
</li>
<li id="section-36">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-36">¶</a>
</div>
<p>Default configuration <strong>options</strong>. All of these may be extended by
user-specified options.</p>
</div>
<div class="content"><div class='highlight'><pre>defaults =
sources: []
layout: <span class="hljs-string">'parallel'</span>
output: <span class="hljs-string">'docs'</span>
template: <span class="hljs-literal">null</span>
css: <span class="hljs-literal">null</span>
extension: <span class="hljs-literal">null</span>
languages: {}
source: <span class="hljs-literal">null</span>
cwd: process.cwd()
separator: <span class="hljs-string">'-'</span>
blocks: <span class="hljs-literal">false</span>
marked_options: {
gfm: <span class="hljs-literal">true</span>,
tables: <span class="hljs-literal">true</span>,
breaks: <span class="hljs-literal">false</span>,
pedantic: <span class="hljs-literal">false</span>,
sanitize: <span class="hljs-literal">false</span>,
smartLists: <span class="hljs-literal">true</span>,
smartypants: <span class="hljs-literal">yes</span>,
langPrefix: <span class="hljs-string">'language-'</span>,
highlight: <span class="hljs-function"><span class="hljs-params">(code, lang)</span> -></span>
code
}
ignore: <span class="hljs-literal">false</span>
tabSize: <span class="hljs-literal">null</span>
indent: <span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-37">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-37">¶</a>
</div>
<p><strong>Configure</strong> this particular run of Docco. We might use a passed-in external
template, one of the built-in <strong>layouts</strong>, or an external <strong>layout</strong>. We only attempt to process
source files for languages for which we have definitions.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">configure</span> = <span class="hljs-params">(options)</span> -></span>
config = _.extend {}, defaults, _.pick(options, _.keys(defaults)...)
config.languages = buildMatchers config.languages</pre></div></div>
</li>
<li id="section-38">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-38">¶</a>
</div>
<p>Determine what the indent should be if the user has supplied a custom tab-size
on the command line.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> config.tabSize
config.indent = Array(parseInt(config.tabSize) + <span class="hljs-number">1</span>).join(<span class="hljs-string">' '</span>)</pre></div></div>
</li>
<li id="section-39">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-39">¶</a>
</div>
<p>The user is able to override the layout file used with the <code>--template</code> parameter.
In this case, it is also necessary to explicitly specify a stylesheet file.
These custom templates are compiled exactly like the predefined ones, but the <code>public</code> folder
is only copied for the latter.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> options.template
<span class="hljs-keyword">unless</span> options.css
<span class="hljs-built_in">console</span>.warn <span class="hljs-string">"docco: no stylesheet file specified"</span>
config.layout = <span class="hljs-literal">null</span>
<span class="hljs-keyword">else</span>
dir = config.layout = <span class="hljs-keyword">if</span> fs.existsSync path.join __dirname, <span class="hljs-string">'resources'</span>, config.layout <span class="hljs-keyword">then</span> path.join __dirname, <span class="hljs-string">'resources'</span>, config.layout <span class="hljs-keyword">else</span> path.join process.cwd(), config.layout;
config.public = path.join dir, <span class="hljs-string">'public'</span> <span class="hljs-keyword">if</span> fs.existsSync path.join dir, <span class="hljs-string">'public'</span>
config.template = path.join dir, <span class="hljs-string">'docco.jst'</span>
config.css = options.css <span class="hljs-keyword">or</span> path.join dir, <span class="hljs-string">'resources/parallel/docco.css'</span>
config.template = _.template fs.readFileSync(config.template).toString()</pre></div></div>
</li>
<li id="section-40">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-40">¶</a>
</div>
<p>When the user specifies custom Marked options in a (JSON-formatted) configuration file,
mix those options which our defaults such that each default option remains active when it has
not been explicitly overridden by the user.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> options.marked_options
config.marked_options = _.extend config.marked_options, JSON.parse fs.readFileSync(options.marked_options)
<span class="hljs-keyword">if</span> options.args
config.sources = options.args.filter(<span class="hljs-function"><span class="hljs-params">(source)</span> -></span>
lang = getLanguage source, config
<span class="hljs-built_in">console</span>.warn <span class="hljs-string">"docco: skipped unknown type (<span class="hljs-subst">#{path.basename source}</span>)"</span> <span class="hljs-keyword">unless</span> lang
lang
).sort()
config</pre></div></div>
</li>
<li id="section-41">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-41">¶</a>
</div>
<h2 id="helpers-initial-setup">Helpers & Initial Setup</h2>