#!/usr/bin/env python3 from pathlib import Path import argparse MAX_TITLE_LENGTH = 40 VERSION = "0.9.0" parser = argparse.ArgumentParser( description="Create index.html, index.gmi, and gophermap files from README file.") parser.add_argument("readme", help="File to work on.", type=Path) parser.add_argument('--version', action='version', version='%(prog)s ' + VERSION) parser.add_argument("--force", "-f", help="Overwrite the output files.", action='store_true') parser.add_argument("--print-gopher", help="Write the Gophermap file to stdout", action='store_true') parser.add_argument("--print-gemini", help="Write the Gemini index file to stdout", action='store_true') parser.add_argument("--print-html", help="Write the HTML index file to stdout", action='store_true') def contains_gopher_illegal(value): for ch in value: if not ch.isprintable() or ch.isspace(): return True return False def gopherFileType(name): if isinstance(name, Path): suffix = name.suffix name = name.stem else: path = Path(name) suffix = path.suffix if not suffix: if path.name == name and '.' not in name: suffix = '.' + name elif path.name and path.name[0] == '.': suffix = path.name if suffix and suffix[0] != '.': suffix = '.' + suffix if not suffix: return '0' elif suffix in ('.txt','.ly','.song','.mma','.sh','.js','.json','.md','.rst','.mdown','.nfo'): return '0' elif suffix in ('.mp3','.flac','.ogg','.wav','.midi','.mid','.kar','.spx'): return 's' elif suffix == '.gif' or suffix.endswith('.gif'): return 'g' elif suffix in ('.png', '.jpg', '.jpeg','.tiff','.tif'): return 'I' elif suffix in ('.docx', '.epub', '.rtf','.doc'): return '9' elif name.lower().startswith('read') or name.lower() == 'file_id.diz': return '0' else: return '9' def main(args): infodict = {} for entry in args.readme.parent.glob('*'): if entry.name.startswith('.'): continue value = name = entry.name if entry.is_symlink(): entry = entry.parent / entry.resolve().name if not entry.exists() or entry.is_symlink(): continue value = entry.name gophertype = None if entry.is_dir(): gophertype = '1' elif entry.is_file(): gophertype = '0' if gophertype is None or contains_gopher_illegal(value): continue infodict[name] = (gophertype, value) gemini = [] html = [] pre_block = [] gophermap = [] body_class = "plain" if body_class: html.append(f'
\n') else: html.append(f'\n') title = None for line in args.readme.read_text().splitlines(): line = line.expandtabs() if line.startswith('.'): pre_block.append(line[1:]) gophermap.append(line) gemini.append(line) continue elif pre_block: pre_text = "\n".join(pre_block) html.append(f'{pre_text}\n') pre_block = [] words = line.strip().split() if not words: html.append('