From cab832d97804a0956ac1fe8245ed063f90d5562f Mon Sep 17 00:00:00 2001 From: Marcos Stefani Date: Sat, 8 Aug 2020 01:12:07 -0300 Subject: [PATCH 1/5] render variable --- sucuri/element/text.py | 12 ++++++++++++ tests/element/test_tag.py | 2 -- tests/element/test_text.py | 8 ++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 sucuri/element/text.py create mode 100644 tests/element/test_text.py diff --git a/sucuri/element/text.py b/sucuri/element/text.py new file mode 100644 index 0000000..3b917f1 --- /dev/null +++ b/sucuri/element/text.py @@ -0,0 +1,12 @@ +class Text(object): + def __init__(self, value, variables): + self.value = value + self.variables = variables + + def render(self): + value = self.value + for key in self.variables: + old = '{' + key + '}' + new = self.variables[key] + value = value.replace(old, new) + return value \ No newline at end of file diff --git a/tests/element/test_tag.py b/tests/element/test_tag.py index a4d1e36..960088c 100644 --- a/tests/element/test_tag.py +++ b/tests/element/test_tag.py @@ -1,8 +1,6 @@ import sys sys.path.append('...') -import pytest - from sucuri.element.tag import Tag def test_should_reflect_when_just_passing_the_tag(): diff --git a/tests/element/test_text.py b/tests/element/test_text.py new file mode 100644 index 0000000..1e4883e --- /dev/null +++ b/tests/element/test_text.py @@ -0,0 +1,8 @@ +import sys +sys.path.append('...') + +from sucuri.element.text import Text + +def test_should_reflect_when_houver_variaveis_one_line(): + text = Text('Hello {a}', {"a": "World!"}) + assert text.render() == 'Hello World!' \ No newline at end of file From 3f0df04830d9c5a97089c25b812a8e2d75c1434f Mon Sep 17 00:00:00 2001 From: Marcos Stefani Date: Sat, 8 Aug 2020 01:20:39 -0300 Subject: [PATCH 2/5] gitignore update --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b099aef..c97af67 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ sucuri.egg-info/ dist/ build/ *.pyc -env/ \ No newline at end of file +env/ +__pycache__/ \ No newline at end of file From 6a5bb706eef2e52459018e9d587ff7240335d8b5 Mon Sep 17 00:00:00 2001 From: Marcos Stefani Date: Sat, 8 Aug 2020 01:22:38 -0300 Subject: [PATCH 3/5] gitignore update --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c97af67..552d533 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ dist/ build/ *.pyc env/ -__pycache__/ \ No newline at end of file +__pycache__/ +.pytest_cache/ \ No newline at end of file From c44ce7d60cd833087814c55aa66a916b623dc66d Mon Sep 17 00:00:00 2001 From: Marcos Stefani Date: Sat, 8 Aug 2020 02:03:27 -0300 Subject: [PATCH 4/5] more than one line --- sucuri/element/text.py | 14 ++++++++++++-- tests/element/test_text.py | 22 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/sucuri/element/text.py b/sucuri/element/text.py index 3b917f1..b0bd24a 100644 --- a/sucuri/element/text.py +++ b/sucuri/element/text.py @@ -1,12 +1,22 @@ class Text(object): - def __init__(self, value, variables): + def __init__(self, value, variables={}): self.value = value self.variables = variables def render(self): + self._replace_variables() + self._clean() + return self.value + + def _replace_variables(self): value = self.value for key in self.variables: old = '{' + key + '}' new = self.variables[key] value = value.replace(old, new) - return value \ No newline at end of file + self.value = value + + def _clean(self): + one_line = self.value.replace("\n", " ") + split = one_line.split("|") + self.value = "\n".join(map(str.strip, split)) diff --git a/tests/element/test_text.py b/tests/element/test_text.py index 1e4883e..a4a7208 100644 --- a/tests/element/test_text.py +++ b/tests/element/test_text.py @@ -3,6 +3,24 @@ from sucuri.element.text import Text -def test_should_reflect_when_houver_variaveis_one_line(): +def test_should_reflect_when_there_are_variables_one_line(): text = Text('Hello {a}', {"a": "World!"}) - assert text.render() == 'Hello World!' \ No newline at end of file + assert text.render() == 'Hello World!' + +def test_should_reflect_when_there_are_variables_more_than_one_line(): + old = """Hello! + | Text + | with + | variable {foo} and + | more than + | one line + """ + + new = """Hello! +Text +with +variable foo and +more than +one line""" + text = Text(old, {"foo": "foo"}) + assert text.render() == new \ No newline at end of file From 91ba4dc4112ef416e3aa5bd1843e04e99dd25a77 Mon Sep 17 00:00:00 2001 From: Marcos Stefani Date: Sat, 8 Aug 2020 02:06:35 -0300 Subject: [PATCH 5/5] fix workflow --- .github/workflows/python-package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 364cb8c..bc83dec 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -25,4 +25,4 @@ jobs: pip install -r requirements.txt - name: Test with pytest run: | - pytest test/ \ No newline at end of file + pytest \ No newline at end of file