Skip to content

Instantly share code, notes, and snippets.

@timotta
Last active August 29, 2015 14:15
Show Gist options
  • Save timotta/94ee1bf8e582d01a2d6b to your computer and use it in GitHub Desktop.
Save timotta/94ee1bf8e582d01a2d6b to your computer and use it in GitHub Desktop.
require 'sprockets/directive_processor'
class MiddleIncludeProcessor < Sprockets::DirectiveProcessor
def prepare
@pathname = Pathname.new(file)
@header = ''
@body = data
@included_pathnames = []
@compat = false
end
def evaluate(context, locals, &block)
@context = context
@lines ||= body.lines.each_with_index.map do |line, index|
if directive = line[DIRECTIVE_PATTERN, 1]
name, *args = Shellwords.shellwords(directive)
if name == 'include'
context.evaluate(args.first)
end
else
line
end
end
@result = @lines.compact.join
end
end
@timotta
Copy link
Author

timotta commented Feb 11, 2015

sprockets = Sprockets::Environment.new
sprockets.register_processor('application/javascript', MiddleIncludeProcessor)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment