Repository: marcosstefani/sucuri (language: python)

Issue:
Create Text Class:
This class will be in charge of handling the content received in the Tag class. It should be used within the Tag class to treat the content value.


Discussion / hints:

Fix patch:
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
diff --git a/.gitignore b/.gitignore
index b099aef..552d533 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,4 +4,6 @@ sucuri.egg-info/
 dist/
 build/
 *.pyc
-env/
\ No newline at end of file
+env/
+__pycache__/
+.pytest_cache/
\ No newline at end of file
diff --git a/sucuri/element/text.py b/sucuri/element/text.py
new file mode 100644
index 0000000..b0bd24a
--- /dev/null
+++ b/sucuri/element/text.py
@@ -0,0 +1,22 @@
+class Text(object):
+    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)
+        self.value = value
+    
+    def _clean(self):
+        one_line = self.value.replace("\n", " ")
+        split = one_line.split("|")
+        self.value = "\n".join(map(str.strip, split))


Test patch:
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..a4a7208
--- /dev/null
+++ b/tests/element/test_text.py
@@ -0,0 +1,26 @@
+import sys
+sys.path.append('...')
+
+from sucuri.element.text import Text
+
+def test_should_reflect_when_there_are_variables_one_line():
+    text = Text('Hello {a}', {"a": "World!"})
+    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