diff options
-rw-r--r-- | README | 4 | ||||
-rw-r--r-- | sample/html/empty.html | 3 | ||||
-rw-r--r-- | sample/html/happy.html | 3 | ||||
-rw-r--r-- | sample/template.html | 3 | ||||
-rwxr-xr-x | strike.py | 22 |
5 files changed, 25 insertions, 10 deletions
diff --git a/README b/README index 93fac3e..80ad5c4 100644 --- a/README +++ b/README @@ -41,8 +41,8 @@ TBD. Right now, templates are just HTML in a Python f-string. You have access to the following variables: {content} : Complete file contents. -{meta['title']} : Filename, with extension dropped. -{meta['date']} : File modification date. +{meta[title]} : Filename, with extension dropped. +{meta[date]} : File modification date, in HTTP header format (e.g. "Sat, 23 Oct 2021 18:28:37 GMT") Front matter ------------ diff --git a/sample/html/empty.html b/sample/html/empty.html index da72fca..b8951a7 100644 --- a/sample/html/empty.html +++ b/sample/html/empty.html @@ -1,9 +1,10 @@ <!DOCTYPE html> <html lang=en-US dir=ltr> -<title>aaaa need a title</title> +<title>empty</title> <meta charset=UTF-8> <meta name=robots content="noindex, nofollow"> <meta name=viewport content="width=device-width, initial-scale=1"> +<meta http-equiv=last-modified content="Sat, 23 Oct 2021 02:55:53 GMT"> <section> diff --git a/sample/html/happy.html b/sample/html/happy.html index 734a9b7..3c239f1 100644 --- a/sample/html/happy.html +++ b/sample/html/happy.html @@ -1,9 +1,10 @@ <!DOCTYPE html> <html lang=en-US dir=ltr> -<title>aaaa need a title</title> +<title>happy</title> <meta charset=UTF-8> <meta name=robots content="noindex, nofollow"> <meta name=viewport content="width=device-width, initial-scale=1"> +<meta http-equiv=last-modified content="Sat, 23 Oct 2021 02:57:17 GMT"> <section> Hello, world! diff --git a/sample/template.html b/sample/template.html index c8e401a..2552906 100644 --- a/sample/template.html +++ b/sample/template.html @@ -1,9 +1,10 @@ <!DOCTYPE html> <html lang=en-US dir=ltr> -<title>aaaa need a title</title> +<title>{meta[title]}</title> <meta charset=UTF-8> <meta name=robots content="noindex, nofollow"> <meta name=viewport content="width=device-width, initial-scale=1"> +<meta http-equiv=last-modified content="{meta[date]}"> <section> {content} diff --git a/strike.py b/strike.py index f3ae173..b417bf1 100755 --- a/strike.py +++ b/strike.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from os import listdir, mkdir, path from sys import exit +from time import gmtime, strftime from argparse import ArgumentParser from configparser import ConfigParser @@ -23,8 +24,20 @@ def read_config(location): basedir = path.dirname(path.abspath(location[0])) return basedir, parser -def get_output(content, template="{content}"): - return template.format(content = content) +def handle_file(location): + metadata = dict( + title = '.'.join(path.basename(location).split('.')[:-1]), + date = strftime('%a, %d %b %Y %H:%M:%S GMT', gmtime(path.getmtime(location))) + ) + with open(location, 'r') as fin: + file_contents = fin.read() + return file_contents, metadata + +def apply_template(content, metadata, template="{content}"): + return template.format( + content = content, + meta = metadata + ) def main(): args = handle_args() @@ -44,9 +57,8 @@ def main(): for file in listdir(input_dir): if file in exclusions: continue - with open(path.join(input_dir, file), 'r') as fin: - file_contents = fin.read() - output = get_output(file_contents, template) + file_contents, metadata = handle_file(path.join(input_dir, file)) + output = apply_template(file_contents, metadata, template) with open(path.join(output_dir, file), 'w') as fout: fout.write(output) |