Beispiel #1
0
 def test_merge_emph2(self):
     ast = deepcopy(self.ast2)
     ast.walk(self.merge_consecutive_type)
     res = convert_text(ast, input_format="panflute", output_format="native")
     ref = pf.Para(self.ElementType(pf.Str("a"), pf.Space, pf.Str("b")))
     ref_native = convert_text(ref, input_format="panflute", output_format="native")
     assert res == ref_native
Beispiel #2
0
 def test_cancel_emph(self):
     ast = deepcopy(self.ast_double_type)
     ast.walk(self.cancel_repeated_type)
     res = convert_text(ast, input_format="panflute", output_format="native")
     ref = pf.Para(pf.Str("a"), pf.Space, self.ElementType(pf.Str("b")))
     ref_native = convert_text(ref, input_format="panflute", output_format="native")
     assert res == ref_native
Beispiel #3
0
 def test_to_emph_simple(self):
     ast = deepcopy(self.simple)
     ast.walk(self.to_type)
     res = convert_text(ast, input_format="panflute", output_format="native")
     ref = pf.Para(self.ElementType(pf.Str("a")), pf.Space, self.ElementType(pf.Str("b")))
     ref_native = convert_text(ref, input_format="panflute", output_format="native")
     assert res == ref_native
Beispiel #4
0
 def test_all_emph_together(self):
     ast = deepcopy(self.combined)
     ast.walk(self.to_type)
     ast.walk(self.cancel_repeated_type)
     ast.walk(self.merge_consecutive_type)
     res = convert_text(ast, input_format="panflute", output_format="native")
     ref = pf.Para(pf.Str("a"), pf.Str("b"), pf.Space, self.ElementType(pf.Str("c"), pf.Str("d")))
     ref_native = convert_text(ref, input_format="panflute", output_format="native")
     assert res == ref_native
Beispiel #5
0
 def test_filter_non_latex(self):
     doc = convert_text(self.text, standalone=True)
     # force to convert to latex
     doc.format = "markdown"
     main(doc)
     res = convert_text(
         doc,
         input_format="panflute",
         output_format="markdown",
     )
     assert res.strip() == self.ref_md.strip()
Beispiel #6
0
 def test_filter_latex(self):
     doc = convert_text(self.text, standalone=True)
     # force to convert to latex
     doc.format = "latex"
     main(doc)
     res = convert_text(
         doc,
         input_format="panflute",
         output_format="latex",
         extra_args=["--top-level-division=chapter", "--toc", "-N"],
     )
     assert res.strip() == self.ref_latex.strip()
Beispiel #7
0
def parse_markdown_as_inline(markdown: str) -> list[Element]:
    """Convert markdown string to panflute AST inline elements."""
    ast = convert_text(markdown)
    res: list[Element] = []
    for e in ast:
        if isinstance(e, pf.Para):
            res += list(e.content)
        else:
            res.append(e)
    return res
Beispiel #8
0
def iter_convert_texts_markdown_to_panflute(
    texts: Iterable[str],
    extra_args: Optional[List[str]] = None,
) -> Iterator[ListContainer]:
    '''a faster, specialized convert_texts
    '''
    # put each text in a Div together
    text = '\n\n'.join((f'''::: PanTableDiv :::

{text}

:::''' for text in texts))
    pf = convert_text(text,
                      input_format='markdown',
                      output_format='panflute',
                      extra_args=extra_args)
    return (elem.content for elem in pf)
Beispiel #9
0
def parse_markdown_codeblock(text: str) -> dict:
    '''parse markdown CodeBlock just as `panflute.yaml_filter` would

    useful for development to obtain the objects that the filter
    would see after passed to `panflute.yaml_filter`

    :param str text: must be a single codeblock of class table in markdown
    '''
    def function(**kwargs):
        return kwargs

    doc = convert_text(text, standalone=True)
    return yaml_filter(doc.content[0],
                       doc,
                       tag='table',
                       function=function,
                       strict_yaml=True)
Beispiel #10
0
def iter_convert_texts_panflute_to_markdown(
    elems: Iterable[ListContainer],
    extra_args: Optional[List[str]] = None,
    seperator: str = np.random.randint(
        65, 91, size=256, dtype=np.uint8).view('S256')[0].decode(),
) -> Iterator[str]:
    '''a faster, specialized convert_texts

    :param list elems: must be list of ListContainer of Block.
        This is more restrictive than convert_texts which can also accept list of Block
    :param str seperator: a string for seperator in the temporary markdown output
    '''
    def iter_seperator(elems: List[ListContainer], inserter: Para):
        '''insert between every element in a ListContainer'''
        for elem in elems:
            for i in elem:
                yield i
            yield inserter

    def iter_split_by_seperator(text: str, seperator: str) -> Iterator[str]:
        '''split the text into list by the seperator
        '''
        temp = []
        for line in text.splitlines():
            if line != seperator:
                temp.append(line)
            else:
                res = '\n'.join(temp).strip()
                # reset for next yield
                temp = []
                yield res

    inserter = Para(Str(seperator))

    elems_inserted = ListContainer(*iter_seperator(elems, inserter))
    # reference-location=block for footnotes, see issue #58
    texts_converted = convert_text(elems_inserted,
                                   input_format='panflute',
                                   output_format='markdown',
                                   extra_args=['--reference-location=block'])
    return iter_split_by_seperator(texts_converted, seperator)