=================================================================== RCS file: /cvs/texi2mdoc/main.c,v retrieving revision 1.17 retrieving revision 1.18 diff -u -p -r1.17 -r1.18 --- texi2mdoc/main.c 2015/02/19 10:29:17 1.17 +++ texi2mdoc/main.c 2015/02/19 15:34:20 1.18 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.17 2015/02/19 10:29:17 kristaps Exp $ */ +/* $Id: main.c,v 1.18 2015/02/19 15:34:20 kristaps Exp $ */ /* * Copyright (c) 2015 Kristaps Dzonsons * @@ -56,6 +56,7 @@ enum texicmd { TEXICMD_CITE, TEXICMD_CODE, TEXICMD_COLON, + TEXICMD_COLUMNFRACTIONS, TEXICMD_COMMAND, TEXICMD_COMMENT, TEXICMD_COMMENT_LONG, @@ -102,6 +103,7 @@ enum texicmd { TEXICMD_GROUP, TEXICMD_HEADING, TEXICMD_HEADINGS, + TEXICMD_HEADITEM, TEXICMD_HYPHEN, TEXICMD_I, TEXICMD_IFCLEAR, @@ -125,10 +127,12 @@ enum texicmd { TEXICMD_INSERTCOPYING, TEXICMD_ITEM, TEXICMD_ITEMIZE, + TEXICMD_KEY, TEXICMD_KBD, TEXICMD_LATEX, TEXICMD_MATH, TEXICMD_MENU, + TEXICMD_MULTITABLE, TEXICMD_NEED, TEXICMD_NEWLINE, TEXICMD_NODE, @@ -165,6 +169,7 @@ enum texicmd { TEXICMD_SUBTITLE, TEXICMD_T, TEXICMD_TAB, + TEXICMD_TABSYM, TEXICMD_TABLE, TEXICMD_TEX, TEXICMD_TEXSYM, @@ -180,6 +185,7 @@ enum texicmd { TEXICMD_URL, TEXICMD_VAR, TEXICMD_VERBATIMINCLUDE, + TEXICMD_VINDEX, TEXICMD_VSKIP, TEXICMD_W, TEXICMD_XREF, @@ -219,6 +225,7 @@ enum texilist { TEXILIST_NONE = 0, TEXILIST_ITEM, TEXILIST_NOITEM, + TEXILIST_TABLE }; /* @@ -270,6 +277,7 @@ static void doitem(struct texi *, enum texicmd, const static void doitemize(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dolink(struct texi *, enum texicmd, const char *, size_t, size_t *); static void domath(struct texi *, enum texicmd, const char *, size_t, size_t *); +static void domultitable(struct texi *, enum texicmd, const char *, size_t, size_t *); static void doquotation(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dotable(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dotop(struct texi *, enum texicmd, const char *, size_t, size_t *); @@ -277,6 +285,7 @@ static void dosection(struct texi *, enum texicmd, con static void dosp(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dosubsection(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dosymbol(struct texi *, enum texicmd, const char *, size_t, size_t *); +static void dotab(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dotitle(struct texi *, enum texicmd, const char *, size_t, size_t *); static void doverbinclude(struct texi *, enum texicmd, const char *, size_t, size_t *); @@ -301,6 +310,7 @@ static const struct texitok texitoks[TEXICMD__MAX] = { { dofont, "code", 4 }, /* TEXICMD_CODE */ { dofont, "cite", 4 }, /* TEXICMD_CITE */ { dosymbol, ":", 1 }, /* TEXICMD_COLON */ + { NULL, "columnfractions", 15 }, /* TEXICMD_COLUMNFRACTIONS */ { doinline, "command", 7 }, /* TEXICMD_COMMAND */ { doignline, "c", 1 }, /* TEXICMD_COMMENT */ { doignline, "comment", 7 }, /* TEXICMD_COMMENT_LONG */ @@ -347,6 +357,7 @@ static const struct texitok texitoks[TEXICMD__MAX] = { { doblock, "group", 5 }, /* TEXICMD_GROUP */ { dosection, "heading", 7 }, /* TEXICMD_HEADING */ { doignline, "headings", 8 }, /* TEXICMD_HEADINGS */ + { doitem, "headitem", 8 }, /* TEXICMD_HEADITEM */ { dosymbol, "-", 1 }, /* TEXICMD_HYPHEN */ { dofont, "i", 1 }, /* TEXICMD_I */ { doignblock, "ifclear", 7 }, /* TEXICMD_IFCLEAR */ @@ -370,10 +381,12 @@ static const struct texitok texitoks[TEXICMD__MAX] = { { doignline, "insertcopying", 13 }, /* TEXICMD_INSERTCOPYING */ { doitem, "item", 4 }, /* TEXICMD_ITEM */ { doitemize, "itemize", 7 }, /* TEXICMD_ITEMIZE */ + { dobracket, "key", 3 }, /* TEXICMD_KEY */ { dofont, "kbd", 3 }, /* TEXICMD_KBD */ { dosymbol, "LaTeX", 5 }, /* TEXICMD_LATEX */ { domath, "math", 4 }, /* TEXICMD_MATH */ { doignblock, "menu", 4 }, /* TEXICMD_MENU */ + { domultitable, "multitable", 10 }, /* TEXICMD_MULTITABLE */ { doignline, "need", 4 }, /* TEXICMD_NEED */ { dosymbol, "\n", 1 }, /* TEXICMD_NEWLINE */ { doignline, "node", 4 }, /* TEXICMD_NODE */ @@ -409,7 +422,8 @@ static const struct texitok texitoks[TEXICMD__MAX] = { { dosubsection, "subsection", 10 }, /* TEXICMD_SUBSECTION */ { doignline, "subtitle", 8 }, /* TEXICMD_SUBTITLE */ { dofont, "t", 1 }, /* TEXICMD_T */ - { dosymbol, "\t", 1 }, /* TEXICMD_TAB */ + { dotab, "tab", 3 }, /* TEXICMD_TAB */ + { dosymbol, "\t", 1 }, /* TEXICMD_TABSYM */ { dotable, "table", 5 }, /* TEXICMD_TABLE */ { doignblock, "tex", 3 }, /* TEXICMD_TEX */ { dosymbol, "TeX", 3 }, /* TEXICMD_TEXSYM */ @@ -425,6 +439,7 @@ static const struct texitok texitoks[TEXICMD__MAX] = { { dolink, "url", 3 }, /* TEXICMD_URL */ { doinline, "var", 3 }, /* TEXICMD_VAR */ { doverbinclude, "verbatiminclude", 15 }, /* TEXICMD_VERBATIMINCLUDE */ + { doignline, "vindex", 6 }, /* TEXICMD_VINDEX */ { dosp, "vskip", 5 }, /* TEXICMD_VSKIP */ { dobracket, "w", 1 }, /* TEXICMD_W */ { dolink, "xref", 4 }, /* TEXICMD_XREF */ @@ -523,6 +538,7 @@ texierr(struct texi *p, const char *fmt, ...) /* * Put a single data character to the output if we're not ignoring. * Adjusts our output status. + * This shouldn't be called for macros: just for ordinary text. */ static void texiputchar(struct texi *p, char c) @@ -530,6 +546,10 @@ texiputchar(struct texi *p, char c) if (p->ign) return; + + if ('.' == c && 0 == p->outcol) + fputs("\\&", stdout); + putchar(c); p->seenvs = 0; if ('\n' == c) { @@ -541,6 +561,7 @@ texiputchar(struct texi *p, char c) /* * Put multiple characters (see texiputchar()). + * This shouldn't be called for macros: just for ordinary text. */ static void texiputchars(struct texi *p, const char *s) @@ -558,9 +579,13 @@ static void teximacroclose(struct texi *p) { - /* FIXME: punctuation. */ - if (0 == --p->outmacro) - texiputchar(p, '\n'); + if (p->ign) + return; + + if (0 == --p->outmacro) { + putchar('\n'); + p->outcol = p->seenws = 0; + } } /* @@ -571,15 +596,26 @@ teximacroclose(struct texi *p) static void teximacroopen(struct texi *p, const char *s) { + int rc; - if (p->outcol && 0 == p->outmacro) - texiputchar(p, '\n'); + if (p->ign) + return; + + if (p->outcol && 0 == p->outmacro) { + putchar('\n'); + p->outcol = 0; + } + if (0 == p->outmacro) - texiputchar(p, '.'); + putchar('.'); else - texiputchar(p, ' '); - texiputchars(p, s); - texiputchar(p, ' '); + putchar(' '); + + if (EOF != (rc = fputs(s, stdout))) + p->outcol += rc; + + putchar(' '); + p->outcol++; p->outmacro++; p->seenws = 0; } @@ -591,17 +627,20 @@ static void teximacro(struct texi *p, const char *s) { + if (p->ign) + return; + if (p->outmacro) texierr(p, "\"%s\" in open line scope!?", s); - else if (p->literal) + if (p->literal) texierr(p, "\"%s\" in a literal scope!?", s); if (p->outcol) - texiputchar(p, '\n'); + putchar('\n'); - texiputchar(p, '.'); - texiputchars(p, s); - texiputchar(p, '\n'); + putchar('.'); + puts(s); + p->outcol = p->seenws = 0; } static void @@ -737,7 +776,7 @@ texiword(struct texi *p, const char *buf, size_t sz, size_t *pos, char extra) { - if (0 == p->outmacro && p->outcol > 72 && 0 == p->literal) + if (p->seenws && 0 == p->outmacro && p->outcol > 72 && 0 == p->literal) texiputchar(p, '\n'); /* FIXME: abstract this: we use it elsewhere. */ if (p->seenws && p->outcol && 0 == p->literal) @@ -1437,6 +1476,10 @@ dofont(struct texi *p, enum texicmd cmd, abort(); } + if (p->seenws) { + texiputchar(p, ' '); + p->seenws = 0; + } texiputchars(p, font); parsebracket(p, buf, sz, pos); texiputchars(p, "\\fP"); @@ -1600,7 +1643,7 @@ dosymbol(struct texi *p, enum texicmd cmd, case (TEXICMD_ASTERISK): case (TEXICMD_NEWLINE): case (TEXICMD_SPACE): - case (TEXICMD_TAB): + case (TEXICMD_TABSYM): texiputchar(p, ' '); break; case (TEXICMD_AT): @@ -1817,6 +1860,13 @@ dotop(struct texi *p, enum texicmd cmd, time_t t; char date[32]; + /* + * Here we print our standard mdoc(7) prologue. + * We use the title set with @settitle for the `Nd' description + * and the source document filename (the first one as invoked on + * the command line) for the title. + * The date is set to the current date. + */ t = time(NULL); strftime(date, sizeof(date), "%F", localtime(&t)); @@ -1847,6 +1897,12 @@ doitem(struct texi *p, enum texicmd cmd, const char *buf, size_t sz, size_t *pos) { + /* Multitable is using raw tbl(7). */ + if (TEXILIST_TABLE == p->list) { + texiputchar(p, '\n'); + return; + } + if (p->outmacro) texierr(p, "item in open line scope!?"); else if (p->literal) @@ -1863,14 +1919,89 @@ doitem(struct texi *p, enum texicmd cmd, texivspace(p); break; } - p->seenvs = 1; + /* Trick so we don't start with Pp. */ + p->seenvs = 1; parseeoln(p, buf, sz, pos); if (TEXILIST_ITEM == p->list) teximacroclose(p); else if (p->outcol > 0) texiputchar(p, '\n'); +} + +static void +dotab(struct texi *p, enum texicmd cmd, + const char *buf, size_t sz, size_t *pos) +{ + + /* This command is only useful in @multitable. */ + if (TEXILIST_TABLE == p->list) + texiputchar(p, '\t'); +} + +static void +domultitable(struct texi *p, enum texicmd cmd, + const char *buf, size_t sz, size_t *pos) +{ + enum texilist sv = p->list; + enum texicmd type; + size_t i, end, columns; + + p->list = TEXILIST_TABLE; + teximacro(p, "TS"); + columns = 0; + + /* Advance to the first argument... */ + while (*pos < sz && isws(buf[*pos])) + advance(p, buf, pos); + + /* Make sure we don't print anything when scanning. */ + p->ign++; + if ('@' == buf[*pos]) { + /* + * Look for @columnfractions. + * We ignore these, but we do use the number of + * arguments to set the number of columns that we'll + * have. + */ + type = texicmd(p, buf, *pos, sz, &end); + advanceto(p, buf, pos, end); + if (TEXICMD_COLUMNFRACTIONS != type) + texierr(p, "unknown multitable type"); + while (*pos < sz && '\n' != buf[*pos]) { + while (*pos < sz && isws(buf[*pos])) + advance(p, buf, pos); + while (*pos < sz && ! isws(buf[*pos])) { + if ('\n' == buf[*pos]) + break; + advance(p, buf, pos); + } + columns++; + } + } else + /* + * We have arguments. + * We could parse these, but it's easier to just let + * tbl(7) figure it out. + * So use this only to count arguments. + */ + while (parselinearg(p, buf, sz, pos) > 0) + columns++; + p->ign--; + + /* Left-justify each table entry. */ + for (i = 0; i < columns; i++) { + if (i > 0) + texiputchar(p, ' '); + texiputchar(p, 'l'); + } + texiputchars(p, ".\n"); + p->outmacro++; + parseto(p, buf, sz, pos, texitoks[cmd].tok); + p->outmacro--; + teximacro(p, "TE"); + p->list = sv; } static void