diff options
-rw-r--r-- | pymdvar/pymdvar.py | 31 | ||||
-rw-r--r-- | pyproject.toml | 25 | ||||
-rw-r--r-- | requirements_dev.txt | 5 | ||||
-rw-r--r-- | setup.cfg | 13 | ||||
-rw-r--r-- | src/pymdvar/__init__.py (renamed from pymdvar/__init__.py) | 0 | ||||
-rw-r--r-- | src/pymdvar/py.typed | 0 | ||||
-rw-r--r-- | src/pymdvar/pymdvar.py | 44 | ||||
-rw-r--r-- | tests/test_pymdvar.py | 9 |
8 files changed, 90 insertions, 37 deletions
diff --git a/pymdvar/pymdvar.py b/pymdvar/pymdvar.py deleted file mode 100644 index 1173f20..0000000 --- a/pymdvar/pymdvar.py +++ /dev/null @@ -1,31 +0,0 @@ -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/pyproject.toml b/pyproject.toml index e74c9ec..dcaca35 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,23 @@ [build-system] -requires = [ - "setuptools>=42", - "wheel" +requires = ["pbr>=5.7.0", "setuptools>=36.6.0"] +build-backend = "pbr.build" + +[tool.pytest.ini_options] +addopts = "--cov-report term-missing --cov=pymdvar" +testpaths = [ + "tests", ] -build-backend = "setuptools.build_meta" + +[tool.mypy] +mypy_path = "src" +check_untyped_defs = true +disallow_any_generics = true +ignore_missing_imports = true +no_implicit_optional = true +show_error_codes = true +strict_equality = true +warn_redundant_casts = true +warn_return_any = true +warn_unreachable = true +warn_unused_configs = true +no_implicit_reexport = true
\ No newline at end of file diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..9979866 --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1,5 @@ +pytest>=7.2.1 +pytest-cov>=4.0.0 +mypy>=1.0.0 +flake8>=6.0.0 +types-Markdown>=3.4.2.4
\ No newline at end of file @@ -24,5 +24,18 @@ packages = universal = 0 [options] +package_dir = + = src packages = find: include_package_data=True + +[options.packages.find] +where = src + +[options.package_data] +pymdvar = py.typed + +[flake8] +max-line-length = 80 +per-file-ignores = + __init__.py: W292
\ No newline at end of file diff --git a/pymdvar/__init__.py b/src/pymdvar/__init__.py index b64a3b6..b64a3b6 100644 --- a/pymdvar/__init__.py +++ b/src/pymdvar/__init__.py diff --git a/src/pymdvar/py.typed b/src/pymdvar/py.typed new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/pymdvar/py.typed diff --git a/src/pymdvar/pymdvar.py b/src/pymdvar/pymdvar.py new file mode 100644 index 0000000..b6ea344 --- /dev/null +++ b/src/pymdvar/pymdvar.py @@ -0,0 +1,44 @@ +from os import getenv +from markdown.extensions import Extension +from markdown.inlinepatterns import Pattern + +VARIABLE_RE = r'(\$\{)([a-zA-Z_]*)(\})' + + +class VariablePattern(Pattern): + # need to redefine as an extra attribute needs to be passed + def __init__(self, pattern, variables, enable_env, md=None): + self.variables = variables + self.enable_env = enable_env + super().__init__(pattern, md) + + def handleMatch(self, m): + # for some reason the group is offest by 1 + variable = m.group(3) + value = '' + + if variable in self.variables: + value = self.variables[variable] + else: + if self.enable_env: + value = getenv(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): + variables = self.getConfig('variables', dict()) + enable_env = self.getConfig('enable_env', False) + variable_pattern = VariablePattern(VARIABLE_RE, variables, enable_env) + md.inlinePatterns.register(variable_pattern, 'variable', 175) + + +def makeExtension(*args, **kwargs): + return VariableExtension(*args, **kwargs) diff --git a/tests/test_pymdvar.py b/tests/test_pymdvar.py index 2e305ec..69ffe56 100644 --- a/tests/test_pymdvar.py +++ b/tests/test_pymdvar.py @@ -14,7 +14,10 @@ def test_empty_input(): @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>') + ('foo **test** bar', '<p>foo <strong>test</strong> bar</p>'), + ('foo $test bar', '<p>foo $test 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()]) @@ -24,7 +27,9 @@ def test_non_replacements(in_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>') + ('foo **${test}** bar', '<p>foo <strong>value</strong> bar</p>'), + ('foo [link](${test}/a.html) bar', '<p>foo <a href="value/a.html">link</a> bar</p>'), + ('foo ![image](${test}/a.jpg) bar', '<p>foo <img alt="image" src="value/a.jpg" /> bar</p>'), ]) def test_simple_replacements(in_str, exp_str): out_str = markdown(in_str, extensions=[VariableExtension(variables={'test':'value'})]) |