Class: Emote
- Inherits:
-
Object
- Object
- Emote
- Defined in:
- lib/yescode/emote.rb
Defined Under Namespace
Modules: Helpers
Constant Summary collapse
- PATTERN =
/ ^[^\S\n]*(%)[^\S\n]*(.*?)(?:\n|\Z) | # Ruby evaluated lines (<\?)\s+(.*?)\s+\?> | # Multiline Ruby blocks (\$\{)(.*?)\} | # Ruby evaluated to strings unescaped (\{\{)(.*?)\}\} # Ruby evaluated to strings html escaped /mx
Class Method Summary collapse
- .h(value) ⇒ Object
- .parse(template, context = self, vars = [], name = "template") ⇒ Object
- .src(template, vars = []) ⇒ Object
Class Method Details
.h(value) ⇒ Object
31 32 33 |
# File 'lib/yescode/emote.rb', line 31 def self.h(value) CGI.escapeHTML(value.to_s) end |
.parse(template, context = self, vars = [], name = "template") ⇒ Object
62 63 64 |
# File 'lib/yescode/emote.rb', line 62 def self.parse(template, context = self, vars = [], name = "template") context.instance_eval(src(template, vars), name, -1) end |
.src(template, vars = []) ⇒ Object
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 |
# File 'lib/yescode/emote.rb', line 35 def self.src(template, vars = []) terms = template.split(PATTERN) code = "proc do |params, __o| params ||= {}; __o ||= '';" vars.each do |var| code << format("%<val>s = params[%<key>p];", { val: var, key: var }) end while (term = terms.shift) code << case term when "<?" "#{terms.shift}\n" when "%" "#{terms.shift}\n" when "${" "__o << (#{terms.shift}).to_s\n" when "{{" "__o << Emote.h(#{terms.shift}).to_s\n" else "__o << #{term.dump}\n" end end code << "__o; end" end |