Compare commits
9 Commits
9e8fa5b9aa
...
cfe7193d8b
Author | SHA1 | Date |
---|---|---|
wgroeneveld | cfe7193d8b | |
Wouter Groeneveld | 782ae4e6c0 | |
Steve Miller | c80ece8ca3 | |
Wouter Groeneveld | da67d10270 | |
Steve Miller | 941bd62f97 | |
Jianfei Hu | 7bfa0d027e | |
Jianfei Hu | a9c9e518df | |
Jianfei Hu | 2e615322ec | |
Jianfei Hu | dc14c99f33 |
|
@ -1,3 +1,5 @@
|
||||||
|
# default output directory for the converter.
|
||||||
|
output/*
|
||||||
|
|
||||||
# Created by https://www.gitignore.io/api/pycharm,python
|
# Created by https://www.gitignore.io/api/pycharm,python
|
||||||
|
|
||||||
|
@ -149,4 +151,7 @@ ENV/
|
||||||
# Rope project settings
|
# Rope project settings
|
||||||
.ropeproject
|
.ropeproject
|
||||||
|
|
||||||
# End of https://www.gitignore.io/api/pycharm,python
|
# End of https://www.gitignore.io/api/pycharm,python
|
||||||
|
|
||||||
|
# Ignore output directory generated from program
|
||||||
|
output/
|
||||||
|
|
16
README.md
16
README.md
|
@ -8,9 +8,19 @@ See https://www.dokuwiki.org/wiki:syntax
|
||||||
|
|
||||||
##### Running it from command line
|
##### Running it from command line
|
||||||
|
|
||||||
Just use the main python class. It uses `argparse` - only provide one argument: the directory you want to parse:
|
Just use the main python class. It uses `argparse`:
|
||||||
|
|
||||||
```python main.py --dir='some_dokuwiki_root_dir'```
|
`python src/main.py --dir='some_dokuwiki_root_dir'`
|
||||||
|
|
||||||
|
Command line options:
|
||||||
|
|
||||||
|
- `--dir`, the directory of your DokuWiki pages collection.
|
||||||
|
- `--frontmatter_tags=true`, whether to generate tags in the converted Hugo markdown. By default, the tool generates
|
||||||
|
tags based on the path to the document.
|
||||||
|
|
||||||
|
##### Running unittests
|
||||||
|
|
||||||
|
`python runtests.py` in root folder. `-m unittest` does not work as imports mess up `src`/`test` subfolder structures.
|
||||||
|
|
||||||
##### Including it into your python project:
|
##### Including it into your python project:
|
||||||
|
|
||||||
|
@ -123,4 +133,4 @@ That way it's auto-loaded and wired in the main conversion.
|
||||||
|
|
||||||
* embedding php - kill it with fire?
|
* embedding php - kill it with fire?
|
||||||
* macro's - kill it with fire?
|
* macro's - kill it with fire?
|
||||||
* what to do with footnotes?
|
* what to do with footnotes?
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
from src.dokuwiki_to_hugo import DokuWikiToHugo
|
||||||
|
|
||||||
|
|
||||||
|
def str2bool(v):
|
||||||
|
if v.lower() in ('yes', 'true', 't', 'y', '1'):
|
||||||
|
return True
|
||||||
|
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
raise argparse.ArgumentTypeError('Boolean value expected.')
|
||||||
|
|
||||||
|
def main(directory, options):
|
||||||
|
DokuWikiToHugo(options.root, frontmatter_tags=options.frontmatter_tags).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("--frontmatter_tags", default=True, type=str2bool,
|
||||||
|
help="whether to generate tags in front matter in the converted markdown.")
|
||||||
|
opts = parser.parse_args()
|
||||||
|
main(opts.dir, opts)
|
|
@ -0,0 +1,9 @@
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
sys.path.append('src')
|
||||||
|
|
||||||
|
loader = unittest.TestLoader()
|
||||||
|
testSuite = loader.discover('test')
|
||||||
|
testRunner = unittest.TextTestRunner(verbosity=2)
|
||||||
|
testRunner.run(testSuite)
|
|
@ -1,2 +1,2 @@
|
||||||
# this initializes the submodule __init__
|
# this initializes the submodule __init__
|
||||||
from src.markdown import *
|
from markdown import *
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from src.hugo_file_config import HugoFileConfig
|
from hugo_front_matter import HugoFrontMatter
|
||||||
from src.markdown_converter import MarkdownConverter
|
from markdown_converter import MarkdownConverter
|
||||||
|
|
||||||
|
|
||||||
class DokuWikiToHugo:
|
class DokuWikiToHugo:
|
||||||
root_dir = ""
|
root_dir = ""
|
||||||
|
|
||||||
def __init__(self, root=None):
|
def __init__(self, root=None, frontmatter_tags=True):
|
||||||
self.header_converter = HugoFileConfig()
|
self.header_converter = HugoFrontMatter()
|
||||||
|
self.frontmatter_tags = frontmatter_tags
|
||||||
DokuWikiToHugo.root_dir = root
|
DokuWikiToHugo.root_dir = root
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -24,7 +25,10 @@ class DokuWikiToHugo:
|
||||||
for root, subFolders, files in os.walk(directory):
|
for root, subFolders, files in os.walk(directory):
|
||||||
files = [f for f in files if not f[0] == '.']
|
files = [f for f in files if not f[0] == '.']
|
||||||
for file in files:
|
for file in files:
|
||||||
self.process_file(root, file)
|
try:
|
||||||
|
self.process_file(root, file)
|
||||||
|
except:
|
||||||
|
print('failed to convert ' + file)
|
||||||
|
|
||||||
def process_file(self, root, file):
|
def process_file(self, root, file):
|
||||||
destination_dir = 'output/' + root
|
destination_dir = 'output/' + root
|
||||||
|
@ -35,8 +39,7 @@ class DokuWikiToHugo:
|
||||||
|
|
||||||
if not os.path.exists(destination_dir):
|
if not os.path.exists(destination_dir):
|
||||||
os.makedirs(destination_dir)
|
os.makedirs(destination_dir)
|
||||||
|
header = self.header_converter.create(source_file, frontmatter_tags=self.frontmatter_tags)
|
||||||
header = self.header_converter.create(source_file)
|
|
||||||
converted_text = MarkdownConverter(source_file).convert()
|
converted_text = MarkdownConverter(source_file).convert()
|
||||||
|
|
||||||
with open(destination_dir + '/' + destination_file, "w") as text_file:
|
with open(destination_dir + '/' + destination_file, "w") as text_file:
|
||||||
|
|
|
@ -2,7 +2,7 @@ import os.path
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
class HugoFileConfig:
|
class HugoFrontMatter:
|
||||||
|
|
||||||
def filename(self, location):
|
def filename(self, location):
|
||||||
return location.split('/')[-1][0:-4]
|
return location.split('/')[-1][0:-4]
|
||||||
|
@ -12,16 +12,15 @@ class HugoFileConfig:
|
||||||
return filename[0:len(filename) - 4]
|
return filename[0:len(filename) - 4]
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
def create(self, file_location):
|
def create(self, file_location, frontmatter_tags=True):
|
||||||
title = self.filename(file_location)
|
title = self.filename(file_location)
|
||||||
tags = list(map(self.strip_extension, file_location.split('/')))
|
tags = list(map(self.strip_extension, file_location.split('/')))
|
||||||
|
tags_content = '''\ntags = [
|
||||||
|
%s
|
||||||
|
]\n''' % (',\n'.join(map(lambda tag: ' "' + tag + '"', tags)))
|
||||||
date = time.strftime('%Y-%m-%d', time.gmtime(os.path.getmtime(file_location)))
|
date = time.strftime('%Y-%m-%d', time.gmtime(os.path.getmtime(file_location)))
|
||||||
|
|
||||||
return """+++
|
return """+++
|
||||||
title = "%s"
|
title = "%s"
|
||||||
draft = false
|
draft = false%s
|
||||||
tags = [
|
|
||||||
%s
|
|
||||||
]
|
|
||||||
date = "%s"
|
date = "%s"
|
||||||
+++""" % (title, ',\n'.join(map(lambda tag: ' "' + tag + '"', tags)), date)
|
+++""" % (title, tags_content if frontmatter_tags else '', date)
|
|
@ -1,6 +1,6 @@
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
from src.dokuwiki_to_hugo import DokuWikiToHugo
|
from dokuwiki_to_hugo import DokuWikiToHugo
|
||||||
|
|
||||||
|
|
||||||
def main(directory, root):
|
def main(directory, root):
|
||||||
|
|
|
@ -9,16 +9,17 @@ from src.dokuwiki_to_hugo import DokuWikiToHugo
|
||||||
class TestDokuWikiToHugo(TestCase):
|
class TestDokuWikiToHugo(TestCase):
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
shutil.rmtree('output')
|
shutil.rmtree('output')
|
||||||
|
pass
|
||||||
|
|
||||||
def test_doku_to_hugo_converts_home_to_index_markdown_files(self):
|
def test_doku_to_hugo_converts_home_to_index_markdown_files(self):
|
||||||
DokuWikiToHugo().doku_to_hugo('subdir')
|
DokuWikiToHugo().doku_to_hugo('test/subdir')
|
||||||
expected = Path("output/subdir/_index.md").read_text()
|
expected = Path("output/test/subdir/_index.md").read_text()
|
||||||
|
|
||||||
self.assertIn('subdir index', expected)
|
self.assertIn('subdir index', expected)
|
||||||
|
|
||||||
def test_convert_whole_dir(self):
|
def test_convert_whole_dir(self):
|
||||||
DokuWikiToHugo().doku_to_hugo('subdir')
|
DokuWikiToHugo().doku_to_hugo('test/subdir')
|
||||||
expected = Path("output/subdir/moar/dokuwiki_header_in_subdir.md").read_text()
|
expected = Path("output/test/subdir/moar/dokuwiki_header_in_subdir.md").read_text()
|
||||||
|
|
||||||
self.assertIn('+++', expected) # header is there, check
|
self.assertIn('+++', expected) # header is there, check
|
||||||
self.assertIn('##### some header', expected) # some conversion done, check
|
self.assertIn('##### some header', expected) # some conversion done, check
|
||||||
|
|
|
@ -3,33 +3,35 @@ from os import utime
|
||||||
from time import mktime
|
from time import mktime
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
from src.hugo_file_config import HugoFileConfig
|
from src.hugo_front_matter import HugoFrontMatter
|
||||||
|
|
||||||
|
|
||||||
class TestHugoFileConfig(TestCase):
|
class TestHugoFrontMatter(TestCase):
|
||||||
def set_file_timestamp(self):
|
def set_file_timestamp(self):
|
||||||
date = datetime(2014, 10, 10, 12)
|
date = datetime(2014, 10, 10, 12)
|
||||||
u_time = mktime(date.timetuple())
|
u_time = mktime(date.timetuple())
|
||||||
utime('dokuwiki_header_example.txt', (u_time, u_time))
|
utime('test/dokuwiki_header_example.txt', (u_time, u_time))
|
||||||
utime('subdir/moar/dokuwiki_header_in_subdir.txt', (u_time, u_time))
|
utime('test/subdir/moar/dokuwiki_header_in_subdir.txt', (u_time, u_time))
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.set_file_timestamp()
|
self.set_file_timestamp()
|
||||||
self.header = HugoFileConfig()
|
self.header = HugoFrontMatter()
|
||||||
|
|
||||||
def test_dokuwiki_in_subdir_creates_tags_for_each_dir(self):
|
def test_dokuwiki_in_subdir_creates_tags_for_each_dir(self):
|
||||||
expected_header = """+++
|
expected_header = """+++
|
||||||
title = "dokuwiki_header_in_subdir"
|
title = "dokuwiki_header_in_subdir"
|
||||||
draft = false
|
draft = false
|
||||||
tags = [
|
tags = [
|
||||||
|
"test",
|
||||||
"subdir",
|
"subdir",
|
||||||
"moar",
|
"moar",
|
||||||
"dokuwiki_header_in_subdir"
|
"dokuwiki_header_in_subdir"
|
||||||
]
|
]
|
||||||
|
|
||||||
date = "2014-10-10"
|
date = "2014-10-10"
|
||||||
+++"""
|
+++"""
|
||||||
|
|
||||||
actual_header = self.header.create('subdir/moar/dokuwiki_header_in_subdir.txt')
|
actual_header = self.header.create('test/subdir/moar/dokuwiki_header_in_subdir.txt')
|
||||||
self.assertEqual(expected_header, actual_header)
|
self.assertEqual(expected_header, actual_header)
|
||||||
|
|
||||||
def test_dokuwiki_header_example(self):
|
def test_dokuwiki_header_example(self):
|
||||||
|
@ -37,10 +39,12 @@ date = "2014-10-10"
|
||||||
title = "dokuwiki_header_example"
|
title = "dokuwiki_header_example"
|
||||||
draft = false
|
draft = false
|
||||||
tags = [
|
tags = [
|
||||||
|
"test",
|
||||||
"dokuwiki_header_example"
|
"dokuwiki_header_example"
|
||||||
]
|
]
|
||||||
|
|
||||||
date = "2014-10-10"
|
date = "2014-10-10"
|
||||||
+++"""
|
+++"""
|
||||||
|
|
||||||
actual_header = self.header.create('dokuwiki_header_example.txt')
|
actual_header = self.header.create('test/dokuwiki_header_example.txt')
|
||||||
self.assertEqual(expected_header, actual_header)
|
self.assertEqual(expected_header, actual_header)
|
|
@ -7,11 +7,11 @@ from src.markdown_converter import MarkdownConverter
|
||||||
|
|
||||||
class TestMarkdownHeader(TestCase):
|
class TestMarkdownHeader(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.converter = MarkdownConverter("dokuwiki_example.txt")
|
self.converter = MarkdownConverter("test/dokuwiki_example.txt")
|
||||||
|
|
||||||
def test_acceptance_test_case(self):
|
def test_acceptance_test_case(self):
|
||||||
# python 3.5 and up
|
# python 3.5 and up
|
||||||
expected = Path("expected_markdown_output.txt").read_text()
|
expected = Path("test/expected_markdown_output.txt").read_text()
|
||||||
actual = self.converter.convert()
|
actual = self.converter.convert()
|
||||||
|
|
||||||
print(actual)
|
print(actual)
|
||||||
|
|
Loading…
Reference in New Issue