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)