Removed abs2rel and replaced with convert_href
This commit is contained in:
parent
bed30994a8
commit
49ce4aad5f
|
@ -99,18 +99,6 @@ def redirect(match: re.Match, domains: dict) -> str:
|
|||
return match.group(1) + domains[this_domain] + match.group(3)
|
||||
else: return match.group(0)
|
||||
|
||||
# TODO: get rid of this
|
||||
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(parser: GemtextParser, rotate_extension=True) -> BeautifulSoup:
|
||||
"""Converts gemtext to html format"""
|
||||
|
||||
|
@ -126,14 +114,14 @@ def gemtext2html(parser: GemtextParser, rotate_extension=True) -> BeautifulSoup:
|
|||
el.append(p)
|
||||
|
||||
if item.type == 'link':
|
||||
# put image if it's link to image file
|
||||
# put an image if this is a link to image file
|
||||
if os.path.splitext(item.href)[1] in ('.jpg', '.png', '.gif'):
|
||||
el = soup.new_tag('img', src=item.href)
|
||||
if item.label:
|
||||
el.attrs['alt'] = item.label
|
||||
else:
|
||||
href = item.href
|
||||
# html links will typically point to .html files
|
||||
# html links typically points to .html files
|
||||
if rotate_extension and href.endswith('.gmi') and not re.match(r'^[\w]+:', href):
|
||||
href = os.path.splitext(href)[0] + '.html'
|
||||
el = soup.new_tag('a', href=href)
|
||||
|
@ -255,26 +243,13 @@ if __name__ == '__main__':
|
|||
)
|
||||
content = evaluate_this(tpl_item.content, namespace)
|
||||
|
||||
# """
|
||||
# do redirections (http/https only!)
|
||||
if 'redirections' in config:
|
||||
content = re.sub(r'(https?://)([-\w]+[-\w.]*)(/\S*)?', partial(
|
||||
redirect,
|
||||
domains=dict(config['redirections'])
|
||||
), content)
|
||||
|
||||
# convert absolute links to relative (gemtext only!)
|
||||
if path.endswith('.gmi'):
|
||||
content = re.sub(r'(=>[ \t]+)/(\S+)', partial(
|
||||
abs2rel, path=path
|
||||
), content)
|
||||
# """
|
||||
|
||||
# redirections, path conversion
|
||||
if path.endswith('.gmi'):
|
||||
for line in content.strip().split('\n'):
|
||||
if line.startswith('=> '):
|
||||
...
|
||||
parser = GemtextParser(content)
|
||||
for el in parser.elements:
|
||||
if el.type == 'link':
|
||||
el.href = convert_href(el.href, item.path)
|
||||
content = str(parser)
|
||||
|
||||
# save results
|
||||
save_this('../'+path, content)
|
||||
|
|
Reference in New Issue