Change to address the real problem whether generate tags in the front matter.

This commit is contained in:
Jianfei Hu 2018-08-04 08:40:36 -07:00
parent 2e615322ec
commit a9c9e518df
3 changed files with 13 additions and 15 deletions

View File

@ -12,7 +12,7 @@ def str2bool(v):
raise argparse.ArgumentTypeError('Boolean value expected.') raise argparse.ArgumentTypeError('Boolean value expected.')
def main(directory, options): 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__": 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("-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, parser.add_argument("--frontmatter_tags", default=True, type=str2bool,
help="whether to generate front matter in the converted markdown.") help="whether to generate tags in front matter in the converted markdown.")
opts = parser.parse_args() opts = parser.parse_args()
main(opts.dir, opts) main(opts.dir, opts)

View File

@ -8,9 +8,9 @@ from src.markdown_converter import MarkdownConverter
class DokuWikiToHugo: class DokuWikiToHugo:
root_dir = "" root_dir = ""
def __init__(self, root=None, front_matter=True): def __init__(self, root=None, frontmatter_tags=True):
self.header_converter = HugoFrontMatter() self.header_converter = HugoFrontMatter()
self.convert_frontmatter = front_matter self.frontmatter_tags = frontmatter_tags
DokuWikiToHugo.root_dir = root DokuWikiToHugo.root_dir = root
pass pass
@ -39,11 +39,10 @@ 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)
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:
if self.convert_frontmatter: text_file.write(header)
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)

View File

@ -12,16 +12,15 @@ class HugoFrontMatter:
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)