diff --git a/.gitignore b/.gitignore index 8570dc5..19b9dac 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,6 @@ coverage.xml # Sphinx documentation docs/_build/ + +# Deployment-specific extra head HTML (e.g. analytics scripts) +extra_head.html \ No newline at end of file diff --git a/ckanext/digitizationknowledge/helpers.py b/ckanext/digitizationknowledge/helpers.py index 19edf06..e8ef0b6 100644 --- a/ckanext/digitizationknowledge/helpers.py +++ b/ckanext/digitizationknowledge/helpers.py @@ -3,6 +3,12 @@ import ckan.authz as authz from sqlalchemy import and_, not_, exists from typing import Any +import os +import logging +from functools import lru_cache +from markupsafe import Markup + +log = logging.getLogger(__name__) def is_group_private(group): @@ -156,10 +162,30 @@ def get_custom_featured_organizations(count: int = 1): return [] +@lru_cache(maxsize=1) +def get_extra_head_html(): + """ + Read extra HTML to inject in from assets/extra_head.html. + Content is cached after first read to avoid repeated file I/O. + Returns empty string if the file doesn't exist. + """ + file_path = os.path.join( + os.path.dirname(__file__), 'assets', 'extra_head.html' + ) + try: + with open(file_path, 'r', encoding='utf-8') as f: + content = f.read() + log.info('Loaded extra head HTML from %s', file_path) + return Markup(content) + except FileNotFoundError: + log.debug('No extra head HTML file at %s', file_path) + return Markup('') + def get_helpers(): return { "get_custom_featured_groups": get_custom_featured_groups, "get_custom_featured_organizations": get_custom_featured_organizations, "is_group_private": is_group_private, "user_can_view_group": user_can_view_group, + "get_extra_head_html": get_extra_head_html, } diff --git a/ckanext/digitizationknowledge/templates/base.html b/ckanext/digitizationknowledge/templates/base.html index cd81518..9ec2fbc 100644 --- a/ckanext/digitizationknowledge/templates/base.html +++ b/ckanext/digitizationknowledge/templates/base.html @@ -1,7 +1,12 @@ {% ckan_extends %} {% block styles %} - {{ super() }} - {% asset 'digitizationknowledge_theme/main-css' %} - +{{ super() }} +{% asset 'digitizationknowledge_theme/main-css' %} + +{% endblock %} + +{% block head_extras %} +{{ super() }} +{{ h.get_extra_head_html() }} {% endblock %} \ No newline at end of file