source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mandoc.bsd.lv
To: source@mandoc.bsd.lv
Subject: docbook2mdoc: Provide an easy way to parse an XML element without
Date: Tue, 26 Mar 2019 15:55:13 -0500 (EST)	[thread overview]
Message-ID: <e3fcc1a402c71532@fantadrom.bsd.lv> (raw)

Log Message:
-----------
Provide an easy way to parse an XML element without generating a node,
either ignoring it outright or emitting a warning if -W was specified.
Use this to handle <xi:include> more cleanly, fixing two FIXMEs.

Modified Files:
--------------
    docbook2mdoc:
        node.h
        parse.c

Revision Data
-------------
Index: node.h
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/node.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -Lnode.h -Lnode.h -u -p -r1.1 -r1.2
--- node.h
+++ node.h
@@ -26,7 +26,8 @@
  * More DocBook XML elements are recognized, but remapped or discarded.
  */
 enum	nodeid {
-	NODE_NONE = 0,  /* Must come first. */
+	NODE_IGNORE = 0,  /* Must come first. */
+	NODE_WARN,
 	/* Alpha-ordered hereafter. */
 	NODE_ACRONYM,
 	NODE_AFFILIATION,
Index: parse.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/parse.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -Lparse.c -Lparse.c -u -p -r1.1 -r1.2
--- parse.c
+++ parse.c
@@ -176,6 +176,7 @@ static	const struct element elements[] =
 	{ "varname",		NODE_VARNAME },
 	{ "warning",		NODE_WARNING },
 	{ "wordasword",		NODE_WORDASWORD },
+	{ "xi:include",		NODE_WARN },
 	{ "year",		NODE_YEAR },
 	{ NULL,			NODE__MAX }
 };
@@ -262,10 +263,6 @@ xml_elem_start(void *arg, const XML_Char
 	if (ps->tree->flags && TREE_FAIL)
 		return;
 
-	/* FIXME: find a better way to ditch other namespaces. */
-	if (strcmp(name, "xi:include") == 0)
-		return;
-
 	/* Close out the text node, if there is one. */
 	if (ps->cur != NULL && ps->cur->node == NODE_TEXT) {
 		pnode_trim(ps->cur);
@@ -284,8 +281,22 @@ xml_elem_start(void *arg, const XML_Char
 		return;
 	}
 
-	if (elem->node == NODE_INLINEEQUATION)
+	switch (elem->node) {
+	case NODE_WARN:
+		if (ps->warn)
+			fprintf(stderr, "%s:%zu:%zu: warning: "
+			    "ignoring element <%s>\n", ps->fname,
+			    XML_GetCurrentLineNumber(ps->xml),
+			    XML_GetCurrentColumnNumber(ps->xml), name);
+		/* FALLTHROUGH */
+	case NODE_IGNORE:
+		return;
+	case NODE_INLINEEQUATION:
 		ps->tree->flags |= TREE_EQN;
+		break;
+	default:
+		break;
+	}
 
 	if ((dat = calloc(1, sizeof(*dat))) == NULL) {
 		perror(NULL);
@@ -333,21 +344,30 @@ static void
 xml_elem_end(void *arg, const XML_Char *name)
 {
 	struct parse	*ps;
+	const struct element *elem;
 
 	ps = arg;
 	if (ps->tree->flags && TREE_FAIL)
 		return;
 
-	/* FIXME: find a better way to ditch other namespaces. */
-	if (strcmp(name, "xi:include") == 0)
-		return;
-
 	/* Close out the text node, if there is one. */
 	if (ps->cur->node == NODE_TEXT) {
 		pnode_trim(ps->cur);
 		ps->cur = ps->cur->parent;
 	}
-	ps->cur = ps->cur->parent;
+
+	for (elem = elements; elem->name != NULL; elem++)
+		if (strcmp(elem->name, name) == 0)
+			break;
+
+	switch (elem->node) {
+	case NODE_IGNORE:
+	case NODE_WARN:
+		break;
+	default:
+		assert(elem->node == ps->cur->node);
+		ps->cur = ps->cur->parent;
+	}
 }
 
 struct parse *
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv

                 reply	other threads:[~2019-03-26 20:55 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=e3fcc1a402c71532@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).