Commit 9e8a03f6 authored by Joseph Nuthalapati's avatar Joseph Nuthalapati Committed by James Valleroy

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's avatarJoseph Nuthalapati <njoseph@thoughtworks.com>
Reviewed-by: James Valleroy's avatarJames Valleroy <jvalleroy@mailbox.org>
parent b1685991
......@@ -31,7 +31,7 @@ def parse_arguments():
subparser = subparsers.add_parser('fix-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
return parser.parse_args()
......@@ -52,27 +52,36 @@ def subcommand_fix_wiki_urls(arguments):
def subcommand_remove_footer(arguments):
"""Remove the footer template from the given wiki page."""
filename = arguments.filename
tree = etree.parse(filename)
root = tree.getroot()
informaltables = []
def recurse(elem):
for child in elem:
if child.tag == 'informaltable':
informaltables.append((elem, child))
else:
recurse(child)
# The footer will always be in the last <section>
def find_last_section(elem):
if elem.getchildren():
last_element = elem[-1]
if last_element.tag == 'section':
return find_last_section(last_element)
return elem
last_section = find_last_section(root)
recurse(root)
if last_section.getchildren():
# Remove all elements till <informaltable> is reached
while last_section[-1].tag != 'informaltable':
last_section.remove(last_section[-1])
# remove <informaltable> itself
last_section.remove(last_section[-1])
if informaltables:
parent, child = informaltables[-1]
parent.remove(child)
# 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()
with open(filename, 'r') as xml_file:
# <xml> and <DOCTYPE> elements which etree skips
header = xml_file.readlines()[:2]
with open(filename, 'w') as xml_file:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment