=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.32 retrieving revision 1.41 diff -u -p -r1.32 -r1.41 --- docbook2mdoc/docbook2mdoc.c 2014/04/30 09:04:40 1.32 +++ docbook2mdoc/docbook2mdoc.c 2014/10/12 15:34:44 1.41 @@ -1,4 +1,4 @@ -/* $Id: docbook2mdoc.c,v 1.32 2014/04/30 09:04:40 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.41 2014/10/12 15:34:44 kristaps Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * @@ -64,6 +64,7 @@ struct pattr { struct pnode { enum nodeid node; /* node type */ char *b; /* binary data buffer */ + char *real; /* store for "b" */ size_t bsz; /* data buffer size */ struct pnode *parent; /* parent (or NULL if top) */ struct pnodeq childq; /* queue of children */ @@ -73,7 +74,9 @@ struct pnode { static const char *attrkeys[ATTRKEY__MAX] = { "choice", + "close", "id", + "open", "rep" }; @@ -95,6 +98,7 @@ static const struct node nodes[NODE__MAX] = { { "citerefentry", NODE_IGNTEXT }, { "cmdsynopsis", NODE_IGNTEXT }, { "code", 0 }, + { "colspec", NODE_IGNTEXT }, { "command", 0 }, { "constant", 0 }, { "copyright", NODE_IGNTEXT }, @@ -102,6 +106,7 @@ static const struct node nodes[NODE__MAX] = { { "emphasis", 0 }, { "entry", 0 }, { "envar", 0 }, + { "fieldsynopsis", NODE_IGNTEXT }, { "filename", 0 }, { "funcdef", 0 }, { "funcprototype", NODE_IGNTEXT }, @@ -111,11 +116,24 @@ static const struct node nodes[NODE__MAX] = { { "group", NODE_IGNTEXT }, { "holder", NODE_IGNTEXT }, { "info", NODE_IGNTEXT }, + { "informalequation", NODE_IGNTEXT }, + { "informaltable", NODE_IGNTEXT }, + { "inlineequation", NODE_IGNTEXT }, { "itemizedlist", NODE_IGNTEXT }, { "link", 0 }, { "listitem", NODE_IGNTEXT }, { "literal", 0 }, { "manvolnum", 0 }, + { "mml:math", NODE_IGNTEXT }, + { "mml:mfenced", 0 }, + { "mml:mfrac", 0 }, + { "mml:mi", 0 }, + { "mml:mn", 0 }, + { "mml:mo", 0 }, + { "mml:mrow", 0 }, + { "mml:msub", 0 }, + { "mml:msup", 0 }, + { "modifier", 0 }, { "note", NODE_IGNTEXT }, { "option", 0 }, { "orderedlist", NODE_IGNTEXT }, @@ -131,6 +149,7 @@ static const struct node nodes[NODE__MAX] = { { "refentryinfo", NODE_IGNTEXT }, { "refentrytitle", 0 }, { "refmeta", NODE_IGNTEXT }, + { "refmetainfo", NODE_IGNTEXT }, { "refmiscinfo", NODE_IGNTEXT }, { "refname", 0 }, { "refnamediv", NODE_IGNTEXT }, @@ -156,6 +175,8 @@ static const struct node nodes[NODE__MAX] = { { "thead", NODE_IGNTEXT }, { "tip", NODE_IGNTEXT }, { "title", 0 }, + { "trademark", 0 }, + { "type", 0 }, { "ulink", 0 }, { "userinput", 0 }, { "variablelist", NODE_IGNTEXT }, @@ -166,6 +187,8 @@ static const struct node nodes[NODE__MAX] = { { "year", NODE_IGNTEXT }, }; +static int warn = 0; + static void pnode_print(struct parse *p, struct pnode *pn); @@ -231,6 +254,7 @@ xml_char(void *arg, const XML_Char *p, int sz) } memcpy(ps->cur->b + ps->cur->bsz, p, sz); ps->cur->bsz += (size_t)sz; + ps->cur->real = ps->cur->b; } static void @@ -268,7 +292,8 @@ xml_elem_start(void *arg, const XML_Char *name, const struct pattr *pattr; const XML_Char **att; - if (ps->stop) + /* FIXME: find a better way to ditch other namespaces. */ + if (ps->stop || 0 == strcmp(name, "xi:include")) return; /* Close out text node, if applicable... */ @@ -280,6 +305,7 @@ xml_elem_start(void *arg, const XML_Char *name, const ps->node = ps->cur->node; } + for (node = 0; node < NODE__MAX; node++) if (NULL == nodes[node].name) continue; @@ -340,32 +366,35 @@ xml_elem_start(void *arg, const XML_Char *name, const if (0 == strcmp(*att, attrkeys[key])) break; if (ATTRKEY__MAX == key) { - fprintf(stderr, "%s:%zu:%zu: warning: " - "unknown attribute \"%s\"\n", - ps->fname, - XML_GetCurrentLineNumber(ps->xml), - XML_GetCurrentColumnNumber(ps->xml), - *att); + if (warn) + fprintf(stderr, "%s:%zu:%zu: warning: " + "unknown attribute \"%s\"\n", + ps->fname, + XML_GetCurrentLineNumber(ps->xml), + XML_GetCurrentColumnNumber(ps->xml), + *att); continue; } else if ( ! isattrkey(node, key)) { - fprintf(stderr, "%s:%zu:%zu: warning: " - "bad attribute \"%s\"\n", - ps->fname, - XML_GetCurrentLineNumber(ps->xml), - XML_GetCurrentColumnNumber(ps->xml), - *att); + if (warn) + fprintf(stderr, "%s:%zu:%zu: warning: " + "bad attribute \"%s\"\n", + ps->fname, + XML_GetCurrentLineNumber(ps->xml), + XML_GetCurrentColumnNumber(ps->xml), + *att); continue; } for (val = 0; val < ATTRVAL__MAX; val++) if (0 == strcmp(*(att + 1), attrvals[val])) break; if (ATTRVAL__MAX != val && ! isattrval(key, val)) { - fprintf(stderr, "%s:%zu:%zu: warning: " - "bad attribute value \"%s\"\n", - ps->fname, - XML_GetCurrentLineNumber(ps->xml), - XML_GetCurrentColumnNumber(ps->xml), - *(att + 1)); + if (warn) + fprintf(stderr, "%s:%zu:%zu: warning: " + "bad attribute value \"%s\"\n", + ps->fname, + XML_GetCurrentLineNumber(ps->xml), + XML_GetCurrentColumnNumber(ps->xml), + *(att + 1)); continue; } pattr = calloc(1, sizeof(struct pattr)); @@ -388,8 +417,11 @@ xml_elem_end(void *arg, const XML_Char *name) { struct parse *ps = arg; + /* FIXME: find a better way to ditch other namespaces. */ if (ps->stop || NODE_ROOT == ps->node) return; + else if (0 == strcmp(name, "xi:include")) + return; /* Close out text node, if applicable... */ if (NODE_TEXT == ps->node) { @@ -429,7 +461,7 @@ pnode_free(struct pnode *pn) free(ap); } - free(pn->b); + free(pn->real); free(pn); } @@ -613,6 +645,41 @@ pnode_printmclose(struct parse *p, int sv) } /* + * Like pnode_printmclose() except we look to the next node, and, if + * found, see if it starts with punctuation. + * If it does, then we print that punctuation before the newline. + */ +static void +pnode_printmclosepunct(struct parse *p, struct pnode *pn, int sv) +{ + /* We wouldn't have done anything anyway. */ + if ( ! (sv && ! p->newln)) + return; + + /* No next node or it's not text. */ + if (NULL == (pn = TAILQ_NEXT(pn, child))) { + pnode_printmclose(p, sv); + return; + } else if (NODE_TEXT != pn->node) { + pnode_printmclose(p, sv); + return; + } + + /* Only do this for the comma/period. */ + if (pn->bsz > 0 && + (',' == pn->b[0] || '.' == pn->b[0]) && + (1 == pn->bsz || isspace((int)pn->b[1]))) { + putchar(' '); + putchar(pn->b[0]); + pn->b++; + pn->bsz--; + } + + putchar('\n'); + p->newln = 1; +} + +/* * If the SYNOPSIS macro has a superfluous title, kill it. */ static void @@ -692,16 +759,12 @@ pnode_printciterefentry(struct parse *p, struct pnode struct pnode *pp, *title, *manvol; title = manvol = NULL; - assert(p->newln); TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_MANVOLNUM == pp->node) manvol = pp; else if (NODE_REFENTRYTITLE == pp->node) title = pp; - fputs(".Xr", stdout); - p->newln = 0; - if (NULL != title) { pnode_printmacrolinepart(p, title); } else @@ -711,7 +774,7 @@ pnode_printciterefentry(struct parse *p, struct pnode puts(" 1"); p->newln = 1; } else - pnode_printmacroline(p, manvol); + pnode_printmacrolinepart(p, manvol); } static void @@ -801,7 +864,76 @@ pnode_printparamdef(struct parse *p, struct pnode *pn) p->newln = 1; } +/* + * The node is a little peculiar. + * First, it can have arbitrary open and closing tokens, which default + * to parentheses. + * Second, >1 arguments are separated by commas. + */ static void +pnode_printmathfenced(struct parse *p, struct pnode *pn) +{ + struct pnode *pp; + struct pattr *ap; + + TAILQ_FOREACH(ap, &pn->attrq, child) + if (ATTRKEY_OPEN == ap->key) { + printf("left %s ", ap->rawval); + break; + } + if (NULL == ap) + printf("left ( "); + + pp = TAILQ_FIRST(&pn->childq); + pnode_print(p, pp); + + while (NULL != (pp = TAILQ_NEXT(pp, child))) { + putchar(','); + pnode_print(p, pp); + } + + TAILQ_FOREACH(ap, &pn->attrq, child) + if (ATTRKEY_CLOSE == ap->key) { + printf("right %s ", ap->rawval); + break; + } + if (NULL == ap) + printf("right ) "); +} + +/* + * These math nodes require special handling because they have infix + * syntax, instead of the usual prefix or prefix. + * So we need to break up the first and second child node with a + * particular eqn(7) word. + */ +static void +pnode_printmath(struct parse *p, struct pnode *pn) +{ + struct pnode *pp; + + pp = TAILQ_FIRST(&pn->childq); + pnode_print(p, pp); + + switch (pn->node) { + case (NODE_MML_MSUP): + printf(" sup "); + break; + case (NODE_MML_MFRAC): + printf(" over "); + break; + case (NODE_MML_MSUB): + printf(" sub "); + break; + default: + break; + } + + pp = TAILQ_NEXT(pp, child); + pnode_print(p, pp); +} + +static void pnode_printfuncprototype(struct parse *p, struct pnode *pn) { struct pnode *pp, *fdef; @@ -944,6 +1076,7 @@ pnode_printvarlistentry(struct parse *p, struct pnode assert(p->newln); TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_TERM == pp->node) { + assert(p->newln); fputs(".It", stdout); p->newln = 0; pnode_print(p, pp); @@ -1085,7 +1218,8 @@ pnode_print(struct parse *p, struct pnode *pn) pnode_unlinksub(pn); break; case (NODE_CITEREFENTRY): - assert(p->newln); + pnode_printmopen(p); + fputs("Xr", stdout); pnode_printciterefentry(p, pn); pnode_unlinksub(pn); break; @@ -1097,6 +1231,10 @@ pnode_print(struct parse *p, struct pnode *pn) pnode_printmopen(p); fputs("Nm", stdout); break; + case (NODE_CONSTANT): + pnode_printmopen(p); + fputs("Dv", stdout); + break; case (NODE_EMPHASIS): pnode_printmopen(p); fputs("Em", stdout); @@ -1135,6 +1273,28 @@ pnode_print(struct parse *p, struct pnode *pn) pnode_printmopen(p); fputs("Li", stdout); break; + case (NODE_MML_MATH): + if ( ! p->newln) + putchar('\n'); + puts(".EQ"); + p->newln = 0; + break; + case (NODE_MML_MFENCED): + pnode_printmathfenced(p, pn); + pnode_unlinksub(pn); + break; + case (NODE_MML_MROW): + case (NODE_MML_MI): + case (NODE_MML_MN): + case (NODE_MML_MO): + putchar('{'); + break; + case (NODE_MML_MFRAC): + case (NODE_MML_MSUB): + case (NODE_MML_MSUP): + pnode_printmath(p, pn); + pnode_unlinksub(pn); + break; case (NODE_OPTION): pnode_printmopen(p); fputs("Fl", stdout); @@ -1233,6 +1393,8 @@ pnode_print(struct parse *p, struct pnode *pn) fputs("Vt", stdout); break; case (NODE_TABLE): + /* FALLTHROUGH */ + case (NODE_INFORMALTABLE): assert(p->newln); pnode_printtable(p, pn); pnode_unlinksub(pn); @@ -1240,8 +1402,15 @@ pnode_print(struct parse *p, struct pnode *pn) case (NODE_TEXT): if (0 == p->newln) putchar(' '); + bufclear(p); bufappend(p, pn); + + if (0 == p->bsz) { + assert(pn->real != pn->b); + break; + } + /* * Output all characters, squeezing out whitespace * between newlines. @@ -1250,6 +1419,7 @@ pnode_print(struct parse *p, struct pnode *pn) */ assert(p->bsz); cp = p->b; + /* * There's often a superfluous "-" in its