Skip to content

Commit

Permalink
global: add meta generator tag
Browse files Browse the repository at this point in the history
- allows defining the value of the <meta generator> header tag by a
  string or a callable. Can be disabled setting it to None.
- see inveniosoftware/invenio-app-rdm#2689
  • Loading branch information
ntarocco committed Jun 21, 2024
1 parent 9dd290d commit 07b3d39
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 36 deletions.
10 changes: 8 additions & 2 deletions invenio_theme/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@
THEME_ERROR_TEMPLATE = "invenio_theme/page_error.html"
"""Base template for error pages."""

THEME_GENERATOR = "Invenio"
"""Generator meta tag to identify the software that generated the page.
Accepts a string or a func returning a string. Set it to `None` to disable it.
"""

THEME_GOOGLE_SITE_VERIFICATION = []
"""List of Google Site Verification tokens to be used.
This adds the Google Site Verfication into the meta tags of all pages.
This adds the Google Site Verification into the meta tags of all pages.
"""

THEME_LOGO = "images/invenio-white.svg"
Expand All @@ -101,7 +107,7 @@
"""Enable or disable basic frontpage view."""

THEME_FRONTPAGE_TITLE = _("Invenio")
"""The title shown on the fronpage."""
"""The title shown on the frontpage."""

THEME_FRONTPAGE_TEMPLATE = "invenio_theme/frontpage.html"
"""Template for front page."""
Expand Down
8 changes: 8 additions & 0 deletions invenio_theme/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""Invenio standard theme."""

from flask_menu import Menu
from invenio_base.utils import load_or_import_from_config

from . import config
from .icons import ThemeIcons
Expand Down Expand Up @@ -64,6 +65,13 @@ def init_config(self, app):

app.config.setdefault("ADMIN_BASE_TEMPLATE", config.ADMIN_BASE_TEMPLATE)

# inject the Meta-Generator string as a func in Jinja
def _generator_func_or_str():
value = app.config.get("THEME_GENERATOR")
return value() if callable(value) else value

app.jinja_env.globals["get_meta_generator"] = _generator_func_or_str

@property
def icons(self):
"""Return icons."""
Expand Down
4 changes: 4 additions & 0 deletions invenio_theme/templates/invenio_theme/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<meta name="google-site-verification" content="{{google_id}}"/>
{%- endfor %}
{%- endif %}
{% set meta_generator = get_meta_generator() %}
{% if meta_generator %}
<meta name="generator" content="{{ meta_generator }}"/>
{%- endif %}
{%- endblock head_meta %}
{%- block head_title %}
{%- set title = title or _(config.THEME_SITENAME) or _('Invenio') %}
Expand Down
30 changes: 18 additions & 12 deletions invenio_theme/templates/invenio_theme/page_admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@
<head>
{%- block head %}
{%- block head_meta %}
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{%- if description %}
<meta name="description" content="{{ description }}"/>{% endif %}
{%- if keywords %}
<meta name="keywords" content="{{ keywords }}"/>{% endif %}
{%- if config.get('THEME_GOOGLE_SITE_VERIFICATION', None) %}
{%- for google_id in config.THEME_GOOGLE_SITE_VERIFICATION %}
<meta name="google-site-verification" content="{{ google_id }}"/>
{%- endfor %}
{%- endif %}
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{%- if description %}
<meta name="description" content="{{ description }}"/>
{% endif %}
{%- if keywords %}
<meta name="keywords" content="{{ keywords }}"/>
{% endif %}
{%- if config.get('THEME_GOOGLE_SITE_VERIFICATION', None) %}
{%- for google_id in config.THEME_GOOGLE_SITE_VERIFICATION %}
<meta name="google-site-verification" content="{{ google_id }}"/>
{%- endfor %}
{%- endif %}
{% set meta_generator = get_meta_generator() %}
{% if meta_generator %}
<meta name="generator" content="{{ meta_generator }}"/>
{%- endif %}
{%- endblock head_meta %}
{%- block head_title %}
{%- set title = title or _(config.THEME_SITENAME) or _('Invenio') %}
Expand Down
24 changes: 14 additions & 10 deletions invenio_theme/templates/semantic-ui/invenio_theme/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
{%- block head %}

{%- block head_meta %}
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{%- if description %}<meta name="description" content="{{ description }}" />{% endif %}
{%- if keywords %}<meta name="keywords" content="{{ keywords }}" />{% endif %}
{%- if config.get('THEME_GOOGLE_SITE_VERIFICATION', None) %}
{%- for google_id in config.THEME_GOOGLE_SITE_VERIFICATION %}
<meta name="google-site-verification" content="{{google_id}}"/>
{%- endfor %}
{%- endif %}
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{%- if description %}<meta name="description" content="{{ description }}" />{% endif %}
{%- if keywords %}<meta name="keywords" content="{{ keywords }}" />{% endif %}
{%- if config.get('THEME_GOOGLE_SITE_VERIFICATION', None) %}
{%- for google_id in config.THEME_GOOGLE_SITE_VERIFICATION %}
<meta name="google-site-verification" content="{{google_id}}"/>
{%- endfor %}
{%- endif %}
{% set meta_generator = get_meta_generator() %}
{% if meta_generator %}
<meta name="generator" content="{{ meta_generator }}"/>
{%- endif %}
{%- endblock head_meta %}

{%- block head_title %}
Expand Down
30 changes: 18 additions & 12 deletions invenio_theme/templates/semantic-ui/invenio_theme/page_admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@
<head>
{%- block head %}
{%- block head_meta %}
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{%- if description %}
<meta name="description" content="{{ description }}"/>{% endif %}
{%- if keywords %}
<meta name="keywords" content="{{ keywords }}"/>{% endif %}
{%- if config.get('THEME_GOOGLE_SITE_VERIFICATION', None) %}
{%- for google_id in config.THEME_GOOGLE_SITE_VERIFICATION %}
<meta name="google-site-verification" content="{{ google_id }}"/>
{%- endfor %}
{%- endif %}
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{%- if description %}
<meta name="description" content="{{ description }}"/>
{% endif %}
{%- if keywords %}
<meta name="keywords" content="{{ keywords }}"/>
{% endif %}
{%- if config.get('THEME_GOOGLE_SITE_VERIFICATION', None) %}
{%- for google_id in config.THEME_GOOGLE_SITE_VERIFICATION %}
<meta name="google-site-verification" content="{{ google_id }}"/>
{%- endfor %}
{%- endif %}
{% set meta_generator = get_meta_generator() %}
{% if meta_generator %}
<meta name="generator" content="{{ meta_generator }}"/>
{%- endif %}
{%- endblock head_meta %}
{%- block head_title %}
{%- set title = title or _(config.THEME_SITENAME) or _('Invenio') %}
Expand Down
26 changes: 26 additions & 0 deletions tests/test_invenio_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,29 @@ def test_frontpage_exists(app_frontpage_handler):
with app_frontpage_handler.test_client() as client:
response = client.get("/")
assert b"Jessica Jones" in response.data


def test_meta_generator(app):
"""Test meta generator tag."""
# remove css/js to avoid generating assets
base_tpl = r"""{% extends 'invenio_theme/page.html' %}
{% block css %}{% endblock %}
{% block javascript %}{% endblock %}
"""

InvenioTheme(app)
InvenioAssets(app)

with app.test_request_context():
app.config.update(dict(THEME_GENERATOR=None))
assert '<meta name="generator"' not in render_template_string(base_tpl)

app.config.update(dict(THEME_GENERATOR="Invenio"))
assert '<meta name="generator" content="Invenio"' in render_template_string(
base_tpl
)

app.config.update(dict(THEME_GENERATOR=lambda: "App-version"))
assert '<meta name="generator" content="App-version"' in render_template_string(
base_tpl
)

0 comments on commit 07b3d39

Please sign in to comment.