-
Notifications
You must be signed in to change notification settings - Fork 20
/
ddoc.d
482 lines (428 loc) · 11.2 KB
/
ddoc.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
/**
DietDoc/DDOC support routines
Copyright: © 2012 RejectedSoftware e.K.
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
Authors: Sönke Ludwig
*/
module ddox.ddoc;
import vibe.core.log;
import vibe.utils.string;
import std.algorithm;
import std.array;
import std.conv;
/**
Takes a DDOC string and outputs formatted HTML.
The hlevel parameter specifies the header level used for section names (<h2> by default).
By specifying a display_section callback it is also possible to output only certain sections.
*/
string formatDdocComment(string ddoc_, int hlevel = 2, bool delegate(string) display_section = null)
{
return formatDdocComment(new BareContext(ddoc_), hlevel, display_section);
}
/// ditto
string formatDdocComment(DdocContext context, int hlevel = 2, bool delegate(string) display_section = null)
{
auto dst = appender!string();
filterDdocComment(dst, context, hlevel, display_section);
return dst.data;
}
/// ditto
void filterDdocComment(R)(ref R dst, DdocContext context, int hlevel = 2, bool delegate(string) display_section = null)
{
auto lines = splitLines(context.docText);
if( !lines.length ) return;
string[string] macros;
parseMacros(macros, s_standardMacros);
parseMacros(macros, s_defaultMacros);
parseMacros(macros, context.defaultMacroDefinitions);
int getLineType(int i)
{
auto ln = strip(lines[i]);
if( ln.length == 0 ) return BLANK;
else if( ln.length >= 3 && ln.allOf("-") ) return CODE;
else if( ln.countUntil(':') > 0 && !ln[0 .. ln.countUntil(':')].anyOf(" \t") ) return SECTION;
return TEXT;
}
int skipCodeBlock(int start)
{
do {
start++;
} while(start < lines.length && getLineType(start) != CODE);
return start+1;
}
int skipSection(int start)
{
while(start < lines.length && getLineType(start) != SECTION){
if( getLineType(start) == CODE )
start = skipCodeBlock(start);
else start++;
}
return start;
}
int skipBlock(int start)
{
do {
start++;
} while(start < lines.length && getLineType(start) == TEXT);
return start;
}
int i = 0;
Section[] sections;
// special case short description on the first line
while( i < lines.length && getLineType(i) == BLANK ) i++;
if( i < lines.length && getLineType(i) == TEXT ){
auto j = skipBlock(i);
sections ~= Section("$Short", lines[i .. j].join("\n"));
i = j;
}
// first section is implicitly the long description
{
auto j = skipSection(i);
if( j > i ) sections ~= Section("$Long", lines[i .. j]);
i = j;
}
// parse all other sections
while( i < lines.length ){
assert(getLineType(i) == SECTION);
auto j = skipSection(i+1);
auto pidx = lines[i].countUntil(':');
auto sect = strip(lines[i][0 .. pidx]);
lines[i] = strip(lines[i][pidx+1 .. $]);
if( lines[i].empty ) i++;
if( sect == "Macros" ) parseMacros(macros, lines[i .. j]);
else sections ~= Section(sect, lines[i .. j]);
i = j;
}
foreach( s; sections ){
if( display_section && !display_section(s.name) ) continue;
if( s.name == "$Short") renderTextLine(dst, s.lines[0], context, macros);
else parseSection(dst, s.name, s.lines, context, hlevel, macros);
}
}
/**
Sets a set of macros that will be available to all calls to formatDdocComment.
*/
void setDefaultDdocMacroFile(string filename)
{
import vibe.core.file;
import vibe.stream.stream;
auto text = readAllUtf8(openFile(filename));
s_defaultMacros = splitLines(text);
}
/**
Provides context information about the documented element.
*/
interface DdocContext {
/// The DDOC text
@property string docText();
/// A line array with macro definitions
@property string[] defaultMacroDefinitions();
/// Looks up a symbol in the scope of the documented element and returns a link to it.
string lookupScopeSymbolLink(string name);
}
private class BareContext : DdocContext {
private string m_ddoc;
this(string ddoc)
{
m_ddoc = ddoc;
}
@property string docText() { return m_ddoc; }
@property string[] defaultMacroDefinitions() { return null; }
string lookupScopeSymbolLink(string name) { return null; }
}
private enum {
BLANK,
TEXT,
CODE,
SECTION
}
private struct Section {
string name;
string[] lines;
this(string name, string[] lines...)
{
this.name = name;
this.lines = lines;
}
}
private {
string[] s_defaultMacros;
}
/// private
private void parseSection(R)(ref R dst, string sect, string[] lines, DdocContext context, int hlevel, string[string] macros)
{
void putHeader(string hdr){
dst.put("<section>");
if( sect.length > 0 && sect[0] != '$' ){
dst.put("<h"~to!string(hlevel)~">");
dst.put(hdr);
dst.put("</h"~to!string(hlevel)~">\n");
}
}
void putFooter(){
dst.put("</section>\n");
}
int getLineType(int i)
{
auto ln = strip(lines[i]);
if( ln.length == 0 ) return BLANK;
else if( ln.length >= 3 &&ln.allOf("-") ) return CODE;
else if( ln.countUntil(':') > 0 && !ln[0 .. ln.countUntil(':')].anyOf(" \t") ) return SECTION;
return TEXT;
}
int skipBlock(int start)
{
do {
start++;
} while(start < lines.length && getLineType(start) == TEXT);
return start;
}
int skipCodeBlock(int start)
{
do {
start++;
} while(start < lines.length && getLineType(start) != CODE);
return start;
}
switch( sect ){
default:
putHeader(sect);
int i = 0;
while( i < lines.length ){
int lntype = getLineType(i);
if( lntype == BLANK ){ i++; continue; }
switch( lntype ){
default: assert(false, "Unexpected line type "~to!string(lntype)~": "~lines[i]);
case SECTION:
case TEXT:
dst.put("<p>");
auto j = skipBlock(i);
renderTextLine(dst, lines[i .. j].join("\n"), context, macros);
dst.put("</p>\n");
i = j;
break;
case CODE:
dst.put("<pre class=\"code prettyprint\">");
auto j = skipCodeBlock(i);
auto base_indent = baseIndent(lines[i+1 .. j]);
foreach( ln; lines[i+1 .. j] ){
dst.put(ln.unindent(base_indent));
dst.put("\n");
}
dst.put("</pre>\n");
i = j+1;
break;
}
}
putFooter();
break;
case "Params":
putHeader("Parameters");
dst.put("<table><col class=\"caption\"><tr><th>Parameter name</th><th>Description</th></tr>\n");
bool in_dt = false;
foreach( ln; lines ){
auto eidx = ln.countUntil("=");
if( eidx < 0 ){
if( in_dt ){
dst.put(' ');
dst.put(ln.strip());
} else logWarn("Out of place text in param section: %s", ln.strip());
} else {
auto pname = ln[0 .. eidx].strip();
auto pdesc = ln[eidx+1 .. $].strip();
if( in_dt ) dst.put("</td></tr>\n");
dst.put("<tr><td>");
dst.put(pname);
dst.put("</td><td>\n");
dst.put(pdesc);
in_dt = true;
}
}
if( in_dt ) dst.put("</td>\n");
dst.put("</tr></table>\n");
putFooter();
break;
}
}
/// private
private void renderTextLine(R)(ref R dst, string line, DdocContext context, string[string] macros, string[] params = null)
{
while( line.length > 0 ){
switch( line[0] ){
default:
dst.put(line[0]);
line = line[1 .. $];
break;
case '_':
line = line[1 .. $];
auto ident = skipIdent(line);
if( ident.length ) dst.put(ident);
else dst.put('_');
break;
case 'a': .. case 'z':
case 'A': .. case 'Z':
assert(line[0] >= 'a' && line[0] <= 'z' || line[0] >= 'A' && line[0] <= 'Z');
auto ident = skipIdent(line);
auto link = context.lookupScopeSymbolLink(ident);
if( link.length ){
dst.put("<a href=\"");
dst.put(link);
dst.put("><code class=\"prettyprint lang-d\">");
dst.put(ident);
dst.put("</code></a>");
} else dst.put(ident);
break;
case '$':
renderMacro(dst, line, context, macros, params);
break;
}
}
}
/// private
private void renderMacro(R)(ref R dst, ref string line, DdocContext context, string[string] macros, string[] params = null)
{
line = line[1 .. $];
if( line.length < 1) return;
if( line[0] >= '0' && line[0] <= '9' ){
int pidx = line[0]-'0';
if( pidx < params.length )
dst.put(params[pidx]);
line = line[1 .. $];
} else if( line[0] == '+' ){
if( params.length ){
auto idx = params[0].countUntil(',');
if( idx >= 0 ) dst.put(params[0][idx+1 .. $]);
}
line = line[1 .. $];
} else if( line[0] == '(' ){
line = line[1 .. $];
int l = 1;
size_t cidx = 0;
for( cidx = 0; cidx < line.length && l > 0; cidx++ ){
if( line[cidx] == '(' ) l++;
else if( line[cidx] == ')' ) l--;
}
if( l > 0 ){
logDebug("Unmatched parenthesis in DDOC comment: %s", line[0 .. cidx]);
return;
}
if( cidx < 1 ){
logDebug("Empty macro parens.");
return;
}
auto mnameidx = line[0 .. cidx-1].countUntilAny(" \t\r\n");
if( mnameidx < 0 ) mnameidx = cidx-1;
if( mnameidx == 0 ){
logDebug("Macro call in DDOC comment is missing macro name.");
return;
}
auto mname = line[0 .. mnameidx];
string[] args;
if( mnameidx+1 < cidx ){
auto rawargs = splitParams(line[mnameidx+1 .. cidx-1]);
foreach( arg; rawargs ){
auto argtext = appender!string();
renderTextLine(argtext, arg, context, macros, params);
args ~= argtext.data();
}
}
args = join(args) ~ args;
logTrace("PARAMS for %s: %s", mname, args);
line = line[cidx .. $];
if( auto pm = mname in macros ){
logTrace("MACRO %s: %s", mname, *pm);
renderTextLine(dst, *pm, context, macros, args);
} else {
logTrace("Macro '%s' not found.", mname);
if( args.length ) dst.put(args[0]);
}
}
}
private string[] splitParams(string ln)
{
string[] ret;
size_t i = 0, start = 0;
while(i < ln.length){
if( ln[i] == ',' ){
ret ~= ln[start .. i];
start = ++i;
} else if( ln[i] == '(' ){
i++;
int l = 1;
for( ; i < ln.length && l > 0; i++ ){
if( ln[i] == '(' ) l++;
else if( ln[i] == ')' ) l--;
}
} else i++;
}
if( i > start ) ret ~= ln[start .. i];
return ret;
}
private string skipWhitespace(ref string ln)
{
string ret = ln;
while( ln.length > 0 ){
if( ln[0] == ' ' || ln[0] == '\t' )
break;
ln = ln[1 .. $];
}
return ret[0 .. ret.length - ln.length];
}
private string skipIdent(ref string str)
{
size_t i = 0;
while( i < str.length ){
if( str[i] != '_' && (str[i] < 'a' || str[i] > 'z') && (str[i] < 'A' || str[i] > 'Z') && (str[i] < '0' || str[i] > '9') )
break;
i++;
}
auto ret = str[0 .. i];
str = str[i .. $];
return ret;
}
private void parseMacros(ref string[string] macros, in string[] lines)
{
foreach( ln; lines ){
auto pidx = ln.countUntil('=');
if( pidx > 0 ){
string name = strip(ln[0 .. pidx]);
string value = strip(ln[pidx+1 .. $]);
macros[name] = value;
}
}
}
private int baseIndent(string[] lines)
{
if( lines.length == 0 ) return 0;
int ret = int.max;
foreach( ln; lines ){
int i = 0;
while( i < ln.length && (ln[i] == ' ' || ln[i] == '\t') )
i++;
if( i < ln.length ) ret = min(ret, i);
}
return ret;
}
private string unindent(string ln, int amount)
{
while( amount > 0 && ln.length > 0 && (ln[0] == ' ' || ln[0] == '\t') )
ln = ln[1 .. $], amount--;
return ln;
}
private immutable s_standardMacros = [
"P = <p>$0</p>",
"DL = <dl>$0</dl>",
"DT = <dt>$0</dt>",
"DD = <dd>$0</dd>",
"TABLE = <table>$0</table>",
"TR = <tr>$0</tr>",
"TH = <th>$0</th>",
"TD = <td>$0</td>",
"OL = <ol>$0</ol>",
"UL = <ul>$0</ul>",
"LI = <li>$0</li>",
"LINK = <a href=\"$0\">$0</a>",
"LINK2 = <a href=\"$1\">$+</a>",
"LPAREN= (",
"RPAREN= )"
];