首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在混乱过滤器中得到带有标记的标题?

如何在混乱过滤器中得到带有标记的标题?
EN

Stack Overflow用户
提问于 2020-01-28 20:01:10
回答 1查看 496关注 0票数 3

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

这个过滤器的问题(以及所有使用pandocfilters的过滤器)是,标题不支持标记。也就是说,传递caption="Here is a diagram that is *not* what you'd expect."将导致LaTeX中有* to *而不是to(斜体)的图形。

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

代码语言:javascript
复制
```{.plantuml hide-image=true plantuml-filename=foo.pdf}

@startuml

A -> B:你好

@enduml

代码语言:javascript
复制
![Here is a diagram that is *not* what you'd expect](foo.pdf)

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

get_caption in pandocfilters.py是这样的:

代码语言:javascript
复制
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中定义的构造函数,可能是因为在处理过程中调用过滤器的地方,它不被假定是嵌套的。

我的(黑)版本的PlantUML过滤器在GitHub上:

代码语言:javascript
复制
#!/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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-17 12:11:56

PlantUML过滤器的Lua版本只是起作用,至少当我将项目转移到使用夸托时是如此。

如果我使用latexmk作为PDF引擎,使用独立的作为html格式,我也不必担心文件名。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59956163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档