Skip to content

Commit

Permalink
Detect inline code
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
xuhcc committed May 12, 2020
1 parent 51d3bcb commit 0091d1b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
16 changes: 10 additions & 6 deletions export.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@ def prepare_docx(file_name: str, drawing_dir: str = None) -> bytes:
if len(para.runs) == 0:
continue

# Find snippets in monospace font and add SourceCode style to them
# https://groups.google.com/d/msg/pandoc-discuss/SIwE9dhGF4U/Wjy8zmQ1CQAJ
for run in para.runs:
if run.text == '\t':
for run_idx, run in enumerate(para.runs):
if run_idx == 0 and run.text == '\t':
continue # Ignore leading tabs
if run.font.name in ['Consolas', 'Courier New']:
if run_idx == 0 and run.font.name in ['Consolas', 'Courier New']:
# If paragraph starts with a snippet in monospace font,
# consider it a code block and mark it with SourceCode style
# https://groups.google.com/d/msg/pandoc-discuss/SIwE9dhGF4U/Wjy8zmQ1CQAJ
para.style = doc.styles['SourceCode']
break
continue
if run.font.name in ['Consolas', 'Courier New']:
# Mark with striketrough style to convert to inline code later
run.font.strike = True

if para.runs[0].element.xpath(
'.//*[@uri="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"]'
Expand Down
14 changes: 14 additions & 0 deletions filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
run_filter,
stringify,
BlockQuote,
Code,
CodeBlock,
Header,
LineBreak,
Expand All @@ -15,6 +16,7 @@
RawInline,
Space,
Str,
Strikeout,
)
import requests

Expand Down Expand Up @@ -93,6 +95,18 @@ def action(elem, doc):
else:
return CodeBlock(code)

elif isinstance(elem, Strikeout):
if (
doc.get_metadata('title') == 'A Comparison of Beancount and Ledger'
and len(elem.parent.content) == 1
):
# Preserve strikethrough paragraphs
# in 'A Comparison of Beancount and Ledger' document
pass
else:
text = stringify(elem)
return Code(text)

elif isinstance(elem, Header):
# There must be only one level 1 header
if elem.identifier != 'title':
Expand Down

0 comments on commit 0091d1b

Please sign in to comment.