fifty 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/fifty.rb +91 -69
  2. metadata +2 -2
data/lib/fifty.rb CHANGED
@@ -2,32 +2,27 @@ module Fifty
2
2
 
3
3
  require 'handlebars'
4
4
  require 'haml'
5
- require 'binding_of_caller'
6
-
7
- VERSION = '0.0.1'
8
-
5
+
6
+ VERSION = '0.0.2'
7
+
9
8
  class << self
10
9
  attr_accessor :views
11
10
  attr_accessor :compiled
11
+ attr_accessor :context
12
12
  end
13
-
13
+
14
14
  self.views = ['./views/**/*.haml']
15
15
  self.compiled = false
16
-
17
- def self.cdn(name)
18
- if name == :handlebars
19
- 'https://cdnjs.cloudflare.com/ajax/libs/' +
20
- 'handlebars.js/1.0.0-rc.3/handlebars.min.js'
21
- elsif name == :jquery
22
- 'https://cdnjs.cloudflare.com/' +
23
- 'ajax/libs/jquery/1.9.1/jquery.min.js'
24
- end
25
- end
26
16
 
27
17
  @@handlebars = Handlebars::Context.new
18
+
19
+ @@handlebars.partial_missing do |a|
20
+ Fifty.hbs2html(Fifty.render_haml(a, context), @@current)
21
+ end
22
+
28
23
  @@helpers, @@shared, @@partials = {}, {}, {}
29
24
  @@view_paths = []
30
-
25
+
31
26
  def self.helper(name, fn)
32
27
  code = self.register_helper(name, fn)
33
28
  @@helpers[name] = code
@@ -39,7 +34,7 @@ module Fifty
39
34
  @@shared[name] = code
40
35
  self.eval_js(code)
41
36
  end
42
-
37
+
43
38
  def self.compile
44
39
  self.compile_statics
45
40
  self.compile_inlines
@@ -50,24 +45,32 @@ module Fifty
50
45
  views = self.views.map { |p| Dir[p] }.flatten
51
46
  context = binding.of_caller(3)
52
47
  views.each do |file|
48
+
49
+ puts "Trying to register #{file}"
50
+ next if file.index('layout')
53
51
  path = File.dirname(file)
52
+
53
+ name = '_' + file.gsub('./', '')
54
+ .gsub('.haml', '')
55
+ .split('/')[1..-1].join('-')
56
+
54
57
  unless @@view_paths.include?(path)
58
+ puts path.inspect
55
59
  @@view_paths << path
56
60
  end
57
61
  template = File.read(file)
58
- name = '_' + File.basename(file, '.haml')
59
- next if name == '_layout'
62
+
60
63
  partial = Haml::Engine.
61
- new(template).render(
62
- context, layout: false)
64
+ new(template).render(context, layout: false)
63
65
  partial = self.escape_hbs(partial)
64
66
  @@partials[name] = partial
67
+ puts "Registered #{name}"
68
+
65
69
  end
66
70
  end
67
71
 
68
72
  def self.compile_inlines
69
73
  inlines = Sinatra::Application.templates
70
- context = binding.of_caller(3)
71
74
  inlines.each do |name, info|
72
75
  next if name == :layout
73
76
  partial = Haml::Engine.
@@ -76,7 +79,7 @@ module Fifty
76
79
  @@partials[name] = partial
77
80
  end
78
81
  end
79
-
82
+
80
83
  def self.register_helper(name, fn)
81
84
  "Handlebars.registerHelper('#{name}', #{fn});"
82
85
  end
@@ -86,7 +89,7 @@ module Fifty
86
89
  @js.runtime.eval(code)
87
90
  end
88
91
  end
89
-
92
+
90
93
  def self.escape_hbs(partial)
91
94
  2.times do
92
95
  partial.gsub!("{{", "{%{")
@@ -94,68 +97,71 @@ module Fifty
94
97
  end
95
98
  partial
96
99
  end
97
-
100
+
98
101
  def self.included(base)
99
102
  base.class_eval {
100
103
  Fifty::Helpers::Locales.compile }
101
104
  end
102
105
 
103
- def fifty(view, data)
106
+ def fifty(view, data, globals = {})
104
107
  Fifty.compile unless Fifty.compiled
105
- hbs2html(haml2hbs(view), data)
108
+ Fifty.hbs2html(Fifty.haml2hbs(view, globals), data)
106
109
  end
107
110
 
108
111
  alias :fu :fifty
109
-
110
- def self.partials
111
- partials_hbs = ''
112
- @@partials.each do |name, code|
113
- partials_hbs += script_tag(name,
114
- code, 'x-text-handlebars')
112
+
113
+ def self.core_js
114
+
115
+ core_js = ''
116
+ @@helpers.each do |name, code|
117
+ core_js += code + "\n"
115
118
  end
119
+ script_tag('helpers', core_js)
120
+
121
+ end
122
+
123
+ def self.partials_js
116
124
  partials_js = ''
117
125
  @@partials.each do |name, code|
118
- partials_js += register_partial(name) + "\n"
126
+ partials_js += register_partial(name, code) + "\n"
119
127
  end
120
- partials_js = script_tag('partials', partials_js)
121
- [partials_hbs, partials_js].join
128
+ script_tag('partials', partials_js)
122
129
  end
123
130
 
124
- def self.scripts
125
- scripts_js = ''
126
- @@helpers.each do |name, code|
127
- scripts_js += code + "\n"
128
- end
129
- scripts_js = script_tag('helpers', scripts_js)
131
+ def self.shared_js
132
+
130
133
  shared_js = "\n"
131
134
  @@shared.each do |name, code|
132
135
  shared_js += code + "\n"
133
136
  end
134
- shared_js = script_tag('shared', shared_js)
135
- [scripts_js, shared_js].join
137
+ script_tag('shared', shared_js)
138
+
139
+ end
140
+
141
+ def self.helpers
142
+ [core_js, partials_js, shared_js].join
136
143
  end
137
144
 
138
145
  require_relative 'fifty/helpers'
139
146
 
140
147
  private
141
148
 
142
- def haml2hbs(name)
143
- context = binding.of_caller(4)
149
+ def self.haml2hbs(name, options = {})
150
+ puts "Rendering #{name} from HAML to HBS"
144
151
  result = render_haml(name, context, layout: false)
145
- while data = result.match(/{{> ([^}]*)}}/)
152
+ while data = result.match(/{{> ([^}]*)}}/)
146
153
  partial = render_haml($1, context, layout: false)
147
154
  result = result.gsub(data[0], partial)
148
155
  end
149
- render_layout(context, result)
156
+ render_layout(context, result, options)
150
157
  end
151
-
152
- def render_layout(context, insert)
153
- render_haml(:layout, context) do
154
- insert
155
- end
158
+
159
+ def self.render_layout(context, insert, options)
160
+ !options[:layout] ? insert :
161
+ render_haml(:layout, context) { insert }
156
162
  end
157
163
 
158
- def get_template(name)
164
+ def self.get_template(name)
159
165
  inlines = Sinatra::Application.templates
160
166
  if path = find_haml_template(name)
161
167
  File.read(path)
@@ -165,24 +171,25 @@ module Fifty
165
171
  raise "No template for #{name}."
166
172
  end
167
173
  end
168
-
169
- def find_haml_template(name)
170
- @@view_paths.each do |path|
171
- file = File.join(path, name.to_s + '.haml')
172
- return file if File.readable?(file)
173
- end
174
- nil
174
+
175
+ def self.find_haml_template(name)
176
+ file = './views/' + name.to_s.gsub('-', '/') + '.haml'
177
+ return file if File.readable?(file)
175
178
  end
176
179
 
177
- def render_haml(name, context = nil, options = {}, &block)
178
- Haml::Engine.new(get_template(name), options).render(context, &block)
180
+ def self.render_haml(name, context = nil, options = {}, &block)
181
+ puts "Rendering HAML template #{name}"
182
+ Haml::Engine.new(get_template(name),
183
+ options).render(context, &block)
179
184
  end
180
-
181
- def hbs2html(html, data)
185
+
186
+ def self.hbs2html(html, data)
187
+ @@current = data
188
+ puts "-> with partials #{data}"
182
189
  unescape_hbs(@@handlebars.compile(html).call(data))
183
190
  end
184
191
 
185
- def unescape_hbs(html)
192
+ def self.unescape_hbs(html)
186
193
  html.gsub("{%{", "{{").gsub("}%}", "}}")
187
194
  end
188
195
 
@@ -190,9 +197,24 @@ module Fifty
190
197
  "\n<script id='#{id}' type='#{type}'>#{code}</script>\n"
191
198
  end
192
199
 
193
- def self.register_partial(name)
194
- "Handlebars.registerPartial('#{name[1..-1]}'," +
195
- " document.getElementById('#{name}').innerText);"
200
+ def self.register_partial(name, code)
201
+ "Handlebars.registerPartial('#{name[1..-1]}', " +
202
+ "'#{escape_javascript(code)}');"
203
+ end
204
+
205
+ JS_ESCAPE_MAP = {
206
+ '\\' => '\\\\',
207
+ '</' => '<\/',
208
+ "\r\n" => '\n',
209
+ "\n" => '\n',
210
+ "\r" => '\n',
211
+ '"' => '\\"',
212
+ "'" => "\\'"
213
+ }
214
+
215
+ def self.escape_javascript(javascript)
216
+ regexp = /(\\|<\/|\r\n|\342\200\250|[\n\r"'])/u
217
+ javascript.gsub(regexp) {|match| JS_ESCAPE_MAP[match] }
196
218
  end
197
219
 
198
220
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fifty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-17 00:00:00.000000000 Z
12
+ date: 2013-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json