=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.2 retrieving revision 1.124 diff -u -p -r1.2 -r1.124 --- docbook2mdoc/docbook2mdoc.c 2014/03/28 02:06:29 1.2 +++ docbook2mdoc/docbook2mdoc.c 2019/04/14 20:13:25 1.124 @@ -1,6 +1,7 @@ -/* $Id: docbook2mdoc.c,v 1.2 2014/03/28 02:06:29 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.124 2019/04/14 20:13:25 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,758 +15,1351 @@ * 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 -/* - * All recognised node types. - */ -enum nodeid { - NODE_ROOT = 0, /* Must comes first. */ - /* Alpha-ordered hereafter. */ - NODE_CITEREFENTRY, - NODE_CODE, - NODE_FUNCSYNOPSIS, - NODE_FUNCSYNOPSISINFO, - NODE_MANVOLNUM, - NODE_PARA, - NODE_PROGRAMLISTING, - NODE_REFCLASS, - NODE_REFDESCRIPTOR, - NODE_REFENTRY, - NODE_REFENTRYTITLE, - NODE_REFMETA, - NODE_REFMISCINFO, - NODE_REFNAME, - NODE_REFNAMEDIV, - NODE_REFPURPOSE, - NODE_REFSECT1, - NODE_REFSYNOPSISDIV, - NODE_SYNOPSIS, - NODE_TEXT, - NODE_TITLE, - NODE__MAX -}; +#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 { - enum nodeid node; /* current (NODE_ROOT if pre-tree) */ - int stop; /* should we stop now? */ - struct pnode *root; /* root of parse tree */ - struct pnode *cur; /* current node in tree */ - char *b; - size_t bsz; - size_t mbsz; -}; -struct node { - const char *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); -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 */ - TAILQ_ENTRY(pnode) child; -}; +static void +pnode_printtext(struct format *f, struct pnode *n) +{ + struct pnode *nn; + char *cp; + int accept_arg; -static const struct node nodes[NODE__MAX] = { - { NULL, 0 }, - { "citerefentry", NODE_IGNTEXT }, - { "code", 0 }, - { "funcsynopsis", NODE_IGNTEXT }, - { "funcsynopsisinfo", 0 }, - { "manvolnum", 0 }, - { "para", 0 }, - { "programlisting", 0 }, - { "refclass", NODE_IGNTEXT }, - { "refdescriptor", NODE_IGNTEXT }, - { "refentry", NODE_IGNTEXT }, - { "refentrytitle", 0 }, - { "refmeta", NODE_IGNTEXT }, - { "refmiscinfo", NODE_IGNTEXT }, - { "refname", 0 }, - { "refnamediv", NODE_IGNTEXT }, - { "refpurpose", 0 }, - { "refsect1", 0 }, - { "refsynopsisdiv", NODE_IGNTEXT }, - { "synopsis", 0 }, - { NULL, 0 }, - { "title", 0 }, -}; + cp = n->b; + accept_arg = f->flags & FMT_ARG; + if (f->linestate == LINE_MACRO && !n->spc && !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 + * 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->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->spc = 1; + break; + default: + break; + } + } + + switch (f->linestate) { + case LINE_NEW: + break; + case LINE_TEXT: + if (n->spc) { + if (n->node == NODE_TEXT) + macro_close(f); + else + putchar(' '); + } + break; + case LINE_MACRO: + if (accept_arg) + putchar(' '); + else + macro_close(f); + break; + } + + if (n->node == NODE_ESCAPE) { + fputs(n->b, stdout); + if (f->linestate == LINE_NEW) + f->linestate = LINE_TEXT; + return; + } + + /* + * Remove the prefix '-' from