Internal URLs now points to html files in html version

This commit is contained in:
faildev_mode 2023-06-04 22:44:48 +02:00
parent 0cd4e2c1cc
commit 953dcb183c
No known key found for this signature in database
GPG Key ID: 70845C70C0F5E205
1 changed files with 12 additions and 4 deletions

View File

@ -133,9 +133,12 @@ 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)
# 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>')
elif match := re.match(r'>\s+(.*)', line):
elif match := re.match(r'^>\s*(.*)', line):
html.append(f'<blockquote>{quote_html(match.group(1))}</blockquote>')
else:
html.append(f'<p>{quote_html(line)}</p>')
@ -147,6 +150,11 @@ def gemtext2html(gemtext: str) -> str:
return '\n'.join(html)
def abs2rel(match: re.Match, path: str) -> str:
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}'
if __name__ == '__main__':
os.chdir(sys.path[0])
@ -205,9 +213,9 @@ if __name__ == '__main__':
# convert absolute links to relative
if path.endswith('.gmi'):
content = re.sub(r'^=>(\s+)/(\S+)',
lambda a: '=>'+a.group(1)+os.path.relpath(a.group(2), start=os.path.dirname(path)),
content)
content = re.sub(r'=>\s+/(\S+)(\s+.*)?', partial(
abs2rel, path=path
), content)
# produce HTML version
html_path = os.path.splitext(path)[0]+'.html'