mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Move Webassets in a plugin
This commit is contained in:
parent
1b20319074
commit
4cfb0cd24e
5 changed files with 66 additions and 48 deletions
|
|
@ -158,11 +158,6 @@ class Pelican(object):
|
||||||
|
|
||||||
writer = self.get_writer()
|
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:
|
for p in generators:
|
||||||
if hasattr(p, 'generate_output'):
|
if hasattr(p, 'generate_output'):
|
||||||
p.generate_output(writer)
|
p.generate_output(writer)
|
||||||
|
|
|
||||||
|
|
@ -465,37 +465,6 @@ class StaticGenerator(Generator):
|
||||||
copy(path, source, os.path.join(output_path, destination),
|
copy(path, source, os.path.join(output_path, destination),
|
||||||
final_path, overwrite=True)
|
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
|
|
||||||
# <link rel="stylesheet" href="{{ SITEURL }}/{{ ASSET_URL }}">
|
|
||||||
# instead of
|
|
||||||
# <link rel="stylesheet" href="{{ ASSET_URL }}">
|
|
||||||
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):
|
def generate_output(self, writer):
|
||||||
|
|
||||||
self._copy_paths(self.settings['STATIC_PATHS'], self.path,
|
self._copy_paths(self.settings['STATIC_PATHS'], self.path,
|
||||||
|
|
|
||||||
64
pelican/plugins/assets.py
Normal file
64
pelican/plugins/assets.py
Normal file
|
|
@ -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::
|
||||||
|
|
||||||
|
<link rel="stylesheet" href="{{ SITEURL }}/{{ ASSET_URL }}">
|
||||||
|
|
||||||
|
instead of::
|
||||||
|
|
||||||
|
<link rel="stylesheet" href="{{ ASSET_URL }}">
|
||||||
|
|
||||||
|
.. _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)
|
||||||
|
|
@ -76,7 +76,6 @@ _DEFAULT_CONFIG = {'PATH': '.',
|
||||||
'ARTICLE_PERMALINK_STRUCTURE': '',
|
'ARTICLE_PERMALINK_STRUCTURE': '',
|
||||||
'TYPOGRIFY': False,
|
'TYPOGRIFY': False,
|
||||||
'SUMMARY_MAX_LENGTH': 50,
|
'SUMMARY_MAX_LENGTH': 50,
|
||||||
'WEBASSETS': False,
|
|
||||||
'PLUGINS': [],
|
'PLUGINS': [],
|
||||||
'MARKDOWN_EXTENSIONS': ['toc', ],
|
'MARKDOWN_EXTENSIONS': ['toc', ],
|
||||||
'TEMPLATE_PAGES': {}
|
'TEMPLATE_PAGES': {}
|
||||||
|
|
@ -187,16 +186,7 @@ def configure_settings(settings):
|
||||||
|
|
||||||
if 'LESS_GENERATOR' in settings:
|
if 'LESS_GENERATOR' in settings:
|
||||||
logger.warn("The LESS_GENERATOR setting has been removed in favor "
|
logger.warn("The LESS_GENERATOR setting has been removed in favor "
|
||||||
"of WEBASSETS")
|
"of the Webassets plugin")
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
if 'OUTPUT_SOURCES_EXTENSION' in settings:
|
if 'OUTPUT_SOURCES_EXTENSION' in settings:
|
||||||
if not isinstance(settings['OUTPUT_SOURCES_EXTENSION'], str):
|
if not isinstance(settings['OUTPUT_SOURCES_EXTENSION'], str):
|
||||||
|
|
|
||||||
|
|
@ -257,7 +257,7 @@ class TestWebAssets(unittest.TestCase):
|
||||||
self.settings = read_settings(override={
|
self.settings = read_settings(override={
|
||||||
'PATH': os.path.join(CUR_DIR, 'content', 'TestCategory'),
|
'PATH': os.path.join(CUR_DIR, 'content', 'TestCategory'),
|
||||||
'OUTPUT_PATH': self.temp_path,
|
'OUTPUT_PATH': self.temp_path,
|
||||||
'WEBASSETS': True,
|
'PLUGINS': ['pelican.plugins.assets', ],
|
||||||
'THEME': self.theme_dir,
|
'THEME': self.theme_dir,
|
||||||
})
|
})
|
||||||
pelican = Pelican(settings=self.settings)
|
pelican = Pelican(settings=self.settings)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue