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