From 8c4057745929c8c8480809f6203bf48582fda555 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Sun, 15 Jan 2017 15:52:25 +0100 Subject: [PATCH] main class wiring --- README.md | 26 ++++++++++++++++++----- src/dokuwiki_to_hugo.py | 39 +++++++++++++++++++++++++++++++++++ test/test_dokuwiki_to_hugo.py | 19 +++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 src/dokuwiki_to_hugo.py create mode 100644 test/test_dokuwiki_to_hugo.py diff --git a/README.md b/README.md index bc4430c..ae37d06 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,27 @@ A DokuWiki to Hugo file exporter to quickly migrate your existing PHP wiki to Hu See https://www.dokuwiki.org/wiki:syntax +**How do I run this thing?** + +Main wiring in `DokuWikiToHugo` - see the tests for an elaborate example. + +```python + DokuWikiToHugo().doku_to_hugo('some_dokuwiki_root_dir') +``` + +This generates files in a new folder called 'output' with the same directory structure. + +## TOML File headers + +Every converted file contains a TOML header with: + +* datestamp - looking at the file modified date (transfer from your FTP using 'keep timestamps' option) +* draft automatically set to false +* tags: every subfolder is a tag, including the name of the file +* title: name of the file + +See `test_hugo_file_config.py` for an example. + ## Following Dokuwiki syntax converted: ### general @@ -44,11 +65,6 @@ Simply add custom.html and link to the website of your choice. Use Hugo's `{{ in * emoticons * no formatting (nowiki, %%) - should this just be a pre? -### structure - -* build file structure - wire everything together -* build header TOML with timestamps, draft false etc - ## Not supported and probably will never be * embedding php - kill it with fire? diff --git a/src/dokuwiki_to_hugo.py b/src/dokuwiki_to_hugo.py new file mode 100644 index 0000000..b8de66c --- /dev/null +++ b/src/dokuwiki_to_hugo.py @@ -0,0 +1,39 @@ +import os +import shutil + +from src.hugo_file_config import HugoFileConfig +from src.markdown_converter import MarkdownConverter + + +class DokuWikiToHugo: + + def __init__(self): + self.header_converter = HugoFileConfig() + 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') + + def doku_to_hugo(self, dir): + self.create_output_dir() + for root, subFolders, files in os.walk(dir): + for file in files: + self.process_file(root, file) + + def process_file(self, root, file): + destination_dir = 'output/' + root + source_file = root + '/' + file + print('generating ' + destination_dir + '/' + file + '\n') + + 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 + '/' + file, "w") as text_file: + text_file.write(header) + text_file.write('\n') + text_file.write(converted_text) diff --git a/test/test_dokuwiki_to_hugo.py b/test/test_dokuwiki_to_hugo.py new file mode 100644 index 0000000..c1e76de --- /dev/null +++ b/test/test_dokuwiki_to_hugo.py @@ -0,0 +1,19 @@ +import shutil +from unittest import TestCase + +from pathlib import Path + +from src.dokuwiki_to_hugo import DokuWikiToHugo + + +class TestDokuWikiToHugo(TestCase): + + def tearDown(self): + shutil.rmtree('output') +` + def test_convert_whole_dir(self): + DokuWikiToHugo().doku_to_hugo('subdir') + expected = Path("output/subdir/moar/dokuwiki_header_in_subdir.txt").read_text() + + self.assertIn('+++', expected) # header is there, check + self.assertIn('##### some header', expected) # some conversion done, check \ No newline at end of file