=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.89 retrieving revision 1.142 diff -u -p -r1.89 -r1.142 --- docbook2mdoc/docbook2mdoc.c 2019/04/03 16:52:51 1.89 +++ docbook2mdoc/docbook2mdoc.c 2019/04/28 19:05:11 1.142 @@ -1,4 +1,4 @@ -/* $Id: docbook2mdoc.c,v 1.89 2019/04/03 16:52:51 schwarze Exp $ */ +/* $Id: docbook2mdoc.c,v 1.142 2019/04/28 19:05:11 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * Copyright (c) 2019 Ingo Schwarze @@ -19,7 +19,9 @@ #include #include #include +#include +#include "xmalloc.h" #include "node.h" #include "macro.h" #include "format.h" @@ -29,188 +31,260 @@ */ static void pnode_print(struct format *, struct pnode *); +static void pnode_printrefentry(struct format *, struct pnode *); static void -pnode_printpara(struct format *p, struct pnode *pn) +pnode_printtext(struct format *f, struct pnode *n) { - struct pnode *pp; + struct pnode *nn; + char *cp; + int accept_arg; - if ((pp = TAILQ_PREV(pn, pnodeq, child)) == NULL && - (pp = pn->parent) == NULL) - return; + cp = n->b; + accept_arg = f->flags & FMT_ARG; + if (f->linestate == LINE_MACRO && !accept_arg && + (n->flags & NFLAG_SPC) == 0) { + for (;;) { + if (*cp == '\0') + return; + if (strchr("!),.:;?]", *cp) == NULL) + break; + printf(" %c", *cp++); + } + if (isspace((unsigned char)*cp)) { + while (isspace((unsigned char)*cp)) + cp++; + macro_close(f); + } else { + fputs(" Ns", stdout); + f->flags &= FMT_IMPL; + accept_arg = 1; + } + } + if (f->linestate == LINE_MACRO && !accept_arg && + (f->flags & (FMT_CHILD | FMT_IMPL)) == 0) + macro_close(f); - switch (pp->node) { - case NODE_ENTRY: - case NODE_LISTITEM: - return; - case NODE_PREFACE: - case NODE_SECTION: - if (p->level < 3) - return; + /* + * Text preceding a macro without intervening whitespace + * requires a .Pf macro. + * Set the spacing flag to avoid a redundant .Ns macro. + */ + + if (f->linestate != LINE_MACRO && + (nn = TAILQ_NEXT(n, child)) != NULL && + (nn->flags & NFLAG_SPC) == 0) { + switch (pnode_class(nn->node)) { + case CLASS_LINE: + case CLASS_ENCL: + macro_open(f, "Pf"); + accept_arg = 1; + f->flags |= FMT_CHILD; + nn->flags |= NFLAG_SPC; + break; + default: + break; + } + } + + switch (f->linestate) { + case LINE_NEW: break; - default: + case LINE_TEXT: + if (n->flags & NFLAG_SPC) { + if (n->flags & NFLAG_LINE && + pnode_class(n->node) == CLASS_TEXT) + macro_close(f); + else + putchar(' '); + } break; + case LINE_MACRO: + if (accept_arg == 0) + macro_close(f); + else if (n->flags & NFLAG_SPC || + (f->flags & FMT_ARG) == 0 || + (nn = TAILQ_PREV(n, pnodeq, child)) == NULL || + pnode_class(nn->node) != CLASS_TEXT) + putchar(' '); + break; } - macro_line(p, "Pp"); + + if (n->node == NODE_ESCAPE) { + fputs(n->b, stdout); + if (f->linestate == LINE_NEW) + f->linestate = LINE_TEXT; + return; + } + + /* + * Remove the prefix '-' from