#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import string import sys import pathlib def _sanitize_line(l: str) -> str: if '\n' in l and l.startswith('"') and l.endswith('"'): return l[1:-1].replace('""', '"') return l def _get_path(basepath: pathlib.Path, deckname: str) -> pathlib.Path: for p in basepath.iterdir(): if p.name == deckname: return p if p.name.startswith(f'{deckname}.'): return p p = basepath / deckname p.mkdir() return p def process_line( *, columns: list[str], columns_indices: list[int], deck_index: int, target_dir: pathlib.Path, ) -> None: columns = [_sanitize_line(l) for l in columns] deckpath = _get_path(target_dir, columns[deck_index]) columns = [columns[i] for i in columns_indices] slug = ''.join(c for c in columns[0].lower() if c in string.ascii_lowercase + string.digits + ' ') slug = slug.replace(' ', '-') base_filename = deckpath / slug for i, c in enumerate(columns): with (base_filename.parent / (base_filename.name + f'.{i}.html')).open('w') as fp: fp.write(c) if not c.endswith('\n'): fp.write('\n') def main( *, target_column_count: int = 6, columns_indices: list[int] = [3, 4], deck_index: int = 2, sep: str = '\t', ): target_dir = pathlib.Path(next(iter(sys.argv[1:] or ['.']))) buff = '' process_header = True for l in sys.stdin.readlines(): if process_header and l.startswith('#'): continue else: process_header = False buff += l split_ = buff.split(sep) if len(split_) == target_column_count: process_line( columns=split_, columns_indices=columns_indices, deck_index=deck_index, target_dir=target_dir, ) buff = '' if __name__ == '__main__': main()