summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <david@luevano.xyz>2023-02-15 22:05:29 -0600
committerDavid Luevano Alvarado <david@luevano.xyz>2023-02-15 22:05:29 -0600
commit30ccf03fa849b5c6b550ab85aabce860a16b16cf (patch)
treea847fe8767547e91bdd076b01d3b4bb54e13ecbc /src
parent26dabc676ac74812f2dbcfaf0e2b7c5b21e804c9 (diff)
add more testing and fix small bugs
Diffstat (limited to 'src')
-rw-r--r--src/pymdvar/__init__.py3
-rw-r--r--src/pymdvar/py.typed0
-rw-r--r--src/pymdvar/pymdvar.py44
3 files changed, 47 insertions, 0 deletions
diff --git a/src/pymdvar/__init__.py b/src/pymdvar/__init__.py
new file mode 100644
index 0000000..b64a3b6
--- /dev/null
+++ b/src/pymdvar/__init__.py
@@ -0,0 +1,3 @@
+from .pymdvar import VariableExtension
+
+__all__ = ['VariableExtension'] \ No newline at end of file
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)