mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
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:
parent
b168599106
commit
9e8a03f61b
@ -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
|
||||
|
||||
recurse(root)
|
||||
last_section = find_last_section(root)
|
||||
|
||||
if informaltables:
|
||||
parent, child = informaltables[-1]
|
||||
parent.remove(child)
|
||||
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])
|
||||
|
||||
# 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:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user