From 26dabc676ac74812f2dbcfaf0e2b7c5b21e804c9 Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Tue, 14 Feb 2023 13:22:19 -0600 Subject: add simple tests and simple working solution --- pymdvar/__init__.py | 3 +++ pymdvar/pymdvar.py | 31 +++++++++++++++++++++++++++++++ setup.cfg | 5 ----- src/pymdvar/__init__.py | 3 --- src/pymdvar/pymdvar.py | 19 ------------------- tests/__init__.py | 0 tests/test_pymdvar.py | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 pymdvar/__init__.py create mode 100644 pymdvar/pymdvar.py delete mode 100644 src/pymdvar/__init__.py delete mode 100644 src/pymdvar/pymdvar.py create mode 100644 tests/__init__.py create mode 100644 tests/test_pymdvar.py diff --git a/pymdvar/__init__.py b/pymdvar/__init__.py new file mode 100644 index 0000000..b64a3b6 --- /dev/null +++ b/pymdvar/__init__.py @@ -0,0 +1,3 @@ +from .pymdvar import VariableExtension + +__all__ = ['VariableExtension'] \ No newline at end of file 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 diff --git a/setup.cfg b/setup.cfg index 640e3e0..e44d6c2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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/__init__.py b/src/pymdvar/__init__.py deleted file mode 100644 index b64a3b6..0000000 --- a/src/pymdvar/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from .pymdvar import VariableExtension - -__all__ = ['VariableExtension'] \ No newline at end of file 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 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', '

foo bar

'), + ('foo *test* bar', '

foo test bar

'), + ('foo **test** bar', '

foo test bar

') +]) +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', '

foo value bar

'), + ('foo *${test}* bar', '

foo value bar

'), + ('foo **${test}** bar', '

foo value bar

') +]) +def test_simple_replacements(in_str, exp_str): + out_str = markdown(in_str, extensions=[VariableExtension(variables={'test':'value'})]) + assert out_str == exp_str + -- cgit v1.2.3-70-g09d2