=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.35 retrieving revision 1.37 diff -u -p -r1.35 -r1.37 --- docbook2mdoc/docbook2mdoc.c 2014/04/30 09:43:55 1.35 +++ docbook2mdoc/docbook2mdoc.c 2014/04/30 12:34:44 1.37 @@ -1,4 +1,4 @@ -/* $Id: docbook2mdoc.c,v 1.35 2014/04/30 09:43:55 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.37 2014/04/30 12: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 */ @@ -233,6 +234,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 @@ -270,7 +272,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... */ @@ -282,6 +285,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; @@ -390,8 +394,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) { @@ -431,7 +438,7 @@ pnode_free(struct pnode *pn) free(ap); } - free(pn->b); + free(pn->real); free(pn); } @@ -615,6 +622,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 @@ -1246,8 +1288,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. @@ -1256,6 +1305,7 @@ pnode_print(struct parse *p, struct pnode *pn) */ assert(p->bsz); cp = p->b; + /* * There's often a superfluous "-" in its