comparison trunk/src/cmd/DDoc.d @ 761:307905dadf5d

DDoc code sections are highlighted now.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sat, 16 Feb 2008 00:12:13 +0100
parents c7a5499faa77
children 4579e8505d5e
comparison
equal deleted inserted replaced
760:ea9e8b141742 761:307905dadf5d
2 Author: Aziz Köksal 2 Author: Aziz Köksal
3 License: GPL3 3 License: GPL3
4 +/ 4 +/
5 module cmd.DDoc; 5 module cmd.DDoc;
6 6
7 import cmd.Generate;
7 import dil.doc.Parser; 8 import dil.doc.Parser;
8 import dil.doc.Macro; 9 import dil.doc.Macro;
9 import dil.doc.Doc; 10 import dil.doc.Doc;
10 import dil.ast.Node; 11 import dil.ast.Node;
11 import dil.ast.Declarations, 12 import dil.ast.Declarations,
45 } 46 }
46 47
47 // foreach (k, v; mtable.table) 48 // foreach (k, v; mtable.table)
48 // Stdout(k)("=")(v.text); 49 // Stdout(k)("=")(v.text);
49 50
51 auto tokenHL = new TokenHighlighter(infoMan); // For DDoc code sections.
52
53 // Process D files.
50 foreach (filePath; filePaths) 54 foreach (filePath; filePaths)
51 { 55 {
52 auto mod = new Module(filePath, infoMan); 56 auto mod = new Module(filePath, infoMan);
53 // Parse the file. 57 // Parse the file.
54 mod.parse(); 58 mod.parse();
68 { 72 {
69 Stdout.formatln("{} > {}", mod.filePath, dest); 73 Stdout.formatln("{} > {}", mod.filePath, dest);
70 infoMan2 = new InfoManager(); 74 infoMan2 = new InfoManager();
71 } 75 }
72 76
73 writeDocFile(dest.toString(), mod, mtable, incUndoc, infoMan2); 77 writeDocFile(dest.toString(), mod, mtable, incUndoc, tokenHL, infoMan2);
74 78
75 if (infoMan2) 79 if (infoMan2)
76 infoMan ~= infoMan2.info; 80 infoMan ~= infoMan2.info;
77 } 81 }
78 } 82 }
79 83
80 void writeDocFile(string dest, Module mod, MacroTable mtable, bool incUndoc, 84 void writeDocFile(string dest, Module mod, MacroTable mtable, bool incUndoc,
81 InfoManager infoMan) 85 TokenHighlighter tokenHL, InfoManager infoMan)
82 { 86 {
83 // Create a macro environment for this module. 87 // Create a macro environment for this module.
84 mtable = new MacroTable(mtable); 88 mtable = new MacroTable(mtable);
85 // Define runtime macros. 89 // Define runtime macros.
86 mtable.insert("TITLE", mod.getFQN()); 90 mtable.insert("TITLE", mod.getFQN());
91 char* str = ctime(&time_val); 95 char* str = ctime(&time_val);
92 char[] time_str = str[0 .. strlen(str)-1]; // -1 removes trailing '\n'. 96 char[] time_str = str[0 .. strlen(str)-1]; // -1 removes trailing '\n'.
93 mtable.insert("DATETIME", time_str.dup); 97 mtable.insert("DATETIME", time_str.dup);
94 mtable.insert("YEAR", time_str[20..24].dup); 98 mtable.insert("YEAR", time_str[20..24].dup);
95 99
96 auto doc = new DDocEmitter(mtable, incUndoc); 100 auto doc = new DDocEmitter(mtable, incUndoc, mod, tokenHL);
97 doc.emit(mod); 101 doc.emit();
98 // Set BODY macro to the text produced by the DDocEmitter. 102 // Set BODY macro to the text produced by the DDocEmitter.
99 mtable.insert("BODY", doc.text); 103 mtable.insert("BODY", doc.text);
100 // Do the macro expansion pass. 104 // Do the macro expansion pass.
101 auto fileText = MacroExpander.expand(mtable, "$(DDOC)", mod.filePath, infoMan); 105 auto fileText = MacroExpander.expand(mtable, "$(DDOC)", mod.filePath, infoMan);
102 // fileText ~= "\n<pre>\n" ~ doc.text ~ "\n</pre>"; 106 // fileText ~= "\n<pre>\n" ~ doc.text ~ "\n</pre>";
114 class DDocEmitter : DefaultVisitor 118 class DDocEmitter : DefaultVisitor
115 { 119 {
116 char[] text; 120 char[] text;
117 bool includeUndocumented; 121 bool includeUndocumented;
118 MacroTable mtable; 122 MacroTable mtable;
119 123 Module modul;
120 this(MacroTable mtable, bool includeUndocumented) 124 TokenHighlighter tokenHL;
125
126 this(MacroTable mtable, bool includeUndocumented, Module modul,
127 TokenHighlighter tokenHL)
121 { 128 {
122 this.mtable = mtable; 129 this.mtable = mtable;
123 this.includeUndocumented = includeUndocumented; 130 this.includeUndocumented = includeUndocumented;
131 this.modul = modul;
132 this.tokenHL = tokenHL;
124 } 133 }
125 134
126 /// Entry method. 135 /// Entry method.
127 char[] emit(Module mod) 136 char[] emit()
128 { 137 {
129 if (auto d = mod.moduleDecl) 138 if (auto d = modul.moduleDecl)
130 { 139 {
131 if (ddoc(d)) 140 if (ddoc(d))
132 { 141 {
133 if (auto copyright = cmnt.takeCopyright()) 142 if (auto copyright = cmnt.takeCopyright())
134 mtable.insert(new Macro("COPYRIGHT", copyright.text)); 143 mtable.insert(new Macro("COPYRIGHT", copyright.text));
135 DESC({ writeComment(); }); 144 DESC({ writeComment(); });
136 } 145 }
137 } 146 }
138 MEMBERS("MODULE", { visitD(mod.root); }); 147 MEMBERS("MODULE", { visitD(modul.root); });
139 write(\n); 148 write(\n);
140 return text; 149 return text;
141 } 150 }
142 151
143 char[] textSpan(Token* left, Token* right) 152 char[] textSpan(Token* left, Token* right)
334 auto codeBegin = p; 343 auto codeBegin = p;
335 p--; 344 p--;
336 while (++p < end) 345 while (++p < end)
337 if (p+2 < end && *p == '-' && p[1] == '-' && p[2] == '-') 346 if (p+2 < end && *p == '-' && p[1] == '-' && p[2] == '-')
338 break; 347 break;
339 result ~= "$(D_CODE " ~ scanCodeSection(makeString(codeBegin, p)) ~ ")"; 348 auto codeText = makeString(codeBegin, p);
349 result ~= tokenHL.highlight(codeText, modul.filePath);
340 while (p < end && *p == '-') 350 while (p < end && *p == '-')
341 p++; 351 p++;
342 continue; 352 continue;
343 } 353 }
344 //goto default; 354 //goto default;
346 result ~= *p; 356 result ~= *p;
347 } 357 }
348 p++; 358 p++;
349 } 359 }
350 return result; 360 return result;
351 }
352
353 char[] scanCodeSection(char[] text)
354 {
355 return text;
356 } 361 }
357 362
358 /// Escapes '<', '>' and '&' with named HTML entities. 363 /// Escapes '<', '>' and '&' with named HTML entities.
359 char[] escape(char[] text) 364 char[] escape(char[] text)
360 { 365 {
696 return d; 701 return d;
697 char[] type = "auto"; 702 char[] type = "auto";
698 if (d.typeNode) 703 if (d.typeNode)
699 type = textSpan(d.typeNode.baseType.begin, d.typeNode.end); 704 type = textSpan(d.typeNode.baseType.begin, d.typeNode.end);
700 foreach (name; d.names) 705 foreach (name; d.names)
701 {
702 DECL({ write(escape(type), " "); SYMBOL(name.str); }); 706 DECL({ write(escape(type), " "); SYMBOL(name.str); });
703 DESC({ writeComment(); }); 707 DESC({ writeComment(); });
704 }
705 return d; 708 return d;
706 } 709 }
707 710
708 D visit(InvariantDeclaration d) 711 D visit(InvariantDeclaration d)
709 { 712 {