diff --git a/pelican/__init__.py b/pelican/__init__.py index 443d330b..c0f33687 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -158,11 +158,6 @@ class Pelican(object): writer = self.get_writer() - # pass the assets environment to the generators - if self.settings['WEBASSETS']: - generators[1].env.assets_environment = generators[0].assets_env - generators[2].env.assets_environment = generators[0].assets_env - for p in generators: if hasattr(p, 'generate_output'): p.generate_output(writer) diff --git a/pelican/generators.py b/pelican/generators.py index 33d0ff20..00b0d6d3 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -465,37 +465,6 @@ class StaticGenerator(Generator): copy(path, source, os.path.join(output_path, destination), final_path, overwrite=True) - def generate_context(self): - - if self.settings['WEBASSETS']: - from webassets import Environment as AssetsEnvironment - - # Define the assets environment that will be passed to the - # generators. The StaticGenerator must then be run first to have - # the assets in the output_path before generating the templates. - - # Let ASSET_URL honor Pelican's RELATIVE_URLS setting. - # Hint for templates: - # Current version of webassets seem to remove any relative - # paths at the beginning of the URL. So, if RELATIVE_URLS - # is on, ASSET_URL will start with 'theme/', regardless if we - # set assets_url here to './theme/' or to 'theme/'. - # XXX However, this breaks the ASSET_URL if user navigates to - # a sub-URL, e.g. if he clicks on a category. To workaround this - # issue, I use - # - # instead of - # - if self.settings.get('RELATIVE_URLS'): - assets_url = './theme/' - else: - assets_url = self.settings['SITEURL'] + '/theme/' - assets_src = os.path.join(self.output_path, 'theme') - self.assets_env = AssetsEnvironment(assets_src, assets_url) - - if logging.getLevelName(logger.getEffectiveLevel()) == "DEBUG": - self.assets_env.debug = True - def generate_output(self, writer): self._copy_paths(self.settings['STATIC_PATHS'], self.path, diff --git a/pelican/plugins/assets.py b/pelican/plugins/assets.py new file mode 100644 index 00000000..58f9f863 --- /dev/null +++ b/pelican/plugins/assets.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +""" +Asset management plugin for Pelican +=================================== + +This plugin allows you to use the `webassets`_ module to manage assets such as +CSS and JS files. + +Hint for templates: Current version of webassets seems to remove any relative +paths at the beginning of the URL. So, if ``RELATIVE_URLS`` is on, +``ASSET_URL`` will start with ``theme/``, regardless if we set ``assets_url`` +here to ``./theme/`` or to ``theme/``. + +However, this breaks the ``ASSET_URL`` if user navigates to a sub-URL, e.g. if +he clicks on a category. A workaround for this issue is to use:: + + + +instead of:: + + + +.. _webassets: https://webassets.readthedocs.org/ + +""" + +import os +import logging + +from pelican import signals +from webassets import Environment +from webassets.ext.jinja2 import AssetsExtension + + +def add_jinja2_ext(pelican): + """Add Webassets to Jinja2 extensions in Pelican settings.""" + + pelican.settings['JINJA_EXTENSIONS'].append(AssetsExtension) + + +def create_assets_env(generator): + """Define the assets environment and pass it to the generator.""" + + logger = logging.getLogger(__name__) + + # Let ASSET_URL honor Pelican's RELATIVE_URLS setting. + if generator.settings.get('RELATIVE_URLS'): + assets_url = './theme/' + else: + assets_url = generator.settings['SITEURL'] + '/theme/' + assets_src = os.path.join(generator.output_path, 'theme') + + generator.env.assets_environment = Environment(assets_src, assets_url) + + if logging.getLevelName(logger.getEffectiveLevel()) == "DEBUG": + generator.env.assets_environment.debug = True + + +def register(): + """Plugin registration.""" + + signals.initialized.connect(add_jinja2_ext) + signals.article_generator_init.connect(create_assets_env) + signals.pages_generator_init.connect(create_assets_env) diff --git a/pelican/settings.py b/pelican/settings.py index 2c318997..b4fe04f4 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -76,7 +76,6 @@ _DEFAULT_CONFIG = {'PATH': '.', 'ARTICLE_PERMALINK_STRUCTURE': '', 'TYPOGRIFY': False, 'SUMMARY_MAX_LENGTH': 50, - 'WEBASSETS': False, 'PLUGINS': [], 'MARKDOWN_EXTENSIONS': ['toc', ], 'TEMPLATE_PAGES': {} @@ -187,16 +186,7 @@ def configure_settings(settings): if 'LESS_GENERATOR' in settings: logger.warn("The LESS_GENERATOR setting has been removed in favor " - "of WEBASSETS") - - if 'WEBASSETS' in settings and settings['WEBASSETS'] is not False: - try: - from webassets.ext.jinja2 import AssetsExtension - settings['JINJA_EXTENSIONS'].append(AssetsExtension) - except ImportError: - logger.warn("You must install the webassets module to use " - "WEBASSETS") - settings['WEBASSETS'] = False + "of the Webassets plugin") if 'OUTPUT_SOURCES_EXTENSION' in settings: if not isinstance(settings['OUTPUT_SOURCES_EXTENSION'], str): diff --git a/tests/test_generators.py b/tests/test_generators.py index ba923980..6b714082 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -257,7 +257,7 @@ class TestWebAssets(unittest.TestCase): self.settings = read_settings(override={ 'PATH': os.path.join(CUR_DIR, 'content', 'TestCategory'), 'OUTPUT_PATH': self.temp_path, - 'WEBASSETS': True, + 'PLUGINS': ['pelican.plugins.assets', ], 'THEME': self.theme_dir, }) pelican = Pelican(settings=self.settings)