Переглянути джерело

specify in pelican config which params should be passed to liquid tag sub-plugins

Erik Paulson 6 роки тому
батько
коміт
e2ee4b3716

+ 11 - 0
liquid_tags/Readme.md

@@ -220,6 +220,17 @@ comment line `# <!-- collapse=False -->` will be expanded on load but
 can be collapsed by tapping on their header. Cells without collapsed
 comments are rendered as standard code input cells.
 
+## Configuration settings in custom tags
+
+Tags do not have access to the full Pelicans settings, and instead arrange for 
+the variables to be passed to the tag.  For tag authors who plan to add their 
+tag as in-tree tags, they can just add the variables they need to an array in 
+`mdx_liquid_tags.py`, but out-of-tree tags can specify which variables they 
+need by including a tuple of (variable, default value, helptext) in the 
+user's `pelicanconf.py` settings:
+
+    LIQUID_CONFIGS = (('PATH', '.', "The default path"), ('SITENAME', 'Default Sitename', 'The name of the site'))
+
 ## Testing
 
 To test the plugin in multiple environments we use [tox](http://tox.readthedocs.org/en/latest/). To run the entire test suite:

+ 36 - 0
liquid_tags/generic.py

@@ -0,0 +1,36 @@
+"""
+Generic Tag
+-----------
+This implements a tag that that is mostly useful for testing.
+
+This tag does not implement anything useful, but is a place that can be
+used for testing liquid_tags infrastructure, in situations that need
+a full lifecycle test.
+
+The first use case is a test of a tag that will pull out data from
+the configuration file and make it available during the test.
+
+A tag of
+{% generic config <config file variable> %>
+will be replaced with the value of that config file in html
+
+Not all config file variables are exposed - the set
+of variables are from the LIQUID_CONFIGS setting, which is a list of 
+variables to pass to the liquid tags.
+"""
+from .mdx_liquid_tags import LiquidTags
+
+@LiquidTags.register('generic')
+def generic(preprocessor, tag, markup):
+    (cmd, args) = markup.split(' ', 1)
+    if cmd.lower() == 'config':
+        config_param = args.split()[0].upper()
+        config_val = preprocessor.configs.getConfig(config_param)	
+        return(config_val)
+    else:
+        return 'generic: %s ' % markup
+
+#----------------------------------------------------------------------
+# This import allows image tag to be a Pelican plugin
+from liquid_tags import register
+

+ 6 - 1
liquid_tags/liquid_tags.py

@@ -1,5 +1,5 @@
 from pelican import signals
-from .mdx_liquid_tags import LiquidTags, LT_CONFIG
+from .mdx_liquid_tags import LiquidTags, LT_CONFIG, LT_HELP
 
 
 def addLiquidTags(gen):
@@ -7,6 +7,11 @@ def addLiquidTags(gen):
         from pelican.settings import DEFAULT_CONFIG
         gen.settings['MARKDOWN'] = DEFAULT_CONFIG['MARKDOWN']
 
+    if gen.settings.get('LIQUID_CONFIGS'):
+        for param,default,helptext in gen.settings.get('LIQUID_CONFIGS'):
+            LT_CONFIG[param] = default
+            LT_HELP[param] = helptext
+
     if LiquidTags not in gen.settings['MARKDOWN']:
         configs = dict()
         for key,value in LT_CONFIG.items():

+ 7 - 0
liquid_tags/test_data/content/test-generic-config-tag.md

@@ -0,0 +1,7 @@
+Title: test generic config tag
+Date: 2017-12-03
+Authors: A. Person
+
+This post is written by 
+{% generic config author %}
+if all goes well the blog post author (from settings, not from the post) shows up one line above

+ 2 - 1
liquid_tags/test_data/pelicanconf.py

@@ -29,6 +29,7 @@ CATEGORIES_SAVE_AS = ''
 TAGS_SAVE_AS = ''
 
 PLUGIN_PATHS = ['../../']
-PLUGINS = ['liquid_tags.notebook']
+PLUGINS = ['liquid_tags.notebook', 'liquid_tags.generic']
 
 NOTEBOOK_DIR = 'notebooks'
+LIQUID_CONFIGS = (('PATH', '.', "The default path"), ('THEME', '', 'The theme in use'), ('SITENAME', 'Default Sitename', 'The name of the site'), ('AUTHOR', '', 'Name of the blog author'))

+ 61 - 0
liquid_tags/test_generic.py

@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+from __future__ import print_function
+
+import filecmp
+import os
+import unittest
+from shutil import rmtree
+from tempfile import mkdtemp
+
+import pytest
+from pelican import Pelican
+from pelican.settings import read_settings
+
+PLUGIN_DIR = os.path.dirname(__file__)
+TEST_DATA_DIR = os.path.join(PLUGIN_DIR, 'test_data')
+
+
+class TestFullRun(unittest.TestCase):
+    '''Test running Pelican with the Plugin'''
+
+    def setUp(self):
+        '''Create temporary output and cache folders'''
+        self.temp_path = mkdtemp(prefix='pelicantests.')
+        self.temp_cache = mkdtemp(prefix='pelican_cache.')
+        os.chdir(TEST_DATA_DIR)
+
+    def tearDown(self):
+        '''Remove output and cache folders'''
+        rmtree(self.temp_path)
+        rmtree(self.temp_cache)
+        os.chdir(PLUGIN_DIR)
+
+
+    def test_generic_tag_with_config(self):
+        '''Test generation of site with a generic tag that reads in a config file.'''
+
+        base_path = os.path.dirname(os.path.abspath(__file__))
+        base_path = os.path.join(base_path, 'test_data')
+        content_path = os.path.join(base_path, 'content')
+        output_path = os.path.join(base_path, 'output')
+        settings_path = os.path.join(base_path, 'pelicanconf.py')
+        settings = read_settings(path=settings_path,
+                                 override={'PATH': content_path,
+                                           'OUTPUT_PATH': self.temp_path,
+                                           'CACHE_PATH': self.temp_cache,
+                                           }
+                                 )
+
+        pelican = Pelican(settings)
+        pelican.run()
+
+        assert os.path.exists(os.path.join(self.temp_path,
+                                           'test-generic-config-tag.html'))
+
+        assert "Tester" in open(os.path.join(self.temp_path,
+                                           'test-generic-config-tag.html')).read()
+        # test differences
+        #assert filecmp.cmp(os.path.join(output_path,
+        #                                'test-ipython-notebook-v3.html'),
+        #                   os.path.join(self.temp_path,
+        #                                'test-ipython-notebook.html'))

+ 3 - 0
liquid_tags/test_notebook.py

@@ -1,5 +1,8 @@
 import re
 
+import pytest
+pytest.skip("Test is currently broken, see pelican pr #1618", allow_module_level=True)
+
 from pelican.tests.support import unittest
 
 from . import notebook

+ 0 - 1
liquid_tags/tox.ini

@@ -10,7 +10,6 @@ commands = py.test
 
 deps =
 	pytest
-  	pytest-capturelog
 	pelican
 	markdown
 	mock