diff options
author | Starfall <us@starfall.systems> | 2021-10-22 23:13:45 -0500 |
---|---|---|
committer | Starfall <us@starfall.systems> | 2023-12-05 10:00:30 -0600 |
commit | 6476982874fec8a7e0e77b0f0484c7c8c0c56ea9 (patch) | |
tree | 68553e7a203fd3060d10a08582a5d1faf60773ba | |
parent | 5b63c384ef8e100518ff50b9cb023ac07265df35 (diff) |
strike: read and process from config file
-rw-r--r-- | sample/html/empty.html | 13 | ||||
-rw-r--r-- | sample/html/happy.html | 14 | ||||
-rw-r--r-- | sample/template.html | 2 | ||||
-rwxr-xr-x | strike.py | 39 |
4 files changed, 58 insertions, 10 deletions
diff --git a/sample/html/empty.html b/sample/html/empty.html new file mode 100644 index 0000000..da72fca --- /dev/null +++ b/sample/html/empty.html @@ -0,0 +1,13 @@ +<!DOCTYPE html> +<html lang=en-US dir=ltr> +<title>aaaa need a title</title> +<meta charset=UTF-8> +<meta name=robots content="noindex, nofollow"> +<meta name=viewport content="width=device-width, initial-scale=1"> + +<section> + +</section> +<footer> + <p>This page was generated by <a href=https://git.starfall.systems/strike>Strike</a>. +</footer> diff --git a/sample/html/happy.html b/sample/html/happy.html new file mode 100644 index 0000000..734a9b7 --- /dev/null +++ b/sample/html/happy.html @@ -0,0 +1,14 @@ +<!DOCTYPE html> +<html lang=en-US dir=ltr> +<title>aaaa need a title</title> +<meta charset=UTF-8> +<meta name=robots content="noindex, nofollow"> +<meta name=viewport content="width=device-width, initial-scale=1"> + +<section> + Hello, world! + +</section> +<footer> + <p>This page was generated by <a href=https://git.starfall.systems/strike>Strike</a>. +</footer> diff --git a/sample/template.html b/sample/template.html index 3e94683..c8e401a 100644 --- a/sample/template.html +++ b/sample/template.html @@ -6,7 +6,7 @@ <meta name=viewport content="width=device-width, initial-scale=1"> <section> - ${content} + {content} </section> <footer> <p>This page was generated by <a href=https://git.starfall.systems/strike>Strike</a>. diff --git a/strike.py b/strike.py index dfcb2ed..b6d526b 100755 --- a/strike.py +++ b/strike.py @@ -1,32 +1,53 @@ #!/usr/bin/env python3 -from os import listdir, mkdir +from os import listdir, mkdir, path from sys import exit from argparse import ArgumentParser +from configparser import ConfigParser def handle_args(): parser = ArgumentParser() - parser.add_argument('--input', '-i', help='input directory (default "blog")', default='blog') - parser.add_argument('--output', '-o', help='output directory (default "output")', default='output') - parser.add_argument('--template', '-t', help='template to use (default "template")', default='template') + parser.add_argument('config', help='location of strike.ini file (or a directory containing it)') +# does it make any sense to override the config file? probably not for cases involving multiple templates or excludes. but for simple ones i could see it +# parser.add_argument('--input', '-i', help='input directory (overrides config file)', default='') +# parser.add_argument('--output', '-o', help='output directory (overrides config file)', default='') +# parser.add_argument('--template', '-t', help='template to use (overrides config file)', default='') return parser.parse_args() +def read_config(location): + parser = ConfigParser() + if path.isdir(location): + location = location + '/strike.ini' + if not path.exists(location): + raise FileNotFoundError(f'Config file not found at {location}.') + location = parser.read(location) + basedir = path.dirname(path.abspath(location[0])) + return basedir, parser + def get_output(content, template="{content}"): return template.format(content = content) def main(): args = handle_args() + basedir, config = read_config(args.config) + + input_dir = path.join(basedir, config['Input']['directory']) + exclusions = config['Input']['excludes'].splitlines() + default_template = path.join(basedir, config['Templates']['default']) + output_dir = path.join(basedir, config['Output']['directory']) - try: mkdir(args.output) + try: mkdir(output_dir) except FileExistsError: pass - with open(args.template) as t: + with open(default_template) as t: template_contents = t.read() - for file in listdir(args.input): - with open(args.input + '/' + file, 'r') as fin: + 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=template_contents) - with open(args.output + '/' + file, 'w') as fout: + with open(path.join(output_dir, file), 'w') as fout: fout.write(output) if __name__ == '__main__': |