Wie bekomme ich Bildunterschriften mit Markdown in PandocFilters?Python

Python-Programme
Anonymous
 Wie bekomme ich Bildunterschriften mit Markdown in PandocFilters?

Post by Anonymous »

Ich habe den Plantuml -Filter ausprobiert, um Latexzahlen aus dem Plantuml -Code in Markdown -Quelle zu generieren. Es funktioniert gut (ich habe es geändert, um PDF für Latex zu generieren, da es die Textelemente in den Plantuml -Diagrammen bewahrt). < /p>

Das Problem mit diesem Filter (und allen Filtern, die die PandocFilters -API unter Verwendung der API von PandocFilters) nicht unterstützen. Das heißt, caption = "Hier ist ein Diagramm, das * nicht *, was Sie erwarten würden." Plantuml-fileName = foo.pdf (mit der Logik soll im AST für das Diagramm nichts zurückgeben und eine Ausgabedatei foo.pdf erstellen). Dann kann ich eine Bildunterschrift mit einer traditionellen Abbildung eine Markierungsformatierung erhalten: < /p>

Code: Select all

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

Dies funktioniert gut, aber das Definieren des Dateinamens ist zusätzliche Arbeit. < /p>

get_caption< /code> in pandocfilters.py ist wie folgt: < /p>

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
< /code>

Gibt es eine (einfache) Möglichkeit, dies zu ändern, damit Get_Caption < /code> den Markdown respektieren kann? < /p>

Inline< /code> (was ich für ein Weg sein könnte, um anzugeben, dass eine Beschriftung, die Markdown enthält) ist kein Konstruktor, das in Pandocfiltern definiert ist. class = "Lang-Py PrettyPrint-Override">#!/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)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post