From 2e5a0aa430a7257fcd7d56a83a03923826866b6c Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Thu, 12 Jan 2017 21:45:49 +0100 Subject: [PATCH] update --- .idea/dokuwiki-to-hugo.iml | 2 +- .idea/misc.xml | 2 +- .idea/workspace.xml | 489 +++++++++++++++++++++--------- layouts/shortcodes/wp.html | 1 + src/markdown_converter.py | 21 +- src/markdown_links.py | 54 ++++ test/dokuwiki_example.txt | 7 + test/expected_markdown_output.txt | 7 + test/test_markdown_converter.py | 19 ++ test/test_markdown_links.py | 53 ++++ 10 files changed, 515 insertions(+), 140 deletions(-) create mode 100644 layouts/shortcodes/wp.html create mode 100644 src/markdown_links.py create mode 100644 test/dokuwiki_example.txt create mode 100644 test/expected_markdown_output.txt create mode 100644 test/test_markdown_converter.py create mode 100644 test/test_markdown_links.py diff --git a/.idea/dokuwiki-to-hugo.iml b/.idea/dokuwiki-to-hugo.iml index 6711606..c8a7e47 100644 --- a/.idea/dokuwiki-to-hugo.iml +++ b/.idea/dokuwiki-to-hugo.iml @@ -2,7 +2,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index fef2590..e714a9f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0c9f075..c6b3515 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,18 @@ - + + + + + + + + + + + + - + - - - + + + + + - - - - - - - - - - - + - - + + @@ -47,6 +50,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -56,24 +125,38 @@ + + + contains + cont + + - + + @@ -97,8 +180,32 @@ - + + - - + + + + + + - + - - - - - + @@ -366,25 +497,28 @@ - - - - - + + + + + - - - - - + + + + + + + + @@ -396,54 +530,57 @@ - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + - + - + - - - - + + + + - - - + + + - - + + @@ -451,21 +588,18 @@ - + + + + + - - - - - - - - - + @@ -473,7 +607,7 @@ - + @@ -481,7 +615,7 @@ - + @@ -489,5 +623,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/layouts/shortcodes/wp.html b/layouts/shortcodes/wp.html new file mode 100644 index 0000000..190237d --- /dev/null +++ b/layouts/shortcodes/wp.html @@ -0,0 +1 @@ +{{ index .Params 0 }} diff --git a/src/markdown_converter.py b/src/markdown_converter.py index 34de6a0..b0dfe0b 100644 --- a/src/markdown_converter.py +++ b/src/markdown_converter.py @@ -1,5 +1,22 @@ +from src.markdown_headers import MarkdownHeader +from src.markdown_links import MarkdownLinks +from functools import reduce class MarkdownConverter: - def convert(self, text): - pass \ No newline at end of file + def __init__(self, file): + self.file = file + self.converters = (MarkdownHeader(), MarkdownLinks()) + + def convert(self): + converted = [] + with open(self.file, 'r') as file: + for line in file: + converted.append(self.convert_line(line)) + + return "\n".join(converted) + + def convert_line(self, line): + convertfns = map(lambda converter: converter.convert, self.converters) + massconvert = reduce(lambda red1, red2: lambda text: red1(red2(line)), convertfns, lambda text: text) + return massconvert(line) diff --git a/src/markdown_links.py b/src/markdown_links.py new file mode 100644 index 0000000..1cdef87 --- /dev/null +++ b/src/markdown_links.py @@ -0,0 +1,54 @@ +import os +from os import walk + + +class MarkdownLinks: + known_shortcodes = ('wp') + + def convert(self, text): + if not text.startswith("[["): + return text + if "http" in text or "www" in text: + return self.convert_as_external_link(text) + if ">" in text: + return self.convert_as_interwiki_link(text) + return self.convert_as_internal_link(text) + + def parseUrl(self, text): + return text[2:text.index('|')] + + def parseInternalUrl(self, text): + return text[2:len(text)-2].replace(":", "/") + + def parseTitle(self, text): + return text[text.index('|') + 1: text.index(']]') - 2] + + 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 convert_as_internal_link(self, text): + if "|" not in text: + return """{{< relref "%s" >}}""" % (self.parseInternalUrl(text)) + + url = self.parseUrl(text).replace(":", "/") + title = self.parseTitle(text) + + return """[%s]({{< relref "%s" >}})""" % (title, url) + + def convert_as_external_link(self, text): + url = self.parseUrl(text) + title = self.parseTitle(text) + + return "[" + title + "](" + url + ")" + + def assert_interwiki_is_known(self, shortcode): + shortcodes = [] + for (dirpath, dirnames, filenames) in walk("../layouts/shortcodes"): + 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!") \ No newline at end of file diff --git a/test/dokuwiki_example.txt b/test/dokuwiki_example.txt new file mode 100644 index 0000000..223437d --- /dev/null +++ b/test/dokuwiki_example.txt @@ -0,0 +1,7 @@ +====== header 1 ====== + +== header 5 == + +[[hello world|hi]] this is a test! + +[[wp>Dogs]] are cool, look it up. [[link:sub|sublink]] example. diff --git a/test/expected_markdown_output.txt b/test/expected_markdown_output.txt new file mode 100644 index 0000000..de57dd9 --- /dev/null +++ b/test/expected_markdown_output.txt @@ -0,0 +1,7 @@ +# header 1 + +##### header 5 + +[hi]({{< relref "hello world" >}}) this is a test! + +{{< wp Dogs >}} are cool, look it up. [sublink]({{< relref "link/sub" >}} example. diff --git a/test/test_markdown_converter.py b/test/test_markdown_converter.py new file mode 100644 index 0000000..067e3ce --- /dev/null +++ b/test/test_markdown_converter.py @@ -0,0 +1,19 @@ +from unittest import TestCase + +from pathlib import Path + +from src.markdown_converter import MarkdownConverter + +class TestMarkdownHeader(TestCase): + + def setUp(self): + self.converter = MarkdownConverter("dokuwiki_example.txt") + + def test_acceptance_test_case(self): + # python 3.5 and up + expected = Path("expected_markdown_output.txt").read_text() + actual = self.converter.convert() + + print(actual) + self.assertEqual(expected, actual, "Files not matching!") + diff --git a/test/test_markdown_links.py b/test/test_markdown_links.py new file mode 100644 index 0000000..480bc85 --- /dev/null +++ b/test/test_markdown_links.py @@ -0,0 +1,53 @@ +from unittest import TestCase + +from src.markdown_links import MarkdownLinks + + +class TestMarkdownLinks(TestCase): + + def setUp(self): + self.converter = MarkdownLinks() + + 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" >}}""" + dokuLink = "[[bla]]" + + 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_converts_successfully(self): + # see https://gohugo.io/extras/shortcodes/ + 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_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_links_converted_properly(self): + mdLink = """[text]({{< relref "bla" >}})""" + dokuLink = "[[bla|text]]" + + self.assertEqual(mdLink, self.converter.convert(dokuLink)) + + def test_external_links_converted_properly(self): + mdLink = "[Wouter Groeneveld](https://github.com/wgroeneveld/)" + dokuLink = "[[https://github.com/wgroeneveld/|Wouter Groeneveld]]" + + self.assertEqual(mdLink, self.converter.convert(dokuLink))