Browse Source

now accepts a list of template directories

Leonardo 7 years ago
parent
commit
7d8f4de7ef
2 changed files with 21 additions and 13 deletions
  1. 3 3
      jinja2content/README.md
  2. 18 10
      jinja2content/jinja2content.py

+ 3 - 3
jinja2content/README.md

@@ -72,9 +72,9 @@ to html.
 ## Configuration
 
 This plugin accepts the setting "JINJA2CONTENT_TEMPLATES" which should be
-set to a path relative to 'PATH' (the main content directory).
-`jinja2content` will look for templates inside this directory. If they are
-not found here, the theme's templates folder is used.
+set to a list of paths relative to 'PATH' (the main content directory).
+`jinja2content` will look for templates inside these directories, in order.
+If they are not found in any, the theme's templates folder is used.
 
 
 ## Notes

+ 18 - 10
jinja2content/jinja2content.py

@@ -1,5 +1,12 @@
-import os
+"""
+jinja2content.py
+----------------
 
+Pelican plugin that processes Markdown files as jinja templates.
+
+"""
+
+from os import path
 from pelican import signals
 from pelican.readers import Markdown, MarkdownReader
 from pelican.utils import pelican_open
@@ -13,17 +20,18 @@ class JinjaMarkdownReader(MarkdownReader):
 
         # will look first in 'JINJA2CONTENT_TEMPLATES', by default the
         # content root path, then in the theme's templates
-        local_templates_dir = self.settings.get('JINJA2CONTENT_TEMPLATES', '.')
-        local_templates_dir = os.path.join(self.settings['PATH'], local_templates_dir)
-        theme_templates_dir = os.path.join(self.settings['THEME'], 'templates')
-        loader = ChoiceLoader([
-            FileSystemLoader(local_templates_dir),
-            FileSystemLoader(theme_templates_dir)])
-
+        # local_templates_dirs = self.settings.get('JINJA2CONTENT_TEMPLATES', ['.'])
+        # local_templates_dirs = path.join(self.settings['PATH'], local_templates_dirs)
+        local_dirs = self.settings.get('JINJA2CONTENT_TEMPLATES', ['.'])
+        local_dirs = [path.join(self.settings['PATH'], folder)
+                      for folder in local_dirs]
+        theme_dir = path.join(self.settings['THEME'], 'templates')
+
+        loaders = [FileSystemLoader(_dir) for _dir
+                   in local_dirs + [theme_dir]]
         self.env = Environment(trim_blocks=True, lstrip_blocks=True,
                                extensions=self.settings['JINJA_EXTENSIONS'],
-                               loader=loader)
-
+                               loader=ChoiceLoader(loaders))
 
     def read(self, source_path):
         """Parse content and metadata of markdown files.