From: kristaps@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: docbook2mdoc: For some elements, we want to look ahead to see if the
Date: Wed, 30 Apr 2014 08:34:44 -0400 (EDT) [thread overview]
Message-ID: <201404301234.s3UCYimj002490@krisdoz.my.domain> (raw)
Log Message:
-----------
For some elements, we want to look ahead to see if the next node is text
and begins with punctuation. If so, we can print that punctuation or
our line and make it work well with mdoc(7). For example,
<constant>bar</constant>.
will now render as
.Dv bar .
This makes output pages much more readable.
Modified Files:
--------------
docbook2mdoc:
docbook2mdoc.c
Revision Data
-------------
Index: docbook2mdoc.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/docbook2mdoc/docbook2mdoc.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -Ldocbook2mdoc.c -Ldocbook2mdoc.c -u -p -r1.36 -r1.37
--- docbook2mdoc.c
+++ docbook2mdoc.c
@@ -64,6 +64,7 @@ struct pattr {
struct pnode {
enum nodeid node; /* node type */
char *b; /* binary data buffer */
+ char *real; /* store for "b" */
size_t bsz; /* data buffer size */
struct pnode *parent; /* parent (or NULL if top) */
struct pnodeq childq; /* queue of children */
@@ -233,6 +234,7 @@ xml_char(void *arg, const XML_Char *p, i
}
memcpy(ps->cur->b + ps->cur->bsz, p, sz);
ps->cur->bsz += (size_t)sz;
+ ps->cur->real = ps->cur->b;
}
static void
@@ -436,7 +438,7 @@ pnode_free(struct pnode *pn)
free(ap);
}
- free(pn->b);
+ free(pn->real);
free(pn);
}
@@ -620,6 +622,41 @@ pnode_printmclose(struct parse *p, int s
}
/*
+ * Like pnode_printmclose() except we look to the next node, and, if
+ * found, see if it starts with punctuation.
+ * If it does, then we print that punctuation before the newline.
+ */
+static void
+pnode_printmclosepunct(struct parse *p, struct pnode *pn, int sv)
+{
+ /* We wouldn't have done anything anyway. */
+ if ( ! (sv && ! p->newln))
+ return;
+
+ /* No next node or it's not text. */
+ if (NULL == (pn = TAILQ_NEXT(pn, child))) {
+ pnode_printmclose(p, sv);
+ return;
+ } else if (NODE_TEXT != pn->node) {
+ pnode_printmclose(p, sv);
+ return;
+ }
+
+ /* Only do this for the comma/period. */
+ if (pn->bsz > 0 &&
+ (',' == pn->b[0] || '.' == pn->b[0]) &&
+ (1 == pn->bsz || isspace((int)pn->b[1]))) {
+ putchar(' ');
+ putchar(pn->b[0]);
+ pn->b++;
+ pn->bsz--;
+ }
+
+ putchar('\n');
+ p->newln = 1;
+}
+
+/*
* If the SYNOPSIS macro has a superfluous title, kill it.
*/
static void
@@ -1251,8 +1288,15 @@ pnode_print(struct parse *p, struct pnod
case (NODE_TEXT):
if (0 == p->newln)
putchar(' ');
+
bufclear(p);
bufappend(p, pn);
+
+ if (0 == p->bsz) {
+ assert(pn->real != pn->b);
+ break;
+ }
+
/*
* Output all characters, squeezing out whitespace
* between newlines.
@@ -1261,6 +1305,7 @@ pnode_print(struct parse *p, struct pnod
*/
assert(p->bsz);
cp = p->b;
+
/*
* There's often a superfluous "-" in its <option> tags
* before the actual flags themselves.
@@ -1333,7 +1378,7 @@ pnode_print(struct parse *p, struct pnod
case (NODE_TEXT):
case (NODE_USERINPUT):
case (NODE_VARNAME):
- pnode_printmclose(p, sv);
+ pnode_printmclosepunct(p, pn, sv);
break;
case (NODE_QUOTE):
pnode_printmclose(p, sv);
--
To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv
reply other threads:[~2014-04-30 12:34 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=201404301234.s3UCYimj002490@krisdoz.my.domain \
--to=kristaps@mdocml.bsd.lv \
--cc=source@mdocml.bsd.lv \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).