=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.102 retrieving revision 1.103 diff -u -p -r1.102 -r1.103 --- docbook2mdoc/docbook2mdoc.c 2019/04/10 14:37:32 1.102 +++ docbook2mdoc/docbook2mdoc.c 2019/04/11 04:23:22 1.103 @@ -1,4 +1,4 @@ -/* $Id: docbook2mdoc.c,v 1.102 2019/04/10 14:37:32 schwarze Exp $ */ +/* $Id: docbook2mdoc.c,v 1.103 2019/04/11 04:23:22 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * Copyright (c) 2019 Ingo Schwarze @@ -19,6 +19,7 @@ #include #include #include +#include #include "node.h" #include "macro.h" @@ -36,12 +37,32 @@ pnode_printtext(struct format *f, struct pnode *n) { struct pnode *nn; char *cp; + int accept_arg; char last; - if (n->bsz == 0) { - assert(n->real < n->b); - return; + cp = n->b; + accept_arg = f->flags & FMT_ARG; + if (f->linestate == LINE_MACRO && n->spc == 0 && !accept_arg) { + 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 @@ -53,16 +74,29 @@ pnode_printtext(struct format *f, struct pnode *n) (nn = TAILQ_NEXT(n, child)) != NULL && nn->spc == 0 && (nn->node != NODE_TEXT && nn->node != NODE_ESCAPE)) { macro_open(f, "Pf"); + accept_arg = 1; + f->flags |= FMT_CHILD; nn->spc = 1; } - if (f->linestate == LINE_NEW) { - last = '\n'; - f->linestate = LINE_TEXT; - } else { + switch (f->linestate) { + case LINE_TEXT: + if (n->spc) + putchar(' '); last = ' '; - if (n->spc || f->linestate == LINE_MACRO) + break; + case LINE_MACRO: + if (accept_arg) { putchar(' '); + last = ' '; + break; + } + macro_close(f); + /* FALLTHROUGH */ + case LINE_NEW: + f->linestate = LINE_TEXT; + last = '\n'; + break; } if (n->node == NODE_ESCAPE) { @@ -75,7 +109,6 @@ pnode_printtext(struct format *f, struct pnode *n) * because the arguments of .Fl macros do not need it. */ - cp = n->b; if (n->parent != NULL && n->parent->node == NODE_OPTION && *cp == '-') cp++; @@ -109,6 +142,8 @@ pnode_printpara(struct format *f, struct pnode *n) if ((np = TAILQ_PREV(n, pnodeq, child)) == NULL) np = n->parent; + f->flags = 0; + switch (np->node) { case NODE_ENTRY: case NODE_GLOSSTERM: @@ -387,10 +422,10 @@ pnode_printarg(struct format *f, struct pnode *n) { struct pnode *nc; struct pattr *a; - int isop, isrep; + int isop, isrep, was_impl; isop = 1; - isrep = 0; + isrep = was_impl = 0; TAILQ_FOREACH(a, &n->attrq, child) { if (a->key == ATTRKEY_CHOICE && (a->val == ATTRVAL_PLAIN || a->val == ATTRVAL_REQ)) @@ -398,8 +433,15 @@ pnode_printarg(struct format *f, struct pnode *n) else if (a->key == ATTRKEY_REP && a->val == ATTRVAL_REPEAT) isrep = 1; } - if (isop) - macro_open(f, "Op"); + if (isop) { + if (f->flags & FMT_IMPL) { + was_impl = 1; + macro_open(f, "Oo"); + } else { + macro_open(f, "Op"); + f->flags |= FMT_IMPL; + } + } TAILQ_FOREACH(nc, &n->childq, child) { if (nc->node == NODE_TEXT) @@ -408,6 +450,12 @@ pnode_printarg(struct format *f, struct pnode *n) if (isrep && nc->node == NODE_TEXT) macro_addarg(f, "...", ARG_SPACE); } + if (isop) { + if (was_impl) + macro_open(f, "Oc"); + else + f->flags &= ~FMT_IMPL; + } pnode_unlinksub(n); } @@ -436,6 +484,7 @@ pnode_printgroup(struct format *f, struct pnode *n) macro_open(f, "Op"); else if (sv) macro_open(f, "No"); + f->flags |= FMT_IMPL; /* * Keep on printing text separated by the vertical bar as long @@ -459,6 +508,7 @@ pnode_printgroup(struct format *f, struct pnode *n) } if (sv) macro_close(f); + f->flags &= ~FMT_IMPL; pnode_unlinksub(n); } @@ -516,6 +566,7 @@ pnode_printauthor(struct format *f, struct pnode *n) */ if ((nc = pnode_findfirst(n, NODE_EMAIL)) != NULL) { + f->flags |= FMT_CHILD; pnode_print(f, nc); pnode_unlink(nc); } @@ -547,8 +598,10 @@ pnode_printlink(struct format *f, struct pnode *n) if (text != NULL) print_text(f, text, ARG_SPACE); } - if (text != NULL) + if (text != NULL) { macro_open(f, "Pq"); + f->flags |= FMT_CHILD; + } macro_open(f, "Sx"); macro_addarg(f, uri, ARG_SPACE); pnode_unlinksub(n); @@ -606,6 +659,7 @@ pnode_printvarlistentry(struct format *f, struct pnode macro_close(f); macro_open(f, "It"); + f->flags |= FMT_IMPL; TAILQ_FOREACH(nc, &n->childq, child) { if (nc->node != NODE_TERM && nc->node != NODE_GLOSSTERM) continue; @@ -673,8 +727,8 @@ pnode_printtgroup2(struct format *f, struct pnode *n) while ((nr = pnode_findfirst(n, NODE_ROW)) != NULL) { if ((ne = pnode_findfirst(n, NODE_ENTRY)) == NULL) break; - macro_close(f); macro_open(f, "It"); + f->flags |= FMT_IMPL; pnode_print(f, ne); macro_close(f); pnode_unlink(ne); @@ -758,8 +812,11 @@ pnode_print(struct format *f, struct pnode *n) if (n == NULL) return; - f->spc = n->spc; sv = f->linestate; + if (n->spc) + f->flags &= ~FMT_NOSPC; + else + f->flags |= FMT_NOSPC; switch (n->node) { case NODE_APPLICATION: @@ -961,6 +1018,11 @@ pnode_print(struct format *f, struct pnode *n) pnode_print(f, nc); switch (n->node) { + case NODE_ESCAPE: + case NODE_TERM: + case NODE_TEXT: + /* Accept more arguments to the previous macro. */ + return; case NODE_INFORMALEQUATION: macro_line(f, "EN"); break; @@ -994,36 +1056,6 @@ pnode_print(struct format *f, struct pnode *n) break; fputs(" } ", stdout); break; - case NODE_APPLICATION: - case NODE_ARG: - case NODE_AUTHOR: - case NODE_CITEREFENTRY: - case NODE_CITETITLE: - case NODE_COMMAND: - case NODE_CONSTANT: - case NODE_EDITOR: - case NODE_EMAIL: - case NODE_EMPHASIS: - case NODE_ENVAR: - case NODE_ERRORNAME: - case NODE_FILENAME: - case NODE_FIRSTTERM: - case NODE_FUNCTION: - case NODE_FUNCSYNOPSISINFO: - case NODE_KEYSYM: - case NODE_LINK: - case NODE_LITERAL: - case NODE_OPTION: - case NODE_PARAMETER: - case NODE_REPLACEABLE: - case NODE_REFPURPOSE: - case NODE_SGMLTAG: - case NODE_TYPE: - case NODE_VARNAME: - if (sv != LINE_MACRO && f->linestate == LINE_MACRO && - (n->parent == NULL || n->parent->node != NODE_MEMBER)) - macro_closepunct(f, n); - break; case NODE_QUOTE: if (sv == LINE_NEW) macro_close(f); @@ -1071,6 +1103,7 @@ pnode_print(struct format *f, struct pnode *n) default: break; } + f->flags &= ~FMT_ARG; } void