00001
00062
00063
00064
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 " ",
00088 2,
00089 0,
00090 '%',
00091
00092
00093
00094 "<style type=\"text/css\">\n"
00095
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
00116 ,
00117 4,
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",
00124 "<p%%{s>0} style=\"font-size:%{s}pt\"%%>", "</p>\n",
00125 "<br />",
00126 "<pre%%{s>0} style=\"font-size:%{s}pt\"%%>\n", "</pre>\n",
00127 "", "\n",
00128 FALSE,
00129 "<ul>\n", "</ul>\n",
00130 "<li%%{s>0} style=\"font-size:%{s}pt\"%%>", "</li>\n",
00131 "<ol>\n", "</ol>\n",
00132 "<li%%{s>0} style=\"font-size:%{s}pt\"%%>", "</li>\n",
00133 "<dl>\n", "</dl>\n",
00134 "<dt%%{s>0} style=\"font-size:%{s}pt\"%%>", "</dt>\n",
00135 NULL,
00136 "<dd%%{s>0} style=\"font-size:%{s}pt\"%%>", "</dd>\n",
00137 "<div style=\"margin-left:2em%%{s>0}; font-size:%{s}pt%%\">\n",
00138 "</div>\n",
00139 "<p%%{s>0} style=\"font-size:%{s}pt\"%%>", "</p>\n",
00140 "<table align=\"center\">\n", "</table>\n",
00141 "<tr>", "</tr>\n",
00142 "<th%%{s>0} style=\"font-size:%{s}pt\"%%>", "</th>\n",
00143 "<td%%{s>0} style=\"font-size:%{s}pt\"%%>", "</td>\n",
00144 "<hr />\n",
00145 #if defined(useHTMLEmphasisTags)
00146 "<strong>", "</strong>",
00147 "<em>", "</em>",
00148 #else
00149 "<b>", "</b>",
00150 "<i>", "</i>",
00151 #endif
00152 "<u>", "</u>",
00153 "<sup>", "</sup>",
00154 "<sub>", "</sub>",
00155 "<tt>", "</tt>",
00156 "<a href=\"", "</a>", "\">", FALSE,
00157 "<img src=\"", "\" />", "\" alt=\"", FALSE, TRUE,
00158 NULL,
00159 NULL, NULL,
00160 NMEEncodeCharFunDict, (void *)NMEXMLCharDict,
00161 NMEEncodeCharFunDict, (void *)NMEXMLCharDict,
00162 70, NULL, NULL,
00163 NULL, NULL,
00164 NULL, NULL, NULL, NULL,
00165 NULL,
00166 NULL,
00167 NULL, NULL
00168 };
00169
00171 static NMEOutputFormat const NMEOutputFormatMediawiki =
00172 {
00173 " ",
00174 0,
00175 10,
00176 '%',
00177 "", "",
00178 6,
00179 "%%{l}=%%", "%%{l}=%%\n",
00180 "", "\n\n",
00181 "<br>",
00182 "", "\n",
00183 " ", "\n",
00184 FALSE,
00185 "", "%%{l=1}\n%%",
00186 "%%{l}*%% ", "\n",
00187 "", "%%{l=1}\n%%",
00188 "%%{l}#%% ", "\n",
00189 "", "%%{l=1}\n%%",
00190 "%%{l};%% ", "\n",
00191 NULL,
00192 ": ", "\n",
00193 "", "%%{l=1}\n%%",
00194 "%%{l}:%% ", "\n",
00195 "{|\n", "|}\n",
00196 "|-\n", "",
00197 "!", "\n",
00198 "|", "\n",
00199 "----\n\n",
00200 "'''", "'''",
00201 "''", "''",
00202 "__", "__",
00203 "<sup>", "</sup>",
00204 "<sub>", "</sub>",
00205 "<code>", "</code>",
00206 "[", "]", "|", FALSE,
00207 "[[Image:", "]]", "|", FALSE, TRUE,
00208 NULL,
00209 NULL, NULL,
00210 NULL, NULL,
00211 NULL, NULL,
00212 65, NULL, NULL,
00213 NULL, NULL,
00214 NULL, NULL, NULL, NULL,
00215 NULL,
00216 NULL,
00217 NULL, NULL
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 " ",
00238 0,
00239 10,
00240 '?',
00241 "", "",
00242 3,
00243 "??{4-l}!?? ", "\n",
00244 "", "\n\n",
00245 "\\\\",
00246 "{{{\n", "}}}\n\n",
00247 "", "\n",
00248 FALSE,
00249 "", "??{l=1}\n??",
00250 "??{l}*?? ", "\n",
00251 "", "??{l=1}\n??",
00252 "??{l}#?? ", "\n",
00253 "", "??{l=1}\n??",
00254 "%%(text-indent:?{20*l}px)\n__", ":__ ",
00255 "%%(text-indent:?{20*l}px)\n",
00256 "", "\n%%\n\n",
00257 "", "",
00258 "%%(text-indent:?{20*l}px)\n", "\n%%\n\n",
00259 "", "\n",
00260 "", "\n",
00261 "|| ", "",
00262 "| ", "",
00263 "----\n\n",
00264 "__", "__",
00265 "''", "''",
00266 "%%(text-decoration:underline)", "%%",
00267 "%%(vertical-align:sup)", "%%",
00268 "%%(vertical-align:sub)", "%%",
00269 "{{", "}}",
00270 "[", "]", "|", TRUE,
00271 "", "", NULL, FALSE, FALSE,
00272 NULL,
00273 NULL, NULL,
00274 NMEEncodeCharFunDict, (void *)jspwikiCharDict,
00275 NULL, NULL,
00276 65, NULL, NULL,
00277 NULL, NULL,
00278 NULL, NULL, NULL, NULL,
00279 NULL,
00280 NULL,
00281 NULL, NULL
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
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
00541 for (i = 0; i < linkLen && isAlphaNum(link[i]); i++)
00542 ;
00543 if (i < linkLen && link[i] == ':')
00544 {
00545
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)
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;
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;
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;
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:
00894 printf("%.*s", destLen, dest);
00895 break;
00896 case 1:
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:
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 }