From 190441453b75da9289050d98271657e66b460001 Mon Sep 17 00:00:00 2001
From: wgroeneveld
Date: Sat, 14 Jan 2017 20:14:18 +0100
Subject: [PATCH] code conversion
---
README.md | 35 +++++++++++-
src/markdown/code.py | 34 +++++++++++
src/markdown/simplestyle.py | 9 +++
src/markdown_converter.py | 1 +
test/markdown/test_code.py | 93 +++++++++++++++++++++++++++++++
test/markdown/test_simplestyle.py | 14 ++++-
6 files changed, 182 insertions(+), 4 deletions(-)
create mode 100644 src/markdown/code.py
create mode 100644 test/markdown/test_code.py
diff --git a/README.md b/README.md
index 272b3d7..1d973f6 100644
--- a/README.md
+++ b/README.md
@@ -4,17 +4,46 @@ A DokuWiki to Hugo file exporter to quickly migrate your existing PHP wiki to Hu
See https://www.dokuwiki.org/wiki:syntax
+## Following Dokuwiki syntax converted:
+
+### general
+
+* code, file, inlinecode with single quotes
+* bold, italic, sub/sup, strikethrough
+* headings with equal sign
+* linebreaks (double backslash) are at this moment replaced with HTML BRs.
+
+### embedding HTML
+
+Since Hugo still supports html tags, we don't need to do anything but to remove the `` tags.
+
+See also the `MarkdownInlineHtml` class in simplestyle module.
+
+### links
+
+Simple internal links are converted to relrefs like `[[mylink]]`. Local links with double colon are replaced by forward slash.
+
+`[[sub:link]]` would become a link to the sub/link article.
+
+If it's a HTTP(S) link, it stays that way.
+
+#### interwiki
+
+You'll have to come up with your own shortcodes for those.
+
+See wp.html in the layouts directory. You could customize interwiki links from dokuwiki: `[[custom>somelink]]` would refer to some custom wiki.
+Simply add custom.html and link to the website of your choice. Use Hugo's `{{ index .Params 0 }}` to get the link content.
+
## TODO
* Figure out image links ala http://php.net|{{wiki:dokuwiki-128.png}}
* Tables, should complex ones be supported or can I do a manual convert?
* build file structure - wire everything together
* build header TOML with timestamps, draft false etc
-* lists
+* ordered lists using dash to markdown?
* emoticons
* no formatting (nowiki) - should this just be a pre?
-* code hilighting (inline and multiline and downloadable)
-* embedding html - remove html tag and done?
+* preventing of wiki markup %% - what to do with it?
## Not supported and probably will never be
diff --git a/src/markdown/code.py b/src/markdown/code.py
new file mode 100644
index 0000000..6b0c5e6
--- /dev/null
+++ b/src/markdown/code.py
@@ -0,0 +1,34 @@
+from abc import ABC
+from re import compile
+
+class BaseMarkdownCode(ABC):
+ markdown = "```"
+
+ def __init__(self, tag):
+ self.tag = tag
+ self.pattern = compile('(<' + tag + '(.*?)>)')
+
+ def strip_lang(self, language):
+ if(language is ''):
+ return language
+
+ lang = language[1:len(language)]
+ if(' ' in lang):
+ lang = lang[0:lang.index(' ')]
+ return lang
+
+ def convert(self, text):
+ result = text
+ for match in self.pattern.findall(text):
+ language = self.strip_lang(match[1])
+ result = result.replace(match[0], BaseMarkdownCode.markdown + language)
+ return result.replace('' + self.tag + '>', BaseMarkdownCode.markdown)
+
+class MarkdownFile(BaseMarkdownCode):
+ def __init__(self):
+ super().__init__('file')
+
+class MarkdownCode(BaseMarkdownCode):
+ def __init__(self):
+ super().__init__('code')
+
diff --git a/src/markdown/simplestyle.py b/src/markdown/simplestyle.py
index f1472a9..503433d 100644
--- a/src/markdown/simplestyle.py
+++ b/src/markdown/simplestyle.py
@@ -33,6 +33,11 @@ class MarkdownLineBreak(SimpleReplacementStyle):
def __init__(self):
super().__init__('
', '\\')
+# inline html is supported with Hugo, don't need the tags.
+class MarkdownInlineHtml:
+ def convert(self, text):
+ return text.replace('', '').replace('', '')
+
# bold in Doku is bold in MD
class MarkdownBold(NopStyle):
pass
@@ -44,3 +49,7 @@ class MarkdownItalic(SimpleStyleBetweenTags):
class MarkdownStrikeThrough(SimpleStyleBetweenTags):
def __init__(self):
super().__init__('~~', '', '')
+
+class MarkdownInlineCode(SimpleStyleBetweenTags):
+ def __init__(self):
+ super().__init__('`', "''", "''")
\ No newline at end of file
diff --git a/src/markdown_converter.py b/src/markdown_converter.py
index de583cc..e40b30d 100644
--- a/src/markdown_converter.py
+++ b/src/markdown_converter.py
@@ -11,6 +11,7 @@ class MarkdownConverter:
def __init__(self, file):
self.file = file
self.converters = (
+ # TODO auto-discover these. How do I do that, without interfaces?
MarkdownHeader(),
MarkdownLinks(),
MarkdownItalic(),
diff --git a/test/markdown/test_code.py b/test/markdown/test_code.py
new file mode 100644
index 0000000..f58a733
--- /dev/null
+++ b/test/markdown/test_code.py
@@ -0,0 +1,93 @@
+from unittest import TestCase
+
+from src.markdown.code import MarkdownCode, MarkdownFile
+
+class TestMarkdownCode(TestCase):
+
+ def setUp(self):
+ self.code_converter = MarkdownCode()
+ self.file_converter = MarkdownFile()
+
+ def test_convert_file_without_language(self):
+ src = """
+ blabla
+
+ this
+ that
+
+ blehbleh
+ """
+
+ expected = """
+ blabla
+ ```
+ this
+ that
+ ```
+ blehbleh
+ """
+
+ self.assertEqual(expected, self.file_converter.convert(src))
+
+ def test_convert_file_with_some_language(self):
+ src = """
+ blabla
+
+ $cool = "yoo";
+ echo $cool;
+
+ blehbleh
+ """
+
+ expected = """
+ blabla
+ ```php
+ $cool = "yoo";
+ echo $cool;
+ ```
+ blehbleh
+ """
+
+ self.assertEqual(expected, self.file_converter.convert(src))
+
+ def test_convert_code_with_specific_language(self):
+ src = """
+ blabla
+
+ $_REQUEST = 'sup';
+ echo "yoo";
+
+ blehbleh
+ """
+
+ expected = """
+ blabla
+ ```php
+ $_REQUEST = 'sup';
+ echo "yoo";
+ ```
+ blehbleh
+ """
+
+ self.assertEqual(expected, self.code_converter.convert(src))
+
+ def test_convert_code_without_language(self):
+ src = """
+ blabla
+
+ this
+ that
+
+ blehbleh
+ """
+
+ expected = """
+ blabla
+ ```
+ this
+ that
+ ```
+ blehbleh
+ """
+
+ self.assertEqual(expected, self.code_converter.convert(src))
\ No newline at end of file
diff --git a/test/markdown/test_simplestyle.py b/test/markdown/test_simplestyle.py
index bf63c14..da921e9 100644
--- a/test/markdown/test_simplestyle.py
+++ b/test/markdown/test_simplestyle.py
@@ -1,10 +1,22 @@
from unittest import TestCase
-from src.markdown.simplestyle import MarkdownBold, MarkdownItalic, MarkdownStrikeThrough, MarkdownLineBreak
+from src.markdown.simplestyle import MarkdownBold, MarkdownItalic, MarkdownStrikeThrough, MarkdownLineBreak, \
+ MarkdownInlineCode, MarkdownInlineHtml
class TestMarkdownSimpleStyles(TestCase):
+ def test_inline_html_simply_removes_tags(self):
+ src = "sup"
+ expected = "sup"
+ self.assertEqual(expected, MarkdownInlineHtml().convert(src))
+
+ def test_convert_inline_code(self):
+ inline_converter = MarkdownInlineCode()
+ src = "hi this is ''some code''"
+ expected = "hi this is `some code`"
+ self.assertEqual(expected, inline_converter.convert(src))
+
def test_some_linebreaks(self):
src = '''
hello \\