Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions scripts/merge_by_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,27 @@ def replace_heading(match):
return replace_heading


# remove <StickyHeaderTable> / </StickyHeaderTable> tags for PDF output
sticky_header_table_pattern = re.compile(r'^\s*</?StickyHeaderTable\s*/?>\s*$')

def remove_sticky_header_table(text):
lines = text.split('\n')
result = []
i = 0
while i < len(lines):
if sticky_header_table_pattern.match(lines[i]):
prev_blank = len(result) > 0 and result[-1].strip() == ''
next_blank = i + 1 < len(lines) and lines[i + 1].strip() == ''
Comment on lines +217 to +218
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

In Python, empty sequences are implicitly false. We can simplify len(result) > 0 to just result. Additionally, checking if a string is empty after stripping can be written more idiomatically using not string.strip() per PEP 8 guidelines.

Suggested change
prev_blank = len(result) > 0 and result[-1].strip() == ''
next_blank = i + 1 < len(lines) and lines[i + 1].strip() == ''
prev_blank = result and not result[-1].strip()
next_blank = i + 1 < len(lines) and not lines[i + 1].strip()
References
  1. PEP 8: For sequences, (strings, lists, tuples), use the fact that empty sequences are false. (link)

if prev_blank and next_blank:
i += 2
else:
i += 1
else:
result.append(lines[i])
i += 1
return '\n'.join(result)


# remove copyable snippet code
def remove_copyable(match):
return ""
Expand All @@ -224,6 +245,7 @@ def remove_copyable(match):
chapter = replace_variables(chapter, variables)
chapter = replace_link_wrap(chapter, name)
chapter = copyable_snippet_pattern.sub(remove_copyable, chapter)
chapter = remove_sticky_header_table(chapter)
chapter = extract_custom_ids_and_clean(chapter)
chapter = replace_custom_id_links(chapter)
# This block is to filter <CustomContent paltform="xxx"> xxx </CustomContent>
Expand Down