From dc14c99f33176d51c2369f053d0e5c5a98a00e88 Mon Sep 17 00:00:00 2001 From: Jianfei Hu Date: Sat, 4 Aug 2018 08:03:49 -0700 Subject: [PATCH 1/4] move src/main.py to main.py --- src/main.py => main.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main.py => main.py (100%) diff --git a/src/main.py b/main.py similarity index 100% rename from src/main.py rename to main.py From 2e615322ecf238e87f4013e650a6d5a5ce240031 Mon Sep 17 00:00:00 2001 From: Jianfei Hu Date: Sat, 4 Aug 2018 08:09:20 -0700 Subject: [PATCH 2/4] add option for --front_matter to control whether generate front matter. --- .gitignore | 4 +++- main.py | 16 +++++++++++++--- src/dokuwiki_to_hugo.py | 18 +++++++++++------- ...ugo_file_config.py => hugo_front_matter.py} | 2 +- ...ile_config.py => test_hugo_front_matter.py} | 6 +++--- 5 files changed, 31 insertions(+), 15 deletions(-) rename src/{hugo_file_config.py => hugo_front_matter.py} (96%) rename test/{test_hugo_file_config.py => test_hugo_front_matter.py} (89%) diff --git a/.gitignore b/.gitignore index b7ed096..da16978 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +# default output directory for the converter. +output/* # Created by https://www.gitignore.io/api/pycharm,python @@ -149,4 +151,4 @@ ENV/ # Rope project settings .ropeproject -# End of https://www.gitignore.io/api/pycharm,python \ No newline at end of file +# End of https://www.gitignore.io/api/pycharm,python diff --git a/main.py b/main.py index 0c6c6ba..0428e5b 100644 --- a/main.py +++ b/main.py @@ -3,8 +3,16 @@ import argparse from src.dokuwiki_to_hugo import DokuWikiToHugo -def main(directory, root): - DokuWikiToHugo(root).doku_to_hugo(directory) +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, front_matter=options.front_matter).doku_to_hugo(directory) 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("-r", "--root", help="the root directory name in your hugo content where to link to", 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() - main(opts.dir, opts.root) + main(opts.dir, opts) diff --git a/src/dokuwiki_to_hugo.py b/src/dokuwiki_to_hugo.py index c6ccd5a..5be2612 100644 --- a/src/dokuwiki_to_hugo.py +++ b/src/dokuwiki_to_hugo.py @@ -1,15 +1,16 @@ import os import shutil -from src.hugo_file_config import HugoFileConfig +from src.hugo_front_matter import HugoFrontMatter from src.markdown_converter import MarkdownConverter class DokuWikiToHugo: root_dir = "" - def __init__(self, root=None): - self.header_converter = HugoFileConfig() + def __init__(self, root=None, front_matter=True): + self.header_converter = HugoFrontMatter() + self.convert_frontmatter = front_matter DokuWikiToHugo.root_dir = root pass @@ -24,7 +25,10 @@ class DokuWikiToHugo: 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) + try: + self.process_file(root, file) + except: + print('failed to convert ' + file) def process_file(self, root, file): destination_dir = 'output/' + root @@ -35,11 +39,11 @@ class DokuWikiToHugo: if not os.path.exists(destination_dir): os.makedirs(destination_dir) - - header = self.header_converter.create(source_file) converted_text = MarkdownConverter(source_file).convert() 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(converted_text) diff --git a/src/hugo_file_config.py b/src/hugo_front_matter.py similarity index 96% rename from src/hugo_file_config.py rename to src/hugo_front_matter.py index 35697e3..a778e17 100644 --- a/src/hugo_file_config.py +++ b/src/hugo_front_matter.py @@ -2,7 +2,7 @@ import os.path import time -class HugoFileConfig: +class HugoFrontMatter: def filename(self, location): return location.split('/')[-1][0:-4] diff --git a/test/test_hugo_file_config.py b/test/test_hugo_front_matter.py similarity index 89% rename from test/test_hugo_file_config.py rename to test/test_hugo_front_matter.py index 1edd3c7..6c4411c 100644 --- a/test/test_hugo_file_config.py +++ b/test/test_hugo_front_matter.py @@ -3,10 +3,10 @@ from os import utime from time import mktime 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): date = datetime(2014, 10, 10, 12) u_time = mktime(date.timetuple()) @@ -15,7 +15,7 @@ class TestHugoFileConfig(TestCase): def setUp(self): self.set_file_timestamp() - self.header = HugoFileConfig() + self.header = HugoFrontMatter() def test_dokuwiki_in_subdir_creates_tags_for_each_dir(self): expected_header = """+++ From a9c9e518df6e2c3ad03606630c8e0d75985c6c2c Mon Sep 17 00:00:00 2001 From: Jianfei Hu Date: Sat, 4 Aug 2018 08:40:36 -0700 Subject: [PATCH 3/4] Change to address the real problem whether generate tags in the front matter. --- main.py | 6 +++--- src/dokuwiki_to_hugo.py | 9 ++++----- src/hugo_front_matter.py | 13 ++++++------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 0428e5b..d782a3c 100644 --- a/main.py +++ b/main.py @@ -12,7 +12,7 @@ def str2bool(v): raise argparse.ArgumentTypeError('Boolean value expected.') def main(directory, options): - DokuWikiToHugo(options.root, front_matter=options.front_matter).doku_to_hugo(directory) + DokuWikiToHugo(options.root, frontmatter_tags=options.frontmatter_tags).doku_to_hugo(directory) if __name__ == "__main__": @@ -20,7 +20,7 @@ if __name__ == "__main__": 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("--front_matter", default=True, type=str2bool, - help="whether to generate front matter in the converted markdown.") + 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) diff --git a/src/dokuwiki_to_hugo.py b/src/dokuwiki_to_hugo.py index 5be2612..e2efd6e 100644 --- a/src/dokuwiki_to_hugo.py +++ b/src/dokuwiki_to_hugo.py @@ -8,9 +8,9 @@ from src.markdown_converter import MarkdownConverter class DokuWikiToHugo: root_dir = "" - def __init__(self, root=None, front_matter=True): + def __init__(self, root=None, frontmatter_tags=True): self.header_converter = HugoFrontMatter() - self.convert_frontmatter = front_matter + self.frontmatter_tags = frontmatter_tags DokuWikiToHugo.root_dir = root pass @@ -39,11 +39,10 @@ class DokuWikiToHugo: if not os.path.exists(destination_dir): os.makedirs(destination_dir) + header = self.header_converter.create(source_file, frontmatter_tags=self.frontmatter_tags) converted_text = MarkdownConverter(source_file).convert() with open(destination_dir + '/' + destination_file, "w") as text_file: - if self.convert_frontmatter: - header = self.header_converter.create(source_file) - text_file.write(header) + text_file.write(header) text_file.write('\n') text_file.write(converted_text) diff --git a/src/hugo_front_matter.py b/src/hugo_front_matter.py index a778e17..15569b3 100644 --- a/src/hugo_front_matter.py +++ b/src/hugo_front_matter.py @@ -12,16 +12,15 @@ class HugoFrontMatter: return filename[0:len(filename) - 4] return filename - def create(self, file_location): + def create(self, file_location, frontmatter_tags=True): title = self.filename(file_location) 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))) - return """+++ title = "%s" -draft = false -tags = [ -%s -] +draft = false%s date = "%s" -+++""" % (title, ',\n'.join(map(lambda tag: ' "' + tag + '"', tags)), date) ++++""" % (title, tags_content if frontmatter_tags else '', date) From 7bfa0d027eb36ec9d767b1a1b62086f4468a36cf Mon Sep 17 00:00:00 2001 From: Jianfei Hu Date: Fri, 24 Aug 2018 14:37:18 -0700 Subject: [PATCH 4/4] Modify README for command line options. --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 09d2ec9..f6fd3ca 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,16 @@ See https://www.dokuwiki.org/wiki:syntax ##### 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'``` +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. + ##### Including it into your python project: Main wiring in `DokuWikiToHugo` - see the tests for an elaborate example.