How to use the panflute.CodeBlock function in panflute

To help you get started, we’ve selected a few panflute examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github chdemko / pandoc-latex-tip / tests / test_tip.py View on Github external
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",
            api_version=(1, 17, 2),
        )
        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")
github chdemko / pandoc-latex-tip / tests / test_tip.py View on Github external
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",
            api_version=(1, 17, 2),
        )
        pandoc_latex_tip.main(doc)
        self.assertEqual(doc.content[0].format, "tex")
github sergiocorreia / panflute / examples / panflute / gabc.py View on Github external
"\n\\smallskip\n{%\n" +
                latexsnippet('\\gregorioscore{' + elem.text + '}', elem.attributes) +
                "%\n}" +
                label
            )
        else:
            infile = elem.text + (
                '.gabc' if '.gabc' not in elem.text else ''
            )
            with open(infile, 'r') as doc:
                code = doc.read().split('%%\n')[1]
            return Image(png(
                    elem.text,
                    latexsnippet('\\gregorioscore', elem.attributes)
                ))
    elif type(elem) == CodeBlock and "gabc" in elem.classes:
        if doc.format == "latex":
            if elem.identifier == "":
                label = ""
            else:
                label = '\\label{' + elem.identifier + '}'
            return latexblock(
                "\n\\smallskip\n{%\n" +
                latexsnippet('\\gabcsnippet{' + elem.text + '}', elem.attributes) +
                "%\n}" +
                label
                )
        else:
            return Para(Image(url=png(elem.text, latexsnippet('\\gabcsnippet', elem.attributes))))
github chdemko / pandoc-numbering / pandoc_numbering.py View on Github external
---------
        elem: elem to scan

    Returns
    -------
        []: if elem is an instance to remove
        None: otherwise
    """
    if isinstance(
        elem,
        (
            BlockQuote,
            BulletList,
            Citation,
            Cite,
            CodeBlock,
            Definition,
            DefinitionItem,
            DefinitionList,
            Div,
            Header,
            HorizontalRule,
            Image,
            LineBlock,
            LineBreak,
            LineItem,
            ListItem,
            Note,
            Para,
            RawBlock,
            RawInline,
            SoftBreak,
github kiwi0fruit / knitty / knitty / knitty.py View on Github external
def action(elem, doc):
    if isinstance(elem, pf.CodeBlock):
        input_ = elem.attributes.get('input', doc.get_metadata('input'))
        if str(input_).lower() == 'true':
            for clss in PANDOC_CODECELL_CLASSES: 
                if clss not in elem.classes:
                    elem.classes.append(clss)
github sergiocorreia / panflute / examples / panflute / plantuml.py View on Github external
def plantuml(elem, doc):
    if type(elem) == CodeBlock and 'plantuml' in elem.classes:
        if 'caption' in elem.attributes:
            caption = [Str(elem.attributes['caption'])]
            typef = 'fig:'
        else:
            caption = []
            typef = ''

        filetype = {'html': 'svg', 'latex': 'eps'}.get(doc.format, 'png')
        src = os.path.join(imagedir, filename + '.uml')
        dest = os.path.join(imagedir, filename + '.' + filetype)

        if not os.path.isfile(dest):
            try:
                os.mkdir(imagedir)
                sys.stderr.write('Created directory ' + imagedir + '\n')
            except OSError:
github sergiocorreia / panflute / examples / panflute / graphviz.py View on Github external
def graphviz(elem, doc):
    if type(elem) == CodeBlock and 'graphviz' in elem.classes:
        code = elem.text
        caption = "caption"
        G = pygraphviz.AGraph(string=code)
        G.layout()
        filename = sha1(code)
        filetype = {'html': 'png', 'latex': 'pdf'}.get(doc.format, 'png')
        alt = Str(caption)
        src = imagedir + '/' + filename + '.' + filetype
        if not os.path.isfile(src):
            try:
                os.mkdir(imagedir)
                sys.stderr.write('Created directory ' + imagedir + '\n')
            except OSError:
                pass
            G.draw(src)
            sys.stderr.write('Created image ' + src + '\n')
github kiwi0fruit / pandoctools / pandoctools / language_prefix.py View on Github external
def action(elem, doc):
    if isinstance(elem, pf.Code) or isinstance(elem, pf.CodeBlock):
        if elem.classes:
            elem.classes[0] = 'language-' + elem.classes[0]
github sergiocorreia / panflute-filters / filters / collapse_code.py View on Github external
def collapse(elem, doc):
    if isinstance(elem, pf.CodeBlock) and "html" in doc.format:
        content = [
            pf.RawBlock("<details>", format="html"),
            pf.RawBlock(
                """
                <summary> Show code </summary>
                """
            ),
            elem,
            pf.RawBlock("</details>", format="html"),
        ]
        div = pf.Div(*content, classes=["collapsible_code"])
        return div
github ickc / pantable / pantable / table_to_csv.py View on Github external
table_body = elem.content
        if options['header']:
            table_body.insert(0, elem.header)
        # table in list
        table_list = [[ast_to_markdown(cell.content)
                       for cell in row.content]
                      for row in table_body]
        # table in CSV
        with io.StringIO() as file:
            writer = csv.writer(file)
            writer.writerows(table_list)
            csv_table = file.getvalue()
        code_block = f"""---
{yaml_metadata}---
{csv_table}"""
        return panflute.CodeBlock(code_block, classes=["table"])
    return None