diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/pymdvar/__init__.py | 9 | ||||
-rw-r--r-- | src/pymdvar/pymdvar.py | 24 |
2 files changed, 17 insertions, 16 deletions
diff --git a/src/pymdvar/__init__.py b/src/pymdvar/__init__.py index b64a3b6..d8d4d4e 100644 --- a/src/pymdvar/__init__.py +++ b/src/pymdvar/__init__.py @@ -1,3 +1,10 @@ +from typing import Any from .pymdvar import VariableExtension -__all__ = ['VariableExtension']
\ No newline at end of file +__all__ = ['VariableExtension'] + + +# this should be in pymdvar.py, but since there is only one extension, +# it is fine in here +def makeExtension(*args: Any, **kwargs: Any): + return VariableExtension(*args, **kwargs) diff --git a/src/pymdvar/pymdvar.py b/src/pymdvar/pymdvar.py index d81f785..2dcdf96 100644 --- a/src/pymdvar/pymdvar.py +++ b/src/pymdvar/pymdvar.py @@ -1,4 +1,4 @@ -from os import getenv +import os from re import Match from typing import Any from xml.etree.ElementTree import Element @@ -6,7 +6,7 @@ from markdown import Markdown from markdown.extensions import Extension from markdown.inlinepatterns import Pattern -VAR_RE: str = r'(\$\{)([a-zA-Z_]*)(\})' +VAR_RE: str = r'(\$\{)([a-zA-Z_0-9]*)(\})' class VarPattern(Pattern): @@ -22,14 +22,12 @@ class VarPattern(Pattern): def handleMatch(self, m: Match[str]) -> str | Element | None: # for some reason the group is offest by 1 var: str | Any = m.group(3) - value: str = '' - if var in self.vars: - value = self.vars[var] - else: - if self.enable_env: - value = getenv(var, '') - return value + return self.vars[var] + if self.enable_env: + if var in os.environ: + return os.environ[var] + return '' class VariableExtension(Extension): @@ -41,11 +39,7 @@ class VariableExtension(Extension): super().__init__(**kwargs) def extendMarkdown(self, md: Markdown) -> None: - vars: dict[str, str] | Any = self.getConfig('variables', dict()) - enable_env: bool = self.getConfig('enable_env', False) + vars: dict[str, str] | Any = self.getConfig('variables') + enable_env: bool = self.getConfig('enable_env') var_pattern: VarPattern = VarPattern(VAR_RE, vars, enable_env) md.inlinePatterns.register(var_pattern, 'variable', 175) - - -def makeExtension(*args: Any, **kwargs: Any): - return VariableExtension(*args, **kwargs) |