2017-01-15 15:52:25 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2020-07-24 16:26:14 +02:00
|
|
|
from hugo_front_matter import HugoFrontMatter
|
2018-09-03 15:02:54 +02:00
|
|
|
from markdown_converter import MarkdownConverter
|
2017-01-15 15:52:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DokuWikiToHugo:
|
2017-01-21 11:37:17 +01:00
|
|
|
root_dir = ""
|
2017-01-15 15:52:25 +01:00
|
|
|
|
2018-08-04 17:40:36 +02:00
|
|
|
def __init__(self, root=None, frontmatter_tags=True):
|
2018-08-04 17:09:20 +02:00
|
|
|
self.header_converter = HugoFrontMatter()
|
2018-08-04 17:40:36 +02:00
|
|
|
self.frontmatter_tags = frontmatter_tags
|
2017-01-21 11:37:17 +01:00
|
|
|
DokuWikiToHugo.root_dir = root
|
2017-01-15 15:52:25 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
def create_output_dir(self):
|
|
|
|
if os.path.exists('output'):
|
|
|
|
print('output already exists, deleting old conversion stuff')
|
|
|
|
shutil.rmtree('output')
|
|
|
|
os.mkdir('output')
|
|
|
|
|
2017-01-30 16:09:23 +01:00
|
|
|
def doku_to_hugo(self, directory):
|
2017-01-15 15:52:25 +01:00
|
|
|
self.create_output_dir()
|
2017-01-30 16:09:23 +01:00
|
|
|
for root, subFolders, files in os.walk(directory):
|
2017-01-21 11:37:17 +01:00
|
|
|
files = [f for f in files if not f[0] == '.']
|
2017-01-15 15:52:25 +01:00
|
|
|
for file in files:
|
2018-08-04 17:09:20 +02:00
|
|
|
try:
|
|
|
|
self.process_file(root, file)
|
|
|
|
except:
|
|
|
|
print('failed to convert ' + file)
|
2017-01-15 15:52:25 +01:00
|
|
|
|
|
|
|
def process_file(self, root, file):
|
|
|
|
destination_dir = 'output/' + root
|
|
|
|
source_file = root + '/' + file
|
2017-01-16 16:37:58 +01:00
|
|
|
destination_file = '_index.md' if file == 'home.txt' else os.path.splitext(file)[0] + '.md'
|
|
|
|
|
|
|
|
print('generating ' + destination_dir + '/' + destination_file + '\n')
|
2017-01-15 15:52:25 +01:00
|
|
|
|
|
|
|
if not os.path.exists(destination_dir):
|
|
|
|
os.makedirs(destination_dir)
|
2018-08-04 17:40:36 +02:00
|
|
|
header = self.header_converter.create(source_file, frontmatter_tags=self.frontmatter_tags)
|
2017-01-15 15:52:25 +01:00
|
|
|
converted_text = MarkdownConverter(source_file).convert()
|
2017-01-16 16:37:58 +01:00
|
|
|
|
|
|
|
with open(destination_dir + '/' + destination_file, "w") as text_file:
|
2017-01-15 15:52:25 +01:00
|
|
|
text_file.write(header)
|
|
|
|
text_file.write('\n')
|
|
|
|
text_file.write(converted_text)
|