Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 223 lines (192 sloc) 6.32 KB
#!/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
from utils.run_xslt import run_xslt
import utils.libeoaconvert as libeoaconvert
import logging
import argparse
from pathlib import Path
from os import environ
from shutil import rmtree, copytree, ignore_patterns
from lxml import etree
# 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
DEFAULT_INPUT_DIR = \
Path(environ['INPUT_DIR'] if 'INPUT_DIR' in environ else './input')
DEFAULT_OUTPUT_DIR = \
Path(environ['OUTPUT_DIR'] if 'OUTPUT_DIR' in environ else './output')
DEFAULT_DEPENDENCIES_DIR = \
Path(environ['DEPENDENCIES_DIR'] if 'DEPENDENCIES_DIR' in environ else './dependencies')
EOA_SCRIPTS_DIR = \
Path(environ['EOA_SCRIPTS_DIR'])
def check_executables():
check_executable( "saxon" )
def mkdir(
dst,
**opts
):
logging.debug( f"creating '{dst}'" )
dst.mkdir(
parents = True,
exist_ok = True
)
def copy_dir(
src,
dst,
**opts
):
logging.debug( "'{}' -> '{}'".format( src, dst ) )
if Path(dst).exists():
rmtree(
dst
)
copytree(
src=src,
dst=dst,
**opts
)
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(
"--log-level",
default = "INFO",
help="log level: choose between DEBUG, INFO, WARNING, ERROR, CRITICAL"
)
parser.add_argument(
"-p", "--param",
action = 'append',
default = [],
help="xslt params"
)
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",
default = Path("with_bibl.xml"),
type = Path,
help = "xml file inside PUBLICATION_DIR, or absolute path. Patterns like '*.xml' are also acceptable"
)
parser.add_argument(
"-o", "--output-dir",
help = f"output directory. default: {DEFAULT_OUTPUT_DIR}/PUBLICATION_NAME/html",
type = Path,
)
parser.add_argument(
"-!", "--overwrite",
action = "store_true",
default = False,
help="overwrite OUTPUT_DIR, if existing"
)
parser.add_argument(
"--output-file",
type = Path,
help="in case of calling a classical stylesheet with output, redirect here"
)
parser.add_argument(
"-w", "--webdesign",
default = 'webdesign_platform',
type = Path,
help="location to the webdesign (relative to OUTPUT_DIR, or absolute)"
)
parser.add_argument(
"-s", "--static",
default = 'publication_static',
type = Path,
help="static files of publication (relative to OUTPUT_DIR, or absolute)"
)
parser.add_argument(
"-n", "--platform-name",
default = 'dummy-platform',
type = str,
help="internal links must be absolute, to this address, or relative, if empty"
)
parser.add_argument(
"-u", "--platform-uri",
default = 'http://dummy-platform.com',
type = str,
help="internal links must be absolute, to this address, or relative, if empty"
)
parser.add_argument(
"PUBLICATION_DIR",
help = "directory containing the publication (including resources like pictures, etc.)",
type = Path,
)
args = parser.parse_args()
publ_dir = args.PUBLICATION_DIR
publ_file = args.filename
tei_filename = \
publ_file if publ_file . is_absolute() else list(publ_dir . glob (str(publ_file)))[0]
output_dir = \
args.output_dir if args.output_dir is not None else (DEFAULT_OUTPUT_DIR / publ_dir.resolve().stem) / "html"
if not tei_filename.is_file():
raise( Exception(
f"not a valid input file: {tei_filename}"
) )
if output_dir.exists():
if args.overwrite:
rmtree( output_dir )
else:
raise( Exception( f"output directory already existing: '{output_dir}'!" ) )
CONFIG_FILE = args.config
log_dir = output_dir / "log"
log_file = (log_dir / SCRIPT_NAME) . with_suffix( ".log" )
print( f"log file: {log_file}" )
CONFIG = load_config(
CONFIG_FILE,
args.log_level,
log_file,
)
check_executables()
mkdir( output_dir )
logging.info( f"tei_file: {tei_filename}, publ_dir: {publ_dir}" )
## copy webdesign:
copy_dir(
DEFAULT_DEPENDENCIES_DIR / "webdesign_platform/dist",
output_dir / "webdesign_platform",
)
## copy publication static files:
static_dest_dir = output_dir / "publication_static"
mkdir( static_dest_dir )
copy_dir(
publ_dir,
static_dest_dir,
ignore = ignore_patterns( "*.xml" )
)
logging.debug( "xslt params: " + str(args.param) )
run_xslt(
input_file = tei_filename,
xslt_file = BASE_DIR / "stylesheets/tei2html" / args.xsl,
params =
[
f"output_dir={output_dir}",
f"webdesign_url={args.webdesign}",
f"publ_static_url={args.static}",
f"platform_name={args.platform_name}",
f"platform_uri={args.platform_uri}",
] +
args.param,
output_file = args.output_file,
exec_command_args = {
'output_to' : ToFile( log_dir / SCRIPT_NAME / "saxon.log" )
},
)
# finis