Skip to content

Commit

Permalink
html2myst: introduce an outlining pass for determining fences.
Browse files Browse the repository at this point in the history
Using the code fences colon extension in MyST, the fences for
outer blocks is one-deeper than the nested blocks, so the tree
needs to be built before the size of fences can be determined.
  • Loading branch information
jessicah committed Aug 23, 2024
1 parent 14ad3f4 commit c811a12
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
5 changes: 4 additions & 1 deletion html2myst/html2myst.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def process_subsection(self, element):
section += self.process_block(child)

if needs_abi_group:
section.set_directive('{abi-group}', 4)
section.set_directive('{abi-group}')
section.output_title(False)

return section
Expand Down Expand Up @@ -499,6 +499,9 @@ def process_link(self, element):
if __name__ == '__main__':
document = Document(sys.argv[1], sys.argv[2], None)
document.process()
# outlining sets the correct level of block fences
# after building the tree
document.section.outline(0)
#print('document:', document)
with open(sys.argv[2], 'w') as file:
print(str(document).replace('\r','\n'), file=file)
42 changes: 33 additions & 9 deletions html2myst/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def fit_to_lines(text, indent=''):

class Item:
def __init__(self):
self.directive = None
self.depth = 0
self.extent = 0
pass

def __str__(self):
Expand All @@ -38,8 +41,15 @@ def __str__(self):
def walk(self, depth):
print(f'{indent(depth)} Empty Item')

def outline(self, depth):
return depth, False

def fence(self):
return ':' * (self.extent - self.depth + 3)

class Text(Item):
def __init__(self, content=''):
super().__init__()
# could we do some line wrapping here?
if isinstance(content, Text):
content = str(content)
Expand All @@ -54,15 +64,18 @@ def walk(self, depth):
class Block(Text):
def __init__(self, directive=None):
super().__init__()
if isinstance(directive, tuple):
self.directive, self.colons = directive
elif isinstance(directive, str):
if isinstance(directive, str):
self.directive = directive
self.colons = ':::'
else:
self.directive = None
self.strip = True

def outline(self, depth):
if self.directive is not None:
self.depth = depth
self.extent = depth
return self.extent, self.directive is not None

def walk(self, depth):
print(f'{indent(depth)} Block')

Expand All @@ -76,23 +89,34 @@ def __iadd__(self, content):
self.content += content
return self

def set_directive(self, directive, colons=3):
self.directive, self.colons = directive, ':' * colons
def set_directive(self, directive):
self.directive = directive

def wrap_content(self):
if self.directive is None:
return self.content
else:
if len(self.content.strip()) == 0:
return f'{self.colons}{self.directive}\n{self.colons}'
return f'{self.fence()}{self.directive}\n{self.fence()}'
else:
return f'{self.colons}{self.directive}\n{self.content}\n{self.colons}'
return f'{self.fence()}{self.directive}\n{self.content}\n{self.fence()}'

class BlockContainer(Block):
def __init__(self, directive=None):
super().__init__(directive)
self.blocks = []

def outline(self, depth):
self.depth = depth
self.extent = depth
new_depth = depth + 1 if self.directive is not None else depth
for block in self.blocks:
if not isinstance(block, str):
new_extent, has_directive = block.outline(new_depth)
if has_directive:
self.extent = max(self.extent, new_extent)
return self.extent, self.directive is not None

def walk(self, depth):
print(f'{indent(depth)} Block Container:')
for block in self.blocks:
Expand Down Expand Up @@ -140,7 +164,7 @@ def __str__(self):
else:
return body

class TableRow(Item):
class TableRow(BlockContainer):
def walk(self, depth):
print(f'{indent(depth)} Table Row')

Expand Down

0 comments on commit c811a12

Please sign in to comment.