Skip to content

Commit

Permalink
doc: add "--input" and "--output-dir" options to htmlsplit.py
Browse files Browse the repository at this point in the history
add "--input" and "--output-dir" options to htmlsplit.py, to avoid
hardwire the input and output parameters in the tool

Signed-off-by: Kefu Chai <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
tchaikov authored and nyh committed Sep 29, 2020
1 parent fd7babb commit 4427dd8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion doc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ add_custom_command (
doc_tutorial_html
${CMAKE_CURRENT_BINARY_DIR}/htmlsplit.py
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/split
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/htmlsplit.py)
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/htmlsplit.py
--input ${CMAKE_CURRENT_BINARY_DIR}/html/tutorial.html
--output-dir ${CMAKE_CURRENT_BINARY_DIR}/split)

add_custom_target (doc_tutorial_html_split
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/split)
Expand Down
18 changes: 14 additions & 4 deletions doc/htmlsplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
# under the License.

from xml.etree import ElementTree
import argparse
import copy
import os

# chapter number to chapter title
titles = {}
Expand Down Expand Up @@ -109,7 +111,12 @@ def get_chap_num(element):
return int(data_num)
assert data_num, "section number not found"

tree = ElementTree.parse('tutorial.html')
parser = argparse.ArgumentParser()
parser.add_argument('--input')
parser.add_argument('--output-dir')
args = parser.parse_args()

tree = ElementTree.parse(args.input)
for e in tree.iter():
remove_ns_prefix(e)
template = copy.deepcopy(tree.getroot())
Expand All @@ -134,12 +141,14 @@ def get_chap_num(element):
toc_tree = ElementTree.ElementTree(copy.deepcopy(template))
body = next(toc_tree.iterfind('./body'))
body.append(e)
toc_tree.write('split/index.html', method='html')
toc_tree.write(os.path.join(args.output_dir, 'index.html'),
method='html')
elif e.tag == 'h1':
assert titles
assert sections
if chap_num > 0:
chap_tree.write(f'split/{chap_num}.html', method='html')
chap_tree.write(os.path.join(args.output_dir, f'{chap_num}.html'),
method='html')
chap_num = get_chap_num(e)
chap_tree = ElementTree.ElementTree(copy.deepcopy(template))
body = next(chap_tree.iterfind('./body'))
Expand All @@ -151,4 +160,5 @@ def get_chap_num(element):
body = next(chap_tree.iterfind('./body'))
body.append(e)

chap_tree.write(f'split/{chap_num}.html', method='html')
chap_tree.write(os.path.join(args.output_dir, f'{chap_num}.html'),
method='html')

0 comments on commit 4427dd8

Please sign in to comment.