=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.77 retrieving revision 1.144 diff -u -p -r1.77 -r1.144 --- docbook2mdoc/docbook2mdoc.c 2019/03/26 22:39:33 1.77 +++ docbook2mdoc/docbook2mdoc.c 2019/05/01 11:34:19 1.144 @@ -1,4 +1,4 @@ -/* $Id: docbook2mdoc.c,v 1.77 2019/03/26 22:39:33 schwarze Exp $ */ +/* $Id: docbook2mdoc.c,v 1.144 2019/05/01 11:34:19 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" @@ -32,214 +34,257 @@ static void pnode_print(struct format *, struct pnode static void -print_text(struct format *p, const char *word) +pnode_printtext(struct format *f, struct pnode *n) { - switch (p->linestate) { + struct pnode *nn; + char *cp; + int accept_arg; + + 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); + + /* + * 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; case LINE_TEXT: - putchar(' '); + 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: - macro_close(p); + 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; } - fputs(word, stdout); - p->linestate = LINE_TEXT; + + if (n->node == NODE_ESCAPE) { + fputs(n->b, stdout); + if (f->linestate == LINE_NEW) + f->linestate = LINE_TEXT; + return; + } + + /* + * Remove the prefix '-' from