nyctergatis.com

Contact

Projects
Sysquake Remote Live
NME
PDF
Hike
Sudoku
GifBuilder
jpeglib for Palm OS
MySQL Client
Cross-GCC for Mac OS
NMEMain.c
Go to the documentation of this file.
00001 
00062 /* License: new BSD license (see NME.h) */
00063 
00064 // To compile: gcc -o nme NME.c NMEAutolink.c NMEMain.c
00065 
00066 #include <stdlib.h>
00067 #include <stdio.h>
00068 #include <string.h>
00069 #include "NME.h"
00070 #include "NMETest.h"
00071 #include "NMEEPub.h"
00072 #include "NMEAutolink.h"
00073 #include "NMEPluginRot13.h"
00074 #include "NMEPluginReverse.h"
00075 #include "NMEPluginUppercase.h"
00076 #include "NMEPluginCalendar.h"
00077 #include "NMEPluginRaw.h"
00078 #include "NMEPluginTOC.h"
00079 #include "NMEPluginWiki.h"
00080 
00082 #define INITIALSIZE (32 * 1024)
00083 
00085 static NMEOutputFormat const NMEOutputFormatSlidesHTML =
00086 {
00087     " ",    // space
00088     2,  // indentSpaces
00089     0,  // defFontSize
00090     '%',    // ctrlChar
00091 //  "<!-- Generated by Nyctergatis Markup Engine, "
00092 //          __DATE__ " " __TIME__ " -->\n"
00093 //  "<html><head><title>Test of CSS</title></head>\n"
00094     "<style type=\"text/css\">\n"
00095 //  "body{background:white;margin:1em 1em}\n"
00096     "div.SL *,div.FSL *,div.TSL * {background:inherit;color:white}\n"
00097     "div.TSL h1,div.SL h2,div.SL h3,div.SL h4{color:white}\n"
00098     "div.SL,div.TSL {background:#000099;width:300pt;height:200pt;\n"
00099     "padding:20pt;margin:10pt;page-break-inside:avoid;position:relative}\n"
00100     "div.FSL{position:absolute;bottom:10px;right:10px}\n"
00101     "div.SL h1,div.SL h2,div.SL h3,div.SL p,div.SL dl,div.SL dt,div.SL ul,div.SL table,div.SL td,div.SL th,div.SL address{font-family:verdana,arial,sans-serif}\n"
00102     "div.SL h1,div.SL h2,div.TSL h1,div.TSL h2{text-align:center}\n"
00103     "div.TSL h1{font-size: 20pt;margin-top:50pt}\n"
00104     "div.SL h2{font-size:18pt}\n"
00105     "div.SL h3{font-size:13pt}\n"
00106     "div.TSL p,div.SL p,div.SL dl,div.SL dt,div.SL ul,div.SL table,div.SL td,div.SL th,div.SL pre,div.SL address{font-size:11pt;line-height:1.3;\n"
00107     "margin-top:3pt;margin-bottom:6pt}\n"
00108     "div.SL p,div.SL dl,div.SL ul{text-align:justify}\n"
00109     "div.SL table{border-style=solid;border-width=1px;border-color=white}\n"
00110     "div.FSL p{font-size:9pt;color:#6666ff}\n"
00111     "div.TSL p{text-align:center}\n"
00112     "</style>\n"
00113     "<div class=\"TSL\">\n",
00114         "</div>"
00115 //      "</body></html>\n"
00116         ,   // doc
00117     4,  // highest heading level
00118     "%%{l=2&i>1}<div class=\"FSL\"><p>%{i-1}</p></div>\n%%"
00119     "%%{l=2}</div>\n<div class=\"SL\">\n%%"
00120             "<h%{l}"
00121             "%%{x} id=\"h%{o}\"%%"
00122             "%%{s>0} style=\"font-size:%{l=1&3*s|l=2&2*s|l=3&3*s/2|5*s/4}pt\"%%>",
00123         "</h%{l}>\n",   // heading
00124     "<p%%{s>0} style=\"font-size:%{s}pt\"%%>", "</p>\n",    // par
00125     "<br />",   // line break
00126     "<pre%%{s>0} style=\"font-size:%{s}pt\"%%>\n", "</pre>\n",  // pre
00127     "", "\n",   // pre line
00128     FALSE,  // sublistInListItem
00129     "<ul>\n", "</ul>\n",    // UL
00130     "<li%%{s>0} style=\"font-size:%{s}pt\"%%>", "</li>\n",  // UL line
00131     "<ol>\n", "</ol>\n",    // OL
00132     "<li%%{s>0} style=\"font-size:%{s}pt\"%%>", "</li>\n",  // OL line
00133     "<dl>\n", "</dl>\n",    // DL
00134     "<dt%%{s>0} style=\"font-size:%{s}pt\"%%>", "</dt>\n",  // DT
00135     NULL,   // emptyDT
00136     "<dd%%{s>0} style=\"font-size:%{s}pt\"%%>", "</dd>\n",  // DD
00137     "<div style=\"margin-left:2em%%{s>0}; font-size:%{s}pt%%\">\n",
00138         "</div>\n", // indented section
00139     "<p%%{s>0} style=\"font-size:%{s}pt\"%%>", "</p>\n",    // indented par
00140     "<table align=\"center\">\n", "</table>\n", // table
00141     "<tr>", "</tr>\n",  // table row
00142     "<th%%{s>0} style=\"font-size:%{s}pt\"%%>", "</th>\n",  // table header cell
00143     "<td%%{s>0} style=\"font-size:%{s}pt\"%%>", "</td>\n",  // table normal cell
00144     "<hr />\n", // hr
00145 #if defined(useHTMLEmphasisTags)
00146     "<strong>", "</strong>",    // strong emphasis for **...**
00147     "<em>", "</em>",    // emphasis for //...//
00148 #else
00149     "<b>", "</b>",  // bold
00150     "<i>", "</i>",  // italic
00151 #endif
00152     "<u>", "</u>",  // underline
00153     "<sup>", "</sup>",  // superscript
00154     "<sub>", "</sub>",  // subscript
00155     "<tt>", "</tt>",    // monospace
00156     "<a href=\"", "</a>", "\">", FALSE, // URL
00157     "<img src=\"", "\" />", "\" alt=\"", FALSE, TRUE,   // image
00158     NULL,   // interwiki
00159     NULL, NULL, // encodeURLFun
00160     NMEEncodeCharFunDict, (void *)NMEXMLCharDict,   // char encoder
00161     NMEEncodeCharFunDict, (void *)NMEXMLCharDict,   // char pre encoder
00162     70, NULL, NULL, // wordwrap
00163     NULL, NULL, // char hook
00164     NULL, NULL, NULL, NULL, // process hooks
00165     NULL,   // plugins
00166     NULL,   // autoconverts
00167     NULL, NULL  // getVar
00168 };
00169 
00171 static NMEOutputFormat const NMEOutputFormatMediawiki =
00172 {
00173     " ",    // space
00174     0,  // indentSpaces
00175     10, // defFontSize
00176     '%',    // ctrlChar
00177     "", "", // doc
00178     6,  // highest heading level
00179     "%%{l}=%%", "%%{l}=%%\n",   // heading
00180     "", "\n\n", // par
00181     "<br>", // line break
00182     "", "\n",   // pre
00183     " ", "\n",  // pre line
00184     FALSE,  // sublistInListItem
00185     "", "%%{l=1}\n%%",  // UL
00186     "%%{l}*%% ", "\n",  // UL line
00187     "", "%%{l=1}\n%%",  // OL
00188     "%%{l}#%% ", "\n",  // OL line
00189     "", "%%{l=1}\n%%",  // DL
00190     "%%{l};%% ", "\n",  // DT
00191     NULL,   // emptyDT
00192     ": ", "\n", // DD
00193     "", "%%{l=1}\n%%",  // indented section
00194     "%%{l}:%% ", "\n",  // indented par
00195     "{|\n", "|}\n", // table
00196     "|-\n", "", // table row
00197     "!", "\n",  // table header cell
00198     "|", "\n",  // table normal cell
00199     "----\n\n", // hr                                   ??
00200     "'''", "'''",   // bold
00201     "''", "''", // italic
00202     "__", "__", // underline                            ??
00203     "<sup>", "</sup>",  // superscript
00204     "<sub>", "</sub>",  // subscript
00205     "<code>", "</code>",    // monospace
00206     "[", "]", "|", FALSE,   // URL
00207     "[[Image:", "]]", "|", FALSE, TRUE, // image
00208     NULL,   // interwiki
00209     NULL, NULL, // encodeURLFun
00210     NULL, NULL, // char encoder                         ??
00211     NULL, NULL, // char pre encoder                     ??
00212     65, NULL, NULL, // wordwrap
00213     NULL, NULL, // char hook
00214     NULL, NULL, NULL, NULL, // process hooks
00215     NULL,   // plugins
00216     NULL,   // autoconverts
00217     NULL, NULL  // getVar
00218 };
00219 
00221 static NMEEncodeCharDict const jspwikiCharDict[] =
00222 {
00223     {'*', "~*"},
00224     {'#', "~#"},
00225     {'\\', "~\\"},
00226     {'_', "~_"},
00227     {'!', "~!"},
00228     {'{', "~{"},
00229     {'}', "~}"},
00230     {'~', "~~"},
00231     {0, NULL}
00232 };
00233 
00235 static NMEOutputFormat const NMEOutputFormatJSPWiki =
00236 {
00237     " ",    // space
00238     0,  // indentSpaces
00239     10, // defFontSize
00240     '?',    // ctrlChar
00241     "", "", // doc
00242     3,  // highest heading level
00243     "??{4-l}!?? ", "\n",    // heading
00244     "", "\n\n", // par
00245     "\\\\", // line break
00246     "{{{\n", "}}}\n\n", // pre
00247     "", "\n",   // pre line
00248     FALSE,  // sublistInListItem
00249     "", "??{l=1}\n??",  // UL
00250     "??{l}*?? ", "\n",  // UL line
00251     "", "??{l=1}\n??",  // OL
00252     "??{l}#?? ", "\n",  // OL line
00253     "", "??{l=1}\n??",  // DL
00254     "%%(text-indent:?{20*l}px)\n__", ":__ ",    // DT
00255     "%%(text-indent:?{20*l}px)\n",  // emptyDT
00256     "", "\n%%\n\n", // DD
00257     "", "", // indented section
00258     "%%(text-indent:?{20*l}px)\n", "\n%%\n\n",  // indented par
00259     "", "\n",   // table
00260     "", "\n",   // table row
00261     "|| ", "",  // table header cell
00262     "| ", "",   // table normal cell
00263     "----\n\n", // hr
00264     "__", "__", // bold
00265     "''", "''", // italic
00266     "%%(text-decoration:underline)", "%%",  // underline
00267     "%%(vertical-align:sup)", "%%", // superscript
00268     "%%(vertical-align:sub)", "%%", // subscript
00269     "{{", "}}", // monospace
00270     "[", "]", "|", TRUE,    // URL
00271     "", "", NULL, FALSE, FALSE, // image
00272     NULL,   // interwiki
00273     NULL, NULL, // encodeURLFun
00274     NMEEncodeCharFunDict, (void *)jspwikiCharDict,  // char encoder
00275     NULL, NULL, // char pre encoder
00276     65, NULL, NULL, // wordwrap
00277     NULL, NULL, // char hook
00278     NULL, NULL, NULL, NULL, // process hooks
00279     NULL,   // plugins
00280     NULL,   // autoconverts
00281     NULL, NULL  // getVar
00282 };
00283 
00285 static NMEPluginTocData tocData;
00286 
00288 static NMEPlugin const pluginsHTML[] =
00289 {
00290     NMEPluginReverseEntry,
00291     NMEPluginRot13Entry,
00292     NMEPluginUppercaseEntry,
00293     NMEPluginRawEntry("rawinpar", kNMEPluginOptDefault),
00294     NMEPluginRawEntry("rawoutpar", kNMEPluginOptBetweenPar),
00295     NMEPluginCalendarEntry,
00296     NMEPluginTOCEntry(&tocData),
00297     
00298     NMEPluginTableEnd
00299 };
00300 
00302 static NMEPlugin const plugins[] =
00303 {
00304     NMEPluginReverseEntry,
00305     NMEPluginRot13Entry,
00306     NMEPluginUppercaseEntry,
00307     NMEPluginCalendarEntry,
00308     
00309     NMEPluginTableEnd
00310 };
00311 
00313 static NMEPlugin const pluginsMetadata[] =
00314 {
00315     NMEPluginKeywordsEntry,
00316     NMEPluginDateEntry(NMEMonthNameEn NMEMonthNameFr NMEMonthNameSp NMEMonthNameIt NMEMonthNameDe),
00317     NMEPluginExecuteEntry,
00318     
00319     NMEPluginTableEnd
00320 };
00321 
00323 static NMEAutoconvert autoconverts[16];
00324 
00326 static NMEInterwiki const interwikis[] =
00327 {
00328     {"Dictionary:",
00329         "http://www.dict.org/bin/Dict?Database=*&Form=Dict1&Strategy=*&Query="},
00330     {"Foldoc:", "http://www.foldoc.org/foldoc/foldoc.cgi?"},
00331     {"Google:", "http://www.google.com/search?q="},
00332     {"WhoIs:", "http://www.whois.sc/"},
00333     {"WikiPedia:", "http://en.wikipedia.org/wiki/"},
00334     {NULL, NULL}
00335 };
00336 
00338 typedef struct
00339 {
00340     NMEInt nesting;
00341 } HookDumpData;
00342 
00344 static NMEErr hookDump(NMEInt level,
00345         NMEInt item,
00346         NMEBoolean enter,
00347         NMEConstText markup,
00348         NMEInt srcIndex,
00349         NMEInt srcLineNumber,
00350         NMEContext *context,
00351         void *data)
00352 {
00353     if (!enter)
00354         ((HookDumpData *)data)->nesting--;
00355     printf("%*s", 2 * ((HookDumpData *)data)->nesting, " ");
00356     if (level > 0)
00357         printf("%-4s L%d %c %5d\n", markup, level, enter ? '<' : '>', srcIndex);
00358     else
00359         printf("%-4s    %c %5d\n", markup, enter ? '<' : '>', srcIndex);
00360     if (enter)
00361         ((HookDumpData *)data)->nesting++;
00362     return kNMEErrOk;
00363 }
00364 
00366 typedef struct
00367 {
00368     NMEInt index;
00369     NMEInt length;
00370     NMEConstText begin;
00371     NMEConstText end;
00372     NMEBoolean done;    
00373 } HookEditData;
00374 
00376 static NMEErr hookEdit(NMEInt level,
00377         NMEInt item,
00378         NMEBoolean enter,
00379         NMEConstText markup,
00380         NMEInt srcIndex,
00381         NMEInt srcLineNumber,
00382         NMEContext *context,
00383         void *data)
00384 {
00385     HookEditData *hookEditData = (HookEditData *)data;
00386     NMEErr err;
00387     
00388     if (!hookEditData->done
00389             && srcIndex >= hookEditData->index)
00390     {
00391         if (srcIndex < hookEditData->index + hookEditData->length)
00392         {
00393             if (!NMEAddString(hookEditData->begin, -1, '\0', context))
00394                 return kNMEErrNotEnoughMemory;
00395             err = NMECopySource(hookEditData->length - (hookEditData->index - srcIndex),
00396                     TRUE, TRUE, context);
00397             if (err != kNMEErrOk)
00398                 return err;
00399             if (!NMEAddString(hookEditData->end, -1, '\0', context))
00400                 return kNMEErrNotEnoughMemory;
00401         }
00402         
00403         hookEditData->done = TRUE;
00404     }
00405     return kNMEErrOk;
00406 }
00407 
00409 #define kHookCheckMaxDepth 256
00410 
00411 typedef struct
00412 {
00413     NMEInt lastSrcIndex;    
00414     NMEInt depth;   
00415     struct
00416     {
00417         NMEChar hook;   
00418         NMEInt level;
00419         NMEInt item;
00420         NMEConstText markup;
00421         NMEInt srcIndex;
00422         NMEInt srcLineNumber;
00423     } stack[kHookCheckMaxDepth];    
00424 } HookCheckData;
00425 
00427 static NMEErr hookCheck(NMEChar hook,
00428         NMEInt level,
00429         NMEInt item,
00430         NMEBoolean enter,
00431         NMEConstText markup,
00432         NMEInt srcIndex,
00433         NMEInt srcLineNumber,
00434         HookCheckData *hookCheckData)
00435 {
00436     if (srcIndex < hookCheckData->lastSrcIndex)
00437         fprintf(stderr, "Last srcIndex = %d\n", hookCheckData->lastSrcIndex);
00438     else if (!enter && hookCheckData->depth <= 0)
00439         fprintf(stderr, "Outside any structure\n");
00440     else if (!enter
00441             && (hookCheckData->stack[hookCheckData->depth - 1].hook != hook
00442                 || hookCheckData->stack[hookCheckData->depth - 1].level != level
00443                 || hookCheckData->stack[hookCheckData->depth - 1].item != item
00444                 || strcmp(hookCheckData->stack[hookCheckData->depth - 1].markup, markup)))
00445     {
00446         fprintf(stderr, "Nonmatching exit:\n");
00447         fprintf(stderr,
00448                 " hook = %c, level = %d, item = %d, markup = \"%s\", srcIndex = %d, srcLineNumber = %d\n",
00449                 hookCheckData->stack[hookCheckData->depth - 1].hook,
00450                 hookCheckData->stack[hookCheckData->depth - 1].level,
00451                 hookCheckData->stack[hookCheckData->depth - 1].item,
00452                 hookCheckData->stack[hookCheckData->depth - 1].markup,
00453                 hookCheckData->stack[hookCheckData->depth - 1].srcIndex,
00454                 hookCheckData->stack[hookCheckData->depth - 1].srcLineNumber);
00455     }
00456     else if (enter && hookCheckData->depth >= kHookCheckMaxDepth)
00457         fprintf(stderr, "Nesting too deep\n");
00458     else if (enter)
00459     {
00460         hookCheckData->stack[hookCheckData->depth].hook = hook;
00461         hookCheckData->stack[hookCheckData->depth].level = level;
00462         hookCheckData->stack[hookCheckData->depth].item = item;
00463         hookCheckData->stack[hookCheckData->depth].markup = markup;
00464         hookCheckData->stack[hookCheckData->depth].srcIndex = srcIndex;
00465         hookCheckData->stack[hookCheckData->depth].srcLineNumber = srcLineNumber;
00466         hookCheckData->depth++;
00467         return kNMEErrOk;
00468     }
00469     else
00470     {
00471         hookCheckData->depth--;
00472         return kNMEErrOk;
00473     }
00474     
00475     fprintf(stderr,
00476             "Hook = %c, level = %d, item = %d, %s, markup = \"%s\", srcIndex = %d, srcLineNumber = %d\n",
00477             hook, level, item, enter ? "enter" : "exit", markup, srcIndex, srcLineNumber);
00478     
00479     return kNMEErrInternal;
00480 }
00481 
00483 static NMEErr divHookCheck(NMEInt level,
00484         NMEInt item,
00485         NMEBoolean enter,
00486         NMEConstText markup,
00487         NMEInt srcIndex,
00488         NMEInt srcLineNumber,
00489         NMEContext *context,
00490         void *data)
00491 {
00492     return hookCheck('d', level, item, enter, markup, srcIndex, srcLineNumber,
00493             (HookCheckData *)data);
00494 }
00495 
00497 static NMEErr parHookCheck(NMEInt level,
00498         NMEInt item,
00499         NMEBoolean enter,
00500         NMEConstText markup,
00501         NMEInt srcIndex,
00502         NMEInt srcLineNumber,
00503         NMEContext *context,
00504         void *data)
00505 {
00506     return hookCheck('p', level, item, enter, markup, srcIndex, srcLineNumber,
00507             (HookCheckData *)data);
00508 }
00509 
00511 static NMEErr spanHookCheck(NMEInt level,
00512         NMEInt item,
00513         NMEBoolean enter,
00514         NMEConstText markup,
00515         NMEInt srcIndex,
00516         NMEInt srcLineNumber,
00517         NMEContext *context,
00518         void *data)
00519 {
00520     return hookCheck('s', level, item, enter, markup, srcIndex, srcLineNumber,
00521             (HookCheckData *)data);
00522 }
00523 
00528 //  absolute URL are copied unprocessed)
00529 NMEErr encodeURLEasylink(NMEConstText link, NMEInt linkLen,
00530         NMEContext *context, void *data)
00531 {
00532     NMEInt i, j;
00533     NMEConstText format = (NMEConstText)data;
00534 #define isAlphaNum(c) \
00535     ((c) >= 'a' && (c) <= 'z' || (c) >= 'A' && (c) <= 'Z' \
00536             || (c) >= '0' && (c) <= '9')
00537 #define toHexa(d) \
00538     ((d) >= 10 ? (d) + ('a' - 10) : (d) + '0')
00539     
00540     // URL?
00541     for (i = 0; i < linkLen && isAlphaNum(link[i]); i++)
00542         ;
00543     if (i < linkLen && link[i] == ':')
00544     {
00545         // yes: copy verbatim
00546         if (!NMEAddString(link, linkLen, 0, context))
00547             return kNMEErrNotEnoughMemory;
00548         return kNMEErrOk;
00549     }
00550     
00551     for (i = 0; format[i]; i++)
00552         if (format[i] == '$')
00553         {
00554             for (j = 0; j < linkLen; j++)
00555                 if (isAlphaNum(link[j]) || link[j] == '-' || link[j] == ',' || link[j] == '.'
00556                         || link[j] == '\'' || link[j] == '(' || link[j] == ')'
00557                         || link[j] == ':')
00558                 {
00559                     if (!NMEAddString(&link[j], 1, 0, context))
00560                         return kNMEErrNotEnoughMemory;
00561                 }
00562                 else if (link[j] == '_' || link[j] == ' ')
00563                 {
00564                     if (!NMEAddString("_", 1, 0, context))
00565                         return kNMEErrNotEnoughMemory;
00566                 }
00567                 else if (link[j] & 0x80)    // non-ascii
00568                 {
00569                     char e[3];
00570                     e[0] = '%';
00571                     e[1] = toHexa((link[j] >> 4) & 0x0f);
00572                     e[2] = toHexa(link[j] & 0x0f);
00573                     if (!NMEAddString(e, 3, 0, context))
00574                         return kNMEErrNotEnoughMemory;
00575                 }
00576         }
00577         else if (!NMEAddString(&format[i], 1, 0, context))
00578             return kNMEErrNotEnoughMemory;
00579         
00580     return kNMEErrOk;
00581 }
00582 
00584 int main(int argc, char **argv)
00585 {
00586     NMEInt size;
00587     NMEText src = NULL, buf, dest;
00588     NMEInt srcLen, destLen;
00589     NMEOutputFormat outputFormat = NMEOutputFormatHTML;
00590     NMEInt options = kNMEProcessOptDefault;
00591     NMEBoolean autoURLLink = FALSE, autoCCLink = FALSE;
00592     NMEBoolean testPhase = 0;   // no test by default
00593     int i;
00594     int fontSize = 0;
00595     HookDumpData hookDumpData;
00596     HookTOCData hookTOCData;
00597     HookEditData hookEditData;
00598     HookCheckData hookCheckData;
00599     NMEErr err;
00600     NMEInt lineNumber;
00601     
00602     outputFormat.plugins = pluginsHTML;
00603     
00604     for (i = 1; i < argc; i++)
00605         if (!strcmp(argv[i], "--body"))
00606             options |= kNMEProcessOptNoPreAndPost;
00607         else if (!strcmp(argv[i], "--1eol"))
00608             options |= kNMEProcessOptNoMultilinePar;
00609         else if (!strcmp(argv[i], "--2eol"))
00610             options &= ~kNMEProcessOptNoMultilinePar;
00611         else if (!strcmp(argv[i], "--xref"))
00612             options |= kNMEProcessOptXRef;
00613         else if (!strcmp(argv[i], "--autocclink"))
00614             autoCCLink = TRUE;
00615         else if (!strcmp(argv[i], "--autourllink"))
00616             autoURLLink = TRUE;
00617         else if (!strcmp(argv[i], "--nme"))
00618         {
00619             outputFormat = NMEOutputFormatNME;
00620             outputFormat.plugins = plugins;
00621         }
00622         else if (!strcmp(argv[i], "--html"))
00623         {
00624             outputFormat = NMEOutputFormatHTML;
00625             outputFormat.plugins = pluginsHTML;
00626         }
00627         else if (!strcmp(argv[i], "--slides"))
00628         {
00629             outputFormat = NMEOutputFormatSlidesHTML;
00630             outputFormat.plugins = pluginsHTML;
00631         }
00632         else if (!strcmp(argv[i], "--epub"))
00633         {
00634             outputFormat = NMEOutputFormatOPSXHTML;
00635             outputFormat.plugins = pluginsHTML;
00636         }
00637         else if (!strcmp(argv[i], "--jspwiki"))
00638         {
00639             outputFormat = NMEOutputFormatJSPWiki;
00640             outputFormat.plugins = plugins;
00641         }
00642         else if (!strcmp(argv[i], "--latex"))
00643         {
00644             outputFormat = NMEOutputFormatLaTeX;
00645             outputFormat.plugins = plugins;
00646         }
00647         else if (!strcmp(argv[i], "--mediawiki"))
00648         {
00649             outputFormat = NMEOutputFormatMediawiki;
00650             outputFormat.plugins = plugins;
00651         }
00652         else if (!strcmp(argv[i], "--null"))
00653         {
00654             outputFormat = NMEOutputFormatNull;
00655             outputFormat.plugins = NULL;
00656         }
00657         else if (!strcmp(argv[i], "--null-plugins"))
00658         {
00659             outputFormat = NMEOutputFormatNull;
00660             outputFormat.plugins = plugins;
00661         }
00662         else if (!strcmp(argv[i], "--rtf"))
00663         {
00664             outputFormat = NMEOutputFormatRTF;
00665             outputFormat.plugins = plugins;
00666         }
00667         else if (!strcmp(argv[i], "--metadata"))
00668         {
00669             outputFormat = NMEOutputFormatNull;
00670             outputFormat.plugins = pluginsMetadata;
00671         }
00672         else if (!strcmp(argv[i], "--editfrag") && i + 4 < argc)
00673         {
00674             outputFormat.parHookFun = hookEdit;
00675             hookEditData.index = strtol(argv[++i], NULL, 0);
00676             hookEditData.length = strtol(argv[++i], NULL, 0);
00677             hookEditData.begin = argv[++i];
00678             hookEditData.end = argv[++i];
00679             hookEditData.done = FALSE;
00680             outputFormat.hookData = (void *)&hookEditData;
00681         }
00682         else if (!strcmp(argv[i], "--structdiv"))
00683         {
00684             outputFormat = NMEOutputFormatNull;
00685             outputFormat.divHookFun = hookDump;
00686             hookDumpData.nesting = 0;
00687             outputFormat.hookData = (void *)&hookDumpData;
00688         }
00689         else if (!strcmp(argv[i], "--structpar"))
00690         {
00691             outputFormat = NMEOutputFormatNull;
00692             outputFormat.parHookFun = hookDump;
00693             hookDumpData.nesting = 0;
00694             outputFormat.hookData = (void *)&hookDumpData;
00695         }
00696         else if (!strcmp(argv[i], "--structspan"))
00697         {
00698             outputFormat = NMEOutputFormatNull;
00699             outputFormat.spanHookFun = hookDump;
00700             hookDumpData.nesting = 0;
00701             outputFormat.hookData = (void *)&hookDumpData;
00702         }
00703         else if (!strcmp(argv[i], "--checkhooks"))
00704         {
00705             outputFormat = NMEOutputFormatNull;
00706             outputFormat.divHookFun = divHookCheck;
00707             outputFormat.parHookFun = parHookCheck;
00708             outputFormat.spanHookFun = spanHookCheck;
00709             hookCheckData.lastSrcIndex = 0;
00710             hookCheckData.depth = 0;
00711             outputFormat.hookData = (void *)&hookCheckData;
00712         }
00713         else if (!strcmp(argv[i], "--debug"))
00714         {
00715             outputFormat = NMEOutputFormatDebug;
00716             outputFormat.plugins = plugins;
00717         }
00718         else if (!strcmp(argv[i], "--debug2"))
00719         {
00720             outputFormat = NMEOutputFormatDebug;
00721             outputFormat.sublistInListItem = TRUE;
00722             outputFormat.plugins = plugins;
00723         }
00724         else if (!strcmp(argv[i], "--easylink"))
00725         {
00726             outputFormat.encodeURLFun = encodeURLEasylink;
00727             outputFormat.encodeURLData = argv[++i];
00728         }
00729         else if (!strcmp(argv[i], "--test"))
00730         {
00731             outputFormat = NMEOutputFormatTest;
00732             outputFormat.plugins = plugins;
00733             testPhase = 1;  // sublistInListItem = FALSE
00734             
00735         }
00736         else if (!strcmp(argv[i], "--testoutput"))
00737         {
00738             outputFormat = NMEOutputFormatTest;
00739             outputFormat.plugins = plugins;
00740             
00741         }
00742         else if (!strcmp(argv[i], "--testoutput2"))
00743         {
00744             outputFormat = NMEOutputFormatTest;
00745             outputFormat.sublistInListItem = TRUE;
00746             outputFormat.plugins = plugins;
00747             
00748         }
00749         else if (!strcmp(argv[i], "--text"))
00750         {
00751             outputFormat = NMEOutputFormatText;
00752             outputFormat.plugins = plugins;
00753         }
00754         else if (!strcmp(argv[i], "--textc"))
00755         {
00756             outputFormat = NMEOutputFormatTextCompact;
00757             outputFormat.plugins = plugins;
00758         }
00759         else if (!strcmp(argv[i], "--man"))
00760         {
00761             outputFormat = NMEOutputFormatMan;
00762             outputFormat.plugins = plugins;
00763         }
00764         else if (!strcmp(argv[i], "--fontsize") && i + 1 < argc)
00765             fontSize = strtol(argv[++i], NULL, 0);
00766         else if (!strcmp(argv[i], "--headernum1"))
00767             options |= kNMEProcessOptH1Num;
00768         else if (!strcmp(argv[i], "--headernum2"))
00769             options |= kNMEProcessOptH2Num;
00770         else if (!strcmp(argv[i], "--strictcreole"))
00771             options |= kNMEProcessOptNoUnderline | kNMEProcessOptNoMonospace
00772                     | kNMEProcessOptNoSubSuperscript | kNMEProcessOptNoIndentedPar
00773                     | kNMEProcessOptNoDL | kNMEProcessOptVerbatimMono;
00774         else if (!strcmp(argv[i], "--toc"))
00775             NMESetTOCOutputFormat(&outputFormat, &hookTOCData);
00776         else
00777         {
00778             if (strcmp(argv[i], "--help"))
00779                 fprintf(stderr, "Unknown option %s\n", argv[i]);
00780             fprintf(stderr, "Usage: %s [options]\n"
00781                     "Filter NME stdin and renders it to another format.\n"
00782                     "--1eol            single eol as paragraph breaks\n"
00783                     "--2eol            double eol as paragraph breaks (default)\n"
00784                     "--autocclink      automatic conversion of camelCase words to links\n"
00785                     "--autourllink     automatic conversion of URLs to links\n"
00786                     "--body            naked body without header and footer\n"
00787                     "--checkhooks      check hooks\n"
00788                     "--debug           XML debug format, sublists outside list items\n"
00789                     "--debug2          XML debug format, sublists inside list items\n"
00790                     "--easylink format links are converted as follows: letters, digits,\n"
00791                     "                  hyphens commas, dots, apostrophes, parentheses,\n"
00792                     "                  colons and underscores are preserved, spaces are\n"
00793                     "                  converted to underscores, non-ascii characters are\n"
00794                     "                  URL-encoded, other characters are ignored, and the\n"
00795                     "                  output link is obtained by replacing the dollar\n"
00796                     "                  character in format with the processed link (e.g.\n"
00797                     "                  format could be '/wiki/$.html').\n"
00798                     "                  URL are used verbatim.\n"
00799                     "--editfrag i l b e   edit a fragment of the source code; in output,\n"
00800                     "                  replace what corresponds to input fragment starting\n"
00801                     "                  at i of length l with this unmodified input fragment\n"
00802                     "                  surrounded by b and e\n"
00803                     "--headernum1      numbering of level-1 headers\n"
00804                     "--headernum2      numbering of level-2 headers\n"
00805                     "--fontsize s      font size (0=default)\n"
00806                     "--help            this help message\n"
00807                     "--html            HTML output (default)\n"
00808                     "--jspwiki         JSPWiki output\n"
00809                     "--latex           LaTeX output\n"
00810                     "--man             man page output\n"
00811                     "--mediawiki       MediaWiki output\n"
00812                     "--metadata        metadata extraction\n"
00813                     "--nme             NME output\n"
00814                     "--null            no output, plugins disabled (still process input)\n"
00815                     "--null-plugins    no normal output, but process plugins\n"
00816                     "--strictcreole    disable monospace, underline, subscript,\n"
00817                     "                  superscript, definition lists, and indented\n"
00818                     "                  paragraphs; and enable nowiki monospace\n"
00819                     "--structdiv       display division structure\n"
00820                     "--structpar       display paragraph structure\n"
00821                     "--structspan      display span structure\n"
00822                     "--rtf             RTF output\n"
00823                     "--test            test (validation of style string nesting)\n"
00824                     "--testoutput      test output\n"
00825                     "--testoutput2     test output, sublists inside list items\n"
00826                     "--slides          HTML slides output\n"
00827                     "--text            plain text output\n"
00828                     "--textc           compact plain text output\n"
00829                     "--xref            headings have hyperlink target labels\n",
00830                 argv[0]);
00831             exit(0);
00832         }
00833     
00834     outputFormat.interwikis = interwikis;
00835     if (autoCCLink || autoURLLink)
00836     {
00837         int n = 0;
00838         
00839         if (autoCCLink)
00840             autoconverts[n++].cb = NMEAutoconvertCamelCase;
00841         if (autoURLLink)
00842             autoconverts[n++].cb = NMEAutoconvertURL;
00843 
00844         outputFormat.autoconverts = autoconverts;
00845     }
00846     
00847     size = INITIALSIZE;
00848     src = malloc(size);
00849     for (srcLen = 0; ; )
00850     {
00851         if (!src)
00852             exit(1);
00853         i = fread(src + srcLen, 1, size - srcLen, stdin);
00854         if (i == 0)
00855             break;
00856         else if (i < 0)
00857         {
00858             free(src);
00859             exit(2);
00860         }
00861         srcLen += i;
00862         size *= 2;
00863         src = realloc(src, size);
00864     }
00865     
00866     size *= 4;  // likely enough
00867     buf = malloc(size);
00868     if (!buf)
00869         exit(1);
00870     
00871     tocData.src = src;
00872     tocData.srcLen = srcLen;
00873     
00874 process:
00875     err = NMEProcess(src, srcLen,
00876             buf, size,
00877             options, "\n", &outputFormat, fontSize,
00878             &dest, &destLen, NULL);
00879     if (err == kNMEErrNotEnoughMemory)
00880     {
00881         free(buf);
00882         size *= 2;
00883         buf = malloc(size);
00884         if (!buf)
00885             exit(1);
00886         goto process;
00887     }
00888     
00889     if (err != kNMEErrOk)
00890         printf("Error %d\n", err);
00891     else switch (testPhase)
00892     {
00893         case 0: // no test, just plain output
00894             printf("%.*s", destLen, dest);
00895             break;
00896         case 1: // sublistInListItem = FALSE
00897             err = NMETest(dest, destLen, &lineNumber);
00898             if (err != kNMEErrOk)
00899             {
00900                 printf("Error %d (sublistInListItem = FALSE, line = %d)\n",
00901                         err, lineNumber);
00902                 break;
00903             }
00904             outputFormat = NMEOutputFormatTest;
00905             outputFormat.sublistInListItem = TRUE;
00906             testPhase++;
00907             goto process;
00908         case 2: // sublistInListItem = TRUE
00909             err = NMETest(dest, destLen, &lineNumber);
00910             if (err != kNMEErrOk)
00911                 printf("Error %d (sublistInListItem = TRUE, line = %d)\n",
00912                         err, lineNumber);
00913             break;
00914     }
00915     
00916     free((void *)buf);
00917     free((void *)src);
00918     
00919     return err != kNMEErrOk;
00920 }
Generated by Doxygen.
Copyright 2007-2013, Yves Piguet.
All rights reserved.