-
Notifications
You must be signed in to change notification settings - Fork 6
/
slim.rb
67 lines (54 loc) · 1.46 KB
/
slim.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
63
64
65
66
67
require 'slim'
require 'opal'
require 'sprockets'
module Opal
module Slim
def self.compiled_slim source
::Slim::Engine.with_options(pretty: false) do
engine = ::Slim::Engine.new
engine.call(source).gsub(/(slim_controls\w+) <</, '\1 +=')
end
end
def self.wrap compiled, file
<<-EOF
Template.new('#{file}') do |slim|
#{compiled}
end
EOF
end
def self.compile source, file='(slim)'
Opal.compile(wrap(compiled_slim(source), file))
end
class Processor < Tilt::Template
self.default_mime_type = 'application/javascript'
def self.engine_initialized?
true
end
def initialize_engine
require_template_library 'opal'
end
def prepare
end
def evaluate context, locals, &block
context.require_asset 'opal-slim'
Opal::Slim.compile data, context.logical_path.sub(/^templates\//, '')
end
end
class SlimProcessor < Opal::BuilderProcessors::RubyProcessor
handles :slim
def initialize *args
super
@source = prepare(@source, @filename)
end
def requires
['opal-slim', 'erb'] + super
end
def prepare source, path
slim = ::Opal::Slim.compiled_slim(source)
::Opal::Slim.wrap(slim, path)
end
end
end
end
Opal.append_path File.expand_path('../../../opal', __FILE__).untaint
Sprockets.register_engine '.slim', Opal::Slim::Processor