From ac924536d9f99e5cffef5f3d53f6f9e20b3b4550 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 19 Jan 2017 18:25:22 +0000 Subject: [PATCH] Use global way to get titles --- pelicanconf.py | 4 +++- plugins/filters.py | 12 ++++++++++++ theme/templates/article.html | 2 +- theme/templates/blog.html | 2 +- theme/templates/category.html | 4 ++-- theme/templates/extras/header.html | 2 +- theme/templates/page-home.html | 2 +- theme/templates/page.html | 2 +- theme/templates/projects.html | 4 ++-- 9 files changed, 24 insertions(+), 10 deletions(-) diff --git a/pelicanconf.py b/pelicanconf.py index 648c65b..17e444f 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -108,7 +108,9 @@ from plugins import filters JINJA_FILTERS = { "datetime": filters.format_datetime, "category_find": filters.category_find, - "limit": filters.limit + "limit": filters.limit, + "get_title": filters.get_title, + "get_html_title": filters.get_html_title } JINJA_ENVIRONMENT = { diff --git a/plugins/filters.py b/plugins/filters.py index 2950233..b52296f 100644 --- a/plugins/filters.py +++ b/plugins/filters.py @@ -1,6 +1,10 @@ import iso8601 +def get_attribute(cls, attr, default=None): + return getattr(cls, attr, default) + + def format_datetime(value): return iso8601.parse_date(str(value)).strftime("%x") @@ -19,3 +23,11 @@ def limit(line, length): return " ".join(line.split(" ")[:length]) + '...' elif isinstance(line, list): return line[:length] + + +def get_title(instance): + return get_attribute(instance, 'title') or (hasattr(instance, 'page') and get_attribute(instance.page, 'name')) or get_attribute(instance, 'name') or '' + + +def get_html_title(instance): + return get_attribute(instance, 'html_title') or get_title(instance) diff --git a/theme/templates/article.html b/theme/templates/article.html index 82d27a4..8fcd85c 100644 --- a/theme/templates/article.html +++ b/theme/templates/article.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block htmltitle %} - {{ article.html_title or article.title }} + {{ article|get_html_title }} {% endblock %} {% block metadata %} diff --git a/theme/templates/blog.html b/theme/templates/blog.html index a6ed1d1..088a813 100644 --- a/theme/templates/blog.html +++ b/theme/templates/blog.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block htmltitle %} - {{ article.title }} + {{ article|get_html_title }} {% endblock %} {% block metadata %} diff --git a/theme/templates/category.html b/theme/templates/category.html index e7cab7e..9a2e056 100644 --- a/theme/templates/category.html +++ b/theme/templates/category.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block htmltitle %} - {{ category.name|title }} + {{ category|get_html_title }} {% endblock %} {% block metadata %} @@ -30,7 +30,7 @@
-

{{ article.title }}

+

{{ article|get_title }}

{{ article.summary|striptags|e }}

diff --git a/theme/templates/extras/header.html b/theme/templates/extras/header.html index 033c1cf..d590185 100644 --- a/theme/templates/extras/header.html +++ b/theme/templates/extras/header.html @@ -1,7 +1,7 @@