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.summary|striptags|e }}