From: schwarze@mandoc.bsd.lv
To: source@mandoc.bsd.lv
Subject: docbook2mdoc: If a text node follows a non-text node without intervening
Date: Fri, 12 Apr 2019 01:47:15 -0500 (EST) [thread overview]
Message-ID: <e3fdb0ca08af148d@fantadrom.bsd.lv> (raw)
Log Message:
-----------
If a text node follows a non-text node without intervening whitespace,
keep the text in it as short as possible.
Modified Files:
--------------
docbook2mdoc:
parse.c
Revision Data
-------------
Index: parse.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/parse.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -Lparse.c -Lparse.c -u -p -r1.34 -r1.35
--- parse.c
+++ parse.c
@@ -322,44 +322,88 @@ warn_msg(struct parse *p, const char *fm
* Otherwise, create a new one as a child of the current node.
*/
static void
-xml_char(struct parse *p, const char *word, int sz)
+xml_text(struct parse *p, const char *word, int sz)
{
- struct pnode *n;
+ struct pnode *n, *np;
size_t oldsz, newsz;
+ int i;
assert(sz > 0);
if (p->del > 0)
return;
if ((n = p->cur) == NULL) {
- error_msg(p, "discarding text before document: %.*s", sz, word);
+ error_msg(p, "discarding text before document: %.*s",
+ sz, word);
return;
}
- if (n->node != NODE_TEXT) {
- if ((n = pnode_alloc(p->cur)) == NULL)
+ /* Append to the current text node, if one is open. */
+
+ if (n->node == NODE_TEXT) {
+ oldsz = strlen(n->b);
+ newsz = oldsz + sz;
+ if (oldsz && (p->flags & PFLAG_SPC))
+ newsz++;
+ if ((n->b = realloc(n->b, newsz + 1)) == NULL)
fatal(p);
- n->node = NODE_TEXT;
- n->spc = (p->flags & PFLAG_SPC) != 0;
- p->cur = n;
+ if (oldsz && (p->flags & PFLAG_SPC))
+ n->b[oldsz++] = ' ';
+ memcpy(n->b + oldsz, word, sz);
+ n->b[newsz] = '\0';
+ p->flags &= ~PFLAG_SPC;
+ return;
}
- if (p->tree->flags & TREE_CLOSED && n->parent == p->tree->root)
+ if (p->tree->flags & TREE_CLOSED && n == p->tree->root)
warn_msg(p, "text after end of document: %.*s", sz, word);
- /* Append to the current text node. */
+ /* Create a new text node. */
- oldsz = n->b == NULL ? 0 : strlen(n->b);
- newsz = oldsz + sz;
- if (oldsz && (p->flags & PFLAG_SPC))
- newsz++;
- if ((n->b = realloc(n->b, newsz + 1)) == NULL)
+ if ((n = pnode_alloc(p->cur)) == NULL)
fatal(p);
- if (oldsz && (p->flags & PFLAG_SPC))
- n->b[oldsz++] = ' ';
- memcpy(n->b + oldsz, word, sz);
- n->b[newsz] = '\0';
+ n->node = NODE_TEXT;
+ n->spc = (p->flags & PFLAG_SPC) != 0;
p->flags &= ~PFLAG_SPC;
+
+ /*
+ * If this node follows a non-text node without intervening
+ * whitespace, keep the text in it as short as possible,
+ * and do not keep it open.
+ */
+
+ if (n->spc == 0 &&
+ (np = TAILQ_PREV(n, pnodeq, child)) != NULL &&
+ np->node != NODE_TEXT && np->node != NODE_ESCAPE) {
+ i = 0;
+ while (i < sz && !isspace((unsigned char)word[i]))
+ i++;
+ if ((n->b = strndup(word, i)) == NULL)
+ fatal(p);
+ if (i == sz)
+ return;
+ while (i < sz && isspace((unsigned char)word[i]))
+ i++;
+ if (i == sz) {
+ p->flags |= PFLAG_SPC;
+ return;
+ }
+
+ /* Put any remaining text into a second node. */
+
+ if ((n = pnode_alloc(p->cur)) == NULL)
+ fatal(p);
+ n->node = NODE_TEXT;
+ n->spc = 1;
+ word += i;
+ sz -= i;
+ }
+ if ((n->b = strndup(word, sz)) == NULL)
+ fatal(p);
+
+ /* The new node remains open for later pnode_closetext(). */
+
+ p->cur = n;
}
/*
@@ -959,7 +1003,7 @@ parse_string(struct parse *p, char *b, s
advance(p, b, rlen, &pend,
p->ncur == NODE_DOCTYPE ? "<&]\n" : "<&\n",
refill);
- xml_char(p, b + poff, pend - poff);
+ xml_text(p, b + poff, pend - poff);
if (b[pend] == '\n')
pnode_closetext(p);
}
--
To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv
reply other threads:[~2019-04-12 6:47 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=e3fdb0ca08af148d@fantadrom.bsd.lv \
--to=schwarze@mandoc.bsd.lv \
--cc=source@mandoc.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).