def assetmutator_source(path, **kw): """ Returns the source data/contents of the mutated asset (and mutates the asset if needed). This is useful when you want to output inline data (e.g. for inline JavaScript blocks). :param path: The Pyramid asset path to process. :type path: string - Required :type mutator: dict or string - Optional :param mutator: Allows you to override/specify a specific mutator to use (e.g. ``coffee``), or assign a brand new mutator dictionary to be used (e.g. ``{'cmd': 'lessc', 'ext': 'css'}``) .. note:: Many template packages escape output by default. Consult your template language's syntax to output an unescaped string. """ request = get_current_request() settings = request.registry.settings mutant = Mutator(settings, path, **kw) if not settings['assetmutator.each_request']: if not mutant.mutated: # TODO: Error? return None return mutant.mutated_data() else: if mutant.mutated: return mutant.mutated_data() else: mutant.process() return mutant.mutated_data()
def assetmutator_url(path, **kw): """ Returns a Pyramid :meth:`~pyramid.request.Request.static_url` of the mutated asset (and mutates the asset if needed). :param path: The Pyramid asset path to process. :type path: string - Required :type mutator: dict or string - Optional :param mutator: Allows you to override/specify a specific mutator to use (e.g. ``coffee``), or assign a brand new mutator dictionary to be used (e.g. ``{'cmd': 'lessc', 'ext': 'css'}``) """ request = get_current_request() settings = request.registry.settings mutant = Mutator(settings, path, **kw) if not settings['assetmutator.each_request']: if not mutant.mutated: # TODO: Error? pass return request.static_url(mutant.new_path) else: if mutant.mutated: return request.static_url(mutant.new_path) else: return request.static_url(mutant.process())
def assetmutator_assetpath(path, **kw): """ Returns a Pyramid `asset specification`_ such as ``pkg:static/path/to/file.ext`` (and mutates the asset if needed). :param path: The Pyramid asset path to process. :type path: string - Required :type mutator: dict or string - Optional :param mutator: Allows you to override/specify a specific mutator to use (e.g. ``coffee``), or assign a brand new mutator dictionary to be used (e.g. ``{'cmd': 'lessc', 'ext': 'css'}``) This function could be used to nest ``pyramid_assetmutator`` calls. e.g. ``assetmutator_path(assetmutator_assetpath('pkg:static/js/script.coffee'))`` could compile a CoffeeScript file into JS, and then further minify the JS file if your mutator configuration looked something like:: config.assign_assetmutator('coffee', 'coffee -c -p', 'js') config.assign_assetmutator('js', 'uglifyjs', 'js') .. _asset specification: http://pyramid.readthedocs.org/en/latest/ glossary.html#term-asset-specification """ request = get_current_request() settings = request.registry.settings mutant = Mutator(settings, path, **kw) if not settings['assetmutator.each_request']: if not mutant.mutated: # log an error pass return mutant.new_path else: if mutant.mutated: return mutant.new_path else: return mutant.process()