=================================================================== RCS file: /cvs/docbook2mdoc/parse.c,v retrieving revision 1.3 retrieving revision 1.4 diff -u -p -r1.3 -r1.4 --- docbook2mdoc/parse.c 2019/03/26 21:52:09 1.3 +++ docbook2mdoc/parse.c 2019/03/26 22:39:33 1.4 @@ -1,4 +1,4 @@ -/* $Id: parse.c,v 1.3 2019/03/26 21:52:09 schwarze Exp $ */ +/* $Id: parse.c,v 1.4 2019/03/26 22:39:33 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * Copyright (c) 2019 Ingo Schwarze @@ -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, int sz) 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 *name, const 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 *name, const } 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 *name) 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 *name) 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 *