/* $Id: docbook2mdoc.c,v 1.146 2019/05/01 15:05:39 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 * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * 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 "xmalloc.h" #include "node.h" #include "macro.h" #include "format.h" /* * The implementation of the mdoc(7) formatter. */ static void pnode_print(struct format *, struct pnode *); static void pnode_printtext(struct format *f, struct pnode *n) { 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: 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; } if (n->node == NODE_ESCAPE) { fputs(n->b, stdout); if (f->linestate == LINE_NEW) f->linestate = LINE_TEXT; return; } /* * Remove the prefix '-' from