Skip to content

Commit

Permalink
feature(preview_generator): Write tests for markdown regex
Browse files Browse the repository at this point in the history
  • Loading branch information
reemo3dp committed Mar 13, 2024
1 parent 23ba330 commit 4410d06
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 8 deletions.
9 changes: 8 additions & 1 deletion packages/preview_generator/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 5 additions & 5 deletions packages/preview_generator/markdown_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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__':
Expand All @@ -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()
Expand Down
97 changes: 96 additions & 1 deletion packages/preview_generator/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/preview_generator/preview_generator/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .preview_generator import generate_preview
from .preview_generator import generate_preview
from .markdown_regex import markdown_regex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import re

markdown_regex = re.compile(r'(\!\[Preview\]\(.+?\)\n\n)?(?P<block>```(layers|\nlayers\:)\n(?P<layers>.*?)\n```)', re.DOTALL)
Original file line number Diff line number Diff line change
@@ -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'
}
3 changes: 3 additions & 0 deletions packages/preview_generator/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 4410d06

Please sign in to comment.