Better descriptions of callables for re.sub

This commit is contained in:
faildev_mode 2023-07-16 18:11:52 +02:00
parent 76648f08b2
commit 5f85660bbc
No known key found for this signature in database
GPG Key ID: 70845C70C0F5E205
1 changed files with 20 additions and 12 deletions

View File

@ -54,8 +54,8 @@ def namespace_from(*extensions) -> dict:
def evaluate_this(content: str, global_ns: dict, local_ns: dict = {}) -> str:
"""Finds some inline Python code in {braces} and evaluates it in-place.
global_ns is a dictionary of globals that it can use. It's recommended to use
namespace_from() to generate one.
global_ns is a dictionary of globals that it can use. It's recommended to
use namespace_from() to generate one.
"""
return re.sub(r'\{([^\s}]+([^}]*[^\s}])?)\}',
@ -65,6 +65,7 @@ def evaluate_this(content: str, global_ns: dict, local_ns: dict = {}) -> str:
def evaluate(match: re.Match, global_ns: dict, local_ns: dict) -> str:
"""Callable for re.sub, returns value of the last Python expression line,
executing all the other lines before.
group 1: python code
"""
match = match.group(1).split('\n')
@ -84,9 +85,12 @@ def evaluate(match: re.Match, global_ns: dict, local_ns: dict) -> str:
return '\n<a real error occured here>\n'
def redirect(match: re.Match, domains: dict) -> str:
"""Callable for re.sub, replaces urls to sites like youtube.com or twitter.com
with their respective privacy-respecting proxy services defined in config section
redirections
"""Callable for re.sub, replaces urls to sites like youtube.com or
twitter.com with their respective privacy-respecting proxy services defined
in config section redirections
group 1: http:// or https://
group 2: hostname
group 3: remainder URL
"""
this_domain = match.group(2)
@ -94,6 +98,17 @@ def redirect(match: re.Match, domains: dict) -> str:
return match.group(1) + domains[this_domain] + match.group(3)
else: return match.group(0)
def abs2rel(match: re.Match, path: str) -> str:
"""Callable for re.sub, converts absolute link to relative to current
document (path argument).
group 1: '=> '
group 2: URL
"""
url = os.path.relpath(match.group(2), start=os.path.dirname(path))
print(' M', url)
return match.group(1) + url
def gemtext2html(gemtext: str) -> str:
"""Converts gemtext to html format"""
@ -159,13 +174,6 @@ def gemtext2html(gemtext: str) -> str:
return '\n'.join(html_data)
def abs2rel(match: re.Match, path: str) -> str:
"""Callable for re.sub, converts absolute link to relative to current document (path argument)."""
url = os.path.relpath(match.group(2), start=os.path.dirname(path))
print(' M', url)
return match.group(1) + url
if __name__ == '__main__':
os.chdir(sys.path[0])