header generation

This commit is contained in:
wgroeneveld 2017-01-15 12:24:26 +01:00
parent 662a7d3f1a
commit 635e746756
4 changed files with 85 additions and 0 deletions

26
src/hugo_file_config.py Normal file
View File

@ -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)

View File

@ -0,0 +1,6 @@
== some header ==
hello there!
This is //dokuwiki// style stuff right there. \\
Thanks for stopping by.

View File

@ -0,0 +1,6 @@
== some header ==
hello there!
This is //dokuwiki// style stuff right there. \\
Thanks for stopping by.

View File

@ -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)