From 4410d06ccd866106a735955e5abb5bf049864d1c Mon Sep 17 00:00:00 2001 From: reemo3dp <150130755+reemo3dp@users.noreply.github.com> Date: Wed, 13 Mar 2024 11:45:51 +0100 Subject: [PATCH] feature(preview_generator): Write tests for markdown regex --- packages/preview_generator/README.md | 9 +- .../preview_generator/markdown_generator.py | 10 +- packages/preview_generator/poetry.lock | 97 ++++++++++++++++++- .../preview_generator/__init__.py | 3 +- .../preview_generator/markdown_regex.py | 3 + .../preview_generator/markdown_regex_test.py | 73 ++++++++++++++ packages/preview_generator/pyproject.toml | 3 + 7 files changed, 190 insertions(+), 8 deletions(-) create mode 100644 packages/preview_generator/preview_generator/markdown_regex.py create mode 100644 packages/preview_generator/preview_generator/markdown_regex_test.py diff --git a/packages/preview_generator/README.md b/packages/preview_generator/README.md index aad374e..db9d410 100644 --- a/packages/preview_generator/README.md +++ b/packages/preview_generator/README.md @@ -1,6 +1,13 @@ WoopWOopWoop -![Preview](preview_1046037910.gif) +![Preview](./preview_1898533314.gif) + +``` +layers: + static() top #FF0000 +``` + +![Preview](./preview_1046037910.gif) ```layers static() top #FF0000 diff --git a/packages/preview_generator/markdown_generator.py b/packages/preview_generator/markdown_generator.py index 8d5c4e8..7e1bc02 100644 --- a/packages/preview_generator/markdown_generator.py +++ b/packages/preview_generator/markdown_generator.py @@ -3,11 +3,13 @@ import os import argparse from preview_generator import generate_preview +from preview_generator import markdown_regex args = None def replace_with_preview(match): - layers = match.group(2) + content = match.group('block') + layers = match.group('layers') output_dir = os.path.dirname(args.markdown.name) filename = os.path.join(output_dir, args.directory, "preview_" + str(zlib.crc32(layers.encode('utf-8')))+".gif") relative_filename = "./"+os.path.relpath(filename, os.path.dirname(args.markdown.name)) @@ -19,9 +21,7 @@ def replace_with_preview(match): print("Using existing "+filename) return """![Preview](%s) -```layers -%s -```""" % (relative_filename, layers) +%s""" % (relative_filename, content) if __name__ == '__main__': @@ -42,7 +42,7 @@ def replace_with_preview(match): markdown = args.markdown.read() args.markdown.close() - markdown = re.sub(r'(!\[Preview\]\(.+\)\n+)?```layers\n(.*?)\n```', replace_with_preview, markdown) + markdown = markdown_regex.sub(replace_with_preview, markdown) file = open(args.markdown.name, 'w') file.write(markdown) file.close() diff --git a/packages/preview_generator/poetry.lock b/packages/preview_generator/poetry.lock index 14280b8..e06b6e2 100644 --- a/packages/preview_generator/poetry.lock +++ b/packages/preview_generator/poetry.lock @@ -1,5 +1,16 @@ # This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. +[[package]] +name = "colorama" +version = "0.4.6" +description = "Cross-platform colored terminal text." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + [[package]] name = "colormath" version = "3.0.0" @@ -14,6 +25,31 @@ files = [ networkx = ">=2.0" numpy = "*" +[[package]] +name = "exceptiongroup" +version = "1.2.0" +description = "Backport of PEP 654 (exception groups)" +optional = false +python-versions = ">=3.7" +files = [ + {file = "exceptiongroup-1.2.0-py3-none-any.whl", hash = "sha256:4bfd3996ac73b41e9b9628b04e079f193850720ea5945fc96a08633c66912f14"}, + {file = "exceptiongroup-1.2.0.tar.gz", hash = "sha256:91f5c769735f051a4290d52edd0858999b57e5876e9f85937691bd4c9fa3ed68"}, +] + +[package.extras] +test = ["pytest (>=6)"] + +[[package]] +name = "iniconfig" +version = "2.0.0" +description = "brain-dead simple config-ini parsing" +optional = false +python-versions = ">=3.7" +files = [ + {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, + {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, +] + [[package]] name = "klippermock" version = "0.1.0" @@ -101,6 +137,17 @@ files = [ {file = "numpy-1.24.4.tar.gz", hash = "sha256:80f5e3a4e498641401868df4208b74581206afbee7cf7b8329daae82676d9463"}, ] +[[package]] +name = "packaging" +version = "24.0" +description = "Core utilities for Python packages" +optional = false +python-versions = ">=3.7" +files = [ + {file = "packaging-24.0-py3-none-any.whl", hash = "sha256:2ddfb553fdf02fb784c234c7ba6ccc288296ceabec964ad2eae3777778130bc5"}, + {file = "packaging-24.0.tar.gz", hash = "sha256:eb82c5e3e56209074766e6885bb04b8c38a0c015d0a30036ebe7ece34c9989e9"}, +] + [[package]] name = "pillow" version = "10.2.0" @@ -186,7 +233,55 @@ tests = ["check-manifest", "coverage", "defusedxml", "markdown2", "olefile", "pa typing = ["typing-extensions"] xmp = ["defusedxml"] +[[package]] +name = "pluggy" +version = "1.4.0" +description = "plugin and hook calling mechanisms for python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pluggy-1.4.0-py3-none-any.whl", hash = "sha256:7db9f7b503d67d1c5b95f59773ebb58a8c1c288129a88665838012cfb07b8981"}, + {file = "pluggy-1.4.0.tar.gz", hash = "sha256:8c85c2876142a764e5b7548e7d9a0e0ddb46f5185161049a79b7e974454223be"}, +] + +[package.extras] +dev = ["pre-commit", "tox"] +testing = ["pytest", "pytest-benchmark"] + +[[package]] +name = "pytest" +version = "8.1.1" +description = "pytest: simple powerful testing with Python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pytest-8.1.1-py3-none-any.whl", hash = "sha256:2a8386cfc11fa9d2c50ee7b2a57e7d898ef90470a7a34c4b949ff59662bb78b7"}, + {file = "pytest-8.1.1.tar.gz", hash = "sha256:ac978141a75948948817d360297b7aae0fcb9d6ff6bc9ec6d514b85d5a65c044"}, +] + +[package.dependencies] +colorama = {version = "*", markers = "sys_platform == \"win32\""} +exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""} +iniconfig = "*" +packaging = "*" +pluggy = ">=1.4,<2.0" +tomli = {version = ">=1", markers = "python_version < \"3.11\""} + +[package.extras] +testing = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] + +[[package]] +name = "tomli" +version = "2.0.1" +description = "A lil' TOML parser" +optional = false +python-versions = ">=3.7" +files = [ + {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, + {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, +] + [metadata] lock-version = "2.0" python-versions = "<3.12,>=3.8" -content-hash = "573bc38d500198b94de6334a254c6a5e8e3e9394c134097f1718f1d52b399f10" +content-hash = "ad7be66b9ff514d517ea6903cd898a402205d1e14c92a9121b5b60dea64ece65" diff --git a/packages/preview_generator/preview_generator/__init__.py b/packages/preview_generator/preview_generator/__init__.py index 850a1ee..1b6b370 100644 --- a/packages/preview_generator/preview_generator/__init__.py +++ b/packages/preview_generator/preview_generator/__init__.py @@ -1 +1,2 @@ -from .preview_generator import generate_preview \ No newline at end of file +from .preview_generator import generate_preview +from .markdown_regex import markdown_regex \ No newline at end of file diff --git a/packages/preview_generator/preview_generator/markdown_regex.py b/packages/preview_generator/preview_generator/markdown_regex.py new file mode 100644 index 0000000..89bd644 --- /dev/null +++ b/packages/preview_generator/preview_generator/markdown_regex.py @@ -0,0 +1,3 @@ +import re + +markdown_regex = re.compile(r'(\!\[Preview\]\(.+?\)\n\n)?(?P```(layers|\nlayers\:)\n(?P.*?)\n```)', re.DOTALL) \ No newline at end of file diff --git a/packages/preview_generator/preview_generator/markdown_regex_test.py b/packages/preview_generator/preview_generator/markdown_regex_test.py new file mode 100644 index 0000000..4c669cd --- /dev/null +++ b/packages/preview_generator/preview_generator/markdown_regex_test.py @@ -0,0 +1,73 @@ +import re +from . import markdown_regex + +def test_with_one_lines(): + text = """ +Long text + +```layers +static +``` +""" + match = markdown_regex.search(text) + assert match.groupdict() == { + 'block': '```layers\nstatic\n```', + 'layers': 'static' + } + +def test_with_two_lines(): + text = """ +Long text + +```layers +static +breathing +``` +""" + match = markdown_regex.search(text) + assert match.groupdict() == { + 'block': '```layers\nstatic\nbreathing\n```', + 'layers': 'static\nbreathing' + } + +def test_with_layers_on_extra_line_with_colon(): + text = """ +Long text + +``` +layers: + static + breathing +``` +""" + match = markdown_regex.search(text) + assert match.groupdict() == { + 'block': '```\nlayers:\n static\n breathing\n```', + 'layers': ' static\n breathing' + } + + +def test_with_preceeding_preview_image(): + text = """ +Long text + +![Preview](./woop.png) + +``` +layers: + static + breathing +``` +""" + match = markdown_regex.search(text) + assert match.group(0) == """![Preview](./woop.png) + +``` +layers: + static + breathing +```""" + assert match.groupdict() == { + 'block': '```\nlayers:\n static\n breathing\n```', + 'layers': ' static\n breathing' + } diff --git a/packages/preview_generator/pyproject.toml b/packages/preview_generator/pyproject.toml index 83436ef..029f411 100644 --- a/packages/preview_generator/pyproject.toml +++ b/packages/preview_generator/pyproject.toml @@ -11,6 +11,9 @@ klippermock = {path = "../klippermock", develop = true} pillow = "^10.2.0" +[tool.poetry.group.dev.dependencies] +pytest = "^8.1.1" + [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api"