#!/usr/bin/env python3 # # Copyright (C) 2023 Jonathan A. Poritz # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # import argparse import fileinput import os import re import sys import time import warnings if not sys.warnoptions: warnings.simplefilter("ignore") parser = argparse.ArgumentParser(description='Add glossary references in html files prepared by OOsplit.py so the appropriate texts will have active PB glossary terms. Text which consists of a non-letter character followed by a term from the glossary_manifest in any case, followed by a non-letter chacter is considered "appropriate" (only the term itself is activated, not the surrounding non-letter characters).') parser.add_argument("glossary_manifest", help="glossary manifest as produced by OOglossary_down.py, used for terms, post ids, and names of html files with glossary definitions", type=argparse.FileType('r')) parser.add_argument("-m","--manifest", help='manifest as produced by OOsplit.py; used only for names of HTML files to process; default is "manifest"', default="manifest", type=argparse.FileType('r')) parser.add_argument("-l", "--logfile", help='Filename for logfile to which will be appended detailed progress information; default is "add_glossarys.log".', default="add_glossary.log", type=argparse.FileType('a')) parser.add_argument('-v', '--verbose', help="print on console all information also going in to the logfile", action='store_true') parser.add_argument('-a', '--activationless', help="do not activate glossary terms in headers; defaut=False", action='store_true') parser.add_argument('-s', '--start_from', help="skip all lines of the manifest up through the first one whose content title contains the given string", default='') parser.add_argument('-u', '--updating_manifest', help='filename of new manifest which can be used with OOreup.py to activate the glossary terms in the PB book; dafault is "manifest.add_glossary"', default='manifest.add_glossary') parser.add_argument('-b', '--backup_changed_files', help='save a backup copy of the original file, in a file with the same name to which "~" is appended', action='store_true') args = parser.parse_args() verbose = args.verbose start_from = args.start_from args.logfile.write("------------------------------------\n") def log_and_print(s): t=time.strftime('%H:%M:%S')+" "+s args.logfile.write(t+"\n") if verbose: print(t) when_work = "On "+time.strftime('%d/%m/%Y') log_and_print(when_work+", doing ") what_work = ' '.join(sys.argv)+" in directory "+os.getcwd() log_and_print(what_work) files2fix = [] file_mls = {} update_fh = None def readcl(fh): while True: r = fh.readline() if not r or r[0]!="#": return(r) while True: mline = readcl(args.manifest) if not mline: break if mline[:5]=="CSS: ": if start_from and not start_from in mline: continue start_from = '' continue fnnl = readcl(args.manifest) fn = fnnl.strip() if not fn: raise ValueError(f"Malformed manifest file: no filename for content line {mline}") if start_from and not start_from in mline: continue start_from = '' file_mls[fn] = mline+fnnl files2fix.append(fn) if mline[:4]=="FM: " or mline[:4]=="BM: " or mline[:8]=="Chapter[": continue if mline[:6]!="Part: ": raise ValueError(f'Malformed manfiest file: unrecognized line "{mline}"') refs_pat = re.compile("