From beb63e70876e87bfdcc72f8c157516b77cf07876 Mon Sep 17 00:00:00 2001 From: Taka Date: Fri, 28 May 2021 21:43:47 +1000 Subject: [PATCH] Changed how the chapter CSS is set up. --- asl_rulebook2/webapp/asop.py | 2 +- asl_rulebook2/webapp/content.py | 24 ++++++++++++++++--- .../chapters.css => data/ASL Rulebook.css} | 1 - asl_rulebook2/webapp/rule_info.py | 12 +++++----- asl_rulebook2/webapp/templates/index.html | 2 +- asl_rulebook2/webapp/utils.py | 15 ++++++++---- 6 files changed, 39 insertions(+), 17 deletions(-) rename asl_rulebook2/webapp/{static/css/chapters.css => data/ASL Rulebook.css} (92%) diff --git a/asl_rulebook2/webapp/asop.py b/asl_rulebook2/webapp/asop.py index b2bc607..9c274db 100644 --- a/asl_rulebook2/webapp/asop.py +++ b/asl_rulebook2/webapp/asop.py @@ -38,7 +38,7 @@ def init_asop( startup_msgs, logger ): # load the ASOP index fname = os.path.join( _asop_dir, "index.json" ) - _asop = load_data_file( fname, "ASOP index", False, logger, startup_msgs.error ) + _asop = load_data_file( fname, "ASOP index", "json", logger, startup_msgs.error ) if not _asop: return None, None, None diff --git a/asl_rulebook2/webapp/content.py b/asl_rulebook2/webapp/content.py index e4c0161..74053ef 100644 --- a/asl_rulebook2/webapp/content.py +++ b/asl_rulebook2/webapp/content.py @@ -1,10 +1,11 @@ """ Manage the content documents. """ import os +import io import re from collections import defaultdict -from flask import jsonify, send_file, url_for, abort +from flask import Response, jsonify, send_file, url_for, abort from asl_rulebook2.webapp import app from asl_rulebook2.webapp.utils import load_data_file, slugify @@ -72,6 +73,7 @@ def load_content_sets( startup_msgs, logger ): logger.warn( "Didn't find targets file: %s", fname ) load_file( fname_stem+".chapters", content_doc, "chapters", startup_msgs.warning ) load_file( fname_stem+".vo-notes", content_doc, "vo-notes", startup_msgs.warning ) + load_file( fname_stem+".css", content_doc, "css", startup_msgs.warning, ftype="text" ) if load_file( fname_stem+".footnotes", content_doc, "footnotes", startup_msgs.warning ): # update the footnote index # NOTE: The front-end doesn't care about what chapter a footnote belongs to, @@ -110,12 +112,12 @@ def load_content_sets( startup_msgs, logger ): chapter[ rtype ] = url_for( "get_chapter_resource", chapter_id=chapter_id, rtype=rtype ) return content_doc - def load_file( rel_fname, save_loc, key, on_error, binary=False ): + def load_file( rel_fname, save_loc, key, on_error, ftype="json" ): fname = os.path.join( data_dir, rel_fname ) if not os.path.isfile( fname ): return False # load the specified file - data = load_data_file( fname, key, binary, logger, on_error ) + data = load_data_file( fname, key, ftype, logger, on_error ) if data is None: return False # save the file data @@ -343,6 +345,22 @@ def get_footnotes(): # --------------------------------------------------------------------- +@app.route( "/content/css" ) +def get_content_css(): + """Return the custom CSS for each content doc.""" + buf = io.StringIO() + fname = os.path.join( os.path.dirname(__file__), "data/ASL Rulebook.css" ) + with open( fname, "r", encoding="utf-8" ) as fp: + print( "/* ASL Rulebook */", file=buf ) + print( fp.read().strip(), file=buf ) + for cset in _content_sets.values(): + for cdoc in cset["content_docs"].values(): + if "css" in cdoc: + print( file=buf ) + print( "/* {} */".format( cdoc["title"] ), file=buf ) + print( cdoc["css"].strip(), file=buf ) + return Response( buf.getvalue(), mimetype="text/css" ) + @app.route( "/chapter//" ) def get_chapter_resource( chapter_id, rtype ): """Return a chapter resource.""" diff --git a/asl_rulebook2/webapp/static/css/chapters.css b/asl_rulebook2/webapp/data/ASL Rulebook.css similarity index 92% rename from asl_rulebook2/webapp/static/css/chapters.css rename to asl_rulebook2/webapp/data/ASL Rulebook.css index e629a7f..b2f1c05 100644 --- a/asl_rulebook2/webapp/static/css/chapters.css +++ b/asl_rulebook2/webapp/data/ASL Rulebook.css @@ -12,4 +12,3 @@ .chapter-o { border: 1px solid #e20000 !important ; } .chapter-p { border: 1px solid #696a65 !important ; } .chapter-t { border: 1px solid #eea63b !important ; } -.chapter-kgs { border: 1px solid #bed2e1 !important ; } diff --git a/asl_rulebook2/webapp/rule_info.py b/asl_rulebook2/webapp/rule_info.py index f903541..3e73c3e 100644 --- a/asl_rulebook2/webapp/rule_info.py +++ b/asl_rulebook2/webapp/rule_info.py @@ -38,7 +38,7 @@ def init_qa( startup_msgs, logger ): def load_qa( fname ): """Load the Q+A entries from a data file.""" logger.info( "Loading Q+A: %s", fname ) - qa_entries = load_data_file( fname, "Q+A", False, logger, startup_msgs.warning ) + qa_entries = load_data_file( fname, "Q+A", "json", logger, startup_msgs.warning ) if qa_entries is None: return for key, entries in qa_entries.items(): @@ -69,7 +69,7 @@ def init_qa( startup_msgs, logger ): fname = os.path.join( base_dir, "fixups.json" ) if os.path.isfile( fname ): logger.info( "Loading Q+A fixups: %s", fname ) - fixups = load_data_file( fname, "fixups", False, logger, startup_msgs.warning ) + fixups = load_data_file( fname, "fixups", "json", logger, startup_msgs.warning ) if fixups: for qa_entries in qa.values(): for qa_entry in qa_entries: @@ -84,7 +84,7 @@ def init_qa( startup_msgs, logger ): fname = os.path.join( base_dir, "sources.json" ) if os.path.isfile( fname ): logger.info( "Loading Q+A sources: %s", fname ) - sources = load_data_file( fname, "sources", False, logger, startup_msgs.warning ) + sources = load_data_file( fname, "sources", "json", logger, startup_msgs.warning ) if sources: logger.info( "- Loaded %s.", plural(len(sources),"source","sources") ) @@ -162,7 +162,7 @@ def init_errata( startup_msgs, logger ): fname = os.path.join( base_dir, "fixups.json" ) if os.path.isfile( fname ): logger.info( "Loading errata fixups: %s", fname ) - fixups = load_data_file( fname, "fixups", False, logger, startup_msgs.warning ) + fixups = load_data_file( fname, "fixups", "json", logger, startup_msgs.warning ) if fixups: for ruleid in _errata: for anno in _errata[ruleid]: @@ -173,7 +173,7 @@ def init_errata( startup_msgs, logger ): fname = os.path.join( base_dir, "sources.json" ) if os.path.isfile( fname ): logger.info( "Loading errata sources: %s", fname ) - sources = load_data_file( fname, "sources", False, logger, startup_msgs.warning ) + sources = load_data_file( fname, "sources", "json", logger, startup_msgs.warning ) if sources: logger.info( "- Loaded %s.", plural(len(sources),"source","sources") ) @@ -190,7 +190,7 @@ def init_errata( startup_msgs, logger ): def _load_anno( fname, atype, save_loc, logger, startup_msgs ): """Load annotations from a data file.""" logger.info( "Loading %s: %s", atype, fname ) - anno_entries = load_data_file( fname, atype, False, logger, startup_msgs.warning ) + anno_entries = load_data_file( fname, atype, "json", logger, startup_msgs.warning ) if not anno_entries: return for anno in anno_entries: diff --git a/asl_rulebook2/webapp/templates/index.html b/asl_rulebook2/webapp/templates/index.html index ba267af..fefa218 100644 --- a/asl_rulebook2/webapp/templates/index.html +++ b/asl_rulebook2/webapp/templates/index.html @@ -21,7 +21,7 @@ - + {%if ASOP_CSS_URL%} {%endif%} diff --git a/asl_rulebook2/webapp/utils.py b/asl_rulebook2/webapp/utils.py index cd24792..838e959 100644 --- a/asl_rulebook2/webapp/utils.py +++ b/asl_rulebook2/webapp/utils.py @@ -22,17 +22,22 @@ def make_config_path( path ): """Generate a path in the config directory.""" return os.path.join( CONFIG_DIR, path ) -def load_data_file( fname, ftype, binary, logger, on_error ): +def load_data_file( fname, caption, ftype, logger, on_error ): """Load a data file.""" try: # load the file - logger.debug("- Loading %s: %s", ftype, fname ) - if binary: - with open( fname, mode="rb" ) as fp: + logger.debug("- Loading %s: %s", caption, fname ) + if ftype == "text": + with open( fname, "r", encoding="utf-8" ) as fp: data = fp.read() - else: + elif ftype == "json": with open( fname, "r", encoding="utf-8" ) as fp: data = json.load( fp ) + elif ftype == "binary": + with open( fname, mode="rb" ) as fp: + data = fp.read() + else: + raise RuntimeError( "Unknown data file type: {}".format( ftype ) ) except Exception as ex: #pylint: disable=broad-except msg = "Couldn't load \"{}\".".format( os.path.basename(fname) ) on_error( msg, str(ex) )