Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raw latex support #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions pandoc_plantuml_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,31 @@
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')

pattern = re.compile('%\{(.*)\}$')

def plantuml(key, value, format_, _):

if key == 'Header':
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="png")
filetype = get_extension(format_, "png", html="svg", latex=plantuml_latex_export)

src = filename + '.uml'
dest = filename + '.' + filetype
Expand All @@ -35,12 +45,13 @@ def plantuml(key, value, format_, _):
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])
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") and (plantuml_latex_export=='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():
Expand Down
28 changes: 28 additions & 0 deletions tests/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,32 @@ 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?
43 changes: 43 additions & 0 deletions tests/sample_latex_uml.md
Original file line number Diff line number Diff line change
@@ -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?