From 0091d1bdbbb312e74b1dbbcd65375779698eca75 Mon Sep 17 00:00:00 2001 From: Kirill Goncharov Date: Tue, 12 May 2020 13:47:37 +0300 Subject: [PATCH] Detect inline code Fixes #10 --- export.py | 16 ++++++++++------ filter.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/export.py b/export.py index 6a867448..76a8ea93 100644 --- a/export.py +++ b/export.py @@ -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"]' diff --git a/filter.py b/filter.py index 30cec805..bddf8e8d 100644 --- a/filter.py +++ b/filter.py @@ -6,6 +6,7 @@ run_filter, stringify, BlockQuote, + Code, CodeBlock, Header, LineBreak, @@ -15,6 +16,7 @@ RawInline, Space, Str, + Strikeout, ) import requests @@ -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':