Optimised by pre-compiling regex patterns
This commit is contained in:
parent
49ce4aad5f
commit
3ec60dbfcb
|
@ -17,6 +17,13 @@ from config import config # from itself
|
|||
from bs4 import BeautifulSoup # from beautifulsoup4
|
||||
from gemtext import GemtextParser # from itself
|
||||
|
||||
# regex pre-compilation
|
||||
REGEX = {
|
||||
'evaluation': re.compile(r'\{([^\s}]+([^}]*[^\s}])?)\}'),
|
||||
'url': re.compile(r'(https?://)([-\w]+[-\w.]*)(/\S*)?'),
|
||||
'generic_url': re.compile(r'^[\w]+:')
|
||||
}
|
||||
|
||||
def template_for(path: str) -> str:
|
||||
"""Determine used template for the file
|
||||
Returns the last matching template from the config or None
|
||||
|
@ -59,7 +66,7 @@ def evaluate_this(content: str, global_ns: dict, local_ns: dict = {}) -> str:
|
|||
use namespace_from() to generate one.
|
||||
"""
|
||||
|
||||
return re.sub(r'\{([^\s}]+([^}]*[^\s}])?)\}',
|
||||
return REGEX['evaluation'].sub(
|
||||
partial(evaluate, global_ns=global_ns, local_ns=local_ns),
|
||||
content)
|
||||
|
||||
|
@ -122,7 +129,7 @@ def gemtext2html(parser: GemtextParser, rotate_extension=True) -> BeautifulSoup:
|
|||
else:
|
||||
href = item.href
|
||||
# html links typically points to .html files
|
||||
if rotate_extension and href.endswith('.gmi') and not re.match(r'^[\w]+:', href):
|
||||
if rotate_extension and href.endswith('.gmi') and not REGEX['generic_url'].match(href):
|
||||
href = os.path.splitext(href)[0] + '.html'
|
||||
el = soup.new_tag('a', href=href)
|
||||
if item.label:
|
||||
|
@ -172,7 +179,7 @@ def convert_href(href: str, path: str) -> str:
|
|||
if href.startswith('/'):
|
||||
output = os.path.relpath(href[1:], start=os.path.dirname(path))
|
||||
elif 'redirections' in config:
|
||||
output = re.sub(r'(https?://)([-\w]+[-\w.]*)(/\S*)?', partial(
|
||||
output = REGEX['url'].sub(partial(
|
||||
redirect,
|
||||
domains=dict(config['redirections'])
|
||||
), href)
|
||||
|
|
Reference in New Issue