source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* docbook2mdoc: Provide a way to exclude elements including their children
@ 2019-03-26 22:40 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2019-03-26 22:40 UTC (permalink / raw)
  To: source

Log Message:
-----------
Provide a way to exclude elements including their children from the tree 
and use that for <anchor>, <indexterm>, <primary>, and <secondary>.

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

Revision Data
-------------
Index: parse.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/parse.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -Lparse.c -Lparse.c -u -p -r1.3 -r1.4
--- parse.c
+++ parse.c
@@ -38,6 +38,7 @@ struct	parse {
 	const char	*fname;  /* Name of the input file. */
 	struct ptree	*tree;   /* Complete parse result. */
 	struct pnode	*cur;	 /* Current node in the tree. */
+	int		 del;    /* Levels of nested nodes being deleted. */
 	int		 warn;
 };
 
@@ -49,7 +50,7 @@ struct	element {
 static	const struct element elements[] = {
 	{ "acronym",		NODE_IGNORE },
 	{ "affiliation",	NODE_AFFILIATION },
-	{ "anchor",		NODE_IGNORE },
+	{ "anchor",		NODE_DELETE },
 	{ "application",	NODE_APPLICATION },
 	{ "arg",		NODE_ARG },
 	{ "author",		NODE_AUTHOR },
@@ -87,7 +88,7 @@ static	const struct element elements[] =
 	{ "group",		NODE_GROUP },
 	{ "holder",		NODE_HOLDER },
 	{ "index",		NODE_INDEX },
-	{ "indexterm",		NODE_INDEXTERM },
+	{ "indexterm",		NODE_DELETE },
 	{ "info",		NODE_INFO },
 	{ "informalequation",	NODE_INFORMALEQUATION },
 	{ "informaltable",	NODE_INFORMALTABLE },
@@ -123,7 +124,7 @@ static	const struct element elements[] =
 	{ "personname",		NODE_PERSONNAME },
 	{ "phrase",		NODE_IGNORE },
 	{ "preface",		NODE_PREFACE },
-	{ "primary",		NODE_PRIMARY },
+	{ "primary",		NODE_DELETE },
 	{ "programlisting",	NODE_PROGRAMLISTING },
 	{ "prompt",		NODE_PROMPT },
 	{ "quote",		NODE_QUOTE },
@@ -148,7 +149,7 @@ static	const struct element elements[] =
 	{ "row",		NODE_ROW },
 	{ "sbr",		NODE_SBR },
 	{ "screen",		NODE_SCREEN },
-	{ "secondary",		NODE_SECONDARY },
+	{ "secondary",		NODE_DELETE },
 	{ "sect1",		NODE_SECTION },
 	{ "sect2",		NODE_SECTION },
 	{ "section",		NODE_SECTION },
@@ -176,7 +177,7 @@ static	const struct element elements[] =
 	{ "varname",		NODE_VARNAME },
 	{ "warning",		NODE_WARNING },
 	{ "wordasword",		NODE_WORDASWORD },
-	{ "xi:include",		NODE_WARN },
+	{ "xi:include",		NODE_DELETE_WARN },
 	{ "year",		NODE_YEAR },
 	{ NULL,			NODE__MAX }
 };
@@ -194,7 +195,7 @@ xml_char(void *arg, const XML_Char *p, i
 	int		 i;
 
 	ps = arg;
-	if (ps->tree->flags && TREE_FAIL)
+	if (ps->del > 0 || ps->tree->flags & TREE_FAIL)
 		return;
 
 	/*
@@ -260,9 +261,18 @@ xml_elem_start(void *arg, const XML_Char
 	const XML_Char	**att;
 
 	ps = arg;
-	if (ps->tree->flags && TREE_FAIL)
+	if (ps->tree->flags & TREE_FAIL)
 		return;
 
+	/*
+	 * An ancestor is excluded from the tree;
+	 * keep track of the number of levels excluded.
+	 */
+	if (ps->del > 0) {
+		ps->del++;
+		return;
+	}
+
 	/* Close out the text node, if there is one. */
 	if (ps->cur != NULL && ps->cur->node == NODE_TEXT) {
 		pnode_trim(ps->cur);
@@ -282,13 +292,16 @@ xml_elem_start(void *arg, const XML_Char
 	}
 
 	switch (elem->node) {
-	case NODE_WARN:
+	case NODE_DELETE_WARN:
 		if (ps->warn)
 			fprintf(stderr, "%s:%zu:%zu: warning: "
-			    "ignoring element <%s>\n", ps->fname,
+			    "skipping element <%s>\n", ps->fname,
 			    XML_GetCurrentLineNumber(ps->xml),
 			    XML_GetCurrentColumnNumber(ps->xml), name);
 		/* FALLTHROUGH */
+	case NODE_DELETE:
+		ps->del = 1;
+		/* FALLTHROUGH */
 	case NODE_IGNORE:
 		return;
 	case NODE_INLINEEQUATION:
@@ -347,11 +360,20 @@ xml_elem_end(void *arg, const XML_Char *
 	const struct element *elem;
 
 	ps = arg;
-	if (ps->tree->flags && TREE_FAIL)
+	if (ps->tree->flags & TREE_FAIL)
 		return;
 
+	/*
+	 * An ancestor is excluded from the tree;
+	 * keep track of the number of levels excluded.
+	 */
+	if (ps->del > 1) {
+		ps->del--;
+		return;
+	}
+
 	/* Close out the text node, if there is one. */
-	if (ps->cur->node == NODE_TEXT) {
+	if (ps->del == 0 && ps->cur->node == NODE_TEXT) {
 		pnode_trim(ps->cur);
 		ps->cur = ps->cur->parent;
 	}
@@ -361,13 +383,18 @@ xml_elem_end(void *arg, const XML_Char *
 			break;
 
 	switch (elem->node) {
+	case NODE_DELETE_WARN:
+	case NODE_DELETE:
+		ps->del--;
+		break;
 	case NODE_IGNORE:
-	case NODE_WARN:
 		break;
 	default:
 		assert(elem->node == ps->cur->node);
 		ps->cur = ps->cur->parent;
+		break;
 	}
+	assert(ps->del == 0);
 }
 
 struct parse *
Index: docbook2mdoc.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/docbook2mdoc.c,v
retrieving revision 1.76
retrieving revision 1.77
diff -Ldocbook2mdoc.c -Ldocbook2mdoc.c -u -p -r1.76 -r1.77
--- docbook2mdoc.c
+++ docbook2mdoc.c
@@ -612,8 +612,6 @@ pnode_print(struct format *p, struct pno
 	case NODE_FUNCSYNOPSISINFO:
 		macro_open(p, "Fd");
 		break;
-	case NODE_INDEXTERM:
-		return;
 	case NODE_INFORMALEQUATION:
 		macro_line(p, "EQ");
 		break;
Index: node.h
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/node.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -Lnode.h -Lnode.h -u -p -r1.3 -r1.4
--- node.h
+++ node.h
@@ -26,8 +26,9 @@
  * More DocBook XML elements are recognized, but remapped or discarded.
  */
 enum	nodeid {
-	NODE_IGNORE = 0,  /* Must come first. */
-	NODE_WARN,
+	NODE_DELETE_WARN,
+	NODE_DELETE,
+	NODE_IGNORE,
 	/* Alpha-ordered hereafter. */
 	NODE_AFFILIATION,
 	NODE_APPLICATION,
@@ -65,7 +66,6 @@ enum	nodeid {
 	NODE_GROUP,
 	NODE_HOLDER,
 	NODE_INDEX,
-	NODE_INDEXTERM,
 	NODE_INFO,
 	NODE_INFORMALEQUATION,
 	NODE_INFORMALTABLE,
@@ -98,7 +98,6 @@ enum	nodeid {
 	NODE_PARAMETER,
 	NODE_PERSONNAME,
 	NODE_PREFACE,
-	NODE_PRIMARY,
 	NODE_PROGRAMLISTING,
 	NODE_PROMPT,
 	NODE_QUOTE,
@@ -119,7 +118,6 @@ enum	nodeid {
 	NODE_ROW,
 	NODE_SBR,
 	NODE_SCREEN,
-	NODE_SECONDARY,
 	NODE_SECTION,
 	NODE_SGMLTAG,
 	NODE_SIMPLELIST,
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-03-26 22:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-26 22:40 docbook2mdoc: Provide a way to exclude elements including their children schwarze

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).