-
Notifications
You must be signed in to change notification settings - Fork 15
/
comment.d
3355 lines (2877 loc) · 85 KB
/
comment.d
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
// <details> html tag
// FIXME: KEY_VALUE like params
module arsd.docgen.comment;
import arsd.docgen.syntaxhighlighter;
import adrdox.main;
import arsd.dom;
import dparse.ast;
import dparse.lexer;
import std.string;
import std.algorithm;
import std.conv;
const(char)[] htmlEncode(const(char)[] s) {
return s.
replace("&", "&").
replace("<", "<").
replace(">", ">");
}
static struct MyOutputRange {
this(string* output) {
this.output = output;
}
string* output;
void put(T...)(T s) {
foreach(i; s)
putTag(i.htmlEncode);
}
void putTag(in char[] s) {
if(s.length == 0)
return;
//foreach(ch; s) {
//assert(s.length);
//assert(s.indexOf("</body>") == -1);
//}
(*output) ~= s;
}
}
enum TexMathOpt {
LaTeX,
KaTeX,
}
TexMathOpt parseTexMathOpt(string str) {
switch (str) with(TexMathOpt) {
case "latex": return LaTeX;
case "katex": return KaTeX;
default: throw new Exception("Unsupported 'tex-math' option");
}
}
/*
Params:
The line, excluding whitespace, must start with
identifier = to start a new thing. If a new thing starts,
it closes the old.
The description may be formatted however. Whitespace is stripped.
*/
string getIdent(T)(T t) {
if(t is null)
return null;
if(t.identifier == tok!"")
return null;
return t.identifier.text;
}
bool hasParam(T)(const T dec, string name, Decl declObject, bool descend = true) {
if(dec is null)
return false;
static if(__traits(compiles, dec.parameters)) {
if(dec.parameters && dec.parameters.parameters)
foreach(parameter; dec.parameters.parameters)
if(parameter.name.text == name)
return true;
}
static if(__traits(compiles, dec.templateParameters)) {
if(dec.templateParameters && dec.templateParameters.templateParameterList)
foreach(parameter; dec.templateParameters.templateParameterList.items) {
if(getIdent(parameter.templateTypeParameter) == name)
return true;
if(getIdent(parameter.templateValueParameter) == name)
return true;
if(getIdent(parameter.templateAliasParameter) == name)
return true;
if(getIdent(parameter.templateTupleParameter) == name)
return true;
if(parameter.templateThisParameter && getIdent(parameter.templateThisParameter.templateTypeParameter) == name)
return true;
}
}
if(!descend)
return false;
// need to check parent template parameters in case they are
// referenced in a child
if(declObject.parent) {
bool h;
if(auto pdec = cast(TemplateDecl) declObject.parent)
h = hasParam(pdec.astNode, name, pdec, false);
else
{}
if(h)
return true;
}
// and eponymous ones
static if(is(T == TemplateDeclaration)) {
auto decl = cast(TemplateDecl) declObject;
if(decl) {
auto e = decl.eponymousMember();
if(e) {
if(auto a = cast(ConstructorDecl) e)
return hasParam(a.astNode, name, a, false);
if(auto a = cast(FunctionDecl) e)
return hasParam(a.astNode, name, a, false);
// FIXME: add more
}
}
}
return false;
}
struct LinkReferenceInfo {
enum Type { none, text, link, image }
Type type;
bool isFootnote;
string text; // or alt
string link; // or src
string toHtml(string fun, string rt) {
if(rt is null)
rt = fun;
if(rt is fun && text !is null && type != Type.text)
rt = text;
string display = isFootnote ? ("[" ~ rt ~ "]") : rt;
Element element;
final switch(type) {
case Type.none:
return null;
case Type.text:
case Type.link:
element = isFootnote ? Element.make("sup", "", "footnote-ref") : Element.make("span");
if(type == Type.text) {
element.addChild("abbr", display).setAttribute("title", text);
} else {
element.addChild("a", display, link);
}
break;
case Type.image:
element = Element.make("img", link, text);
break;
}
return element.toString();
}
}
__gshared string[string] globalLinkReferences;
void loadGlobalLinkReferences(string text) {
foreach(line; text.splitLines) {
line = line.strip;
if(line.length == 0)
continue;
auto idx = line.indexOf("=");
if(idx == -1)
continue;
auto name = line[0 .. idx].strip;
auto value = line[idx + 1 .. $].strip;
if(name.length == 0 || value.length == 0)
continue;
globalLinkReferences[name] = value;
}
}
Element getSymbolGroupReference(string name, Decl decl, string rt) {
auto pm = decl.isModule() ? decl : decl.parentModule;
if(pm is null)
return null;
if(name in pm.parsedDocComment.symbolGroups) {
return Element.make("a", rt.length ? rt : name, pm.fullyQualifiedName ~ ".html#group-" ~ name);
}
return null;
}
LinkReferenceInfo getLinkReference(string name, Decl decl) {
bool numeric = (name.all!((ch) => ch >= '0' && ch <= '9'));
bool onGlobal = false;
auto refs = decl.parsedDocComment.linkReferences;
try_again:
if(name in refs) {
auto txt = refs[name];
assert(txt.length);
LinkReferenceInfo lri;
import std.uri;
auto l = uriLength(txt);
if(l != -1) {
lri.link = txt;
lri.type = LinkReferenceInfo.Type.link;
} else if(txt[0] == '$') {
// should be an image or plain text
if(txt.length > 5 && txt[0 .. 6] == "$(IMG " && txt[$-1] == ')') {
txt = txt[6 .. $-1];
auto idx = txt.indexOf(",");
if(idx == -1) {
lri.link = txt.strip;
} else {
lri.text = txt[idx + 1 .. $].strip;
lri.link = txt[0 .. idx].strip;
}
lri.type = LinkReferenceInfo.Type.image;
} else goto plain_text;
} else if(txt[0] == '[' && txt[$-1] == ']') {
txt = txt[1 .. $-1];
auto idx = txt.indexOf("|");
if(idx == -1) {
lri.link = txt;
} else {
lri.link = txt[0 .. idx].strip;
lri.text = txt[idx + 1 .. $].strip;
}
lri.link = getReferenceLink(lri.link, decl).href;
lri.type = LinkReferenceInfo.Type.link;
} else {
plain_text:
lri.type = LinkReferenceInfo.Type.text;
lri.link = null;
lri.text = txt;
}
lri.isFootnote = numeric;
return lri;
} else if(!onGlobal && !numeric) {
if(decl.parent !is null)
decl = decl.parent;
else {
onGlobal = true;
refs = globalLinkReferences;
// decl = null; // so I kinda want it to be null, but that breaks like everything so I can't.
}
goto try_again;
}
return LinkReferenceInfo.init;
}
struct DocComment {
string ddocSummary;
string synopsis;
string details;
string[] params; // stored as ident=txt. You can split on first index of =.
string returns;
string examples;
string diagnostics;
string throws;
string bugs;
string see_alsos;
string[string] symbolGroups; // stored as "name" : "raw text"
string[] symbolGroupsOrder;
string group; // the group this belongs to, if any
string[string] otherSections;
string[string] linkReferences;
string[string] userDefinedMacros;
Decl decl;
bool synopsisEndedOnDoubleBlank;
void writeSynopsis(MyOutputRange output) {
output.putTag("<div class=\"documentation-comment synopsis\">");
output.putTag(formatDocumentationComment(synopsis, decl));
if(details !is synopsis && details.strip.length && details.strip != "<div></div>")
output.putTag(` <a id="more-link" href="#details">More...</a>`);
output.putTag("</div>");
}
void writeDetails(T = FunctionDeclaration)(MyOutputRange output) {
writeDetails!T(output, cast(T) null, null);
}
void writeDetails(T = FunctionDeclaration)(MyOutputRange output, const T functionDec = null, Decl.ProcessedUnittest[] utInfo = null) {
auto f = new MyFormatter!(typeof(output))(output, decl);
string[] params = this.params;
static if(is(T == FunctionDeclaration) || is(T == Constructor)) {
if(functionDec !is null) {
auto dec = functionDec;
if(dec.templateParameters && dec.templateParameters.templateParameterList)
foreach(parameter; dec.templateParameters.templateParameterList.items) {
if(auto name = getIdent(parameter.templateTypeParameter))
if(name.length && parameter.comment.length) params ~= name ~ "=" ~ preprocessComment(parameter.comment, decl);
if(auto name = getIdent(parameter.templateValueParameter))
if(name.length && parameter.comment.length) params ~= name ~ "=" ~ preprocessComment(parameter.comment, decl);
if(auto name = getIdent(parameter.templateAliasParameter))
if(name.length && parameter.comment.length) params ~= name ~ "=" ~ preprocessComment(parameter.comment, decl);
if(auto name = getIdent(parameter.templateTupleParameter))
if(name.length && parameter.comment.length) params ~= name ~ "=" ~ preprocessComment(parameter.comment, decl);
if(parameter.templateThisParameter)
if(auto name = getIdent(parameter.templateThisParameter.templateTypeParameter))
if(name.length && parameter.comment.length) params ~= name ~ "=" ~ preprocessComment(parameter.comment, decl);
}
foreach(p; functionDec.parameters.parameters) {
auto name = p.name.text;
if(name.length && p.comment.length)
params ~= name ~ "=" ~ preprocessComment(p.comment, decl);
}
}
}
if(params.length) {
int count = 0;
foreach(param; params) {
auto split = param.indexOf("=");
auto paramName = param[0 .. split];
if(!hasParam(functionDec, paramName, decl)) {
//import std.stdio; writeln("no param " ~ paramName, " ", functionDec);
continue;
}
count++;
}
if(count) {
output.putTag("<h2 id=\"parameters\">Parameters</h2>");
output.putTag("<dl class=\"parameter-descriptions\">");
foreach(param; params) {
auto split = param.indexOf("=");
auto paramName = param[0 .. split];
if(!hasParam(functionDec, paramName, decl))
continue;
output.putTag("<dt id=\"param-"~param[0 .. split]~"\">");
output.putTag("<a href=\"#param-"~param[0 .. split]~"\" class=\"parameter-name\" data-ident=\"");
output.put(param[0 .. split]);
output.putTag("\">");
output.put(param[0 .. split]);
output.putTag("</a>");
static if(is(T == FunctionDeclaration) || is(T == Constructor))
if(functionDec !is null) {
const(Parameter)* paramAst;
foreach(ref p; functionDec.parameters.parameters) {
if(p.name.type != tok!"")
if(p.name.text == param[0 .. split]) {
paramAst = &p;
break;
}
}
if(paramAst) {
output.putTag(" <span class=\"parameter-type\">");
f.format(paramAst.type);
output.putTag("</span>");
}
if(paramAst && paramAst.atAttributes.length) {
output.putTag("<div class=\"parameter-attributes\">");
output.put("Attributes:");
foreach (attribute; paramAst.atAttributes) {
output.putTag("<div class=\"parameter-attribute\">");
f.format(attribute);
output.putTag("</div>");
}
output.putTag("</div>");
}
}
output.putTag("</dt>");
output.putTag("<dd>");
output.putTag("<div class=\"documentation-comment\">");
output.putTag(formatDocumentationComment(param[split + 1 .. $], decl));
output.putTag("</div></dd>");
}
output.putTag("</dl>");
}
}
if(returns !is null) {
output.putTag("<h2 id=\"returns\">Return Value</h2>");
output.putTag("<div>");
static if(is(T == FunctionDeclaration))
if(functionDec !is null) {
output.putTag("<div class=\"return-type-holder\">");
output.putTag("Type: ");
output.putTag("<span class=\"return-type\">");
if(functionDec.hasAuto && functionDec.hasRef)
output.putTag(`<a class="lang-feature" href="http://dpldocs.info/auto-ref-function-return-prototype">auto ref</a> `);
else {
if (functionDec.hasAuto)
output.putTag(`<a class="lang-feature" href="http://dpldocs.info/auto-function-return-prototype">auto</a> `);
if (functionDec.hasRef)
output.putTag(`<a class="lang-feature" href="http://dpldocs.info/ref-function-return-prototype">ref</a> `);
}
if (functionDec.returnType !is null)
f.format(functionDec.returnType);
output.putTag("</span>");
output.putTag("</div>");
}
output.putTag("<div class=\"documentation-comment returns-description\">");
output.putTag(formatDocumentationComment(returns, decl));
output.putTag("</div>");
output.putTag("</div>");
}
if(details.strip.length && details.strip != "<div></div>") {
output.putTag("<h2 id=\"details\">Detailed Description</h2>");
output.putTag("<div class=\"documentation-comment detailed-description\">");
output.putTag(formatDocumentationComment(details, decl));
output.putTag("</div>");
}
if(throws.strip.length) {
output.putTag("<h2 id=\"throws\">Throws</h2>");
output.putTag("<div class=\"documentation-comment throws-description\">");
output.putTag(formatDocumentationComment(throws, decl));
output.putTag("</div>");
}
if(diagnostics.strip.length) {
output.putTag("<h2 id=\"diagnostics\">Common Problems</h2>");
output.putTag("<div class=\"documentation-comment diagnostics\">");
output.putTag(formatDocumentationComment(diagnostics, decl));
output.putTag("</div>");
}
if(bugs.strip.length) {
output.putTag("<h2 id=\"bugs\">Bugs</h2>");
output.putTag("<div class=\"documentation-comment bugs-description\">");
output.putTag(formatDocumentationComment(bugs, decl));
output.putTag("</div>");
}
bool hasUt;
foreach(ut; utInfo) if(ut.embedded == false){hasUt = true; break;}
if(examples.length || hasUt) {
output.putTag("<h2 id=\"examples\"><a href=\"#examples\" class=\"header-anchor\">Examples</a></h2>");
output.putTag("<div class=\"documentation-comment\">");
output.putTag(formatDocumentationComment(examples, decl));
output.putTag("</div>");
foreach(example; utInfo) {
if(example.embedded) continue;
output.putTag(formatUnittestDocTuple(example, decl).toString());
}
}
if(group)
see_alsos ~= "\n\n[" ~ group ~ "]";
if(see_alsos.length) {
output.putTag("<h2 id=\"see-also\">See Also</h2>");
output.putTag("<div class=\"documentation-comment see-also-section\">");
output.putTag(formatDocumentationComment(see_alsos, decl));
output.putTag("</div>");
}
if(otherSections.keys.length) {
output.putTag("<h2 id=\"meta\">Meta</h2>");
}
foreach(section, content; otherSections) {
output.putTag("<div class=\"documentation-comment " ~ section ~ "-section other-section\">");
output.putTag("<h3>");
output.put(section.capitalize);
output.putTag("</h3>");
output.putTag(formatDocumentationComment(content, decl));
output.putTag("</div>");
}
}
}
Element formatUnittestDocTuple(Decl.ProcessedUnittest example, Decl decl) {
auto holder = Element.make("div").addClass("unittest-example-holder");
holder.addChild(formatDocumentationComment2(preprocessComment(example.comment, decl), decl).addClass("documentation-comment"));
auto pre = holder.addChild("pre").addClass("d_code highlighted");
// trim off leading/trailing newlines since they just clutter output
auto codeToWrite = example.code;
while(codeToWrite.length && (codeToWrite[0] == '\n' || codeToWrite[0] == '\r'))
codeToWrite = codeToWrite[1 .. $];
while(codeToWrite.length && (codeToWrite[$-1] == '\n' || codeToWrite[$-1] == '\r'))
codeToWrite = codeToWrite[0 .. $ - 1];
pre.innerHTML = highlight(outdent(codeToWrite));
return holder;
}
string preprocessComment(string comment, Decl decl) {
if(comment.length < 3)
return comment;
comment = comment.replace("\r\n", "\n");
comment = comment[1 .. $]; // trim off the /
auto commentType = comment[0];
while(comment.length && comment[0] == commentType)
comment = comment[1 .. $]; // trim off other opening spam
string closingSpam;
if(commentType == '*' || commentType == '+') {
comment = comment[0 .. $-1]; // trim off the closing /
bool closingSpamFound;
auto tidx = 0;
while(comment.length && comment[$-1 - tidx] == commentType) {
//comment = comment[0 .. $-1]; // trim off other closing spam
tidx++;
closingSpamFound = true;
}
if(closingSpamFound) {
// if there was closing spam we also want to count the spaces before it
// to trim off other line spam. The goal here is to clean up
/**
* Resolve host name.
* Returns: false if unable to resolve.
*/
// into just "Resolve host name.\n Returns: false if unable to resolve."
// give or take some irrelevant whitespace.
// new algorithm in response to github issue # 36
auto li = lastIndexOf(comment, "\n");
if(li != -1) {
// the closing line must be only whitespace and the marker to consider this
size_t spot = size_t.max;
foreach(idx, ch; comment[li + 1 .. $]) {
if(!(ch == ' ' || ch == '\t' || ch == commentType)) {
spot = size_t.max;
// "foo +/" on the end
// need to cut off the + from the end
// since the final / was already cut
comment = comment[0 .. $-1];
break;
} else {
if(spot == size_t.max && ch == commentType)
spot = idx + 1;
}
}
if(spot != size_t.max) {
closingSpam = comment[li + 1 .. li + 1 + spot];
comment = comment[0 .. li];
}
} else {
// single liner, strip off the closing spam
// need to cut off the + from the end
// since the final / was already cut
comment = comment[0 .. $-1];
}
// old algorithm
/+
while(comment.length && (comment[$-1] == '\t' || comment[$-1] == ' ')) {
closingSpam = comment[$-1] ~ closingSpam;
comment = comment[0 .. $-1]; // trim off other closing spam
}
+/
if(closingSpam.length == 0)
closingSpam = " "; // some things use the " *" leader, but still end with "*/" on a line of its own
}
}
string poop;
if(commentType == '/')
poop = "///";
else
poop = closingSpam;//closingSpam ~ commentType ~ " ";
string newComment;
foreach(line; comment.splitter("\n")) {
// check for " * some text"
if(line.length >= poop.length && line.startsWith(poop)) {
if(line.length > poop.length && line[poop.length] == ' ')
newComment ~= line[poop.length + 1 .. $];
else if(line.length > poop.length && line[poop.length] == '/' && poop != "/")
newComment ~= line;
else
newComment ~= line[poop.length .. $];
}
// check for an empty line with just " *"
else if(line.length == poop.length-1 && line[0..poop.length-1] == poop[0..$-1])
{} // this space is intentionally left blank; it is an empty line
else if(line.length > 1 && commentType != '/' && line[0] == commentType && line[1] == ' ')
newComment ~= line[1 .. $]; // cut off stupid leading * with no space before too
else
newComment ~= line;
newComment ~= "\n";
}
comment = newComment;
return specialPreprocess(comment, decl);
}
DocComment parseDocumentationComment(string comment, Decl decl) {
DocComment c;
if(generatingSource) {
if(decl.lineNumber)
c.otherSections["source"] ~= "$(LINK2 source/"~decl.parentModule.name~".d.html#L"~to!string(decl.lineNumber)~", See Implementation)$(BR)";
else if(!decl.fakeDecl)
c.otherSections["source"] ~= "$(LINK2 source/"~decl.parentModule.name~".d.html, See Source File)$(BR)";
}
// FIXME: add links to ddoc and ddox iff std.* or core.*
c.decl = decl;
comment = preprocessComment(comment, decl);
void parseSections(string comment) {
string remaining;
string section;
bool inSynopsis = true;
bool justSawBlank = false;
bool inCode = false;
string inCodeStyle;
bool hasAnySynopsis = false;
bool inDdocSummary = true;
bool synopsisEndedOnDoubleBlank = false;
string currentMacroName;
// for symbol_groups
string* currentValue;
bool maybeGroupLine = true;
auto lastLineHelper = comment.strip;
auto lastLine = lastLineHelper.lastIndexOf("\n");
if(lastLine != -1) {
auto lastLineText = lastLineHelper[lastLine .. $].strip;
if(lastLineText.startsWith("Group: ") || lastLineText.startsWith("group: ")) {
c.group = lastLineText["Group: ".length .. $];
maybeGroupLine = false;
comment = lastLineHelper[0 .. lastLine].strip;
}
}
foreach(line; comment.splitter("\n")) {
if(maybeGroupLine) {
maybeGroupLine = false;
if(line.strip.startsWith("Group: ") || line.strip.startsWith("group: ")) {
c.group = line.strip["Group: ".length .. $]; // cut off closing paren
continue;
}
}
auto maybe = line.strip.toLower;
if(maybe.startsWith("---") || maybe.startsWith("```")) {
justSawBlank = false;
if(inCode && inCodeStyle == maybe[0 .. 3]) {
inCode = false;
inCodeStyle = null;
} else if(inCode)
goto ss; // just still inside code section
else {
inCodeStyle = maybe[0 .. 3];
inDdocSummary = false;
inCode = !inCode;
}
}
if(inCode){
justSawBlank = false;
goto ss; // sections never change while in a code example
}
if(inSynopsis && hasAnySynopsis && maybe.length == 0) {
// any empty line ends the ddoc summary
inDdocSummary = false;
// two blank lines in a row ends the synopsis
if(justSawBlank) {
inSynopsis = false;
// synopsis can also end on the presence of another
// section, so this is tracked to see how intentional
// the break looked (Phobos docs aren't written with
// a double-break in mind)
synopsisEndedOnDoubleBlank = true;
}
justSawBlank = true;
} else {
justSawBlank = false;
}
if(maybe.startsWith("params:")) {
section = "params";
inSynopsis = false;
} else if(maybe.startsWith("parameters:")) {
section = "params";
inSynopsis = false;
} else if(maybe.startsWith("returns:")) {
section = "returns";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("throws:")) {
section = "throws";
inSynopsis = false;
line = line[line.indexOf(":")+1 .. $];
} else if(maybe.startsWith("author:")) {
section = "authors";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("details:")) {
section = "details";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("authors:")) {
section = "authors";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("source:")) {
section = "source";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("history:")) {
section = "history";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("credits:")) {
section = "credits";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("version:")) {
section = "version";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("since:")) {
section = "since";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("license:")) {
section = "license";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("copyright:")) {
section = "copyright";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("see_also:")) {
section = "see_also";
inSynopsis = false;
line = line[line.indexOf(":")+1 .. $];
} else if(maybe.startsWith("diagnostics:")) {
inSynopsis = false;
section = "diagnostics";
line = line[line.indexOf(":")+1 .. $];
} else if(maybe.startsWith("examples:")) {
inSynopsis = false;
section = "examples";
line = line[line.indexOf(":")+1 .. $];
} else if(maybe.startsWith("example:")) {
inSynopsis = false;
line = line[line.indexOf(":")+1 .. $];
section = "examples"; // Phobos uses example, the standard is examples.
} else if(maybe.startsWith("standards:")) {
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
section = "standards";
} else if(maybe.startsWith("deprecated:")) {
inSynopsis = false;
section = "deprecated";
} else if(maybe.startsWith("date:")) {
inSynopsis = false;
section = "date";
} else if(maybe.startsWith("bugs:")) {
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
section = "bugs";
} else if(maybe.startsWith("macros:")) {
inSynopsis = false;
section = "macros";
currentMacroName = null;
} else if(maybe.startsWith("symbol_groups:")) {
section = "symbol_groups";
line = line[line.indexOf(":")+1 .. $];
inSynopsis = false;
} else if(maybe.startsWith("link_references:")) {
inSynopsis = false;
section = "link_references";
line = line[line.indexOf(":")+1 .. $].strip;
} else if(maybe.isDdocSection()) {
inSynopsis = false;
auto idx2 = line.indexOf(":");
auto name = line[0 .. idx2].replace("_", " ");
line = "$(H3 "~name~")\n\n" ~ line[idx2+1 .. $];
} else {
// no change to section
}
if(inSynopsis == false)
inDdocSummary = false;
ss: switch(section) {
case "symbol_groups":
// basically a copy/paste of Params but it is coincidental
// they might not always share syntax.
bool lookingForIdent = true;
bool inIdent;
bool skippingSpace;
size_t space_at;
auto lol = line.strip;
foreach(idx, ch; lol) {
import std.uni, std.ascii : isAlphaNum;
if(lookingForIdent && !inIdent) {
if(!isAlpha(ch) && ch != '_')
continue;
inIdent = true;
lookingForIdent = false;
}
if(inIdent) {
if(ch == '_' || isAlphaNum(ch) || isAlpha(ch))
continue;
else {
skippingSpace = true;
inIdent = false;
space_at = idx;
}
}
if(skippingSpace) {
if(!isWhite(ch)) {
if(ch == '=') {
// we finally hit a thingy
auto currentName = lol[0 .. space_at];
c.symbolGroups[currentName] = lol[idx + 1 .. $] ~ "\n";
c.symbolGroupsOrder ~= currentName;
currentValue = currentName in c.symbolGroups;
break ss;
} else
// we are expecting whitespace or = and hit
// neither.. this can't be ident = desc!
break;
}
}
}
if(currentValue !is null) {
*currentValue ~= line ~ "\n";
}
break;
case "params":
bool lookingForIdent = true;
bool inIdent;
bool skippingSpace;
size_t space_at;
auto lol = line.strip;
foreach(idx, ch; lol) {
import std.uni, std.ascii : isAlphaNum;
if(lookingForIdent && !inIdent) {
if(!isAlpha(ch) && ch != '_')
continue;
inIdent = true;
lookingForIdent = false;
}
if(inIdent) {
if(ch == '_' || isAlphaNum(ch) || isAlpha(ch))
continue;
else {
skippingSpace = true;
inIdent = false;
space_at = idx;
}
}
if(skippingSpace) {
if(!isWhite(ch)) {
if(ch == '=') {
// we finally hit a thingy
c.params ~= lol[0 .. space_at] ~ "=" ~ lol[idx + 1 .. $] ~ "\n";
break ss;
} else
// we are expecting whitespace or = and hit
// neither.. this can't be ident = desc!
break;
}
}
}
if(c.params.length)
c.params[$-1] ~= line ~ "\n";
break;
case "see_also":
c.see_alsos ~= line ~ "\n";
break;
case "macros":
// ignoring for now
bool lookingForIdent = true;
bool inIdent;
bool skippingSpace;
size_t space_at;
auto lol = line.strip;
foreach(idx, ch; lol) {
import std.uni, std.ascii : isAlphaNum;
if(lookingForIdent && !inIdent) {
if(!isAlpha(ch) && ch != '_')
continue;
inIdent = true;
lookingForIdent = false;
}
if(inIdent) {
if(ch == '_' || isAlphaNum(ch) || isAlpha(ch))
continue;
else {
skippingSpace = true;
inIdent = false;
space_at = idx;
}
}
if(skippingSpace) {
if(!isWhite(ch)) {
if(ch == '=') {
// we finally hit a thingy
currentMacroName = lol[0 .. space_at].strip;
auto currentMacroBody = lol[idx + 1 .. $] ~ "\n";
c.userDefinedMacros[currentMacroName] = currentMacroBody;
break ss;
} else
// we are expecting whitespace or = and hit
// neither.. this can't be ident = desc!
break;
}
} else {
if(currentMacroName.length)
c.userDefinedMacros[currentMacroName] ~= lol;
}
}
break;
case "link_references":
auto eql = line.indexOf("=");
if(eql != -1) {
string name = line[0 .. eql].strip;
string value = line[eql + 1 .. $].strip;
c.linkReferences[name] = value;
} else if(line.strip.length) {
section = null;
goto case_default;
}