From e539f4aa7c84f5a572b9c25d3a9bb8a282c7bec5 Mon Sep 17 00:00:00 2001 From: "R.Higgins" Date: Mon, 16 Apr 2018 13:43:27 +0200 Subject: [PATCH 1/4] added latex output support, split plantuml command added ability to write pdf output as latex as opposed to a png --- pandoc_plantuml_filter.py | 17 +++++++++-------- tests/sample.md | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/pandoc_plantuml_filter.py b/pandoc_plantuml_filter.py index 9fa3cfa..db16ff4 100644 --- a/pandoc_plantuml_filter.py +++ b/pandoc_plantuml_filter.py @@ -10,7 +10,7 @@ import sys import subprocess -from pandocfilters import toJSONFilter, Para, Image +from pandocfilters import toJSONFilter, Para, Image, RawBlock from pandocfilters import get_filename4code, get_caption, get_extension PLANTUML_BIN = os.environ.get('PLANTUML_BIN', 'plantuml') @@ -24,7 +24,7 @@ def plantuml(key, value, format_, _): caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("plantuml", code) - filetype = get_extension(format_, "png", html="svg", latex="png") + filetype = get_extension(format_, "png", html="svg", latex="latex") src = filename + '.uml' dest = filename + '.' + filetype @@ -34,13 +34,14 @@ def plantuml(key, value, format_, _): if not txt.startswith(b"@start"): txt = b"@startuml\n" + txt + b"\n@enduml\n" with open(src, "wb") as f: - f.write(txt) - - subprocess.check_call([ - PLANTUML_BIN, "-t" + filetype, src]) + f.write(txt) + subprocess.check_call(PLANTUML_BIN.split() + ["-t" + filetype, src]) sys.stderr.write('Created image ' + dest + '\n') - - return Para([Image([ident, [], keyvals], caption, [dest, typef])]) + if filetype=="latex": + latex = open(dest).read() + return RawBlock('latex', latex.split("\\begin{document}")[-1].split("\\end{document}")[0]) + else: + return Para([Image([ident, [], keyvals], caption, [dest, typef])]) def main(): diff --git a/tests/sample.md b/tests/sample.md index 7cb34a0..50a16fd 100644 --- a/tests/sample.md +++ b/tests/sample.md @@ -1,3 +1,7 @@ +--- +header-includes: | + \usepackage{tikz} +--- # Some PantUML sample ```plantuml @@ -8,4 +12,30 @@ Alice -> Bob: Another authentication Request Alice <-- Bob: another authentication Response ``` +```plantuml + +Alice -> Bob: Authentication Request + +alt successful case + + Bob -> Alice: Authentication Accepted + +else some kind of failure + + Bob -> Alice: Authentication Failure + group My own label + Alice -> Log : Log attack start + loop 1000 times + Alice -> Bob: DNS Attack + end + Alice -> Log : Log attack end + end + +else Another type of failure + + Bob -> Alice: Please repeat + +end +``` + Nice, huh? From bfaccc0d613e499735ea2439ae1ff2081bf5dd12 Mon Sep 17 00:00:00 2001 From: Ronan Higgins Date: Tue, 1 May 2018 23:05:11 +0200 Subject: [PATCH 2/4] first passed at filter options via header --- pandoc_plantuml_filter.py | 9 +++++++- tests/sample.md | 6 ++---- tests/sample_latex_uml.md | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 tests/sample_latex_uml.md diff --git a/pandoc_plantuml_filter.py b/pandoc_plantuml_filter.py index db16ff4..7e7004f 100644 --- a/pandoc_plantuml_filter.py +++ b/pandoc_plantuml_filter.py @@ -9,14 +9,21 @@ import os import sys import subprocess +import re from pandocfilters import toJSONFilter, Para, Image, RawBlock from pandocfilters import get_filename4code, get_caption, get_extension PLANTUML_BIN = os.environ.get('PLANTUML_BIN', 'plantuml') +pattern = re.compile('%\{(.*)\}$') def plantuml(key, value, format_, _): + + if key == 'Header': + sys.stderr.write(key+"\n") + sys.stderr.write(format_+"\n") + sys.stderr.write(str(_)'header-includes'].keys())+"\n") if key == 'CodeBlock': [[ident, classes, keyvals], code] = value @@ -34,7 +41,7 @@ def plantuml(key, value, format_, _): if not txt.startswith(b"@start"): txt = b"@startuml\n" + txt + b"\n@enduml\n" with open(src, "wb") as f: - f.write(txt) + f.write(txt) subprocess.check_call(PLANTUML_BIN.split() + ["-t" + filetype, src]) sys.stderr.write('Created image ' + dest + '\n') if filetype=="latex": diff --git a/tests/sample.md b/tests/sample.md index 50a16fd..a23ee14 100644 --- a/tests/sample.md +++ b/tests/sample.md @@ -1,7 +1,3 @@ ---- -header-includes: | - \usepackage{tikz} ---- # Some PantUML sample ```plantuml @@ -36,6 +32,8 @@ else Another type of failure Bob -> Alice: Please repeat end + + ``` Nice, huh? diff --git a/tests/sample_latex_uml.md b/tests/sample_latex_uml.md new file mode 100644 index 0000000..2c1f55f --- /dev/null +++ b/tests/sample_latex_uml.md @@ -0,0 +1,43 @@ +--- +header-includes: | + \usepackage{tikz} +--- +# Some PantUML sample + +```plantuml +Alice -> Bob: Authentication Request +Bob --> Alice: Authentication Response + +Alice -> Bob: Another authentication Request +Alice <-- Bob: another authentication Response +``` + +```plantuml + +Alice -> Bob: Authentication Request + +alt successful case + + Bob -> Alice: Authentication Accepted + +else some kind of failure + + Bob -> Alice: Authentication Failure + group My own label + Alice -> Log : Log attack start + loop 1000 times + Alice -> Bob: DNS Attack + end + Alice -> Log : Log attack end + end + +else Another type of failure + + Bob -> Alice: Please repeat + +end + + +``` + +Nice, huh? From 525315d1ffb66b5483ca74a77fb9f54099925929 Mon Sep 17 00:00:00 2001 From: Ronan Higgins Date: Tue, 1 May 2018 23:31:05 +0200 Subject: [PATCH 3/4] working example with latex export activated via header-includes \usepackage{tikz} in the markdown meta data --- pandoc_plantuml_filter.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pandoc_plantuml_filter.py b/pandoc_plantuml_filter.py index 7e7004f..51072b0 100644 --- a/pandoc_plantuml_filter.py +++ b/pandoc_plantuml_filter.py @@ -21,17 +21,21 @@ def plantuml(key, value, format_, _): if key == 'Header': - sys.stderr.write(key+"\n") - sys.stderr.write(format_+"\n") - sys.stderr.write(str(_)'header-includes'].keys())+"\n") + if 'tikz'in (str(_)): + os.environ["PLANTUML_LATEX_EXPORT"] = 'latex' if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plantuml" in classes: caption, typef, keyvals = get_caption(keyvals) - + # + if "PLANTUML_LATEX_EXPORT" in os.environ: + plantuml_latex_export="latex" + else: + plantuml_latex_export="png" + # filename = get_filename4code("plantuml", code) - filetype = get_extension(format_, "png", html="svg", latex="latex") + filetype = get_extension(format_, "png", html="svg", latex=plantuml_latex_export) src = filename + '.uml' dest = filename + '.' + filetype @@ -44,7 +48,7 @@ def plantuml(key, value, format_, _): f.write(txt) subprocess.check_call(PLANTUML_BIN.split() + ["-t" + filetype, src]) sys.stderr.write('Created image ' + dest + '\n') - if filetype=="latex": + if (filetype=="latex") and (plantuml_latex_export=='latex'): latex = open(dest).read() return RawBlock('latex', latex.split("\\begin{document}")[-1].split("\\end{document}")[0]) else: From 1dc1ab2acb60f412a1a6f834c5c2b02cfdcca8ee Mon Sep 17 00:00:00 2001 From: Ronan Higgins Date: Tue, 1 May 2018 23:33:00 +0200 Subject: [PATCH 4/4] removed re library --- pandoc_plantuml_filter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandoc_plantuml_filter.py b/pandoc_plantuml_filter.py index 51072b0..3215f0d 100644 --- a/pandoc_plantuml_filter.py +++ b/pandoc_plantuml_filter.py @@ -9,7 +9,6 @@ import os import sys import subprocess -import re from pandocfilters import toJSONFilter, Para, Image, RawBlock from pandocfilters import get_filename4code, get_caption, get_extension