def test_handle_mugraphics_block(self): """MUGraphics command (block) with description""" content = r"\MUGraphics{foobar.png}{width=0.3\linewidth}{Footitle $a^2$}" doc = pf.Doc(pf.RawBlock(content, format="latex"), metadata={"lang": "en"}) elem = doc.content[0] # this sets up elem.parent elem_args = ["foobar.png", r"width=0.3\linewidth", "Footitle $a^2$"] ret = self.commands.handle_mugraphics(elem_args, elem) self.assertIsInstance(ret, pf.Div) img = ret.content[0].content[0] self.assertIsInstance(img, pf.Image) self.assertEqual(img.url, "foobar.png") self.assertEqual(img.title, "Footitle a^2") descr = ret.content[1] self.assertIsInstance(descr, pf.Para) descr1 = descr.content[0] self.assertIsInstance(descr1, pf.Str) self.assertEqual(descr1.text, "Footitle") descr2 = descr.content[1] self.assertIsInstance(descr2, pf.Space) descr3 = descr.content[2] self.assertIsInstance(descr3, pf.Math)
def test_env(): # A Doc() created by panflute has no environment vars print(f'\n - Testing Doc() created by panflute:') doc = pf.Doc() assert doc.pandoc_version is None assert isinstance(doc.pandoc_reader_options, dict) and not doc.pandoc_reader_options print(f' - No environment vars; as expected') # A Doc() created by running convert_text also doesn't print(f'\n - Testing Doc() created by panflute.convert_text():') fn = Path("./tests/sample_files/fenced/example.md") with fn.open(encoding='utf-8') as f: markdown_text = f.read() json_pandoc = pf.convert_text(markdown_text, input_format='markdown', output_format='json', standalone=True) doc = pf.convert_text(json_pandoc, input_format='json', output_format='panflute', standalone=True) assert doc.pandoc_version is None assert isinstance(doc.pandoc_reader_options, dict) and not doc.pandoc_reader_options print(f' - No environment vars; as expected') print(f'\n - Testing Doc() as created by a filter:') pf.run_pandoc(text='Hello!', args=['--filter=./tests/filters/assert_env.py']) print(f' - Found environment vars; as expected')
def test_handle_newline(self): """newline command""" content = r"\newline" doc = pf.Doc(pf.RawBlock(content, format="latex")) elem = doc.content[0] # this sets up elem.parent ret = self.commands.handle_newline([], elem) self.assertIsInstance(ret, pf.LineBreak)
def test_handle_mxinfo_math_title(self): """MXInfo with Math in title""" doc = pf.Doc(metadata={"lang": "en"}) elem_content = r""" \begin{MXInfo}{Ableitung $x^n$} Foo bar \end{MXInfo} """ doc.content.extend([pf.RawBlock(elem_content, format="latex")]) elem = doc.content[0] # this sets up elem.parent ret = self.environments.handle_mxinfo("Foo bar", ["Ableitung $x^n$"], elem) self.assertIsInstance(ret, pf.Div) header = ret.content[0] self.assertIsInstance(header, pf.Header) self.assertIsInstance(header.content[0], pf.Str) self.assertEqual(header.content[0].text, "Ableitung") self.assertIsInstance(header.content[1], pf.Space) self.assertIsInstance(header.content[2], pf.Math) self.assertEqual(header.content[2].text, "x^n") self.assertEqual(header.content[2].format, "InlineMath") para = ret.content[1] self.assertIsInstance(para.content[0], pf.Str) self.assertEqual(para.content[0].text, "Foo") self.assertIsInstance(para.content[1], pf.Space) self.assertIsInstance(para.content[2], pf.Str) self.assertEqual(para.content[2].text, "bar")
def main(): with open("markdown.md", "r") as f: md = f.read() doc = pf.Doc(*pf.convert_text(md), format="docx") pf.debug("doc: {}".format(*doc.content)) ebl = ExtractBulletList() pf.run_filter(ebl.action, doc=doc)
def test_handle_mtest_section_title(self): """MTest""" doc = pf.Doc(metadata={"lang": "en"}) elem_content = r""" \begin{MTest}{Abschlusstest Kapitel \arabic{section}} Foo bar \end{MXContent}""" doc.content.extend([pf.RawBlock(elem_content, format="latex")]) elem = doc.content[0] # this sets up elem.parent ret = self.environments.handle_mtest( "Foo bar", [r"Abschlusstest Kapitel \arabic{section}"], elem) self.assertEqual(len(ret), 2) header = ret[0] self.assertIsInstance(header, pf.Header) self.assertEqual(pf.stringify(header), "Abschlusstest") para = ret[1] self.assertIsInstance(para.content[0], pf.Str) self.assertEqual(para.content[0].text, "Foo") self.assertIsInstance(para.content[1], pf.Space) self.assertIsInstance(para.content[2], pf.Str) self.assertEqual(para.content[2].text, "bar")
def test_handle_html(self): """html""" doc = pf.Doc(metadata={"lang": "en"}) html = r"""<p> <h3 class="start">Suitable browsers</h3> The following browsers can be used for the course: Firefox, Internet Explorer, Chrome, Safari, Opera.<br /> Some other browsers have difficulties rendering our unit pages correctly. <br /> We recommend using only the fully updated latest versions of these browsers. In particular, the course cannot be completed with obsolete browsers such as Internet Explorer 8 or earlier. </p>""" elem_content = r"\\begin{{html}}\n{}\n\\end{{html}}" "".format(html) doc.content.extend([pf.RawBlock(elem_content, format="latex")]) elem = doc.content[0] # this sets up elem.parent ret = self.environments.handle_html(html, [], elem) header = ret[0] self.assertIsInstance(header, pf.Header) self.assertEqual(header.content[0].text, "Suitable") self.assertEqual(header.content[2].text, "browsers") para = ret[1] self.assertIsInstance(para, pf.Para) self.assertEqual(len(para.content), 107) self.assertEqual(para.content[106].text, "earlier.")
def test_handle_special_other(self): """special command with non-html code should be ignored""" # Note: 'html:' occuring in the middle of the string. doc = pf.Doc(pf.RawBlock(r'\special{python:print("html:")}'), format="latex") elem = doc.content[0] ret = self.commands.handle_special([r'python:print("html:")'], elem) self.assertIsNone(ret)
def test_handle_mlabel_no_last_header(self): """MTitle command without a last header element""" doc = pf.Doc(pf.RawBlock(r"\MLabel{TEST_LABEL}"), format="latex") elem = doc.content[0] ret = self.commands.handle_mlabel(["TEST_LABEL"], elem) self.assertIsInstance(ret, pf.Div) self.assertIn(INDEX_LABEL_PREFIX, ret.classes) self.assertEqual(ret.identifier, "{}-TEST_LABEL".format(INDEX_LABEL_PREFIX))
def test_handle_mugraphicssolo_inline(self): """MUGraphicsSolo command (inline)""" content = r"\MUGraphicsSolo{foo.jpg}{width=0.3\linewidth}{}" doc = pf.Doc(pf.Para(pf.RawInline(content, format="latex"))) elem = doc.content[0].content[0] # this sets up elem.parent elem_args = ["foo.jpg", r"width=0.3\linewidth", ""] ret = self.commands.handle_mugraphicssolo(elem_args, elem) self.assertIsInstance(ret, pf.Image) self.assertEqual(ret.url, "foo.jpg")
def test_handle_mvideo(self): video = pf.RawBlock(r"\MVideo{vidbsp1}{Carry out a case analysis.}", format="latex") doc = pf.Doc(video) elem = doc.content[0] # this sets up elem.parent cmd_args = ["vidbsp1", "Carry out a case analysis."] ret = self.commands.handle_mvideo(cmd_args, elem) self.assertIsInstance(ret.content[0], pf.Link) self.assertEqual(ret.content[0].title, "Carry out a case analysis.") self.assertEqual(ret.content[0].url, "vidbsp1.mp4")
def test_handle_msref(self): """MSRef command""" doc = pf.Doc(pf.RawBlock(r"\MSRef{fooid}{linktext}"), format="latex") elem = doc.content[0] # this sets up elem.parent ret = self.commands.handle_msref(["fooid", "linktext"], elem) link = ret.content[0] self.assertIsInstance(link, pf.Link) self.assertIsInstance(link.content[0], pf.Str) self.assertEqual(link.content[0].text, "linktext") self.assertEqual(link.url, "#fooid")
def setUp(self): self.doc = pf.Doc(metadata={"lang": "en"}) self.environments = Environments() self.elem_content = r""" \begin{MSectionStart} Lorem ipsum \end{MSectionStart}""" self.doc.content.extend( [pf.RawBlock(self.elem_content, format="latex")]) self.elem = self.doc.content[0] # this sets up elem.parent
def test_all(): x=pf.Para(pf.Str("a")) y=pf.Para(pf.Str("b")) c1=pf.TableCell(x) c2=pf.TableCell(y) row=pf.TableRow(c1,c2) t1 = pf.Table(row) t2 = pf.Table(row, header=row) print(t1.header) print(t2.header) with io.StringIO() as f: pf.dump(pf.Doc(t1), f) print(f.getvalue()) with io.StringIO() as f: pf.dump(pf.Doc(t2), f) print(f.getvalue())
def test_handle_special_detect_html(self): """special command should not be confused by a 'html:'.""" doc = pf.Doc( pf.RawBlock(r'\special{bar:<a href="#foo">html:</a>}'), format="latex", ) elem = doc.content[0] ret = self.commands.handle_special([r'bar:<a href="#foo">html:</a>'], elem) self.assertIsNone(ret)
def test_handle_msubject(self): """MSubject command""" doc = pf.Doc(pf.RawBlock(r"\MSubject{footitle}", format="latex")) elem = doc.content[0] # this sets up elem.parent ret = self.commands.handle_msubject(["footitle"], elem) self.assertIsInstance(ret, pf.Header) self.assertEqual(ret.level, 1) self.assertEqual(ret.content[0].text, "footitle") self.assertIsInstance(doc.metadata, pf.MetaMap) # pylint: disable=no-member self.assertEqual(doc.get_metadata("title"), "footitle")
def test_handle_mugraphicssolo_block(self): """MUGraphicsSolo command (block)""" content = r"\MUGraphicsSolo{foobar.png}{width=0.3\linewidth}{}" doc = pf.Doc(pf.RawBlock(content, format="latex")) elem = doc.content[0] # this sets up elem.parent elem_args = ["foobar.png", r"width=0.3\linewidth", ""] ret = self.commands.handle_mugraphicssolo(elem_args, elem) self.assertIsInstance(ret, pf.Div) img = ret.content[0].content[0] self.assertIsInstance(img, pf.Image) self.assertEqual(img.url, "foobar.png")
def test_handle_mlabel_last_header(self): """MTitle command with a last header element""" mlabel = pf.RawBlock(r"\MLabel{HEADER}") header = pf.Header(pf.Str("headertext")) doc = pf.Doc(header, mlabel, format="latex") elem = doc.content[0] remember(doc, "label", elem) ret = self.commands.handle_mlabel(["HEADER"], elem) self.assertFalse(ret) # pylint: disable=no-member self.assertEqual(header.identifier, "HEADER")
def test_handle_msubsection(self): """MSubsection command""" doc = pf.Doc(pf.RawBlock(r"\MSubsection{Foo title}"), format="latex") elem = doc.content[0] ret = self.commands.handle_msubsection(["Foo title"], elem) self.assertIsInstance(ret, pf.Header) self.assertIsInstance(ret.content[0], pf.Str) self.assertEqual(ret.content[0].text, "Foo") self.assertIsInstance(ret.content[1], pf.Space) self.assertIsInstance(ret.content[2], pf.Str) self.assertEqual(ret.content[2].text, "title") self.assertEqual(ret.level, 2)
def test_handle_msubsubsubsectionx(self): doc = pf.Doc( pf.RawBlock(r"\MSubsubsubsectionx{Subsubsubsectionx}"), format="latex", ) elem = doc.content[0] ret = self.commands.handle_msubsubsubsectionx(["Subsubsubsectionx"], elem) self.assertIsInstance(ret, pf.Header) self.assertIsInstance(ret.content[0], pf.Str) self.assertEqual(ret.content[0].text, "Subsubsubsectionx") self.assertEqual(ret.level, 4)
def test_handle_mtitle(self): """MTitle command""" doc = pf.Doc(pf.RawBlock(r"\MTitle{Schöne Titel nach Maß?}"), format="latex") elem = doc.content[0] ret = self.commands.handle_mtitle(["Schöne Titel nach Maß?"], elem) self.assertIsInstance(ret, pf.Header) self.assertIsInstance(ret.content[0], pf.Str) self.assertEqual(ret.content[0].text, "Schöne") self.assertIsInstance(ret.content[6], pf.Str) self.assertEqual(ret.content[6].text, "Maß?") self.assertEqual(ret.level, 4)
def test_get_variable(): doc = pf.Doc(metadata={ "a": pf.MetaString("x"), "b": pf.MetaMap(c=pf.MetaString("y")) }) assert pf.get_option(default="a") == "a" assert pf.get_option({"a": 1}, "a") == 1 assert pf.get_option({"a": None}, "a", default=2) == 2 assert pf.get_option({"a": None}, "a", doc, "a") == "x" assert pf.get_option(doc=doc, doc_tag="b.c") == "y"
def test_handle_mentry(self): r"""\MEntry without math""" doc = pf.Doc(metadata={"lang": "en"}) doc.content.extend( [pf.Para(pf.RawInline(r"\MEntry{Bla bla}{bla}", format="latex"))]) elem = doc.content[0].content[0] ret = self.commands.handle_mentry(["Bla bla", "bla"], elem) self.assertIsInstance(ret, pf.Span) self.assertIn(INDEX_ATTRIBUTE, ret.attributes) strong = ret.content[0] self.assertIsInstance(strong, pf.Strong) self.assertEqual(strong.content[0].text, "Bla") self.assertEqual(strong.content[2].text, "bla")
def test_handle_special_html(self): """special command embedded html""" doc = pf.Doc( pf.RawBlock( r'\special{html:<a href="http://www.example.com">Bar</a>}'), format="latex", ) elem = doc.content[0] ret = self.commands.handle_special( [r'html:<a href="http://www.example.com">Bar</a>'], elem) self.assertIsInstance(ret, pf.RawBlock) self.assertEqual(ret.format, "html") self.assertEqual(ret.text, '<a href="http://www.example.com">Bar</a>')
def test_handle_special_html_replacing(self): """special command should not remove 'html:' in the middle of a string.""" doc = pf.Doc( pf.RawBlock(r'\special{html:<a href="#bar">html:</a>}'), format="latex", ) elem = doc.content[0] ret = self.commands.handle_special([r'html:<a href="#bar">html:</a>'], elem) self.assertIsInstance(ret, pf.RawBlock) self.assertEqual(ret.format, "html") self.assertEqual(ret.text, '<a href="#bar">html:</a>')
def test_handle_mugraphics_inline(self): """MUGraphics command (inline)""" content = r"\MUGraphics{foobar.png}{width=0.3\linewidth}{Footitle}" doc = pf.Doc( pf.Para(pf.RawInline(content, format="latex")), metadata={"lang": "en"}, ) elem = doc.content[0].content[0] # this sets up elem.parent elem_args = ["foobar.png", r"width=0.3\linewidth", "Footitle"] ret = self.commands.handle_mugraphics(elem_args, elem) self.assertIsInstance(ret, pf.Image) self.assertEqual(ret.url, "foobar.png") self.assertEqual(ret.title, "Footitle")
def test_handle_modsemph(self): """modsemph command""" content = r"\modsemph{foo $x^2$}" doc = pf.Doc(pf.RawBlock(content, format="latex"), metadata={"lang": "en"}) elem = doc.content[0] # this sets up elem.parent emph = self.commands.handle_modsemph(["foo $x^2$"], elem) self.assertIsInstance(emph, pf.Emph) string = emph.content[0] self.assertIsInstance(string, pf.Str) self.assertEqual(string.text, "foo") self.assertIsInstance(emph.content[1], pf.Space) math = emph.content[2] self.assertIsInstance(math, pf.Math) self.assertEqual(math.text, "x^2")
def test_remove_empty_paragraphs(self): """It should remove empty paras in document""" doc = pf.Doc( pf.Para(pf.Str("Foo"), pf.Space(), pf.Str("Bar")), pf.Para(), pf.Para(pf.Str("Bar"), pf.Space(), pf.Str("Baz")), ) remove_empty_paragraphs(doc) self.assertEqual(len(doc.content), 2) para1 = doc.content[0] self.assertEqual(para1.content[0].text, "Foo") self.assertEqual(para1.content[2].text, "Bar") para2 = doc.content[1] self.assertEqual(para2.content[0].text, "Bar") self.assertEqual(para2.content[2].text, "Baz")
def test_handle_mextlink(self): """MExtLink command""" block = pf.Para( pf.RawInline( r"\MExtLink{https://www.example.com/}{Example link}", format="latex", )) doc = pf.Doc(block) elem = doc.content[0].content[0] # this sets up elem.parent ret = self.commands.handle_mextlink( ["https://www.example.com/", "Example link"], elem) self.assertIsInstance(ret, pf.Link) self.assertIsInstance(ret.content[0], pf.Str) self.assertEqual(ret.content[0].text, "Example") self.assertEqual(ret.url, "https://www.example.com/")
def test_handle_msection(self): """MSection command""" doc = pf.Doc(pf.RawBlock(r"\MSection{A Test Title}"), format="latex") elem = doc.content[0] # this sets up elem.parent ret = self.commands.handle_msection(["A Test Title"], elem) self.assertIsInstance(ret, pf.Header) self.assertEqual(ret.level, 1) self.assertIsInstance(ret.content[0], pf.Str) self.assertEqual(ret.content[0].text, "A") self.assertIsInstance(ret.content[1], pf.Space) self.assertIsInstance(ret.content[2], pf.Str) self.assertEqual(ret.content[2].text, "Test") self.assertIsInstance(ret.content[4], pf.Str) self.assertEqual(ret.content[4].text, "Title")