Changeset 6

Show
Ignore:
Timestamp:
11/05/05 23:26:30 (3 years ago)
Author:
jcc7
Message:

Made more progress in developing convertToWiki and added some files.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/tutorials/converttowiki/convertToWiki.d

    r5 r6  
    11 
    22/* 
    3     Purpose: To convert a snapshot of the dsource tutorials into wiki pages for Trac 
     3    Purpose: To convert a snapshot of the dsource tutorials (http://www.dsource.org/tutorials/)  
     4             into wiki pages for Trac (http://trac.dsource.org/projects/tutorials/) 
    45    Author:  J C Calvarese 
    56    License: Public Domain 
    67*/ 
    78 
    8 import std.stdio; 
     9 
     10import std.stdio; /* for writefln */ 
    911import std.stream; 
    10 import std.string; 
    11  
    12  
    13  
    14 const char[] inputFile = "dsource_tutorials_snapshot.html"; 
    15  
    16 const char[] outputPath = r"categories\"; 
    17 const char[] cssFile = r"style.css"; 
    18  
    19 void main() 
     12import std.string; /* for find */ 
     13 
     14import names; 
     15 
     16const char[] cssFile = "style.css"; 
     17 
     18const char[] begCategoriesMarker = `<h3 class="ex_disp_h3">Categories</h3><ul>`; 
     19const char[] endCategoriesMarker = `<h3 class="ex_disp_h3">Examples</h3>`;  /* stop looking for category names */ 
     20const char[] begTopicMarker = `<li><a href="#topic`; 
     21const char[] endCatNameMarker = "</a></li>"; 
     22 
     23 
     24void main(char[][] args) 
    2025{ 
     26    char[] inputFile; 
     27    char[] outputPath; 
     28 
     29    char[] inStr; /* used to store the entire contents of the input file (at the beginning) */ 
     30 
    2131    char[][] wikiCategories; 
    2232    char[][] wikiExampleNames; 
     
    2434    char[] oldCategoryName; 
    2535    char[] wikiCategory; 
    26     char[[ wikiExample; 
     36    char[] wikiExample; 
    2737    char[] postedBy; 
    2838    char[] datePosted; 
     
    3242    char[] oldDsourceExampleLink; 
    3343 
     44    int begPos; 
     45    int midPos; 
     46    int endPos; 
     47 
     48    bit done; 
     49 
     50 
     51    if(args.length > 2) 
     52    { 
     53        inputPath = args[1]; 
     54        outputPath = args[2] ~ `\`; 
     55    } 
     56    else 
     57        writefln("Please specify the input file and output path at the commandline."); 
     58 
     59 
    3460    File inFile = new File(inputFile); 
    35  
    36     /* for each category: */ 
    37  
    38     /* find `<h3 class="ex_disp_h3">Categories</h3><ul>` */ 
    39  
     61    while (!inFile.eof()) 
     62        inStr ~= inFile.readLine();     
     63    f.close();     
     64 
     65 
     66    begPos = find(inStr, begCategoriesMarker ); 
     67    if(begPos==0) writefln("Error: beginning of Categories section not found!"); 
     68    inStr = inStr[begPos..$]; 
     69 
     70    begPos = find(inStr, begTopicMarker); 
     71     
     72    /* find `<li><a href="#topic` skip the number  
     73       and the text after ">" and before the "</a></li>" is the name */ 
     74     
     75    while(begPos) /* for each category: */ 
    4076    { 
    41         /* get wiki category name */ 
    42  
    43         /* find `<li><a href="#topic` skip the number  
    44            and the text after ">" and before the "</a></li>" is the name */ 
    45  
    46         /* stop looking for category names when you find `<h3 class="ex_disp_h3">Examples</h3>` */ 
    47  
     77        inStr = inStr[begPos + begTopicMarker.length .. $]; 
     78        endPos = find(inStr, endCategoriesMarker); 
     79 
     80        begPos = find(inStr, `>`); 
     81        if(begPos==0) writefln("Error: Category name not found!"); 
     82        inStr = inStr[begPos + 1 .. $]; 
     83        midPos = find(inStr, endCatNameMarker); 
     84        oldCategoryName = inStr[0..midPos];  
     85 
     86        wikiCategory = wikiCategoryName(oldCategoryName); 
    4887        wikiCategories ~= wikiCategory; 
    4988 
     
    5796        outFile.writeLine(`<link rel="stylesheet" href="` ~ cssfile ~ `"/>`); 
    5897        outFile.writeLine("</head>"); 
    59          
    6098 
    6199        outFile.writeLine(`<body><pre><hr/>= ` ~ oldCategoryName ~ ` =<br/><a href=#"examples"<br/>`); 
    62      
    63         /* Create Category Path */ 
    64      
    65          
    66         wikiExampleNames.length = 0; 
    67          
    68         // for each example: 
    69         while(stillMoreExamples) 
    70         { 
    71  
    72  
    73             /* get wikiExample, exampleBody, postedDate, postedBy, etc. */ 
    74  
    75             // <div class="app" style="margin-left: 0.5em;"><h3 class="ex_disp_h3">Fundamentals</h3><h4 id="ex14">It Compiles</h4><div class="axial"><table class="gray" border="0" cellpadding="3" cellspacing="2">        <tbody><tr> 
    76             //      <th style="color: rgb(0, 0, 0); background-color: rgb(239, 239, 239); background-image: none;">Description</th><td>It doesn't actually do anything, but it compiles and runs.</td> 
    77             //      </tr> 
    78             //      <tr> 
    79             //      <th style="color: rgb(0, 0, 0); background-color: rgb(239, 239, 239); background-image: none;">Date/Time</th><td>Sat May 8, 2004 4:17 pm</td> 
    80             //      </tr> 
    81             //      <tr> 
    82             //      <th style="color: rgb(0, 0, 0); background-color: rgb(239, 239, 239); background-image: none;">Posted by</th><td>jcc7</td> 
    83  
    84             //      </tr></tbody></table><table><tbody><tr><td colspan="2"><pre><code class="example"><span class="keyword">void</span> main() 
    85             //{ 
    86             //}</code></pre> For an explanation of what this means, visit <a href="http://www.wikiservice.at/d/wiki.cgi?D__Tutorial/StartingWithD">Wiki4D.</a></td></tr><tr><td colspan="2"> </td></tr></tbody></table></div><hr><h4 id="ex26">Do Something</h4><div class="axial"><table class="gray" border="0" cellpadding="3" cellspacing="2">      <tbody><tr> 
    87             //                                                                                                                                                                                                                   ^-- next example                                
    88  
    89  
    90             // find category name (again?) a little after `<li id="topic` and before `</li>`. 
    91  
    92  
    93             // find `<h4 id="ex` 
    94             // ex # should be the number in the example link (should test it to make sure) 
    95  
    96             // find `Description</th><td>` to get exampleDescription (which is before `</td>`) 
    97  
    98  
    99             // find `Date/Time</th><td>` 
    100             // The time and date will follow 
    101  
    102  
    103             oldDsourceExampleLink = "http://www.dsource.org/tutorials/index.php?show_example=" ~ assert(0); 
    104  
    105             // exampleBody starts after `<table><tbody><tr><td colspan="2">` 
    106             // ends before `</td></tr><tr><td colspan="2"> </td></tr></tbody></table></div><hr>` 
    107  
    108  
    109             wikiExampleNames ~= wikiExample 
    110  
    111             outFile.writeLine(`<hr id="` ~ wikiExample ~ `"/>= ` ~ oldExampleName ~ " =<br/>"); 
    112             outFile.writeLine("''Part of'' " ~ wikiCategory ~ "<br/>"); 
    113      
    114             outFile.writeLine("== Description ==<br/>"); 
    115             outFile.writeLine(exampleDescription); 
    116      
    117             outFile.writeLine("== Example ==<br/>"); 
    118             outFile.writeLine("{{{"); 
    119             outFile.writeLine("#!d"); 
    120  
    121             //outFile.writeLine("<code>"); 
    122  
    123             /* output example body */ 
    124  
    125             outFile.writeLine(exampleBody); 
    126  
    127             //outFile.writeLine("</code>"); 
    128  
    129             outFile.writeLine("== Source ==<br/>");          
    130             outFile.writeLine("|| Link || " ~ oldDsourceExampleLink ~ " ||"); 
    131             outFile.writeLine("|| Posted by || " ~ postedBy ~ " ||"); 
    132             outFile.writeLine("|| Date/Time || " ~ datePosted ~ " ||"); 
    133  
    134             outFile.writeLine("}}}"); 
    135         } 
    136  
    137         outFile.writeLine(`<hr id="examples"` ~ wikiCategory ~ `" />= ` ~ oldCategoryName ~ " =<br/><br/>"); 
     100 
     101 
     102        outFile.writeLine(`<hr id="` ~ wikiCategory ~ `" />= ` ~ oldCategoryName ~ " =<br/><br/>"); 
    138103        foreach(char[] we; wikiExamples) 
    139104          outFile.writeLine(`  * <#"` ~ we ~ `">` & we & `</a><br/>`); 
    140      
     105 
    141106        outFile.writeLine("</pre></body></html>"); 
    142107        outFile.close(); 
    143108 
     109        begPos = find(inStr, begTopicMarker); 
     110 
    144111    } 
     112 
     113 
     114 
     115    /* Create Category File */ 
     116 
     117 
     118    wikiExampleNames.length = 0; 
     119 
     120 
     121    while(stillMoreExamples) /* for each example: */ 
     122    { 
     123 
     124 
     125        /* get wikiExample, exampleBody, postedDate, postedBy, etc. */ 
     126 
     127        // <div class="app" style="margin-left: 0.5em;"><h3 class="ex_disp_h3">Fundamentals</h3><h4 id="ex14">It Compiles</h4><div class="axial"><table class="gray" border="0" cellpadding="3" cellspacing="2">        <tbody><tr> 
     128        //      <th style="color: rgb(0, 0, 0); background-color: rgb(239, 239, 239); background-image: none;">Description</th><td>It doesn't actually do anything, but it compiles and runs.</td> 
     129        //      </tr> 
     130        //      <tr> 
     131        //      <th style="color: rgb(0, 0, 0); background-color: rgb(239, 239, 239); background-image: none;">Date/Time</th><td>Sat May 8, 2004 4:17 pm</td> 
     132        //      </tr> 
     133        //      <tr> 
     134        //      <th style="color: rgb(0, 0, 0); background-color: rgb(239, 239, 239); background-image: none;">Posted by</th><td>jcc7</td> 
     135 
     136        //      </tr></tbody></table><table><tbody><tr><td colspan="2"><pre><code class="example"><span class="keyword">void</span> main() 
     137        //{ 
     138        //}</code></pre> For an explanation of what this means, visit <a href="http://www.wikiservice.at/d/wiki.cgi?D__Tutorial/StartingWithD">Wiki4D.</a></td></tr><tr><td colspan="2"> </td></tr></tbody></table></div><hr><h4 id="ex26">Do Something</h4><div class="axial"><table class="gray" border="0" cellpadding="3" cellspacing="2">      <tbody><tr> 
     139        //                                                                                                                                                                                                                   ^-- next example                                
     140 
     141        //i = int find(char[] s, char[] sub);  
     142 
     143        // find category name (again?) a little after `<li id="topic` and before `</li>`. 
     144 
     145 
     146        // find `<h4 id="ex` 
     147        // ex # should be the number in the example link (should test it to make sure) 
     148 
     149        oldDsourceExampleLink = "http://www.dsource.org/tutorials/index.php?show_example=" ~ assert(0); 
     150 
     151 
     152 
     153 
     154const char[] begExampleDescriptionMarker = `Description</th><td>`; 
     155const char[] endExampleDescriptionMarker = `</td>`; 
     156 
     157 
     158const char[] begExampleDateTimeMarker = `Date/Time</th><td>`; /* time and date will follow */ 
     159const char[] endExampleDateTimeMarker = `</td>`; 
     160 
     161 
     162 
     163const char[] begExampleBodyMarker = `<table><tbody><tr><td colspan="2">`; 
     164const char[] endExampleBodyMarker = `</td></tr><tr><td colspan="2"> </td></tr></tbody></table></div><hr>`; 
     165 
     166 
     167 
     168        wikiExample = wikiExampleName(oldCategoryName, oldExampleName); 
     169 
     170        wikiExampleNames ~= wikiExample 
     171 
     172        outFile.writeLine(`<hr id="` ~ wikiExample ~ `"/>= ` ~ oldExampleName ~ " =<br/>"); 
     173        outFile.writeLine("''Part of'' " ~ wikiCategory ~ "<br/>"); 
     174 
     175        outFile.writeLine("== Description ==<br/>"); 
     176        outFile.writeLine(exampleDescription); 
     177 
     178        outFile.writeLine("== Example ==<br/>"); 
     179        outFile.writeLine("{{{"); 
     180        outFile.writeLine("#!d"); 
     181 
     182        //outFile.writeLine("<code>"); 
     183 
     184        /* output example body */ 
     185 
     186        outFile.writeLine(exampleBody); 
     187 
     188        //outFile.writeLine("</code>"); 
     189 
     190        outFile.writeLine("== Source ==<br/>");          
     191        outFile.writeLine("|| Link || " ~ oldDsourceExampleLink ~ " ||"); 
     192        outFile.writeLine("|| Posted by || " ~ postedBy ~ " ||"); 
     193        outFile.writeLine("|| Date/Time || " ~ datePosted ~ " ||"); 
     194 
     195        outFile.writeLine("}}}"); 
     196         
     197        if(begPos == 0 && begPos > endPos) done = true; 
     198    } 
     199 
    145200 
    146201 
     
    161216    outFile.writeLine("</pre></body></html>"); 
    162217    outFile.close(); 
     218 
     219 
     220 
     221 
     222 
    163223     
    164224} 
    165225 
    166 char[] wikiCategoryName(char[] oldCategoryName) 
    167 { 
    168     char[] c; 
    169  
    170     switch(oldCategoryName) 
    171     { 
    172         case "Fundamentals": '(20) 
    173             c = "TutorialFundamentals"; 
    174             break; 
    175  
    176         case "Intermediate": '(14) 
    177             c = "TutorialIntermediate"; 
    178             break; 
    179              
    180         case "Advanced": '(8) 
    181             c = "TutorialAdvanced"; 
    182             break; 
    183      
    184         case "Keywords": '(8) 
    185             c = "KeywordsCategory"; 
    186             break; 
    187      
    188         case "Arrays" '(5) 
    189             c = "ArraysCategory"; 
    190             break; 
    191      
    192         case "Common Errors" '(3) 
    193             c = "CommonErrorsCategory"; 
    194             break; 
    195      
    196         case "Delegates" '(8) 
    197             c = "DelegateCategory"; 
    198             break; 
    199      
    200         case "Foreach": '(6) 
    201             c = "ForeachCategory"; 
    202             break; 
    203      
    204         case "Function Literals": '(2) 
    205             c = "FunctionLiteralsCategory"; 
    206             break; 
    207      
    208         case "Handling errors": '(2) 
    209             c = "HandlingErrorsCategory"; 
    210             break; 
    211      
    212         case "Operator Overloading": '(4) 
    213             c = "OperatorOverloadingCategory"; 
    214             break; 
    215      
    216         case "switch / case": '(3) 
    217             c = "SwitchCaseCategory"; 
    218             break; 
    219      
    220         case "Templates": '(10) 
    221             c = "TemplatesCategory"; 
    222             break; 
    223      
    224         case "Versioning": '(3) 
    225             c = "VersioningCategory"; 
    226             break; 
    227      
    228         case "DFL": '(7) 
    229             c = "DflCategory"; 
    230             break; 
    231      
    232         case "Mango": '(1) 
    233             c = "MangoCategory"; 
    234             break; 
    235      
    236         case "Standard Library": '(22) 
    237             c = "StandardLibraryCategory"; 
    238             break; 
    239      
    240         case "Windows GUI": '(3) 
    241             c = "WindowsGuiCategory"; 
    242             break; 
    243              
    244         default: 
    245             writefln("Unknown category: %s", oldCategoryName); 
    246     } 
    247     return c; 
    248 } 
    249  
    250  
    251  
    252 char[] wikiExampleName(char[] oldCategoryName, char[] e) 
    253 { 
    254     /* e is the example name */ 
    255     char[] s; 
    256  
    257     switch(oldCategoryName) 
    258         case "Fundamentals": 
    259             switch(e) 
    260             { 
    261                 case "It Compiles": s = "ItCompilesExample"; break; 
    262                 case "Do Something": s = "DoSomethingExample"; break; 
    263                 case "Initializing Variables": s = "InitializingVariablesExample"; break; 
    264                 case "Data Types": s = "DataTypesExample"; break; 
    265                 case "If/Else": s = "IfElseExample"; break; 
    266                 case "The \"for\" Loop": s = "ForLoopExample"; break; 
    267                 case "Variables": s = "VariablesExample"; break; 
    268                 case "Expressions": s = "ExpressionsExample"; break; 
    269                 case "Comments": s = "CommentsExample"; break; 
    270                 case "String Comparison Operators": s = "StringComparisonOperatorsExample"; break; 
    271                 case "Wait": s = "WaitExample"; break; 
    272                 case "Function": s = "FunctionExample"; break; 
    273                 case "Nested Functions": s = "NestedFunctionsExample"; break; 
    274                 case "Struct": s = "StructExample"; break; 
    275                 case "Struct Initialization": s = "StructInitializationExample"; break; 
    276                 case "Class": s = "ClassExample"; break; 
    277                 case "Appending": s = "AppendingExample"; break; 
    278                 case "Assertions": s = "AssertionsExample"; break; 
    279                 case "Unittests": s = "UnittestsExample"; break; 
    280                 case "Getch/Getchar": s = ""; break; 
    281                 default: writefln("Unknown example: %", e); 
    282             } break; 
    283          
    284         case "Intermediate": 
    285             switch(e) 
    286             { 
    287                 case "Using more than one function": s = "UsingMultipleExamples"; break; 
    288                 case "Character Initial Values": s = "CharacterInitialValuesExample"; break; 
    289                 case "Module usage": s = "ModuleUsageExample"; break; 
    290                 case "Asm code for splitting ubyte into low and high nibbles": s = "SplittingUbyteWithAsmExample"; break; 
    291                 case "Asm code for finding first occurrence of letter in string": s = "FindingFirstLetterWithAsmExample"; break; 
    292                 case "Towers of Hanoi": s = "TowersOfHanoiExample"; break; 
    293                 case "Factorial": s = "FactorialExample"; break; 
    294                 case "Escape Sequences": s = "EscapeSequencesExample"; break; 
    295                 case "Comments (Reprise)": s = "CommentsExampleTwo"; break; 
    296                 case "Nested Comments": s = "NestedCommentsExample"; break; 
    297                 case "String Literals": s = "StringLiteralsExample"; break; 
    298                 case "String and Number Literals": s = "StringAndNumberLiteralsExample"; break; 
    299                 case "Runtime Type Information (RTTI)": s = "RuntimeTypeInformationExample"; break; 
    300                 case "Statistics": s = "StatisticsExample"; break; 
    301                 default: writefln("Unknown example: %", e); 
    302             } break; 
    303          
    304         case "Advanced": 
    305             switch(e) 
    306             { 
    307                 case "Date Localization using version": s = "DateLocalizationUsingVersionExample"; break; 
    308                 case "Struct \"constructors\"": s = "StructConstructorsExample"; break; 
    309                 case "Application path": s = "ApplicationPathExample"; break; 
    310                 case "Locales": s = "LocalesExample"; break; 
    311                 case "CreateLink using COM": s = "CreateLinkUsingComExample"; break; 
    312                 case "Endless For Loop": s = "EndlessForLoopExample"; break; 
    313                 case "Meta tags (__FILE__, __LINE__, etc.)": s = "MetaTagsExample"; break; 
    314                 case "uudecode": s = "UudecodeExample"; break; 
    315                 default: writefln("Unknown example: %", e); 
    316             } break; 
    317          
    318         case "Keywords": 
    319             switch(e) 
    320             { 
    321                 case "asm": s = "AsmExample"; break; 
    322                 case "auto": s = "AutoExample"; break; 
    323                 case "alias": s = "AliasExample"; break; 
    324                 case "interface": s = "InterfaceExample"; break; 
    325                 case "dchar": s = "DcharExample"; break; 
    326                 case "final": s = "FinalExample"; break; 
    327                 case "goto": s = "GotoExample"; break; 
    328                 case "return": s = "ReturnExample"; break; 
    329                 default: writefln("Unknown example: %", e); 
    330             } break; 
    331          
    332         case "Arrays": 
    333             switch(e) 
    334             { 
    335                 case "Delete All Keys From Associative Array": s = "DeleteAllKeysFromAssocArrayExample"; break; 
    336                 case "Various char arrays": s = "VariousCharArraysExample"; break; 
    337                 case "Dimensioning Static Arrays": s = "DimensioningStaticArraysExample"; break; 
    338                 case "Returning Arrays from Functions": s = "ReturningArraysFromFunctionsExample"; break; 
    339                 case "Using char[][] args": s = "UsingCharArrayArgumentsExample"; break; 
    340                 default: writefln("Unknown example: %", e); 
    341             } break; 
    342          
    343         case "Common Errors": 
    344             switch(e) 
    345             { 
    346                 case "Increasing the Size of a Dynamic Array": s = "IncreasingSizeOfDynamicArrayExample"; break; 
    347                 case "Using Objects": s = "UsingObjectsExample"; break; 
    348                 case "Printing a Slice from a String": s = "PrintingSliceFromStringExample"; break; 
    349                 default: writefln("Unknown example: %", e); 
    350             } break; 
    351          
    352         case "Delegates": 
    353             switch(e) 
    354             { 
    355                 case "DelegateTest.d": s = "UsingDelegatesWithinObjectsExample"; break; 
    356                 case "Delegates": s = "UsingDelegateInsteadOfInterfaceExample"; break; 
    357                 case "Proper creation & use of stack delegates": s = "UsingStackDelegatesExample"; break; 
    358                 case "Don't Return a Stack Delegate": s = "DontReturnStackDelegateExample"; break; 
    359                 case "Copyable stack delegates, implemented with structs and classes": s = "CopyableStackDelegatesWithStructsAndClassesExample"; break; 
    360                 case "Copyable stack delegates (with writefln)": s = "CopyableStackDelegatesWithWriteflnExample"; break; 
    361                 case "Delegate 1": s = "DelegateNonStaticNestedFunctionExample"; break; 
    362                 case "Delegate 2": s = "IntFunctionDelegateExample"; break; 
    363                 default: writefln("Unknown example: %", e); 
    364             } break; 
    365          
    366         case "Foreach": 
    367             switch(e) 
    368             { 
    369                 case "Foreach on a class": s = "ForeachOnClassExample"; break; 
    370                 case "Foreach with an inout": s = "ForeachWithInoutExample"; break; 
    371                 case "Foreach with a string literal": s = "ForeachWithStringLiteralExample"; break; 
    372                 case "Foreach with a char[]": s = "ForeachWithCharArrayExample"; break; 
    373