From 635e7467567987229494c57bef0f24d88584ec7b Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Sun, 15 Jan 2017 12:24:26 +0100 Subject: [PATCH] header generation --- src/hugo_file_config.py | 26 ++++++++++ test/dokuwiki_header_example.txt | 6 +++ .../subdir/moar/dokuwiki_header_in_subdir.txt | 6 +++ test/test_hugo_file_config.py | 47 +++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 src/hugo_file_config.py create mode 100644 test/dokuwiki_header_example.txt create mode 100644 test/subdir/moar/dokuwiki_header_in_subdir.txt create mode 100644 test/test_hugo_file_config.py diff --git a/src/hugo_file_config.py b/src/hugo_file_config.py new file mode 100644 index 0000000..f0db3de --- /dev/null +++ b/src/hugo_file_config.py @@ -0,0 +1,26 @@ +import os.path, time + + +class HugoFileConfig: + + def filename(self, location): + return location.split('/')[-1][0:-4] + + def strip_extension(self, filename): + if '.' in filename: + return filename[0:len(filename) - 4] + return filename + + def create(self, file_location): + title = self.filename(file_location) + tags = list(map(self.strip_extension, file_location.split('/'))) + date = time.strftime('%Y-%m-%d', time.gmtime(os.path.getmtime(file_location))) + + return """+++ +title = "%s" +draft = false +tags = [ +%s +] +date = "%s" ++++""" % (title, ',\n'.join(map(lambda tag: ' "' + tag + '"', tags)), date) \ No newline at end of file diff --git a/test/dokuwiki_header_example.txt b/test/dokuwiki_header_example.txt new file mode 100644 index 0000000..203940d --- /dev/null +++ b/test/dokuwiki_header_example.txt @@ -0,0 +1,6 @@ +== some header == + +hello there! + +This is //dokuwiki// style stuff right there. \\ +Thanks for stopping by. diff --git a/test/subdir/moar/dokuwiki_header_in_subdir.txt b/test/subdir/moar/dokuwiki_header_in_subdir.txt new file mode 100644 index 0000000..203940d --- /dev/null +++ b/test/subdir/moar/dokuwiki_header_in_subdir.txt @@ -0,0 +1,6 @@ +== some header == + +hello there! + +This is //dokuwiki// style stuff right there. \\ +Thanks for stopping by. diff --git a/test/test_hugo_file_config.py b/test/test_hugo_file_config.py new file mode 100644 index 0000000..cd1c75e --- /dev/null +++ b/test/test_hugo_file_config.py @@ -0,0 +1,47 @@ +from datetime import datetime +from os import utime +from time import mktime +from unittest import TestCase + +from src.hugo_file_config import HugoFileConfig + + +class TestHugoFileConfig(TestCase): + + def set_file_timestamp(self): + date = datetime(2014, 10, 10, 12) + u_time = mktime(date.timetuple()) + utime('dokuwiki_header_example.txt', (u_time, u_time)) + utime('subdir/moar/dokuwiki_header_in_subdir.txt', (u_time, u_time)) + + def setUp(self): + self.set_file_timestamp() + self.header = HugoFileConfig() + + def test_dokuwiki_in_subdir_creates_tags_for_each_dir(self): + expected_header = """+++ +title = "dokuwiki_header_in_subdir" +draft = false +tags = [ + "subdir", + "moar", + "dokuwiki_header_in_subdir" +] +date = "2014-10-10" ++++""" + + actual_header = self.header.create('subdir/moar/dokuwiki_header_in_subdir.txt') + self.assertEqual(expected_header, actual_header) + + def test_dokuwiki_header_example(self): + expected_header = """+++ +title = "dokuwiki_header_example" +draft = false +tags = [ + "dokuwiki_header_example" +] +date = "2014-10-10" ++++""" + + actual_header = self.header.create('dokuwiki_header_example.txt') + self.assertEqual(expected_header, actual_header) \ No newline at end of file