Skip to content

Instantly share code, notes, and snippets.

@petebrowne
Created February 10, 2013 17:27
Show Gist options
  • Save petebrowne/4750305 to your computer and use it in GitHub Desktop.
Save petebrowne/4750305 to your computer and use it in GitHub Desktop.
These are some proposed filter configuration designs for Django Assetfiles. The goal is to have a natural API for defining the inputs and filters, while being able to determine the input files from given output files.
from assetfiles.conf import filter, patterns
from assetfiles.filters import (CoffeeFilter, ConcatFilter,
SassFilter, UglifyFilter)
# Resembles Django's URL configuration where the filters are basically views.
# Apps could define their own filter patterns, but you would not need to
# explicitly `include` filter patterns.
#
# The big trick here is reversing the patterns in order to find the input files
# for given output files. Can this be done using Regex?
filterpatterns = patterns(
# Simplest possible configuration. This should default to looking for files
# with the .sass or .scss extension and converting them to .css files.
SassFilter,
# This works the same as above, but explicitly defines the pattern
# for matching the file extension. The SassFilter automatically uses the
# compressed style when running with `DEBUG = False`.
filter(r'^.*\.s[ac]ss$', SassFilter),
# Here we chain a few filters together. The UglifyFilter only compresses
# with `DEBUG = False`.
filter(r'^.*\.coffee$', CoffeeFilter, UglifyFilter),
# When using the ConcatFilter (or any filter for that matter) you can
# explicity set the output file.
filter(r'^application/.*\.coffee$',
ConcatFilter, CoffeeFilter, UglifyFilter,
output='application.js'),
)
from assetfiles.conf import filter, patterns
from assetfiles.filters import (CoffeeFilter, ConcatFilter,
SassFilter, UglifyFilter)
# Maybe it would be much simpler for the API design and implementation if
# we use a glob syntax for defining the inputs.
filterpatterns = patterns(
filter('**/*.s[ac]ss', SassFilter),
filter('application/**/*.coffee',
ConcatFilter, CoffeeFilter, UglifyFilter,
output='application.js'),
)
# Maybe the filter definition should be within the SETTINGS. In this case, it
# should be definable without any imports.
#
# The problem with this is that it is not really clear what's happening. What's
# an input configuration? What's an output configuration? Also it gets really
# silly looking with all the nested tuples.
ASSETFILES_FILTERS = (
# Again the simplest possible definition
'assetfiles.filters.SassFilter',
# Explicitly defining the input pattern.
('**/*.s[ac]ss', 'assetfiles.filters.SassFilter'),
# More complicated example, with chained filters and input and output
# configuration.
(
'application/**/*.coffee',
(
'assetfiles.filters.ConcatFilter',
'assetfiles.filters.CoffeeFilter',
'assetfiles.filters.UglifyFilter'
),
'application.js'
),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment