manual: Remove footer for manual pages using Python XML module

Using Python XML module instead of egrep for removing the lines of the footer.

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Joseph Nuthalapati 2018-09-04 19:53:55 +05:30 committed by James Valleroy
parent b168599106
commit 9e8a03f61b
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -31,7 +31,7 @@ def parse_arguments():
subparser = subparsers.add_parser('fix-wiki-urls', subparser = subparsers.add_parser('fix-wiki-urls',
help='Fix wrongly formatted wiki urls') help='Fix wrongly formatted wiki urls')
subparser.add_argument('filename', help='Name of the XML file') subparser.add_argument('filename', help='Name of the Docbook file')
subparsers.required = True subparsers.required = True
return parser.parse_args() return parser.parse_args()
@ -52,27 +52,36 @@ def subcommand_fix_wiki_urls(arguments):
def subcommand_remove_footer(arguments): def subcommand_remove_footer(arguments):
"""Remove the footer template from the given wiki page."""
filename = arguments.filename filename = arguments.filename
tree = etree.parse(filename) tree = etree.parse(filename)
root = tree.getroot() root = tree.getroot()
informaltables = []
def recurse(elem): # The footer will always be in the last <section>
for child in elem: def find_last_section(elem):
if child.tag == 'informaltable': if elem.getchildren():
informaltables.append((elem, child)) last_element = elem[-1]
else: if last_element.tag == 'section':
recurse(child) return find_last_section(last_element)
return elem
recurse(root) last_section = find_last_section(root)
if informaltables: if last_section.getchildren():
parent, child = informaltables[-1] # Remove all elements till <informaltable> is reached
parent.remove(child) while last_section[-1].tag != 'informaltable':
last_section.remove(last_section[-1])
# remove <informaltable> itself
last_section.remove(last_section[-1])
# Remove the line "Back to Features introduction or manual pages."
if last_section[-1].text.startswith('Back to'):
last_section.remove(last_section[-1])
processed_xml = etree.tostring(root, encoding='utf-8').decode() processed_xml = etree.tostring(root, encoding='utf-8').decode()
with open(filename, 'r') as xml_file: with open(filename, 'r') as xml_file:
# <xml> and <DOCTYPE> elements which etree skips
header = xml_file.readlines()[:2] header = xml_file.readlines()[:2]
with open(filename, 'w') as xml_file: with open(filename, 'w') as xml_file: