From 4e6172e3a4f03e7a70eea90243ba6771f57dc6a4 Mon Sep 17 00:00:00 2001 From: "Fritz J. Pichardo Marcano" Date: Wed, 4 Mar 2026 18:34:15 +0000 Subject: [PATCH 1/5] feat: add support for custom head HTML injection Implement a mechanism to inject custom HTML (e.g., analytics scripts) into the section via an optional extra_head.html file. The file is cached after first read to avoid repeated I/O operations. --- .gitignore | 3 +++ ckanext/digitizationknowledge/helpers.py | 25 +++++++++++++++++++ .../digitizationknowledge/templates/base.html | 8 ++++++ 3 files changed, 36 insertions(+) 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..ed857a0 100644 --- a/ckanext/digitizationknowledge/helpers.py +++ b/ckanext/digitizationknowledge/helpers.py @@ -3,6 +3,11 @@ import ckan.authz as authz from sqlalchemy import and_, not_, exists from typing import Any +import os +import logging +from functools import lru_cache + +log = logging.getLogger(__name__) def is_group_private(group): @@ -156,10 +161,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 content + except FileNotFoundError: + log.debug('No extra head HTML file at %s', file_path) + return '' + 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..550c548 100644 --- a/ckanext/digitizationknowledge/templates/base.html +++ b/ckanext/digitizationknowledge/templates/base.html @@ -4,4 +4,12 @@ {{ super() }} {% asset 'digitizationknowledge_theme/main-css' %} +{% endblock %} + +{% block head_extras %} +{{ super() }} +{% set extra_head_html = h.get_extra_head_html() %} +{% if extra_head_html %} +{{ extra_head_html | safe }} +{% endif %} {% endblock %} \ No newline at end of file From 0aa5a0e8e62ab74741045bd547881875d6335468 Mon Sep 17 00:00:00 2001 From: "Fritz J. Pichardo Marcano" Date: Wed, 4 Mar 2026 18:54:10 +0000 Subject: [PATCH 2/5] fix: simplify extra head HTML injection by returning Markup directly Return Markup objects from get_extra_head_html() helper to eliminate the need for the `| safe` filter and conditional check in the template. This streamlines the template code while maintaining the same functionality. --- ckanext/digitizationknowledge/helpers.py | 5 +++-- ckanext/digitizationknowledge/templates/base.html | 5 +---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/ckanext/digitizationknowledge/helpers.py b/ckanext/digitizationknowledge/helpers.py index ed857a0..e8ef0b6 100644 --- a/ckanext/digitizationknowledge/helpers.py +++ b/ckanext/digitizationknowledge/helpers.py @@ -6,6 +6,7 @@ import os import logging from functools import lru_cache +from markupsafe import Markup log = logging.getLogger(__name__) @@ -175,10 +176,10 @@ def get_extra_head_html(): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() log.info('Loaded extra head HTML from %s', file_path) - return content + return Markup(content) except FileNotFoundError: log.debug('No extra head HTML file at %s', file_path) - return '' + return Markup('') def get_helpers(): return { diff --git a/ckanext/digitizationknowledge/templates/base.html b/ckanext/digitizationknowledge/templates/base.html index 550c548..076b90f 100644 --- a/ckanext/digitizationknowledge/templates/base.html +++ b/ckanext/digitizationknowledge/templates/base.html @@ -8,8 +8,5 @@ {% block head_extras %} {{ super() }} -{% set extra_head_html = h.get_extra_head_html() %} -{% if extra_head_html %} -{{ extra_head_html | safe }} -{% endif %} +{{ h.get_extra_head_html() }} {% endblock %} \ No newline at end of file From cdabca4a2f7a01052030a784aa742c3ca259da9d Mon Sep 17 00:00:00 2001 From: "Fritz J. Pichardo Marcano" Date: Wed, 4 Mar 2026 19:10:01 +0000 Subject: [PATCH 3/5] revert: restore safe filter for extra head HTML injection --- ckanext/digitizationknowledge/templates/base.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/digitizationknowledge/templates/base.html b/ckanext/digitizationknowledge/templates/base.html index 076b90f..ba3c112 100644 --- a/ckanext/digitizationknowledge/templates/base.html +++ b/ckanext/digitizationknowledge/templates/base.html @@ -8,5 +8,5 @@ {% block head_extras %} {{ super() }} -{{ h.get_extra_head_html() }} +{{ h.get_extra_head_html() | safe }} {% endblock %} \ No newline at end of file From 8ba316dc4046272fb1d55f858eda4677b6cb5b94 Mon Sep 17 00:00:00 2001 From: "Fritz J. Pichardo Marcano" Date: Wed, 4 Mar 2026 19:18:38 +0000 Subject: [PATCH 4/5] refactor: replace helper function with direct template include for extra head HTML Replace h.get_extra_head_html() helper call with direct template include to simplify the implementation while maintaining the same functionality for injecting custom head HTML. --- ckanext/digitizationknowledge/templates/base.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/digitizationknowledge/templates/base.html b/ckanext/digitizationknowledge/templates/base.html index ba3c112..f5bcb5f 100644 --- a/ckanext/digitizationknowledge/templates/base.html +++ b/ckanext/digitizationknowledge/templates/base.html @@ -8,5 +8,5 @@ {% block head_extras %} {{ super() }} -{{ h.get_extra_head_html() | safe }} +{% include "extra_head.html" ignore missing %} {% endblock %} \ No newline at end of file From 4714d5d44abfca68887cb82940dcf0455e6aa790 Mon Sep 17 00:00:00 2001 From: "Fritz J. Pichardo Marcano" Date: Wed, 4 Mar 2026 19:29:45 +0000 Subject: [PATCH 5/5] refactor: replace direct template include with helper function for extra head HTML Replace {% include "extra_head.html" ignore missing %} with h.get_extra_head_html() helper call to centralize the logic for injecting custom head HTML. Also normalize indentation in styles block. --- ckanext/digitizationknowledge/templates/base.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ckanext/digitizationknowledge/templates/base.html b/ckanext/digitizationknowledge/templates/base.html index f5bcb5f..9ec2fbc 100644 --- a/ckanext/digitizationknowledge/templates/base.html +++ b/ckanext/digitizationknowledge/templates/base.html @@ -1,12 +1,12 @@ {% ckan_extends %} {% block styles %} - {{ super() }} - {% asset 'digitizationknowledge_theme/main-css' %} - +{{ super() }} +{% asset 'digitizationknowledge_theme/main-css' %} + {% endblock %} {% block head_extras %} {{ super() }} -{% include "extra_head.html" ignore missing %} +{{ h.get_extra_head_html() }} {% endblock %} \ No newline at end of file