如何在pandocfilters中获得带有标记的标题?

2024-06-01 01:53:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用PlantUML filter从markdown源代码中的PlantUML代码生成LaTeX图形。它工作得很好(我将其更改为生成LaTeX的PDF,因为它保留了PlantUML图中的文本项)

这个过滤器(以及所有使用pandocfilters API的过滤器)的问题在于标题不支持标记。也就是说,通过caption="Here is a diagram that is *not* what you'd expect."将产生一个带有*not*的乳胶图形,而不是not(斜体)

我的解决方法是向过滤器添加两个键:hide-image=trueplantuml-filename=foo.pdf(逻辑是不在AST中为图表返回任何内容,而是创建输出文件foo.pdf)。然后,我可以使用传统图形获得标题的降价格式:

```{.plantuml hide-image=true plantuml-filename=foo.pdf}
@startuml
A -> B : hello
@enduml
```

![Here is a diagram that is *not* what you'd expect](foo.pdf)

这很好,但是定义文件名是额外的工作

{a2}中的get_caption是这样的:

def get_caption(kv):
    """get caption from the keyvalues (options)
    Example:
      if key == 'CodeBlock':
        [[ident, classes, keyvals], code] = value
        caption, typef, keyvals = get_caption(keyvals)
        ...
        return Para([Image([ident, [], keyvals], caption, [filename, typef])])
    """
    caption = []
    typef = ""
    value, res = get_value(kv, u"caption")
    if value is not None:
        caption = [Str(value)]
        typef = "fig:"

    return caption, typef, res

有没有一种(简单的)方法来修改它,这样get_caption就可以在内部遵守降价

Inline(我认为这可能是一种指定标题包含标记的方法)不是在pandocfilters.py中定义的构造函数,可能是因为在处理过程中调用过滤器的地方,并不假定它是嵌套的

My (hacked) version of the PlantUML filter位于GitHub上:

#!/usr/bin/env python

"""
Pandoc filter to process code blocks with class "plantuml" into
plant-generated images.

Needs `plantuml.jar` from http://plantuml.com/.
"""

import os
import shutil
import sys
from subprocess import call

from pandocfilters import toJSONFilter, Para, Image, get_filename4code, get_caption, get_extension


def plantuml(key, value, format, _):
    if key == 'CodeBlock':
        [[ident, classes, keyvals], code] = value

        if "plantuml" in classes:
            caption, typef, keyvals = get_caption(keyvals)

            filename = get_filename4code("plantuml", code)
            filetype = get_extension(format, "png", html="svg", latex="pdf")

            src = filename + '.puml'
            plantuml_output = filename + '.' + filetype

            dest_spec = ""
            # Key to specify final destination the file
            for ind, keyval in enumerate(keyvals):
                if keyval[0] == 'plantuml-filename':
                    dest_spec = keyval[1]
                    keyvals.pop(ind)
                    break

            # Generate image only once
            if not os.path.isfile(plantuml_output):
                txt = code.encode(sys.getfilesystemencoding())
                if not txt.startswith("@start"):
                    txt = "@startuml\n" + txt + "\n@enduml\n"
                with open(src, "w") as f:
                    f.write(txt)
                # Must not let messages go to stdout, as it will corrupt JSON in filter
                with open('plantUMLErrors.log', "w") as log_file:
                    call(["java", "-jar", "filters/plantuml/plantuml.jar", "-t"+filetype, src], stdout=log_file)
                sys.stderr.write('Created image ' + plantuml_output + '\n')
                if not dest_spec == "": 
                    sys.stderr.write('Copying image from ' + plantuml_output + ' to ' + dest_spec + '\n')
                    shutil.copy2(plantuml_output, dest_spec)
                    plantuml_output = dest_spec


            for ind, keyval in enumerate(keyvals):
                if keyval[0] == 'hide-image':
                    if keyval[1] == 'true':
                        sys.stderr.write('Not showing image ' + plantuml_output + '\n')
                        return [] # surpress image in JSON

            return Para([Image([ident, [], keyvals], caption, [plantuml_output, typef])])

if __name__ == "__main__":
    toJSONFilter(plantuml)

Tags: imageoutputgetifisvaluenotfilename