gpt4 book ai didi

python - 如何在 pandocfilters 中获取带有 Markdown 的字幕?

转载 作者:行者123 更新时间:2023-12-05 06:21:13 27 4
gpt4 key购买 nike

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

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

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

```{.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)

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

get_captionpandocfilters.py是这样的:

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 可以在内部遵守 Markdown ?

Inline(我认为这可能是一种指定标题包含 Markdown 的方法)不是 pandocfilters.py 中定义的构造函数,可能是因为在哪里filter 在处理中被调用,它不被认为是嵌套的。

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)

最佳答案

Lua version of a PlantUML filter工作正常,至少当我将我的项目移动到使用 quarto 时是这样.

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

关于python - 如何在 pandocfilters 中获取带有 Markdown 的字幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59956163/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com