root/trunk/src/tutorials/converttowiki/convertToWiki.d

Revision 18, 13.2 kB (checked in by jcc7, 3 years ago)

Added PostedBy? name.

Line 
1 /*
2     Purpose: To convert a snapshot of the dsource tutorials (http://www.dsource.org/tutorials/)
3              into wiki pages for Trac (http://trac.dsource.org/projects/tutorials/)
4     Author:  J C Calvarese
5     License: Public Domain
6 */
7
8
9 import std.stdio; /* for writefln */
10 import std.stream;
11 import std.string; /* for find */
12
13 import names;
14
15 const char[] cssFile = "style.css";
16
17 const char[] begCategoriesMarker = `<h3 class="ex_disp_h3">Categories</h3><ul>`;
18 const char[] endCategoriesMarker = `<h3 class="ex_disp_h3">Examples</h3>`;  /* stop looking for category names */
19 const char[] begTopicMarker = `<li><a href="#topic`;
20 const char[] endCatNameMarker = " (";
21 const char[] endPhase2 = `<div class="app" style="margin-left: 0.5em;">`;
22 const char[] begTopicListMarker = `<li id="topic`;
23 const char[] endTopicListMarker = `</a></li></ul>`;
24 const char[] begExampleListMarker = `<li><a href="#ex`;
25 const char[] endExampleListMarker = `</a></li>`;
26
27 const char[] begExampleCategoryName = `<h3 class="ex_disp_h3">`;
28 const char[] endExampleCategoryName = `</h3>`;
29 const char[] begExampleNumber = `<h4 id="ex`;
30 const char[] endExampleNumber = `">`;
31 //const char[] begExName = ``; /* right after the > that signifies the end of the example number */
32 const char[] endExName = `</h4>`;
33
34 const char[] begExampleDescriptionMarker = `Description</th><td>`;
35 const char[] endExampleDescriptionMarker = `</td>`;
36 const char[] begExampleDateTimeMarker = `Date/Time</th><td>`; /* time and date will follow */
37 const char[] endExampleDateTimeMarker = `</td>`;
38 const char[] begExamplePostedByMarker = `Posted by</th><td>`;
39 const char[] endExamplePostedByMarker = `</td>`;
40 const char[] begExampleBodyMarker = `<table><tbody><tr><td colspan="2">`;
41 const char[] endExampleBodyMarker = `</td></tr><tr><td colspan="2">`; //`</td></tr><tr><td colspan="2"> </td></tr></tbody></table></div><hr>`;
42 //                                   </td></tr><tr><td colspan="2"> </td></tr></tbody></table></div><hr>
43
44
45 void main(char[][] args)
46 {
47     char[] inputFile;
48     char[] outputPath;
49
50     char[] inStr; /* used to store the entire contents of the input file (at the beginning) */
51
52     char[][] wikiCategories;
53     char[][] wikiExampleNames;
54
55     char[] oldCategoryName;
56     char[] wikiCategory;
57     char[] wikiExample;
58     char[] postedBy;
59     char[] datePosted;
60     char[] oldExampleName;
61     char[] exampleDescription;
62     char[] exampleBody;
63     char[] oldDsourceExampleLink;
64
65     int begPos;
66     int midPos;
67     int endPos;
68
69     int topicBegPos;
70     int topicEndPos;
71
72 //  bit done;
73
74
75     if(args.length > 2)
76     {
77         inputFile = args[1];
78         outputPath = args[2] ~ `\`;
79     }
80     else
81         writefln("Please specify the input file and output path at the commandline.");
82
83
84     /* *** Read Entire Input File *** */
85
86     File inFile = new File(inputFile);
87     while (!inFile.eof())
88         inStr ~= inFile.readLine() ~ \r\n;   
89     inFile.close();   
90
91
92     /* *** Create Index File *** */
93
94     begPos = find(inStr, begCategoriesMarker );
95     if(begPos < 0)
96     {
97         writefln("Error: beginning of Categories section not found!");
98         assert(0);
99     }
100     inStr = inStr[begPos..$];
101
102     begPos = find(inStr, begTopicMarker);
103    
104     /* find `<li><a href="#topic` skip the number
105        and the text after ">" and before the "</a></li>" is the name */
106
107     while(begPos >= 0) /* for each category: */
108     {
109         inStr = inStr[begPos + begTopicMarker.length .. $];
110         //endPos = find(inStr, endCategoriesMarker);
111
112         begPos = find(inStr, `>`);
113         if(begPos < 0)
114         {
115             writefln("Error: Category name not found!");
116             assert(0);
117         }
118         inStr = inStr[begPos + 1 .. $];
119         endPos = find(inStr, endCategoriesMarker);
120         midPos = find(inStr, endCatNameMarker);     
121         if(midPos < 0)
122         {
123             writefln("Error: End of Category name not found!");
124             assert(0);
125         }
126         oldCategoryName = inStr[0..midPos];
127
128         wikiCategory = wikiCategoryName(oldCategoryName);
129         wikiCategories ~= wikiCategory;
130
131         debug writefln("Found category: " ~ wikiCategory);
132    
133         //mkdir(outputPath ~ `\` ~ wikiCategory);//    Make directory pathname[].
134    
135
136         begPos = find(inStr, begTopicMarker);
137         endPos = find(inStr, endCategoriesMarker);
138
139         debug writefln("begPos: %s, endPos: %s", begPos, endPos);
140
141         if(endPos < begPos) begPos = -1;
142     }
143
144     File outFile = new File(outputPath ~ "index.html", FileMode.OutNew);
145
146     outFile.writeLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
147     outFile.writeLine("<html><head><title>Index</title>");
148     outFile.writeLine(`<link rel="stylesheet" href="` ~ cssFile ~ `"\>`);
149     outFile.writeLine("</head>");
150     outFile.writeLine("<body><pre>= Tutorial Categories =<br/>");
151
152    
153     /* use wikiCategories to list items for main page... */
154     foreach(char[] wc; wikiCategories)
155       outFile.writeLine(`  * <a href="` ~ wc ~ `.html">` ~ wc ~ `</a>`);
156
157     outFile.writeLine("</pre></body></html>");
158     outFile.close();
159
160
161
162     /* *** Create Category Files *** */
163    
164     debug writefln("\n***Create Category Files ***");
165
166     while(topicBegPos >= 0) /* loop for each category */
167     {
168         wikiExampleNames.length = 0;
169        
170         topicBegPos = find(inStr, begTopicListMarker);
171         if(topicBegPos < 0)
172         {
173             writefln("Error: Example name not found!");
174             assert(0);
175         }
176         inStr = inStr[topicBegPos + 1 .. $];
177
178         begPos = find(inStr, `>`);
179         if(begPos < 0)
180         {
181             writefln("Error: Category name not found!");
182             assert(0);
183         }
184         inStr = inStr[begPos + 1 .. $];
185
186         midPos = find(inStr, `</li>`);
187         oldCategoryName = inStr[0..midPos];
188         debug writefln("Found old category name: " ~ oldCategoryName);
189         wikiCategory = wikiCategoryName(oldCategoryName);
190         debug writefln("Found new category: " ~ wikiCategory);
191         topicEndPos = find(inStr, endPhase2); //endTopicListMarker);
192
193
194         begPos = find(inStr, begExampleListMarker);
195
196         while(begPos >= 0) /* loop through the examples */
197         {
198             if(begPos < 0)
199             {
200                 writefln("Error: Example name not found!");
201                 assert(0);
202             }
203             inStr = inStr[begPos + begExampleListMarker.length .. $];
204
205             begPos = find(inStr, `>`);
206             if(begPos < 0)
207             {
208                 writefln("Error: Example name not found!");
209                 assert(0);
210             }
211             inStr = inStr[begPos + 1 .. $];
212             endPos = find(inStr, endExampleListMarker );
213             if(endPos < 0)
214             {
215                 writefln("Error: Example name ending not found!");
216                 assert(0);
217             }
218             oldExampleName = inStr[0..endPos];
219    
220             wikiExample = wikiExampleName(oldCategoryName, oldExampleName);
221             wikiExampleNames ~= wikiExample;
222    
223             begPos = find(inStr, begExampleListMarker);
224             endPos = find(inStr, endTopicListMarker); //endExampleListMarker);
225
226             if(endPos < begPos) begPos = -1;
227         }   
228
229         File outFile = new File(outputPath ~ wikiCategory ~ ".html", FileMode.OutNew);
230
231         outFile.writeLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
232         outFile.writeLine("<html><head><title>" ~ wikiCategory ~ "</title>");
233         outFile.writeLine(`<link rel="stylesheet" href="` ~ cssFile ~ `"/>`);
234         outFile.writeLine("</head>");
235
236         outFile.writeLine(`<body><pre>= ` ~ oldCategoryName ~ ` =<br/>`);
237
238         //outFile.writeLine(`<hr id="` ~ wikiCategory ~ `" />= ` ~ oldCategoryName ~ " =<br/>");
239         foreach(char[] we; wikiExampleNames)
240           outFile.writeLine(`  * <a href="` ~ we ~ `.html">` ~ we ~ `</a>`);
241    
242         outFile.writeLine("</pre></body></html>");
243         outFile.close();
244
245         topicBegPos = find(inStr, begTopicListMarker);
246         topicEndPos = find(inStr, endPhase2); //endTopicListMarker);
247    
248         if(topicEndPos < topicBegPos) topicBegPos = -1;
249     }
250
251
252
253     debug writefln("\n***Create Example Files ***");
254
255     /* *** Create Example Files *** */
256
257     wikiCategory = "";
258
259     begPos = 0;
260     while(begPos != -1) //stillMoreExamples) /* for each example: */
261     {
262
263         /* check to see if there's a new category name before the next example */
264
265         begPos = find(inStr, begExampleCategoryName);
266         midPos = find(inStr, begExampleNumber);
267        
268         if((begPos < midPos && begPos != -1) || wikiCategory == "")
269         {
270             /* The next example is in a new category. */
271
272             inStr = inStr[begPos + begExampleCategoryName.length .. $];
273             endPos = find(inStr, endExampleCategoryName);
274             if(endPos < 0)
275             {
276                 writefln("Error: Category name end not found!");
277                 assert(0);
278             }       
279
280             oldCategoryName = inStr[0 .. endPos];
281             wikiCategory = wikiCategoryName(oldCategoryName);
282             inStr = inStr[endPos .. $];
283
284             begPos = find(inStr, begExampleCategoryName);
285             midPos = find(inStr, begExampleNumber);
286             if((begPos < midPos && begPos != -1) || wikiCategory == "")
287             {
288                 /* The last category didn't actually contain any examples. */
289    
290                 inStr = inStr[begPos + begExampleCategoryName.length .. $];
291                 endPos = find(inStr, endExampleCategoryName);
292                 if(endPos < 0)
293                 {
294                     writefln("Error: Category name end not found!");
295                     assert(0);
296                 }       
297                 oldCategoryName = inStr[0 .. endPos];
298                 wikiCategory = wikiCategoryName(oldCategoryName);               
299             }
300             debug writefln("New category: %s", wikiCategory);
301         }
302         begPos = find(inStr, begExampleNumber);
303         if(begPos < 0)
304         {
305             writefln("Error: Example number not found!");
306             assert(0);
307         }       
308        
309
310
311
312         /* get wikiExample, exampleBody, postedDate, postedBy, etc. */
313
314
315         /* ex # should be the number in the example link (should test it to make sure) */
316
317
318         begPos = find(inStr, begExampleNumber);
319         if(begPos < 0)
320         {
321             writefln("Error: Example name not found!");
322             assert(0);
323         }       
324         inStr = inStr[begPos + begExampleNumber.length .. $];
325
326         endPos = find(inStr, endExampleNumber);
327         if(endPos < 0)
328         {
329             writefln("Error: Example number not found!");
330             assert(0);
331         }       
332         oldDsourceExampleLink = "http://www.dsource.org/tutorials/index.php?show_example=" ~ inStr[0..endPos];
333
334         inStr = inStr[endPos + endExampleNumber.length .. $];
335
336         endPos = find(inStr, endExName);
337         if(endPos < 0)
338         {
339             writefln("Error: Example name end not found!");
340             assert(0);
341         }       
342         oldExampleName = inStr[0..endPos];
343         wikiExample = wikiExampleName(oldCategoryName, oldExampleName);
344         inStr = inStr[endPos + endExName.length .. $];
345
346
347
348         begPos = find(inStr, begExampleDescriptionMarker);
349         if(begPos < 0)
350         {
351             writefln("Error: Example description not found!");
352             assert(0);
353         }       
354         endPos = find(inStr, endExampleDescriptionMarker);
355         if(endPos < 0)
356         {
357             writefln("Error: Example description end not found!");
358             assert(0);
359         }       
360         exampleDescription = inStr[begPos + begExampleDescriptionMarker.length .. endPos];
361         inStr = inStr[endPos + endExampleDescriptionMarker.length .. $];
362
363
364
365         begPos = find(inStr, begExampleDateTimeMarker);
366         if(begPos < 0)
367         {
368             writefln("Error: Example date/time not found!");
369             assert(0);
370         }       
371         endPos = find(inStr, endExampleDateTimeMarker);
372         if(endPos < 0)
373         {
374             writefln("Error: Example date/time end not found!");
375             assert(0);
376         }       
377         datePosted = inStr[begPos + begExampleDateTimeMarker.length .. endPos];
378         inStr = inStr[endPos + endExampleDateTimeMarker.length .. $];
379
380
381
382         begPos = find(inStr, begExamplePostedByMarker);
383         if(begPos < 0)
384         {
385             writefln("Error: Example description not found!");
386             assert(0);
387         }       
388         endPos = find(inStr, endExamplePostedByMarker);
389         if(endPos < 0)
390         {
391             writefln("Error: Example description end not found!");
392             assert(0);
393         }       
394         postedBy = inStr[begPos + begExamplePostedByMarker.length .. endPos];
395         inStr = inStr[endPos + endExamplePostedByMarker.length .. $];
396
397
398
399
400
401
402
403         begPos = find(inStr, begExampleBodyMarker);
404         if(begPos < 0)
405         {
406             writefln("Error: Example body not found!");
407             assert(0);
408         }       
409         endPos = find(inStr, endExampleBodyMarker);
410         if(endPos < 0)
411         {
412             writefln("Error: Example body end not found!");
413             assert(0);
414         }       
415         exampleBody = inStr[begPos + begExampleBodyMarker.length .. endPos];
416         inStr = inStr[endPos + endExampleBodyMarker.length .. $];
417
418
419
420         debug writefln("Writing example file: %s", wikiExample);
421
422         File outFile = new File(outputPath ~ wikiExample ~ ".html", FileMode.OutNew);
423
424         outFile.writeLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
425         outFile.writeLine("<html><head><title>" ~ wikiExample ~ "</title>");
426         outFile.writeLine(`<link rel="stylesheet" href="` ~ cssFile ~ `"/>`);
427         outFile.writeLine("</head>");
428
429         outFile.writeLine(`<body><pre>= ` ~ oldExampleName ~ ` =<br/>`);
430
431         //outFile.writeLine(`<hr id="` ~ wikiExample ~ `"/>= ` ~ oldExampleName ~ " =<br/>");
432         outFile.writeLine("''Part of'' " ~ wikiCategory ~ "<br/>");
433
434         outFile.writeLine("== Description ==<br/>");
435         outFile.writeLine(exampleDescription ~ `<br/>`);
436
437         outFile.writeLine("== Example ==<br/>");
438         outFile.writeLine("{{{");
439         outFile.writeLine("#!d</pre>");
440
441         /* output example body (might need some manual adjustments in "rawhtml" was used */
442
443         outFile.writeLine(exampleBody);
444
445         outFile.writeLine("}}}");
446
447
448         outFile.writeLine("<pre>== Source ==<br/>");           
449         outFile.writeLine("|| Link || " ~ oldDsourceExampleLink ~ " ||");
450         outFile.writeLine("|| Posted by || " ~ postedBy ~ " ||");
451         outFile.writeLine("|| Date/Time || " ~ datePosted ~ " ||");
452
453
454
455         outFile.writeLine("</pre></body></html>");
456         outFile.close();
457
458         begPos = find(inStr, begExampleNumber);
459        
460         //if(begPos == 0 && begPos > endPos) done = true;
461     }
462    
463 }
Note: See TracBrowser for help on using the browser.