Class: Volt::ViewProcessor
- Defined in:
- lib/volt/server/template_handlers/view_processor.rb
Class Method Summary collapse
Instance Method Summary collapse
- #app_reference ⇒ Object
- #cache_key ⇒ Object
-
#call(input) ⇒ Object
def evaluate(context, locals, &block) binding.pry @data = compile(@data) super end.
- #compile(view_path, html, context) ⇒ Object
-
#initialize(client) ⇒ ViewProcessor
constructor
A new instance of ViewProcessor.
Constructor Details
#initialize(client) ⇒ ViewProcessor
Returns a new instance of ViewProcessor.
13 14 15 |
# File 'lib/volt/server/template_handlers/view_processor.rb', line 13 def initialize(client) @client = client end |
Class Method Details
.setup(sprockets = $volt_app.sprockets) ⇒ Object
84 85 86 87 |
# File 'lib/volt/server/template_handlers/view_processor.rb', line 84 def self.setup(sprockets=$volt_app.sprockets) sprockets.register_mime_type 'application/vtemplate', extensions: ['.html', '.email'] sprockets.register_transformer 'application/vtemplate', 'application/javascript', Volt::ViewProcessor.new(true) end |
Instance Method Details
#app_reference ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/volt/server/template_handlers/view_processor.rb', line 17 def app_reference if @client 'Volt.current_app' else 'volt_app' end end |
#cache_key ⇒ Object
25 26 27 |
# File 'lib/volt/server/template_handlers/view_processor.rb', line 25 def cache_key @cache_key ||= "#{self.class.name}:0.1".freeze end |
#call(input) ⇒ Object
def evaluate(context, locals, &block)
binding.pry
@data = compile(@data)
super
end
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/volt/server/template_handlers/view_processor.rb', line 35 def call(input) context = input[:environment].context_class.new(input) # context.link_asset('main/assets/images/lombard.jpg') # puts context.asset_path('main/assets/images/lombard.jpg').inspect # pp input data = input[:data] # input[:accept] = 'application/javascript' # input[:content_type] = 'application/javascript' # input[:environment].content_type = 'application/javascript' compiled = false data, links = input[:cache].fetch([self.cache_key, data]) do compiled = true filename = input[:filename] # puts input[:data].inspect # Remove all semicolons from source # input[:content_type] = 'application/javascript' compile(filename, input[:data], context) end unless compiled links.each do |link| context.link_asset(link) end end context..merge(data: data.to_str) end |
#compile(view_path, html, context) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/volt/server/template_handlers/view_processor.rb', line 64 def compile(view_path, html, context) exts = ComponentTemplates::Preprocessors.extensions template_path = view_path.split('/')[-4..-1].join('/').gsub('/views/', '/').gsub(/[.](#{exts.join('|')})$/, '') exts = ComponentTemplates::Preprocessors.extensions format = File.extname(view_path).downcase.delete('.').to_sym code = '' # Process template if we have a handler for this file type if handler = ComponentTemplates.handler_for_extension(format) html = handler.call(html) parser = ViewParser.new(html, template_path, context) code = parser.code(app_reference) end return [Opal.compile(code), parser.links] end |