From e8e8ade61a4f9a77a367b84bab29c6345cbccb2a Mon Sep 17 00:00:00 2001 From: kthoden Date: Thu, 21 Nov 2019 15:51:25 +0100 Subject: [PATCH] Wrapper script for tei2html --- xxe/eoa/xsl/tei2html.py | 139 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100755 xxe/eoa/xsl/tei2html.py diff --git a/xxe/eoa/xsl/tei2html.py b/xxe/eoa/xsl/tei2html.py new file mode 100755 index 0000000..8f53445 --- /dev/null +++ b/xxe/eoa/xsl/tei2html.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8; mode: python -*- + +"A converter from TEI to HTML." + +__version__ = "1.0" +__date__ = "20190408" +__author__ = "sgfroerer@mpiwg-berlin.mpg.de" + +from utils.load_config import load_config, check_executable, exec_command, ToFile +import utils.libeoaconvert as libeoaconvert + +import logging +import argparse +from pathlib import Path + +# things to be done +# assign ids top to bottom for the following elements: +# div1 div2 div3 note item table EOAfigure EOAequation formula theorem + +BASE_DIR = Path( __file__ ).resolve().parent +SCRIPT_NAME = Path( __file__).stem + +def check_executables(): + check_executable( "saxon" ) + +def main( + input_file, + root_dir, + xslt_file, + params, + output_file = None +): + """Main function""" + + output_options = "" + if output_file is not None: + output_options = f"-o:{output_file}" + cmd = \ + "saxon -t {output_options} -s:{input_file} -xsl:{xslt_file} domain={root_dir}".format( + xslt_file = (BASE_DIR / "tei2html" / xslt_file), + input_file = input_file, + root_dir = root_dir, + output_options = output_options + ) + for param in params: + cmd += (" " + param) + exec_command( + cmd + ) + +# def main ends here + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + parser.add_argument( + "-c", "--config", + default = BASE_DIR / "config" / "eoaconvert.cfg", + help="Name of config file" + ) + parser.add_argument( + "-l", "--log-file", + # default = Path("logs", SCRIPT_NAME).with_suffix(".log"), + help="logfile" + ) + parser.add_argument( + "--log-level", + default = "INFO", + help="log level: choose between DEBUG, INFO, WARNING, ERROR, CRITICAL" + ) + parser.add_argument( + "--root-dir", + default = Path.cwd() / "output", + help="internal html links on the page will use this location as a prefix" + ) + parser.add_argument( + "-p", "--param", + action = 'append', + default = [], + help="internal html links on the page will use this location as a prefix" + ) + parser.add_argument( + "-x", "--xsl", + default = "tei2html.xsl", + help="name of the xsl file in '{dir}/'".format( + dir = BASE_DIR / "tei2html" + ) + ) + parser.add_argument( + "-f", "--filename", + required = True, + help="Name of main EOA-TEI file" + ) + parser.add_argument( + "-o", "--output-file", + help="make saxon redirect the templates output here" + ) + + ''' + TODO: support output directory + parser.add_argument( + "-o", "--output-dir", + default = ".", + help="where to dump all output files" + ) + ''' + + args = parser.parse_args() + + CONFIG_FILE = args.config + + print("The configfile is '%s'." % CONFIG_FILE) + log_file = args.log_file + if log_file is None: + log_file = (Path("logs") / args.xsl).with_suffix(".log") + print("The logfile is '%s'." % log_file) + + CONFIG = load_config( + CONFIG_FILE, + args.log_level, + log_file, + ) + + check_executables() + + print( "params: " + str(args.param) ) + + # run main: + main( + input_file = args.filename, + root_dir = args.root_dir, + xslt_file = args.xsl, + output_file = args.output_file, + params = args.param, + ) + +# finis