PEP-8 convention check in PyCharm

This commit is contained in:
Wouter Groeneveld 2017-01-30 16:09:23 +01:00
parent fe0bcbf330
commit 9e8fa5b9aa
22 changed files with 165 additions and 140 deletions

View File

@ -19,9 +19,9 @@ class DokuWikiToHugo:
shutil.rmtree('output')
os.mkdir('output')
def doku_to_hugo(self, dir):
def doku_to_hugo(self, directory):
self.create_output_dir()
for root, subFolders, files in os.walk(dir):
for root, subFolders, files in os.walk(directory):
files = [f for f in files if not f[0] == '.']
for file in files:
self.process_file(root, file)

View File

@ -1,4 +1,5 @@
import os.path, time
import os.path
import time
class HugoFileConfig:

View File

@ -3,12 +3,14 @@ import argparse
from src.dokuwiki_to_hugo import DokuWikiToHugo
def main(dir, root):
DokuWikiToHugo(root).doku_to_hugo(dir)
def main(directory, root):
DokuWikiToHugo(root).doku_to_hugo(directory)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
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)
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, opts.root)

View File

@ -3,6 +3,18 @@ from re import compile
from src.markdown_converter import MarkdownConverter
# PEP8 fault, could be static or function instead of method (not in class context)
def strip_lang(language):
if language is '':
return language
lang = language[1:len(language)]
if ' ' in lang:
lang = lang[0:lang.index(' ')]
return lang
class BaseMarkdownCode(ABC):
markdown = "```"
@ -10,28 +22,21 @@ class BaseMarkdownCode(ABC):
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])
language = strip_lang(match[1])
result = result.replace(match[0], BaseMarkdownCode.markdown + language)
return result.replace('</' + self.tag + '>', BaseMarkdownCode.markdown)
@MarkdownConverter.Register
@MarkdownConverter.register
class MarkdownFile(BaseMarkdownCode):
def __init__(self):
super().__init__('file')
@MarkdownConverter.Register
@MarkdownConverter.register
class MarkdownCode(BaseMarkdownCode):
def __init__(self):
super().__init__('code')

View File

@ -1,8 +1,8 @@
from src.markdown_converter import MarkdownConverter
@MarkdownConverter.Register
class MarkdownEmoji():
@MarkdownConverter.register
class MarkdownEmoji:
# config as you like. http://www.webpagefx.com/tools/emoji-cheat-sheet/
config = {
'8-)': 'sunglasses',

View File

@ -2,8 +2,9 @@ from re import compile
from src.markdown_converter import MarkdownConverter
@MarkdownConverter.Register
class MarkdownHeader():
@MarkdownConverter.register
class MarkdownHeader:
pattern = compile('(=+)(.*?)(=+)')
head = "="
config = {

View File

@ -1,7 +1,8 @@
from src.markdown_converter import MarkdownConverter
from re import compile
@MarkdownConverter.Register
@MarkdownConverter.register
class MarkdownImages:
pattern = compile('{{(\s?)(.*?)(\s?)}}')
@ -21,6 +22,7 @@ class MarkdownImages:
style.append("height: " + height + "px;")
else:
style.append("width: " + dimensions + "px;")
def parse_position():
if len(left) > 0 and len(right) > 0:
style.append("margin-left: auto; margin-right: auto;")

View File

@ -6,13 +6,14 @@ from src.dokuwiki_to_hugo import DokuWikiToHugo
from src.markdown_converter import MarkdownConverter
@MarkdownConverter.Register
class MarkdownLinks():
@MarkdownConverter.register
class MarkdownLinks:
# see http://pythex.org/
pattern = re.compile('(\[\[)(.*?)(\]\])')
def convert(self, text):
result = text
def starts_with_space(match):
return match[1][0] is ' '
@ -31,7 +32,7 @@ class MarkdownLinks():
result = result.replace(origlink, convertedlink)
return result
def parseUrl(self, text):
def parse_url(self, text):
return text[2:text.index('|')]
def add_md_and_replace_home_with_index(self, src_url):
@ -39,14 +40,16 @@ class MarkdownLinks():
if "." not in url:
url = url + ".md"
return url.replace('home.md', '_index.md')
def parseInternalUrl(self, text):
def parse_internal_url(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(":", "/")
def parse_internal_url_without_title(self, text):
url = self.parse_url(text).replace(":", "/")
return self.add_md_and_replace_home_with_index(url)
def parseTitle(self, text):
def parse_title(self, text):
return text[text.index('|') + 1: text.index(']]')]
def convert_as_interwiki_link(self, text):
@ -65,18 +68,18 @@ class MarkdownLinks():
url = ""
title = ""
if "|" not in text:
url = self.parseInternalUrl(text)
url = self.parse_internal_url(text)
title = text[2:len(text) - 2].replace(":", "/")
else:
url = self.parseInternalUrlWithoutTitle(text)
title = self.parseTitle(text)
url = self.parse_internal_url_without_title(text)
title = self.parse_title(text)
return """[%s]({{< relref "%s%s" >}})""" % (title, self.root_dir(url), url.replace(' ', '_'))
def convert_as_external_link(self, text):
if '|' in text:
url = self.parseUrl(text)
title = self.parseTitle(text)
url = self.parse_url(text)
title = self.parse_title(text)
return "[" + title + "](" + url + ")"
url = text.replace('[', '').replace(']', '')
return "[" + url + "](" + url + ")"
@ -87,5 +90,5 @@ class MarkdownLinks():
for (dirpath, dirnames, filenames) in walk(shortcodes_path):
shortcodes.extend(filenames)
break
if not shortcode in map(lambda x: x.replace(".html", ""), shortcodes):
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!")

View File

@ -3,8 +3,8 @@ import re
from src.markdown_converter import MarkdownConverter
@MarkdownConverter.Register
class MarkdownOrderedList():
@MarkdownConverter.register
class MarkdownOrderedList:
pattern = re.compile('(^(\s*)-\s)(.*)', re.MULTILINE)
def convert(self, text):
@ -15,26 +15,31 @@ class MarkdownOrderedList():
def deeper_depth(depth):
return list(filter(lambda x: x[0] > depth, last_used_linenrs))
def drop_in_depth_detected(depth):
return len(deeper_depth(depth)) > 0
def remove_deeper_depths(depth):
for itm in deeper_depth(depth):
last_used_linenrs.remove(itm)
def last_used_by_depth(depth):
return list(filter(lambda x: x[0] == depth, last_used_linenrs))
def last_used_index(depth):
return last_used_by_depth(depth)[0][2]
def last_used_linenr(depth):
result = last_used_by_depth(depth)
if len(result) == 0:
return 0
return result[0][1]
def set_last_used_linenr(depth, linenr, index):
result = list(filter(lambda x: x[0] == depth, last_used_linenrs))
if len(result) > 0:
last_used_linenrs.remove(result[0])
last_used_linenrs.append((depth, linenr, index))
def last_used_linenr(depth):
linenr_result = last_used_by_depth(depth)
if len(linenr_result) == 0:
return 0
return linenr_result[0][1]
def set_last_used_linenr(depth, linenr, the_index):
last_used_result = list(filter(lambda x: x[0] == depth, last_used_linenrs))
if len(last_used_result) > 0:
last_used_linenrs.remove(last_used_result[0])
last_used_linenrs.append((depth, linenr, the_index))
for match in MarkdownOrderedList.pattern.findall(text):
current_line = (match[0] + match[2]).replace('\n', '')
@ -42,7 +47,7 @@ class MarkdownOrderedList():
current_linenr = lines.index(current_line)
if last_used_linenr(current_depth) + 1 is current_linenr:
index = index + 1
index += 1
elif drop_in_depth_detected(current_depth):
index = last_used_index(current_depth) + 1
remove_deeper_depths(current_depth)

View File

@ -8,6 +8,7 @@ class NopStyle(ABC):
def convert(self, text):
return text
class SimpleReplacementStyle(ABC):
def __init__(self, markdown_style, dokuwiki_style):
self.markdown_style = markdown_style
@ -16,8 +17,8 @@ class SimpleReplacementStyle(ABC):
def convert(self, text):
return text.replace(self.dokuwiki_style, self.markdown_style)
class SimpleStyleBetweenTags(ABC):
class SimpleStyleBetweenTags(ABC):
def __init__(self, markdown_style, dokuwiki_style_begin, dokuwiki_style_end=None):
if dokuwiki_style_end is None:
dokuwiki_style_end = dokuwiki_style_begin
@ -32,33 +33,39 @@ class SimpleStyleBetweenTags(ABC):
result = result.replace(orig_header, new_header)
return result
@MarkdownConverter.Register
@MarkdownConverter.register
class MarkdownLineBreak(SimpleReplacementStyle):
def __init__(self):
super().__init__('<br/>', '\\')
# inline html is supported with Hugo, don't need the tags.
@MarkdownConverter.Register
class MarkdownInlineHtml():
@MarkdownConverter.register
class MarkdownInlineHtml:
def convert(self, text):
return text.replace('<html>', '').replace('</html>', '')
# bold in Doku is bold in MD
@MarkdownConverter.Register
@MarkdownConverter.register
class MarkdownBold(NopStyle):
pass
@MarkdownConverter.Register
@MarkdownConverter.register
class MarkdownItalic(SimpleStyleBetweenTags):
def __init__(self):
super().__init__('*', '//')
@MarkdownConverter.Register
@MarkdownConverter.register
class MarkdownStrikeThrough(SimpleStyleBetweenTags):
def __init__(self):
super().__init__('~~', '<del>', '</del>')
@MarkdownConverter.Register
@MarkdownConverter.register
class MarkdownInlineCode(SimpleStyleBetweenTags):
def __init__(self):
super().__init__('`', "''", "''")

View File

@ -1,8 +1,9 @@
from src.markdown_converter import MarkdownConverter
from re import compile
@MarkdownConverter.Register
class MarkdownTodo():
@MarkdownConverter.register
class MarkdownTodo:
pattern = compile('(<todo(\s#)?>)(.*?)(</todo>)')
todo = '- [ ] '
done = '- [x] '

View File

@ -1,10 +1,11 @@
from pathlib import Path
class MarkdownConverter:
converters = []
@classmethod
def Register(cls, converter_class):
def register(cls, converter_class):
cls.converters.append(converter_class())
return converter_class

View File

@ -2,6 +2,6 @@
##### header 5
[hi]({{< relref "hello world" >}}) this is a test!
[hi]({{< relref "hello_world.md" >}}) this is a test!
{{< wp Dogs >}} are cool, look it up. [sublink]({{< relref "link/sub" >}}) example.
{{< wp "Dogs" >}} are cool, look it up. [sublink]({{< relref "link/sub.md" >}}) example.

View File

@ -2,8 +2,8 @@ from unittest import TestCase
from src.markdown.code import MarkdownCode, MarkdownFile
class TestMarkdownCode(TestCase):
class TestMarkdownCode(TestCase):
def setUp(self):
self.code_converter = MarkdownCode()
self.file_converter = MarkdownFile()

View File

@ -2,6 +2,7 @@ from unittest import TestCase
from src.markdown.images import MarkdownImages
class TestMarkdownImages(TestCase):
def setUp(self):
self.converter = MarkdownImages()

View File

@ -22,105 +22,105 @@ class TestMarkdownLinks(TestCase):
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]]"
md_link = """[magic properties]({{< relref "magic_properties.md" >}})"""
doku_link = "[[magic properties]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
def test_internal_link_to_home_converted_to_index(self):
mdLink = """[subpage/home]({{< relref "subpage/_index.md" >}})"""
dokuLink = "[[subpage/home]]"
md_link = """[subpage/home]({{< relref "subpage/_index.md" >}})"""
doku_link = "[[subpage/home]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
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]]"""
md_link = """[Download SaveGame #one]({{< relref "/games/Wizardry8/Wizardry8_Saves01.rar" >}})"""
doku_link = """[[/games/Wizardry8/Wizardry8_Saves01.rar|Download SaveGame #one]]"""
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
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]]"
md_link = """[bla/blie]({{< relref "pages/bla/blie.md" >}})"""
doku_link = "[[bla:blie]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
def test_internal_links_not_prefixed_with_configured_root_dir_if_currdir(self):
DokuWikiToHugo.root_dir = "pages"
mdLink = """[bla]({{< relref "bla.md" >}})"""
dokuLink = "[[bla]]"
md_link = """[bla]({{< relref "bla.md" >}})"""
doku_link = "[[bla]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
def test_internal_links_without_text_converted_properly(self):
mdLink = """[bla]({{< relref "bla.md" >}})"""
dokuLink = "[[bla]]"
md_link = """[bla]({{< relref "bla.md" >}})"""
doku_link = "[[bla]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
def test_internal_links_with_sublink_converted_properly(self):
mdLink = """[text]({{< relref "bla/blie.md" >}})"""
dokuLink = "[[bla:blie|text]]"
md_link = """[text]({{< relref "bla/blie.md" >}})"""
doku_link = "[[bla:blie|text]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
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"
md_link = """[bla]({{< relref "bla.md" >}}) wow this looks cool and so does [this]({{< relref "this.md" >}}) and such"""
doku_link = "[[bla]] wow this looks cool and so does [[this]] and such"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
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"
md_link = """[text]({{< relref "bla.md" >}}) wow this looks cool"""
doku_link = "[[bla|text]] wow this looks cool"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
def test_internal_link_with_image_tag(self):
dokuLink = "[[code|<img src='code.jpg'>]]"
mdLink = """[<img src='code.jpg'>]({{< relref "code.md" >}})"""
doku_link = "[[code|<img src='code.jpg'>]]"
md_link = """[<img src='code.jpg'>]({{< relref "code.md" >}})"""
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
def test_internal_links_with_extension_not_suffixed_with_md(self):
mdLink = """[text]({{< relref "bla.zip" >}})"""
dokuLink = "[[bla.zip|text]]"
md_link = """[text]({{< relref "bla.zip" >}})"""
doku_link = "[[bla.zip|text]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
def test_internal_links_converted_properly(self):
mdLink = """[text]({{< relref "bla.md" >}})"""
dokuLink = "[[bla|text]]"
md_link = """[text]({{< relref "bla.md" >}})"""
doku_link = "[[bla|text]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
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" >}}"""
dokuLink = "[[lib>Purple Cow: Transform Your Business by Being Remarkable]]"
md_link = """{{< lib "Purple Cow: Transform Your Business by Being Remarkable" >}}"""
doku_link = "[[lib>Purple Cow: Transform Your Business by Being Remarkable]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
def test_known_interwiki_link_converts_successfully(self):
# see https://gohugo.io/extras/shortcodes/
mdLink = """{{< wp "Wiki" >}}"""
dokuLink = "[[wp>Wiki]]"
md_link = """{{< wp "Wiki" >}}"""
doku_link = "[[wp>Wiki]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
def test_external_link_without_title(self):
mdLink = "[https://www.google.com](https://www.google.com)"
dokuLink = "[[https://www.google.com]]"
md_link = "[https://www.google.com](https://www.google.com)"
doku_link = "[[https://www.google.com]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))
def test_external_links_converted_properly(self):
mdLink = "[Wouter Groeneveld](https://github.com/wgroeneveld/)"
dokuLink = "[[https://github.com/wgroeneveld/|Wouter Groeneveld]]"
md_link = "[Wouter Groeneveld](https://github.com/wgroeneveld/)"
doku_link = "[[https://github.com/wgroeneveld/|Wouter Groeneveld]]"
self.assertEqual(mdLink, self.converter.convert(dokuLink))
self.assertEqual(md_link, self.converter.convert(doku_link))

View File

@ -2,6 +2,7 @@ from unittest import TestCase
from src.markdown.lists import MarkdownOrderedList
class TestMarkdownLists(TestCase):
def setUp(self):
self.converter = MarkdownOrderedList()
@ -43,7 +44,6 @@ five - six
actual = self.converter.convert(src)
self.assertEqual(expected, actual)
def test_ordered_lists_nested(self):
src = '''
- Werkt zoals Rummikub:

View File

@ -5,7 +5,6 @@ from src.markdown.simplestyle import MarkdownBold, MarkdownItalic, MarkdownStrik
class TestMarkdownSimpleStyles(TestCase):
def test_inline_html_simply_removes_tags(self):
src = "<html><strong>sup</strong></html>"
expected = "<strong>sup</strong>"

View File

@ -7,7 +7,6 @@ from src.dokuwiki_to_hugo import DokuWikiToHugo
class TestDokuWikiToHugo(TestCase):
def tearDown(self):
shutil.rmtree('output')

View File

@ -7,7 +7,6 @@ from src.hugo_file_config import HugoFileConfig
class TestHugoFileConfig(TestCase):
def set_file_timestamp(self):
date = datetime(2014, 10, 10, 12)
u_time = mktime(date.timetuple())

View File

@ -4,8 +4,8 @@ from pathlib import Path
from src.markdown_converter import MarkdownConverter
class TestMarkdownHeader(TestCase):
class TestMarkdownHeader(TestCase):
def setUp(self):
self.converter = MarkdownConverter("dokuwiki_example.txt")
@ -16,4 +16,3 @@ class TestMarkdownHeader(TestCase):
print(actual)
self.assertEqual(expected, actual, "Files not matching!")