dokuwiki-to-hugo/src/markdown/links.py

91 lines
3.3 KiB
Python
Raw Normal View History

2017-01-13 09:01:09 +01:00
import re
from os import walk
2017-01-19 21:58:16 +01:00
from pathlib import Path
from src.dokuwiki_to_hugo import DokuWikiToHugo
from src.markdown_converter import MarkdownConverter
2017-01-12 21:45:49 +01:00
@MarkdownConverter.Register
class MarkdownLinks():
2017-01-13 13:17:54 +01:00
# see http://pythex.org/
pattern = re.compile('(\[\[)(.*?)(\]\])')
2017-01-12 21:45:49 +01:00
def convert(self, text):
2017-01-13 09:01:09 +01:00
result = text
def starts_with_space(match):
return match[1][0] is ' '
2017-01-13 12:59:45 +01:00
for regex_link in MarkdownLinks.pattern.findall(text):
if starts_with_space(regex_link):
continue
2017-01-13 13:17:54 +01:00
origlink = ''.join(regex_link)
2017-01-13 09:01:09 +01:00
convertedlink = ""
2017-01-13 13:17:54 +01:00
if "http" in origlink or "www" in origlink:
2017-01-13 12:59:45 +01:00
convertedlink = self.convert_as_external_link(origlink)
2017-01-19 21:58:16 +01:00
elif ">" in origlink and not "<" in origlink:
2017-01-13 12:59:45 +01:00
convertedlink = self.convert_as_interwiki_link(origlink)
2017-01-13 09:01:09 +01:00
else:
2017-01-13 12:59:45 +01:00
convertedlink = self.convert_as_internal_link(origlink)
result = result.replace(origlink, convertedlink)
2017-01-13 09:01:09 +01:00
return result
2017-01-12 21:45:49 +01:00
def parseUrl(self, text):
return text[2:text.index('|')]
def add_md_and_replace_home_with_index(self, src_url):
url = src_url
if "." not in url:
url = url + ".md"
return url.replace('home.md', '_index.md')
2017-01-12 21:45:49 +01:00
def parseInternalUrl(self, text):
url = text[2:len(text) - 2].replace(":", "/")
return self.add_md_and_replace_home_with_index(url)
def parseInternalUrlWithoutTitle(self, text):
url = self.parseUrl(text).replace(":", "/")
return self.add_md_and_replace_home_with_index(url)
2017-01-12 21:45:49 +01:00
def parseTitle(self, text):
2017-01-13 09:01:09 +01:00
return text[text.index('|') + 1: text.index(']]')]
2017-01-12 21:45:49 +01:00
def convert_as_interwiki_link(self, text):
interwiki_shortcode = text[2:text.index('>')]
self.assert_interwiki_is_known(interwiki_shortcode)
interwiki_urlpart = text[text.index('>') + 1 : len(text) - 2]
return """{{< %s "%s" >}}""" % (interwiki_shortcode, interwiki_urlpart)
def root_dir(self, url):
if "/" not in url or DokuWikiToHugo.root_dir is None or url[0] is "/":
return ""
return DokuWikiToHugo.root_dir + "/"
2017-01-12 21:45:49 +01:00
def convert_as_internal_link(self, text):
url = ""
title = ""
2017-01-12 21:45:49 +01:00
if "|" not in text:
url = self.parseInternalUrl(text)
title = text[2:len(text)-2].replace(":", "/")
else:
url = self.parseInternalUrlWithoutTitle(text)
title = self.parseTitle(text)
2017-01-12 21:45:49 +01:00
return """[%s]({{< relref "%s%s" >}})""" % (title, self.root_dir(url), url.replace(' ', '_'))
2017-01-12 21:45:49 +01:00
def convert_as_external_link(self, text):
2017-01-19 21:58:16 +01:00
if '|' in text:
url = self.parseUrl(text)
title = self.parseTitle(text)
return "[" + title + "](" + url + ")"
url = text.replace('[', '').replace(']', '')
return "[" + url + "](" + url + ")"
2017-01-12 21:45:49 +01:00
def assert_interwiki_is_known(self, shortcode):
shortcodes = []
2017-01-19 21:58:16 +01:00
shortcodes_path = Path(__file__).parents[2].joinpath('layouts/shortcodes')
for (dirpath, dirnames, filenames) in walk(shortcodes_path):
2017-01-12 21:45:49 +01:00
shortcodes.extend(filenames)
break
if not shortcode in map(lambda x: x.replace(".html", ""), shortcodes):
raise ValueError("Unknown Interwiki code " + shortcode + " - please add a shortcode in the layouts dir!")