This commit is contained in:
faildev_mode 2023-06-04 23:03:02 +02:00
parent 953dcb183c
commit e8d21621ec
No known key found for this signature in database
GPG Key ID: 70845C70C0F5E205
1 changed files with 11 additions and 2 deletions

View File

@ -133,11 +133,18 @@ def gemtext2html(gemtext: str) -> str:
html.append(f'<h3>{quote_html(match.group(1))}</h3>')
elif match := re.match(r'^=>\s+(\S+)(\s+.*)?', line):
url = match.group(1)
text = match.group(2).strip() if len(match.groups()) > 2 else None
# rotate file extension
if url.endswith('.gmi') and not re.match(r'^[-\w]+:', url):
url = os.path.splitext(url)[0] + '.html'
text = match.group(2).strip() if len(match.groups()) > 2 else url
html.append(f'<a href="{quote_html(url)}">{quote_html(text)}</a>')
# if links points to image, display it instead
if os.path.splitext(url)[1] in ('.jpg', '.png', '.gif'):
if text:
html.append(f'<img src="{quote_html(url)}" alt="{quote_html(text)}">')
else:
html.append(f'<img src="{quote_html(url)}">')
else:
html.append(f'<a href="{quote_html(url)}">{quote_html(text or url)}</a>')
elif match := re.match(r'^>\s*(.*)', line):
html.append(f'<blockquote>{quote_html(match.group(1))}</blockquote>')
else:
@ -151,6 +158,8 @@ def gemtext2html(gemtext: str) -> str:
return '\n'.join(html)
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(1), start=os.path.dirname(path))
text = match.group(2).strip() if len(match.groups()) > 2 else url
return f'=> {url} {text}'