From d9ff5bac32ac4e03d38876e04f96ba826b68d0ba Mon Sep 17 00:00:00 2001 From: Magnun Leno Date: Tue, 14 Jan 2014 00:41:48 -0200 Subject: [PATCH 1/2] Adds support for themes functions With this changes the themes can have (not obligatory) a functions.py file bundled with the templates. The purpose of this file is to hold helper functions to be injected into the jinja2 templates. To control which functions will be injected is specified the __export__ list. A sample was inserted in pelican/themes/simple/templates/functions.py. --- pelican/generators.py | 9 +++++++++ pelican/themes/simple/templates/functions.py | 9 +++++++++ 2 files changed, 18 insertions(+) create mode 100644 pelican/themes/simple/templates/functions.py diff --git a/pelican/generators.py b/pelican/generators.py index b7612fa2..f8be3827 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals, print_function import os +import imp import math import random import logging @@ -62,6 +63,14 @@ class Generator(object): ]), extensions=self.settings['JINJA_EXTENSIONS'], ) + try: + theme_functions = imp.load_source('theme_functions', self._templates_path[0]+'/functions.py') + except IOError: + theme_functions = None + + if theme_functions and '__export__' in vars(theme_functions): + for func_name in theme_functions.__export__: + self.env.globals[func_name] = getattr(theme_functions, func_name) logger.debug('template list: {0}'.format(self.env.list_templates())) diff --git a/pelican/themes/simple/templates/functions.py b/pelican/themes/simple/templates/functions.py new file mode 100644 index 00000000..de40bd78 --- /dev/null +++ b/pelican/themes/simple/templates/functions.py @@ -0,0 +1,9 @@ +# encoding: utf-8 + +__export__ = ["helperFunction"] + +def helperFunction(): + return "

I'm from a helper function!

" + +def anotherHelper(): + return "I'm not being exported!" From 5be529f668d6e0d2e72e190309870f6c512ded6a Mon Sep 17 00:00:00 2001 From: Magnun Leno Date: Tue, 21 Jan 2014 14:08:58 -0200 Subject: [PATCH 2/2] Moved functions.py from templates to theme root As pointed by @tcg, the functions.py file is more suited to be located in the theme root. --- pelican/generators.py | 5 +++-- pelican/themes/simple/{templates => }/functions.py | 0 2 files changed, 3 insertions(+), 2 deletions(-) rename pelican/themes/simple/{templates => }/functions.py (100%) diff --git a/pelican/generators.py b/pelican/generators.py index f8be3827..7726726d 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -64,11 +64,12 @@ class Generator(object): extensions=self.settings['JINJA_EXTENSIONS'], ) try: - theme_functions = imp.load_source('theme_functions', self._templates_path[0]+'/functions.py') + theme_functions = imp.load_source('theme_functions', + os.path.join(self.theme, '/functions.py')) except IOError: theme_functions = None - if theme_functions and '__export__' in vars(theme_functions): + if theme_functions and hasattr(theme_functions, '__export__'): for func_name in theme_functions.__export__: self.env.globals[func_name] = getattr(theme_functions, func_name) diff --git a/pelican/themes/simple/templates/functions.py b/pelican/themes/simple/functions.py similarity index 100% rename from pelican/themes/simple/templates/functions.py rename to pelican/themes/simple/functions.py