From 929155613819765fe90b9008a7142d38060854f4 Mon Sep 17 00:00:00 2001 From: fliiiix Date: Sun, 10 Dec 2023 13:20:18 +0100 Subject: [PATCH] Test and support filetype config via cli --- README.md | 16 ++++++++ pandoc_plantuml_filter.py | 23 ++++++++--- tests/test_integration.py | 46 ++++++++++++++++++++-- tests/testdata/single-diagram-with-meta.md | 13 ++++++ 4 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 tests/testdata/single-diagram-with-meta.md 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 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 @@ ["\\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 +```