Internal URLs now points to html files in html version
This commit is contained in:
parent
0cd4e2c1cc
commit
953dcb183c
|
@ -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'
|
||||
|
|
Reference in New Issue