diff options
-rw-r--r-- | pymdvar/__init__.py (renamed from src/pymdvar/__init__.py) | 0 | ||||
-rw-r--r-- | pymdvar/pymdvar.py | 31 | ||||
-rw-r--r-- | setup.cfg | 5 | ||||
-rw-r--r-- | src/pymdvar/pymdvar.py | 19 | ||||
-rw-r--r-- | tests/__init__.py | 0 | ||||
-rw-r--r-- | tests/test_pymdvar.py | 32 |
6 files changed, 63 insertions, 24 deletions
diff --git a/src/pymdvar/__init__.py b/pymdvar/__init__.py index b64a3b6..b64a3b6 100644 --- a/src/pymdvar/__init__.py +++ b/pymdvar/__init__.py diff --git a/pymdvar/pymdvar.py b/pymdvar/pymdvar.py new file mode 100644 index 0000000..1173f20 --- /dev/null +++ b/pymdvar/pymdvar.py @@ -0,0 +1,31 @@ +from markdown.extensions import Extension +from markdown.inlinepatterns import Pattern + +VARIABLE_RE = r'(\$\{)(\w+)(\})' + + +class VariablePattern(Pattern): + def __init__(self, pattern, variables, md=None): + print(type(variables)) + self.variables = variables + super().__init__(pattern, md) + + def handleMatch(self, m): + variable = m.group(3) + value = '' + if variable in self.variables: + value = self.variables[variable] + return value + + +class VariableExtension(Extension): + def __init__(self, **kwargs): + self.config = { + 'enable_env': [False, 'Enable Environment variables parsing.'], + 'variables': [dict(), 'Dictionary holding variables to be used.'] + } + super().__init__(**kwargs) + + def extendMarkdown(self, md): + variable = VariablePattern(VARIABLE_RE, variables=self.getConfig('variables')) + md.inlinePatterns.register(variable, 'variable', 75)
\ No newline at end of file @@ -24,10 +24,5 @@ packages = universal = 0 [options] -package_dir = - = src packages = find: include_package_data=True - -[options.packages.find] -where = src diff --git a/src/pymdvar/pymdvar.py b/src/pymdvar/pymdvar.py deleted file mode 100644 index aa62cfd..0000000 --- a/src/pymdvar/pymdvar.py +++ /dev/null @@ -1,19 +0,0 @@ -from markdown.extensions import Extension -from markdown.inlinepatterns import Pattern - -VARIABLE_RE = r'\$\{(\w+)\}' - - -class VariablePattern(Pattern): - def handleMatch(self, m): - variable = m.group(2) - return variable - - -def makeExtension(*args, **kwargs): - return VariableExtension(*args, **kwargs) - - -class VariableExtension(Extension): - def extendMarkdown(self, md): - md.inlinePatterns.register(VariablePattern(VARIABLE_RE), 'var', 175)
\ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/__init__.py diff --git a/tests/test_pymdvar.py b/tests/test_pymdvar.py new file mode 100644 index 0000000..2e305ec --- /dev/null +++ b/tests/test_pymdvar.py @@ -0,0 +1,32 @@ +import pytest +from markdown import markdown +from pymdvar import VariableExtension + + +def test_empty_input(): + in_str = '' + out_str = markdown(in_str, extensions=[VariableExtension()]) + print(in_str) + print(out_str) + assert in_str == out_str + + +@pytest.mark.parametrize('in_str, exp_str', [ + ('foo bar', '<p>foo bar</p>'), + ('foo *test* bar', '<p>foo <em>test</em> bar</p>'), + ('foo **test** bar', '<p>foo <strong>test</strong> bar</p>') +]) +def test_non_replacements(in_str, exp_str): + out_str = markdown(in_str, extensions=[VariableExtension()]) + assert out_str == exp_str + + +@pytest.mark.parametrize('in_str, exp_str', [ + ('foo ${test} bar', '<p>foo value bar</p>'), + ('foo *${test}* bar', '<p>foo <em>value</em> bar</p>'), + ('foo **${test}** bar', '<p>foo <strong>value</strong> bar</p>') +]) +def test_simple_replacements(in_str, exp_str): + out_str = markdown(in_str, extensions=[VariableExtension(variables={'test':'value'})]) + assert out_str == exp_str + |