2015-06-07 7 views
9

Ich versuche, einen Pandoc-Filter zu verwenden, um eine Markdown-Datei mit einem Tikz-Bild in HTML zu konvertieren. Ich bin auf Win 8.1 (und ich habe alle Abhängigkeiten - Pdflatex, Python 2.7, ImageMagick, und das Python-Paket pandocfilters). Ich verwende das Skript tikz.py, das John MacFarlane unter github bereitstellt.Wie Python-Filter mit Pandoc verwenden, um md mit Tikz zu HTML unter Windows 8.1 zu konvertieren

Ich fand eine ähnliche question auf der Pandoc Google Group und John MacFarlane schlägt vor, den Filter in einem Windows-Batch-Skript (der Filter muss eine ausführbare Datei sein). Hier ist meine Kommandozeileneingabe (Ich werde den Dateiinhalt unten zur Verfügung stellen).

pandoc -o temp.html --filter .\tikz.bat -s temp.md 

Aber ich bekomme immer den folgenden Fehler.

pandoc: Failed reading: satisfyElem 

Das Skript erzeugt die „tikz-images“ Unterordner, aber es ist leer, da die resultierende Ausgabedatei temp.html ist.

Wie kann ich das zum Funktionieren bringen? FWIW, das größere Ziel ist, dass die Eingabedateien R Markdown sind, aber ich möchte den Pandoc Markdown-Prozess zuerst verstehen.

Hier sind die Dateiinhalte.

tikz.bat

python tikz.py %* 

temp.md

\begin{tikzpicture} 

\draw [<->](-3,0)--(3,0); 
\draw (-2,-.2)--(-2,.2); 
\draw (-1,-.2)--(-1,.2); 
\draw(0,-.2)--(0,.2); 
\draw (1,-.2)--(1,.2); 
\draw (2,-.2)--(2,.2); 
\node[align=left,below] at (-4.5,-0.2) {Cash flow}; 
\node[align=left,above] at (-4.5,0.2) {Time period}; 
\node[align=left,above] at (-2,0.2) {-2}; 
\node[align=left,above] at (-1,0.2) {-1}; 
\node[align=left,above] at (0,0.2) {0}; 
\node[align=left,above] at (1,0.2) {+1}; 
\node[align=left,above] at (2,0.2) {+2}; 
\node[align=left,below] at (1,-0.2) {\$100}; 
\node[align=left,below] at (2,-0.2) {\$100}; 

\end{tikzpicture} 

Can this work? 

tikz.py

#!/usr/bin/env python 

""" 
Pandoc filter to process raw latex tikz environments into images. 
Assumes that pdflatex is in the path, and that the standalone 
package is available. Also assumes that ImageMagick's convert 
is in the path. Images are put in the tikz-images directory. 
""" 

import hashlib 
import re 
import os 
import sys 
import shutil 
from pandocfilters import toJSONFilter, Para, Image 
from subprocess import Popen, PIPE, call 
from tempfile import mkdtemp 

imagedir = "tikz-images" 


def sha1(x): 
    return hashlib.sha1(x.encode(sys.getfilesystemencoding())).hexdigest() 


def tikz2image(tikz, filetype, outfile): 
    tmpdir = mkdtemp() 
    olddir = os.getcwd() 
    os.chdir(tmpdir) 
    f = open('tikz.tex', 'w') 
    f.write("""\\documentclass{standalone} 
      \\usepackage{tikz} 
      \\begin{document} 
      """) 
    f.write(tikz) 
    f.write("\n\\end{document}\n") 
    f.close() 
    p = call(["pdflatex", 'tikz.tex'], stdout=sys.stderr) 
    os.chdir(olddir) 
    if filetype == 'pdf': 
     shutil.copyfile(tmpdir + '/tikz.pdf', outfile + '.pdf') 
    else: 
     call(["convert", tmpdir + '/tikz.pdf', outfile + '.' + filetype]) 
    shutil.rmtree(tmpdir) 


def tikz(key, value, format, meta): 
    if key == 'RawBlock': 
     [fmt, code] = value 
     if fmt == "latex" and re.match("\\\\begin{tikzpicture}", code): 
      outfile = imagedir + '/' + sha1(code) 
      if format == "html": 
       filetype = "png" 
      elif format == "latex": 
       filetype = "pdf" 
      else: 
       filetype = "png" 
      src = outfile + '.' + filetype 
      if not os.path.isfile(src): 
       try: 
        os.mkdir(imagedir) 
        sys.stderr.write('Created directory ' + imagedir + '\n') 
       except OSError: 
        pass 
       tikz2image(code, filetype, outfile) 
       sys.stderr.write('Created image ' + src + '\n') 
      return Para([Image([], [src, ""])]) 

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

aktualisieren ich, dass die caps.py in den Kommentaren erwähnen Filter scheitert auch mit den gleichen Symptomen. Vielleicht sollte ich auch die Symptome von python caps.py temp.md hinzufügen, die den Filter außerhalb von Pandoc aufrufen. Mein Verständnis ist, dass dies die caps.py Datei auf dem Bildschirm in Großbuchstaben drucken sollte.

Allerdings, wenn ich python caps.py temp.md von der Windows-Eingabeaufforderung ausführen hängt es. Ich töte den Befehl mit CTRL-C, dann bekomme ich Folgendes.

C:\Users\Richard\Desktop\temp>python caps.py temp.md 
Traceback (most recent call last): 
    File "caps.py", line 15, in <module> 
    toJSONFilter(caps) 

Das gleiche geschieht mit python tikz.py temp.md. Ein Hang, gefolgt von:

C:\Users\Richard\Desktop\temp>python tikz.py temp.md 
Traceback (most recent call last): 
    File "tikz.py", line 70, in <module> 
    toJSONFilter(tikz) 

Update 2 Ich habe versucht, die Windows-Debugger auf der Eingabeaufforderung laufen, aber ich bin mir nicht sicher, dass es funktioniert. Manchmal würde die Eingabeaufforderung hängen. Und es scheint, als würde der Debugger auch hängen. Hier ist die Ausgabe vom Debugger.

*** wait with pending attach 
Symbol search path is: *** Invalid *** 
**************************************************************************** 
* Symbol loading may be unreliable without a symbol search path.   * 
* Use .symfix to have the debugger choose a symbol path.     * 
* After setting your symbol path, use .reload to refresh symbol locations. * 
**************************************************************************** 
Executable search path is: 
ModLoad: 00007ff7`0d920000 00007ff7`0d97d000 C:\windows\system32\cmd.exe 
ModLoad: 00007fff`b7c20000 00007fff`b7dcc000 C:\windows\SYSTEM32\ntdll.dll 
ModLoad: 00007fff`b5c90000 00007fff`b5dce000 C:\windows\system32\KERNEL32.DLL 
ModLoad: 00007fff`b4e40000 00007fff`b4f55000 C:\windows\system32\KERNELBASE.dll 
ModLoad: 00007fff`b7b70000 00007fff`b7c1a000 C:\windows\system32\msvcrt.dll 
ModLoad: 00007fff`b3070000 00007fff`b307e000 C:\windows\SYSTEM32\winbrand.dll 
(1c7c.29a0): Break instruction exception - code 80000003 (first chance) 
*** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\windows\SYSTEM32\ntdll.dll - 
ntdll!DbgBreakPoint: 
00007fff`b7cb2cf0 cc    int  3 

Update 3 Hier sind die Dateien in einem Dropbox folder. Dieser Ordner enthält dieselben Dateien, die ich oben eingefügt habe, sowie die Datei caps.py, die direkt von den Pandoc-Filtern github repo stammt.

+1

Haben Sie versucht, ein einfachen Probenfilter, wie die Aktivierung Filter? – mb21

+0

@ mb21 - Ja, ich habe 'caps.py' mit dem gleichen Setup versucht (zB in einer Batch-Datei mit' python caps.py% * '), aber ich bekomme den gleichen Fehler:' pandoc: Fehler beim Lesen: satisfyElem'. Danke, das hätte ich in meine Frage aufgenommen. Ich denke, der Fehler hat damit zu tun, Argumente an 'tikz.py' übergeben zu können, aber mein Python ist nicht gut genug, um das herauszufinden. –

+0

Könnten Sie vielleicht den gleichen Befehl mit [dieses Windows-Debugging-Tool] (https://msdn.microsoft.com/en-us/library/windows/hardware/ff552060 (v = vs.85) .aspx) ausführen und die Ausgabe ? . – Schopenhauer

Antwort

0

Die -t Option wird durch ein Format nicht eine Datei mit der Erweiterung zum Beispiel pandoc gefolgt -f json -t Abschlag ausgeben wird, dass Abschlag, -t html ausgegeben html etcetera die Ausgabe erfassen eine Umleitung Operator operation> file.some_extension. Aber deine Ausgabe geht zur Konsole. Die korrekte Syntax lautet also wörtlich pandoc -f json -t markdown.

Also the pandoc documentation. Wenn Sie auf Probleme stoßen, versuchen Sie, Ihre Lune zu ändern von: pandoc -o temp.html --filter .\tikz.bat -s temp.md ==> pandoc -t json | ./caps.py latex | pandoc -f json -t html.

Dies ist, wie es funktioniert.

    source format = input_file.html 
         ↓ 
        (pandoc) = pandoc -t json input_file.html 
         ↓ 
       JSON-formatted AST 
         ↓ 
        (filter) = python $HOME/Downloads/pandocfilters-1.2.4/examples/caps.py 
         ↓ 
       JSON-formatted AST 
         ↓ 
        (pandoc) = pandoc -f json -t markdown 
         ↓ 
       target format = output_file.md 

Separate die Befehle Ausgang zu untersuchen und ein Rohr verwenden | Ausgang umleiten:

pandoc -t json ~/testing/testing.html | python examples/caps.py | pandoc -f json -t markdown > output_file.md 

Keine Notwendigkeit pandocfilters laden Sie die TAR-Datei, führen Sie tar -xvf file.xyz oder verwenden Sie eine andere Anwendung der Wahl und beziehen sich auf installieren, um die Beispiele Aufruf Python dir/to/script.py dann pipe die out put wieder in pandoc und redigieren output zu gewünschten Dateiformat. Hier ist Zeile für Zeile:

$pandoc -t json ~/testing/testing.html 
[{"unMeta":{"viewport":{"t":"MetaInlines","c":[{"t":"Str","c":"width=device-width,"},{"t":"Space","c":[]},{"t":"Str","c":"initial-scale=1"}]},"title":{"t":"MetaInlines","c":[]},"description":{"t":"MetaInlines","c":[]}}},[{"t":"Para","c":[{"t":"Str","c":"Hello"},{"t":"Space","c":[]},{"t":"Str","c":"world!"},{"t":"Space","c":[]},{"t":"Str","c":"This"},{"t":"Space","c":[]},{"t":"Str","c":"is"},{"t":"Space","c":[]},{"t":"Str","c":"HTML5"},{"t":"Space","c":[]},{"t":"Str","c":"Boilerplate."}]},{"t":"Para","c":[{"t":"Str","c":"l"}]}]] 

dann:

$pandoc -t json ~/testing/testing.html | python examples/caps.py 
[{"unMeta": {"description": {"c": [], "t": "MetaInlines"}, "viewport": {"c": [{"c": "WIDTH=DEVICE-WIDTH,", "t": "Str"}, {"c": [], "t": "Space"}, {"c": "INITIAL-SCALE=1", "t": "Str"}], "t": "MetaInlines"}, "title": {"c": [], "t": "MetaInlines"}}}, [{"c": [{"c": "HELLO", "t": "Str"}, {"c": [], "t": "Space"}, {"c": "WORLD!", "t": "Str"}, {"c": [], "t": "Space"}, {"c": "THIS", "t": "Str"}, {"c": [], "t": "Space"}, {"c": "IS", "t": "Str"}, {"c": [], "t": "Space"}, {"c": "HTML5", "t": "Str"}, {"c": [], "t": "Space"}, {"c": "BOILERPLATE.", "t": "Str"}], "t": "Para"}, {"c": [{"c": "L", "t": "Str"}], "t": "Para"}]] 

schließlich:

pandoc -t json ~/testing/testing.html | python examples/caps.py | pandoc -f json -t markdown 
HELLO WORLD! THIS IS HTML5 BOILERPLATE. 

Hinweise:

diff -y pandoc_json.txt caps_json.txt 
[{"unMeta":{"viewport":{"t":"MetaInlines","c":[{"t":"Str","c"/[{"unMeta": {"description": {"c": [], "t": "MetaInlines"}, "v 
+0

Mein Batch-Skript ruft dieses Skript auf, obwohl ich 'easy_install pandocfilters' verwende, um es aus dem Repository zu installieren. Der Batch-Skript-Wrapper ist die Lösung, die John MacFarlane vorschlägt (und die Poster-Claims funktionieren, obwohl er die Arbeitslösung nicht bereitstellt) in dem Link, den ich in der Frage anbiete. –

+0

Können Sie 'Pandoc -t Json | versuchen ./caps.py Latex | pandoc -f json -t temp.md'? – Schopenhauer

+0

Gleicher 'satisfyElem' Fehler, obwohl ich das letzte' -t' entfernen musste. –