=================================================================== RCS file: /cvs/docbook2mdoc/parse.c,v retrieving revision 1.2 retrieving revision 1.4 diff -u -p -r1.2 -r1.4 --- docbook2mdoc/parse.c 2019/03/26 20:54:43 1.2 +++ docbook2mdoc/parse.c 2019/03/26 22:39:33 1.4 @@ -1,4 +1,4 @@ -/* $Id: parse.c,v 1.2 2019/03/26 20:54:43 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; }; @@ -47,9 +48,9 @@ struct element { }; static const struct element elements[] = { - { "acronym", NODE_ACRONYM }, + { "acronym", NODE_IGNORE }, { "affiliation", NODE_AFFILIATION }, - { "anchor", NODE_ANCHOR }, + { "anchor", NODE_DELETE }, { "application", NODE_APPLICATION }, { "arg", NODE_ARG }, { "author", NODE_AUTHOR }, @@ -75,7 +76,7 @@ static const struct element elements[] = { { "envar", NODE_ENVAR }, { "fieldsynopsis", NODE_FIELDSYNOPSIS }, { "filename", NODE_FILENAME }, - { "firstname", NODE_FIRSTNAME }, + { "firstname", NODE_IGNORE }, { "firstterm", NODE_FIRSTTERM }, { "footnote", NODE_FOOTNOTE }, { "funcdef", NODE_FUNCDEF }, @@ -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 }, @@ -115,15 +116,15 @@ static const struct element elements[] = { { "option", NODE_OPTION }, { "orderedlist", NODE_ORDEREDLIST }, { "orgname", NODE_ORGNAME }, - { "othername", NODE_OTHERNAME }, + { "othername", NODE_IGNORE }, { "para", NODE_PARA }, { "paramdef", NODE_PARAMDEF }, { "parameter", NODE_PARAMETER }, { "part", NODE_SECTION }, { "personname", NODE_PERSONNAME }, - { "phrase", NODE_PHRASE }, + { "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 }, @@ -157,7 +158,7 @@ static const struct element elements[] = { { "spanspec", NODE_SPANSPEC }, { "structname", NODE_STRUCTNAME }, { "subtitle", NODE_SUBTITLE }, - { "surname", NODE_SURNAME }, + { "surname", NODE_IGNORE }, { "synopsis", NODE_SYNOPSIS }, { "table", NODE_TABLE }, { "tbody", NODE_TBODY }, @@ -167,7 +168,7 @@ static const struct element elements[] = { { "thead", NODE_THEAD }, { "tip", NODE_TIP }, { "title", NODE_TITLE }, - { "trademark", NODE_TRADEMARK }, + { "trademark", NODE_IGNORE }, { "type", NODE_TYPE }, { "ulink", NODE_ULINK }, { "userinput", NODE_USERINPUT }, @@ -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 *