266a3308 by Michael Richards

Implement Rivets.TextTemplateParser. [#181]

1 parent f9a54207
...@@ -250,6 +250,54 @@ class Rivets.View ...@@ -250,6 +250,54 @@ class Rivets.View
250 @models[key] = model 250 @models[key] = model
251 binding.update() for binding in @select (b) -> b.key is key 251 binding.update() for binding in @select (b) -> b.key is key
252 252
253 # Rivets.TextTemplateParser
254 # -------------------------
255
256 # Rivets.js text template parser and tokenizer for mustache-style text content
257 # binding declarations.
258 class Rivets.TextTemplateParser
259 types =
260 text: 0
261 binding: 1
262
263 # Parses the template and returns a set of tokens, separating static portions
264 # of text from binding declarations.
265 @parse: (template) ->
266 tokens = []
267 length = template.length
268 index = 0
269 lastIndex = 0
270
271 while lastIndex < length
272 index = template.indexOf '{{', lastIndex
273
274 if index < 0
275 tokens.push type: types.text, value: template.slice lastIndex
276 break
277 else
278 if index > 0 and lastIndex < index
279 tokens.push type: types.text, value: template.slice lastIndex, index
280
281 lastIndex = index + 2
282 index = template.indexOf '}}', lastIndex
283
284 if index < 0
285 substring = template.slice lastIndex - 2
286 lastToken = tokens[tokens.length - 1]
287
288 if lastToken?.type is types.text
289 lastToken.value += substring
290 else
291 tokens.push type: types.text, value: substring
292
293 break
294
295 value = template.slice(lastIndex, index).trim()
296 tokens.push type: types.binding, value: value
297 lastIndex = index + 2
298
299 tokens
300
253 # Rivets.Util 301 # Rivets.Util
254 # ----------- 302 # -----------
255 303
......