=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.3 retrieving revision 1.97 diff -u -p -r1.3 -r1.97 --- docbook2mdoc/docbook2mdoc.c 2014/03/28 02:46:40 1.3 +++ docbook2mdoc/docbook2mdoc.c 2019/04/07 17:42:36 1.97 @@ -1,6 +1,7 @@ -/* $Id: docbook2mdoc.c,v 1.3 2014/03/28 02:46:40 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.97 2019/04/07 17:42:36 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,794 +15,931 @@ * 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_FUNCDEF, - NODE_FUNCPROTOTYPE, - NODE_FUNCSYNOPSIS, - NODE_FUNCSYNOPSISINFO, - NODE_FUNCTION, - NODE_MANVOLNUM, - NODE_PARA, - NODE_PARAMDEF, - NODE_PARAMETER, - 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 *); -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; + char last; -static const struct node nodes[NODE__MAX] = { - { NULL, 0 }, - { "citerefentry", NODE_IGNTEXT }, - { "code", 0 }, - { "funcdef", 0 }, - { "funcprototype", NODE_IGNTEXT }, - { "funcsynopsis", NODE_IGNTEXT }, - { "funcsynopsisinfo", 0 }, - { "function", 0 }, - { "manvolnum", 0 }, - { "para", 0 }, - { "paramdef", 0 }, - { "parameter", 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 }, -}; + if (n->bsz == 0) { + assert(n->real < n->b); + 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->spc == 0 && + (nn->node != NODE_TEXT && nn->node != NODE_ESCAPE)) { + macro_open(f, "Pf"); + nn->spc = 1; + } + + if (f->linestate == LINE_NEW) { + last = '\n'; + f->linestate = LINE_TEXT; + } else { + last = ' '; + if (n->spc || f->linestate == LINE_MACRO) + putchar(' '); + } + + if (n->node == NODE_ESCAPE) { + fputs(n->b, stdout); + return; + } + + /* + * Remove the prefix '-' from