=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.23 retrieving revision 1.141 diff -u -p -r1.23 -r1.141 --- docbook2mdoc/docbook2mdoc.c 2014/03/30 17:46:17 1.23 +++ docbook2mdoc/docbook2mdoc.c 2019/04/28 17:10:06 1.141 @@ -1,6 +1,7 @@ -/* $Id: docbook2mdoc.c,v 1.23 2014/03/30 17:46:17 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.141 2019/04/28 17:10:06 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons + * Copyright (c) 2019 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,900 +15,1052 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include - #include #include -#include -#include -#include #include #include #include -#include -#include "extern.h" +#include "xmalloc.h" +#include "node.h" +#include "macro.h" +#include "format.h" /* - * Global parse state. - * Keep this as simple and small as possible. + * The implementation of the mdoc(7) formatter. */ -struct parse { - XML_Parser xml; - enum nodeid node; /* current (NODE_ROOT if pre-tree) */ - const char *fname; /* filename */ - int stop; /* should we stop now? */ - struct pnode *root; /* root of parse tree */ - struct pnode *cur; /* current node in tree */ - char *b; /* nil-terminated buffer for pre-print */ - size_t bsz; /* current length of b */ - size_t mbsz; /* max bsz allocation */ - int newln; /* output: are we on a fresh line */ -}; -struct node { - const char *name; /* docbook element name */ - unsigned int flags; -#define NODE_IGNTEXT 1 /* ignore all contained text */ -}; +static void pnode_print(struct format *, struct pnode *); +static void pnode_printrefentry(struct format *, struct pnode *); -TAILQ_HEAD(pnodeq, pnode); -TAILQ_HEAD(pattrq, pattr); -struct pattr { - enum attrkey key; - enum attrval val; - char *rawval; - TAILQ_ENTRY(pattr) child; -}; +static void +pnode_printtext(struct format *f, struct pnode *n) +{ + struct pnode *nn; + char *cp; + int accept_arg; -struct pnode { - enum nodeid node; /* node type */ - char *b; /* binary data buffer */ - size_t bsz; /* data buffer size */ - struct pnode *parent; /* parent (or NULL if top) */ - struct pnodeq childq; /* queue of children */ - struct pattrq attrq; /* attributes of node */ - TAILQ_ENTRY(pnode) child; -}; + 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); -static const char *attrkeys[ATTRKEY__MAX] = { - "choice", - "id", - "rep" -}; + /* + * Text preceding a macro without intervening whitespace + * requires a .Pf macro. + * Set the spacing flag to avoid a redundant .Ns macro. + */ -static const char *attrvals[ATTRVAL__MAX] = { - "norepeat", - "opt", - "plain", - "repeat", - "req" -}; + 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; + } + } -static const struct node nodes[NODE__MAX] = { - { NULL, 0 }, - { "acronym", 0 }, - { "arg", 0 }, - { "citerefentry", NODE_IGNTEXT }, - { "cmdsynopsis", NODE_IGNTEXT }, - { "code", 0 }, - { "command", 0 }, - { "date", 0 }, - { "emphasis", 0 }, - { "envar", 0 }, - { "filename", 0 }, - { "funcdef", 0 }, - { "funcprototype", NODE_IGNTEXT }, - { "funcsynopsis", NODE_IGNTEXT }, - { "funcsynopsisinfo", 0 }, - { "function", 0 }, - { "itemizedlist", NODE_IGNTEXT }, - { "link", 0 }, - { "listitem", NODE_IGNTEXT }, - { "literal", 0 }, - { "manvolnum", 0 }, - { "option", 0 }, - { "orderedlist", NODE_IGNTEXT }, - { "para", 0 }, - { "paramdef", 0 }, - { "parameter", 0 }, - { "programlisting", 0 }, - { "prompt", 0 }, - { "refclass", NODE_IGNTEXT }, - { "refdescriptor", NODE_IGNTEXT }, - { "refentry", NODE_IGNTEXT }, - { "refentryinfo", NODE_IGNTEXT }, - { "refentrytitle", 0 }, - { "refmeta", NODE_IGNTEXT }, - { "refmiscinfo", NODE_IGNTEXT }, - { "refname", 0 }, - { "refnamediv", NODE_IGNTEXT }, - { "refpurpose", 0 }, - { "refsect1", NODE_IGNTEXT }, - { "refsect2", NODE_IGNTEXT }, - { "refsynopsisdiv", NODE_IGNTEXT }, - { "replaceable", 0 }, - { "sbr", NODE_IGNTEXT }, - { "screen", NODE_IGNTEXT }, - { "structname", 0 }, - { "synopsis", 0 }, - { "term", 0 }, - { NULL, 0 }, - { "title", 0 }, - { "ulink", 0 }, - { "userinput", 0 }, - { "variablelist", NODE_IGNTEXT }, - { "varlistentry", NODE_IGNTEXT }, -}; + switch (f->linestate) { + case LINE_NEW: + break; + 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; + } -static void -pnode_print(struct parse *p, struct pnode *pn); + if (n->node == NODE_ESCAPE) { + fputs(n->b, stdout); + if (f->linestate == LINE_NEW) + f->linestate = LINE_TEXT; + return; + } -/* - * Process a stream of characters. - * We store text as nodes in and of themselves. - * If a text node is already open, append to it. - * If it's not open, open one under the current context. - */ + /* + * Remove the prefix '-' from