def action(elem, doc): if doc.get_metadata('title') is None: # No title -> Beancount Options Reference if isinstance(elem, Para): # Convert all paragraphs to code blocks text = stringify(elem) if not text.startswith('option'): text = ' ' + text return CodeBlock(text) # Skip everything else return if isinstance(elem, BlockQuote): if isinstance(elem.parent, ListItem): # Don't use blockquotes in lists assert len(elem.content) == 1 return elem.content[0] elif any(isinstance(item, CodeBlock) for item in elem.content): # Remove blockquotes around code blocks return [item for item in elem.content] elif len(elem.content) == 1: # Convert blockquotes to code blocks code = '' for item in elem.content[0].content: if isinstance(item, Link): # Don't convert links to code break elif isinstance(item, Str): code += item.text elif isinstance(item, Space): code += ' ' elif isinstance(item, LineBreak): code += '\n' else: code += stringify(item) else: return CodeBlock(code) elif isinstance(elem, Header): # There must be only one level 1 header if elem.identifier != 'title': elem.level += 1 elif isinstance(elem, Link): if elem.url == stringify(elem): # Displayed as url, skip pass else: resolved = resolve_url(elem.url) if resolved: elem.url = resolved elif isinstance(elem, CodeBlock): # Remove unnecessary leading tabs from code blocks elem.text = re.sub(r'^\t', '', elem.text, flags=re.MULTILINE) elif isinstance(elem, Image): elem.url = './' + elem.url
def test_table_to_csv(): # get_table_body markdown = """| 1 | 2 | 3 | 4 | |:--|--:|:-:|---| | 1 | 2 | 3 | 4 | : *abcd* """ Panflute = convert_text(markdown) code_block_converted = table_to_csv(*Panflute, doc=None) code_block_referenced = CodeBlock('''--- alignment: LRCD caption: '*abcd*' header: true markdown: true table-width: 0 width: - 0 - 0 - 0 - 0 --- 1,2,3,4\r\n1,2,3,4\r\n''', classes=['table']) assert repr(code_block_converted) == repr(code_block_referenced) return
def content_div(*output): status_attr = {"status": t.status.name} code = elem.text.split("\n---\n") input_code = Div(CodeBlock(code[0], identifier=elem.identifier, classes=elem.classes), classes=["doctestInput"]) return Div(input_code, *output, classes=["doctest"], attributes=status_attr)
def test_codeblock(self): doc = Doc( CodeBlock("", classes=["tip", "listing"]), CodeBlock("", classes=["tip"]), CodeBlock("", classes=["warning"]), CodeBlock( "", attributes={ "latex-tip-icon": "warning", "latex-tip-position": "right", "latex-tip-size": 24, }, ), metadata=self.metadata(), format="latex", ) pandoc_latex_tip.main(doc) self.assertEqual(doc.content[0].format, "tex") self.assertEqual(doc.content[2].format, "tex") self.assertEqual(doc.content[5].format, "tex") self.assertEqual(doc.content[8].format, "tex")
def action(elem, doc): if isinstance(elem, BlockQuote): if isinstance(elem.parent, ListItem): # Don't use blockquotes in lists assert len(elem.content) == 1 return elem.content[0] elif any(isinstance(item, CodeBlock) for item in elem.content): # Remove blockquotes around code blocks return [item for item in elem.content] elif len(elem.content) == 1: # Convert blockquotes to code blocks code = '' for item in elem.content[0].content: if isinstance(item, Link): # Don't convert links to code break elif isinstance(item, Str): code += item.text elif isinstance(item, Space): code += ' ' elif isinstance(item, LineBreak): code += '\n' else: code += stringify(item) else: return CodeBlock(code) elif isinstance(elem, Header): # There must be only one level 1 header if elem.identifier != 'title': elem.level += 1 elif isinstance(elem, Link): if elem.url == stringify(elem): # Displayed as url, skip pass else: resolved = resolve_url(elem.url) if resolved: elem.url = resolved
def action(elem, doc): if doc.get_metadata('title') is None: # No title -> Beancount Options Reference if isinstance(elem, Para): # Convert all paragraphs to code blocks text = stringify(elem) if not text.startswith('option'): text = ' ' + text return CodeBlock(text) # Skip everything else return if isinstance(elem, BlockQuote): if isinstance(elem.parent, ListItem): # Don't use blockquotes in lists assert len(elem.content) == 1 return elem.content[0] elif any(isinstance(item, CodeBlock) for item in elem.content): # Remove blockquotes around code blocks return [item for item in elem.content] elif len(elem.content) == 1: # Convert blockquotes to code blocks code = '' for item in elem.content[0].content: if isinstance(item, Link): # Don't convert links to code break elif isinstance(item, Str): code += item.text elif isinstance(item, Space): code += ' ' elif isinstance(item, LineBreak): code += '\n' else: code += stringify(item) 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: if any(isinstance(item, LineBreak) for item in elem.content): return [*elem.content] else: text = stringify(elem) return Code(text) elif isinstance(elem, Header): # There must be only one level 1 header if elem.identifier != 'title': elem.level += 1 # Add explicit anchor elem.content.append(RawInline(f'<a id="{elem.identifier}"></a>')) elif isinstance(elem, Link): if elem.url == stringify(elem): # Displayed as url, skip pass else: resolved = resolve_url(elem.url) if resolved: elem.url = resolved elif isinstance(elem, CodeBlock): # Remove unnecessary leading tabs from code blocks elem.text = re.sub(r'^\t', '', elem.text, flags=re.MULTILINE)