diff options
-rwxr-xr-x | strike.py | 41 |
1 files changed, 15 insertions, 26 deletions
diff --git a/strike.py b/strike.py index 0679bd1..194c52d 100755 --- a/strike.py +++ b/strike.py @@ -1,51 +1,39 @@ #!/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 + def handle_args(): parser = ArgumentParser() 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() + config = 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 + config.read(location) + return path.dirname(path.abspath(location)), config def handle_file(location): - meta_dict = dict( + meta = 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() - contents = handle_frontmatter(file_contents, meta_dict) - return contents, meta_dict - -def handle_frontmatter(file_contents, meta_dict): + contents = fin.read() delim = '---\n' - if not file_contents.startswith(delim): - return file_contents - parts = file_contents.split(delim, maxsplit=2) - meta_lines = parts[1].splitlines() - meta = dict( - (x.strip(), y.strip()) - for x, y in (kvp.split('=') for kvp in meta_lines) - ) - meta_dict.update(meta) - return parts[2] + if contents.startswith(delim): + parts = contents.split(delim, maxsplit=2) + meta.update(dict((key.strip(), value.strip()) + for key, value in (line.split('=') for line in parts[1].splitlines()) + )) + contents = parts[2] + return contents, meta def apply_template(content, metadata, template="{content}"): return template.format( @@ -77,4 +65,5 @@ def main(): fout.write(output) if __name__ == '__main__': - exit(main()) + import sys + sys.exit(main()) |