support for root links, various fixes on relative links, added doku shortocde
parent
a906cbb84a
commit
fe0bcbf330
|
@ -0,0 +1 @@
|
|||
<a href="dokuwiki.org/wiki:{{ index .Params 0 }}" target="_blank">{{ index .Params 0 }}</a>
|
|
@ -6,9 +6,11 @@ from src.markdown_converter import MarkdownConverter
|
|||
|
||||
|
||||
class DokuWikiToHugo:
|
||||
root_dir = ""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, root = None):
|
||||
self.header_converter = HugoFileConfig()
|
||||
DokuWikiToHugo.root_dir = root
|
||||
pass
|
||||
|
||||
def create_output_dir(self):
|
||||
|
@ -20,6 +22,7 @@ class DokuWikiToHugo:
|
|||
def doku_to_hugo(self, dir):
|
||||
self.create_output_dir()
|
||||
for root, subFolders, files in os.walk(dir):
|
||||
files = [f for f in files if not f[0] == '.']
|
||||
for file in files:
|
||||
self.process_file(root, file)
|
||||
|
||||
|
|
|
@ -3,11 +3,12 @@ import argparse
|
|||
from src.dokuwiki_to_hugo import DokuWikiToHugo
|
||||
|
||||
|
||||
def main(dir):
|
||||
DokuWikiToHugo().doku_to_hugo(dir)
|
||||
def main(dir, root):
|
||||
DokuWikiToHugo(root).doku_to_hugo(dir)
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-d", "--dir", help="the root directory of your DokuWiki pages collection", required = True)
|
||||
parser.add_argument("-d", "--dir", help="the directory of your DokuWiki pages collection", required = True)
|
||||
parser.add_argument("-r", "--root", help="the root directory name in your hugo content where to link to", required = False)
|
||||
opts = parser.parse_args()
|
||||
main(opts.dir)
|
||||
main(opts.dir, opts.root)
|
||||
|
|
|
@ -19,6 +19,8 @@ class MarkdownHeader():
|
|||
result = text
|
||||
for regex_head in MarkdownHeader.pattern.findall(text):
|
||||
orig_header = ''.join(regex_head)
|
||||
new_header = ('#' * MarkdownHeader.config[regex_head[0]]) + regex_head[1]
|
||||
result = result.replace(orig_header, new_header)
|
||||
src_header = regex_head[0]
|
||||
if src_header in MarkdownHeader.config:
|
||||
new_header = ('#' * MarkdownHeader.config[src_header]) + regex_head[1]
|
||||
result = result.replace(orig_header, new_header)
|
||||
return result
|
||||
|
|
|
@ -2,6 +2,7 @@ import re
|
|||
from os import walk
|
||||
from pathlib import Path
|
||||
|
||||
from src.dokuwiki_to_hugo import DokuWikiToHugo
|
||||
from src.markdown_converter import MarkdownConverter
|
||||
|
||||
|
||||
|
@ -12,7 +13,13 @@ class MarkdownLinks():
|
|||
|
||||
def convert(self, text):
|
||||
result = text
|
||||
def starts_with_space(match):
|
||||
return match[1][0] is ' '
|
||||
|
||||
for regex_link in MarkdownLinks.pattern.findall(text):
|
||||
if starts_with_space(regex_link):
|
||||
continue
|
||||
|
||||
origlink = ''.join(regex_link)
|
||||
convertedlink = ""
|
||||
if "http" in origlink or "www" in origlink:
|
||||
|
@ -27,8 +34,17 @@ class MarkdownLinks():
|
|||
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')
|
||||
def parseInternalUrl(self, text):
|
||||
return text[2:len(text)-2].replace(":", "/")
|
||||
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)
|
||||
|
||||
def parseTitle(self, text):
|
||||
return text[text.index('|') + 1: text.index(']]')]
|
||||
|
@ -38,16 +54,24 @@ class MarkdownLinks():
|
|||
self.assert_interwiki_is_known(interwiki_shortcode)
|
||||
interwiki_urlpart = text[text.index('>') + 1 : len(text) - 2]
|
||||
|
||||
return """{{< %s %s >}}""" % (interwiki_shortcode, interwiki_urlpart)
|
||||
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 + "/"
|
||||
|
||||
def convert_as_internal_link(self, text):
|
||||
url = ""
|
||||
title = ""
|
||||
if "|" not in text:
|
||||
return """{{< relref "%s" >}}""" % (self.parseInternalUrl(text))
|
||||
url = self.parseInternalUrl(text)
|
||||
title = text[2:len(text)-2].replace(":", "/")
|
||||
else:
|
||||
url = self.parseInternalUrlWithoutTitle(text)
|
||||
title = self.parseTitle(text)
|
||||
|
||||
url = self.parseUrl(text).replace(":", "/")
|
||||
title = self.parseTitle(text)
|
||||
|
||||
return """[%s]({{< relref "%s" >}})""" % (title, url)
|
||||
return """[%s]({{< relref "%s%s" >}})""" % (title, self.root_dir(url), url.replace(' ', '_'))
|
||||
|
||||
def convert_as_external_link(self, text):
|
||||
if '|' in text:
|
||||
|
|
|
@ -8,6 +8,10 @@ class TestMarkdownHeader(TestCase):
|
|||
def setUp(self):
|
||||
self.converter = MarkdownHeader()
|
||||
|
||||
def test_does_not_convert_if_too_long(self):
|
||||
src = "# ======================================="
|
||||
self.assertEqual(src, self.converter.convert(src))
|
||||
|
||||
def test_convert_does_nothing_if_no_header(self):
|
||||
self.assertEqual("blabla", self.converter.convert("blabla"))
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from unittest import TestCase
|
||||
|
||||
from src.dokuwiki_to_hugo import DokuWikiToHugo
|
||||
from src.markdown.links import MarkdownLinks
|
||||
|
||||
|
||||
|
@ -8,62 +9,110 @@ class TestMarkdownLinks(TestCase):
|
|||
def setUp(self):
|
||||
self.converter = MarkdownLinks()
|
||||
|
||||
def tearDown(self):
|
||||
DokuWikiToHugo.root_dir = None
|
||||
|
||||
def text_convert_no_link_returns_text(self):
|
||||
self.assertEquals("sup", self.converter.convert("sup"))
|
||||
|
||||
def test_internal_links_without_text_converted_properly(self):
|
||||
mdLink = """{{< relref "bla" >}}"""
|
||||
def test_do_not_convert_starting_with_space(self):
|
||||
# I actually should be looking for ``` to see if this shouldn't be converted
|
||||
# but that would be way too complex for a simple onetime conversion so fuck that.
|
||||
no_real_doku_link = """if [[ "$line" =~ "|" ]]; then"""
|
||||
self.assertEqual(no_real_doku_link, self.converter.convert(no_real_doku_link))
|
||||
|
||||
def test_internal_link_with_space_converted_to_underscore(self):
|
||||
mdLink = """[magic properties]({{< relref "magic_properties.md" >}})"""
|
||||
dokuLink = "[[magic properties]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_link_to_home_converted_to_index(self):
|
||||
mdLink = """[subpage/home]({{< relref "subpage/_index.md" >}})"""
|
||||
dokuLink = "[[subpage/home]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_link_with_ref_to_root_not_prefixed_with_root_dir(self):
|
||||
DokuWikiToHugo.root_dir = "pages"
|
||||
mdLink = """[Download SaveGame #one]({{< relref "/games/Wizardry8/Wizardry8_Saves01.rar" >}})"""
|
||||
dokuLink = """[[/games/Wizardry8/Wizardry8_Saves01.rar|Download SaveGame #one]]"""
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_link_prefixed_with_configured_root_dir_if_subdir(self):
|
||||
DokuWikiToHugo.root_dir = "pages"
|
||||
mdLink = """[bla/blie]({{< relref "pages/bla/blie.md" >}})"""
|
||||
dokuLink = "[[bla:blie]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_links_not_prefixed_with_configured_root_dir_if_currdir(self):
|
||||
DokuWikiToHugo.root_dir = "pages"
|
||||
mdLink = """[bla]({{< relref "bla.md" >}})"""
|
||||
dokuLink = "[[bla]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_links_without_text_converted_properly(self):
|
||||
mdLink = """[bla]({{< relref "bla.md" >}})"""
|
||||
dokuLink = "[[bla]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_links_with_sublink_converted_properly(self):
|
||||
mdLink = """[text]({{< relref "bla/blie.md" >}})"""
|
||||
dokuLink = "[[bla:blie|text]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_multiple_links_in_text_converted_properly(self):
|
||||
mdLink = """[bla]({{< relref "bla.md" >}}) wow this looks cool and so does [this]({{< relref "this.md" >}}) and such"""
|
||||
dokuLink = "[[bla]] wow this looks cool and so does [[this]] and such"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_links_with_some_text_in_line_converted_properly(self):
|
||||
mdLink = """[text]({{< relref "bla.md" >}}) wow this looks cool"""
|
||||
dokuLink = "[[bla|text]] wow this looks cool"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_link_with_image_tag(self):
|
||||
dokuLink = "[[code|<img src='code.jpg'>]]"
|
||||
mdLink = """[<img src='code.jpg'>]({{< relref "code.md" >}})"""
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_links_with_extension_not_suffixed_with_md(self):
|
||||
mdLink = """[text]({{< relref "bla.zip" >}})"""
|
||||
dokuLink = "[[bla.zip|text]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_links_converted_properly(self):
|
||||
mdLink = """[text]({{< relref "bla.md" >}})"""
|
||||
dokuLink = "[[bla|text]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_unknown_interwiki_link_throws_exception(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.converter.convert("[[whaddapdawg>Wiki]]")
|
||||
|
||||
def test_known_interwiki_link_with_some_spaces(self):
|
||||
mdLink = """{{< lib Purple Cow: Transform Your Business by Being Remarkable >}}"""
|
||||
mdLink = """{{< lib "Purple Cow: Transform Your Business by Being Remarkable" >}}"""
|
||||
dokuLink = "[[lib>Purple Cow: Transform Your Business by Being Remarkable]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_known_interwiki_link_converts_successfully(self):
|
||||
# see https://gohugo.io/extras/shortcodes/
|
||||
mdLink = """{{< wp Wiki >}}"""
|
||||
mdLink = """{{< wp "Wiki" >}}"""
|
||||
dokuLink = "[[wp>Wiki]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_links_with_sublink_converted_properly(self):
|
||||
mdLink = """[text]({{< relref "bla/blie" >}})"""
|
||||
dokuLink = "[[bla:blie|text]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_multiple_links_in_text_converted_properly(self):
|
||||
mdLink = """{{< relref "bla" >}} wow this looks cool and so does {{< relref "this" >}} and such"""
|
||||
dokuLink = "[[bla]] wow this looks cool and so does [[this]] and such"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_links_with_some_text_in_line_converted_properly(self):
|
||||
mdLink = """[text]({{< relref "bla" >}}) wow this looks cool"""
|
||||
dokuLink = "[[bla|text]] wow this looks cool"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_link_with_image_tag(self):
|
||||
dokuLink = "[[code|<img src='code.jpg'>]]"
|
||||
mdLink = """[<img src='code.jpg'>]({{< relref "code" >}})"""
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_internal_links_converted_properly(self):
|
||||
mdLink = """[text]({{< relref "bla" >}})"""
|
||||
dokuLink = "[[bla|text]]"
|
||||
|
||||
self.assertEqual(mdLink, self.converter.convert(dokuLink))
|
||||
|
||||
def test_external_link_without_title(self):
|
||||
mdLink = "[https://www.google.com](https://www.google.com)"
|
||||
dokuLink = "[[https://www.google.com]]"
|
||||
|
|
Loading…
Reference in New Issue