Repository: timofurrer/pandoc-plantuml-filter

Issue:
Generate PDF (pdflatex) with SVG instead of PNG image
Hi there, 
thanks for your work.

On line [39](https://github.com/timofurrer/pandoc-plantuml-filter/blob/e4db7152c04734e405d1561c2dcd85d6de0df38e/pandoc_plantuml_filter.py#L39) you specify the use of PNG format in combination with latex. 

By installing librsvg and its dependencies I managed to create a PDF with SVG as image output from plantuml. 

```
brew install librsvg
```

On the terminal it produced the following issue: 

```
Warning: Failed to set locale category LC_NUMERIC to en_DE.
Warning: Failed to set locale category LC_TIME to en_DE.
Warning: Failed to set locale category LC_COLLATE to en_DE.
Warning: Failed to set locale category LC_MONETARY to en_DE.
Warning: Failed to set locale category LC_MESSAGES to en_DE.
```

This is solved by exporting a locale that actually exists. In my case: 
```
export LC_ALL=de_DE.UTF-8
```



Discussion:

Patch:
diff --git a/.gitignore b/.gitignore
index e7545c3..39fb51f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,8 @@
 .env/
+build/
+dist/
 __pycache__/
+
 example.png
 images/
 missfont.log
diff --git a/README.md b/README.md
index bea0d26..ba80dfc 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/pandoc_plantuml_filter.py b/pandoc_plantuml_filter.py
index 53509ba..113bc9c 100755
--- a/pandoc_plantuml_filter.py
+++ b/pandoc_plantuml_filter.py
@@ -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
@@ -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


Test patch:
diff --git a/tests/test_integration.py b/tests/test_integration.py
index 41c17ca..d7c8f9b 100644
--- a/tests/test_integration.py
+++ b/tests/test_integration.py
@@ -1,4 +1,5 @@
 import os
+import re
 import subprocess
 
 from pathlib import Path
@@ -18,15 +19,23 @@ __TEST_BASE_DIR__ = Path(os.path.dirname(__file__)) / "testdata"
             ["\\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")
 
@@ -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
diff --git a/tests/testdata/single-diagram-with-meta.md b/tests/testdata/single-diagram-with-meta.md
new file mode 100644
index 0000000..e366534
--- /dev/null
+++ b/tests/testdata/single-diagram-with-meta.md
@@ -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
+```