Skip to content

Commit

Permalink
Port update_inlinedoc.py to new ElementTree
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechMula committed Jan 22, 2021
1 parent 5971e47 commit 44e2ee6
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions update_inlinedoc.py
@@ -1,11 +1,12 @@
import os
from pathlib import Path
import sys
import os
import textwrap
import xml.etree.ElementTree as ET


def main():
dstpath = 'src/inline_doc.h'
dstpath = Path('src/inline_doc.h')
app = Application(dstpath)
app.run()

Expand All @@ -26,14 +27,12 @@ def run(self):
content += '\n' + self.__format_file(path, name)

oldcontent = None
if os.path.exists(self.dstpath):
with open(self.dstpath, 'rt') as f:
oldcontent = f.read()
if self.dstpath.exists():
oldcontent = self.dstpath.read_text()

if content != oldcontent:
with open(self.dstpath, 'wt') as f:
print("Creating %s" % self.dstpath)
f.write(content)
print("Creating %s" % self.dstpath)
self.dstpath.write_text(content)


def __format_file(self, path, name):
Expand All @@ -45,11 +44,10 @@ def __format_file(self, path, name):


def __get_files(self):
rootdir = 'docs'
for name in sorted(os.listdir(rootdir)):
if name.endswith('.rst') and name != 'index.rst':
path = os.path.join(rootdir, name)
name = name[:-4] + '_doc'
rootdir = Path('docs')
for path in sorted(rootdir.glob("*.rst")):
if path.name != 'index.rst':
name = path.stem + '_doc'
yield (path, name)


Expand All @@ -64,7 +62,7 @@ def __init__(self, xml_string, name):

def format(self):
self.lines = []
for node in self.xml.getiterator('document')[0]:
for node in next(self.xml.iter('document')):
if node.tag == 'title':
self.format_title(node)
elif node.tag == 'paragraph':
Expand All @@ -90,24 +88,22 @@ def format_paragraph(self, node):


def format_bullet_list(self, node):
for child in node.iterfind('list_item'):
par = child.getchildren()[0]
assert par.tag == 'paragraph'

text = self.normalize(par)
for list_item in node.iter('list_item'):
for paragraph in list_item.iter('paragraph'):

lines = textwrap.wrap(text, width=(WIDTH - 2))
for i, line in enumerate(lines):
if i == 0:
prefix = '- '
else:
prefix = ' '
text = self.normalize(paragraph)
lines = textwrap.wrap(text, width=(WIDTH - 2))
for i, line in enumerate(lines):
if i == 0:
prefix = '- '
else:
prefix = ' '

self.lines.append(prefix + line)
self.lines.append(prefix + line)


def normalize(self, node):
t = ET.tostring(node, method='text')
t = ET.tostring(node, method='text', encoding='unicode')
t = t.split()
return ' '.join(t)

Expand Down

0 comments on commit 44e2ee6

Please sign in to comment.