Skip to content

Commit

Permalink
Test and support filetype config via cli
Browse files Browse the repository at this point in the history
  • Loading branch information
fliiiix committed Dec 10, 2023
1 parent b8e39c6 commit 9291556
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ Bob --> Alice: Authentication Response

The `plantuml-filename` parameter create a symlink for the destination picture, which could be used in the same file as an image directly.

### Control the output file-type

The generated file-type can be controlled via the file metadata:

```
---
plantuml-format: svg
---
```

Or directly via the cli `--metadata` argument.

```
pandoc tests/sample.md -o sample.pdf --filter pandoc-plantuml --metadata=plantuml-format=svg
```

## But there is ...

There are a few other filters trying to convert PlantUML code blocks however
Expand Down
23 changes: 18 additions & 5 deletions pandoc_plantuml_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ def rel_mkdir_symlink(src, dest):
os.symlink(src, dest)


def calculate_filetype(format_, plantuml_format):
if plantuml_format:
# File-type is overwritten via cli
# --metadata=plantuml-format:svg
if plantuml_format["t"] == "MetaString":
return get_extension(format_, plantuml_format["c"])
# File-type is overwritten in the meta data block of the document
# ---
# plantuml-format: svg
# ---
elif plantuml_format["t"] == "MetaInlines":
return get_extension(format_, plantuml_format["c"][0]["c"])

# Default per output-type eg. output-type: html -> file-type: svg
return get_extension(format_, "png", html="svg", latex="png")


def plantuml(key, value, format_, meta):
if key == "CodeBlock":
[[ident, classes, keyvals], code] = value
Expand All @@ -36,11 +53,7 @@ def plantuml(key, value, format_, meta):
caption, typef, keyvals = get_caption(keyvals)

filename = get_filename4code("plantuml", code)
if meta.get("plantuml-format"):
pformat = meta.get("plantuml-format", None)
filetype = get_extension(format_, pformat["c"][0]["c"])
else:
filetype = get_extension(format_, "png", html="svg", latex="png")
filetype = calculate_filetype(format_, meta.get("plantuml-format"))

src = filename + ".uml"
dest = filename + "." + filetype
Expand Down
46 changes: 43 additions & 3 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import subprocess

from pathlib import Path
Expand All @@ -18,15 +19,23 @@
["\\includegraphics", "images/example.png"],
["images/example.png"],
),
("single-diagram-with-filename-without-subdirectory", ["\\includegraphics", "example.png"], ["example.png"]),
(
"single-diagram-with-filename-without-subdirectory",
["\\includegraphics", "example.png"],
["example.png"],
),
(
"single-diagram-reference",
["\\includegraphics", "images/example.png", "\\includegraphics{images/example.png}"],
[
"\\includegraphics",
"images/example.png",
"\\includegraphics{images/example.png}",
],
["images/example.png"],
),
],
)
def test_digrams(mocker, tmp_path, filename, expected_content, expected_files):
def test_digrams(tmp_path, filename, expected_content, expected_files):
input_file = str(__TEST_BASE_DIR__ / f"{filename}.md")
output_file = str(tmp_path / f"{filename}.tex")

Expand All @@ -41,3 +50,34 @@ def test_digrams(mocker, tmp_path, filename, expected_content, expected_files):

for file in expected_files:
assert os.path.exists(file)


def test_filetype_param_from_meta(tmp_path):
input_file = str(__TEST_BASE_DIR__ / "single-diagram-with-meta.md")
output_file = str(tmp_path / "single-diagram-with-meta.tex")

cmd = subprocess.run(["pandoc", input_file, "-o", output_file, "--filter", "pandoc-plantuml"])
assert cmd.returncode == 0

with open(output_file) as f:
content = f.read()

pattern = re.compile(r"plantuml-images\/.*\.svg")
match = pattern.search(content)
assert match


def test_filetype_metadata_is_overridden_from_cli(tmp_path):
input_file = str(__TEST_BASE_DIR__ / "single-diagram-with-meta.md")
output_file = str(tmp_path / "single-diagram-with-meta.tex")
args = ["--metadata=plantuml-format:jpg"]

cmd = subprocess.run(["pandoc", input_file, "-o", output_file, "--filter", "pandoc-plantuml"] + args)
assert cmd.returncode == 0

with open(output_file) as f:
content = f.read()

pattern = re.compile(r"plantuml-images\/.*\.jpg")
match = pattern.search(content)
assert match
13 changes: 13 additions & 0 deletions tests/testdata/single-diagram-with-meta.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
plantuml-format: svg
---

# Single PlantUML digaram

```plantuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob: Another authentication Request
Alice <-- Bob: another authentication Response
```

0 comments on commit 9291556

Please sign in to comment.