add option for --front_matter to control whether generate front matter.
This commit is contained in:
parent
dc14c99f33
commit
2e615322ec
|
@ -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,4 @@ 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
|
||||||
|
|
16
main.py
16
main.py
|
@ -3,8 +3,16 @@ import argparse
|
||||||
from src.dokuwiki_to_hugo import DokuWikiToHugo
|
from src.dokuwiki_to_hugo import DokuWikiToHugo
|
||||||
|
|
||||||
|
|
||||||
def main(directory, root):
|
def str2bool(v):
|
||||||
DokuWikiToHugo(root).doku_to_hugo(directory)
|
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, front_matter=options.front_matter).doku_to_hugo(directory)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -12,5 +20,7 @@ if __name__ == "__main__":
|
||||||
parser.add_argument("-d", "--dir", help="the 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",
|
parser.add_argument("-r", "--root", help="the root directory name in your hugo content where to link to",
|
||||||
required=False)
|
required=False)
|
||||||
|
parser.add_argument("--front_matter", default=True, type=str2bool,
|
||||||
|
help="whether to generate front matter in the converted markdown.")
|
||||||
opts = parser.parse_args()
|
opts = parser.parse_args()
|
||||||
main(opts.dir, opts.root)
|
main(opts.dir, opts)
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from src.hugo_file_config import HugoFileConfig
|
from src.hugo_front_matter import HugoFrontMatter
|
||||||
from src.markdown_converter import MarkdownConverter
|
from src.markdown_converter import MarkdownConverter
|
||||||
|
|
||||||
|
|
||||||
class DokuWikiToHugo:
|
class DokuWikiToHugo:
|
||||||
root_dir = ""
|
root_dir = ""
|
||||||
|
|
||||||
def __init__(self, root=None):
|
def __init__(self, root=None, front_matter=True):
|
||||||
self.header_converter = HugoFileConfig()
|
self.header_converter = HugoFrontMatter()
|
||||||
|
self.convert_frontmatter = front_matter
|
||||||
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,11 +39,11 @@ 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)
|
|
||||||
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:
|
||||||
text_file.write(header)
|
if self.convert_frontmatter:
|
||||||
|
header = self.header_converter.create(source_file)
|
||||||
|
text_file.write(header)
|
||||||
text_file.write('\n')
|
text_file.write('\n')
|
||||||
text_file.write(converted_text)
|
text_file.write(converted_text)
|
||||||
|
|
|
@ -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]
|
|
@ -3,10 +3,10 @@ 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())
|
||||||
|
@ -15,7 +15,7 @@ class TestHugoFileConfig(TestCase):
|
||||||
|
|
||||||
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 = """+++
|
Loading…
Reference in New Issue