=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.40 retrieving revision 1.43 diff -u -p -r1.40 -r1.43 --- docbook2mdoc/docbook2mdoc.c 2014/10/12 15:08:45 1.40 +++ docbook2mdoc/docbook2mdoc.c 2014/10/19 19:11:29 1.43 @@ -1,4 +1,4 @@ -/* $Id: docbook2mdoc.c,v 1.40 2014/10/12 15:08:45 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.43 2014/10/19 19:11:29 kristaps Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * @@ -37,6 +37,8 @@ struct parse { enum nodeid node; /* current (NODE_ROOT if pre-tree) */ const char *fname; /* filename */ int stop; /* should we stop now? */ +#define PARSE_EQN 1 + unsigned int flags; /* document-wide flags */ struct pnode *root; /* root of parse tree */ struct pnode *cur; /* current node in tree */ char *b; /* nil-terminated buffer for pre-print */ @@ -74,7 +76,9 @@ struct pnode { static const char *attrkeys[ATTRKEY__MAX] = { "choice", + "close", "id", + "open", "rep" }; @@ -303,7 +307,6 @@ 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; @@ -339,6 +342,9 @@ xml_elem_start(void *arg, const XML_Char *name, const return; } + if (NODE_INLINEEQUATION == node) + ps->flags |= PARSE_EQN; + if (NULL == (dat = calloc(1, sizeof(struct pnode)))) { perror(NULL); exit(EXIT_FAILURE); @@ -863,6 +869,43 @@ pnode_printparamdef(struct parse *p, struct pnode *pn) } /* + * 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 @@ -878,13 +921,13 @@ pnode_printmath(struct parse *p, struct pnode *pn) switch (pn->node) { case (NODE_MML_MSUP): - printf(" sup "); + fputs(" sup ", stdout); break; case (NODE_MML_MFRAC): - printf(" over "); + fputs(" over ", stdout); break; case (NODE_MML_MSUB): - printf(" sub "); + fputs(" sub ", stdout); break; default: break; @@ -1027,26 +1070,39 @@ pnode_printprologue(struct parse *p, struct pnode *pn) puts(".Dt UNKNOWN 1"); puts(".Os"); } + + if (PARSE_EQN & p->flags) { + puts(".EQ"); + puts("delim $$"); + puts(".EN"); + } } +/* + * We can have multiple elements within a , which + * we should comma-separate as list headers. + */ static void pnode_printvarlistentry(struct parse *p, struct pnode *pn) { struct pnode *pp; + int first = 1; assert(p->newln); + fputs(".It", stdout); + p->newln = 0; + TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_TERM == pp->node) { - assert(p->newln); - fputs(".It", stdout); - p->newln = 0; + if ( ! first) + putchar(','); pnode_print(p, pp); pnode_unlink(pp); - pnode_printmclose(p, 1); - return; - } + first = 0; + } else + break; - puts(".It"); + putchar('\n'); p->newln = 1; } @@ -1221,6 +1277,16 @@ pnode_print(struct parse *p, struct pnode *pn) pnode_printmopen(p); fputs("Fd", stdout); break; + case (NODE_INFORMALEQUATION): + if ( ! p->newln) + putchar('\n'); + puts(".EQ"); + p->newln = 0; + break; + case (NODE_INLINEEQUATION): + fputc('$', stdout); + p->newln = 0; + break; case (NODE_ITEMIZEDLIST): assert(p->newln); pnode_printlist(p, pn); @@ -1234,20 +1300,17 @@ 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): - printf("left {"); + 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('{'); + if (TAILQ_EMPTY(&pn->childq)) + break; + fputs(" { ", stdout); break; case (NODE_MML_MFRAC): case (NODE_MML_MSUB): @@ -1435,20 +1498,23 @@ pnode_print(struct parse *p, struct pnode *pn) pnode_print(p, pp); switch (pn->node) { - case (NODE_MML_MATH): + case (NODE_INFORMALEQUATION): if ( ! p->newln) putchar('\n'); puts(".EN"); p->newln = 1; break; - case (NODE_MML_MFENCED): - printf("right }"); + case (NODE_INLINEEQUATION): + fputs("$ ", stdout); + p->newln = sv; break; case (NODE_MML_MROW): case (NODE_MML_MI): case (NODE_MML_MN): case (NODE_MML_MO): - putchar('}'); + if (TAILQ_EMPTY(&pn->childq)) + break; + fputs(" } ", stdout); break; case (NODE_APPLICATION): case (NODE_ARG):