Class: Haml2Handlebars::AttributesParser
- Inherits:
-
Object
- Object
- Haml2Handlebars::AttributesParser
- Defined in:
- lib/haml2handlebars/attributes_parser.rb
Defined Under Namespace
Classes: DynamicAttributes
Constant Summary collapse
- CONTENTS =
/^, \{(.*)\}$/
- ROCKET =
'\=\>'
- SYMBOL_TEXT =
'[\w_]+'
- STRING_TEXT =
'[\w_-]+'
- SYMBOL_KEY =
/^(?:\:(#{SYMBOL_TEXT})\s*#{ROCKET}|(#{SYMBOL_TEXT}):)\s*/
- STRING_KEY =
/^(?:'(#{STRING_TEXT})'|"(#{STRING_TEXT})")\s*#{ROCKET}\s*/
- SYMBOL_VALUE =
/^:(#{SYMBOL_TEXT})\s*/
- STRING_VALUE =
/^(?:"([^"]+)"|'([^']+)')\s*/
- STRING_INTERPOLATION =
/^("[^\\]*)#\{/
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#pairs ⇒ Object
readonly
Returns the value of attribute pairs.
Class Method Summary collapse
Instance Method Summary collapse
- #dynamic? ⇒ Boolean
-
#initialize(attributes) ⇒ AttributesParser
constructor
A new instance of AttributesParser.
- #parse! ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(attributes) ⇒ AttributesParser
Returns a new instance of AttributesParser.
6 7 8 9 |
# File 'lib/haml2handlebars/attributes_parser.rb', line 6 def initialize attributes @attributes = attributes @pairs = [] end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
10 11 12 |
# File 'lib/haml2handlebars/attributes_parser.rb', line 10 def attributes @attributes end |
#pairs ⇒ Object (readonly)
Returns the value of attribute pairs.
10 11 12 |
# File 'lib/haml2handlebars/attributes_parser.rb', line 10 def pairs @pairs end |
Class Method Details
.hash_to_html(hash) ⇒ Object
60 61 62 63 64 |
# File 'lib/haml2handlebars/attributes_parser.rb', line 60 def self.hash_to_html hash hash.each_pair.map do |key, value| " #{key}='#{value.gsub("'", ''')}'" end.join('') end |
Instance Method Details
#dynamic? ⇒ Boolean
56 57 58 |
# File 'lib/haml2handlebars/attributes_parser.rb', line 56 def dynamic? @dynamic end |
#parse! ⇒ Object
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 |
# File 'lib/haml2handlebars/attributes_parser.rb', line 25 def parse! rest = attributes.strip.scan(CONTENTS).flatten.first begin while rest and not(rest.empty?) if rest =~ SYMBOL_KEY key = $1 || $2 rest.gsub! SYMBOL_KEY, '' elsif rest =~ STRING_KEY key = $1 || $2 rest.gsub! STRING_KEY, '' else raise DynamicAttributes end if rest =~ STRING_VALUE value = $1 raise DynamicAttributes if rest =~ STRING_INTERPOLATION elsif rest =~ SYMBOL_VALUE value = $1 || $2 else raise DynamicAttributes end pairs << [key, value] end rescue DynamicAttributes @dynamic = true return end end |
#to_html ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/haml2handlebars/attributes_parser.rb', line 66 def to_html if attributes.strip.empty? return '' else parse! if dynamic? hash = attributes.scan(CONTENTS).flatten.first hash.strip! hash.gsub! /\s*,$/, '' hash.gsub! /\#{(\w+)}/, "{{\\1}}" hash = eval "{#{hash}}" " #{hash.keys.first}=\"#{hash.values.first}\"" else pairs.map do |(key, value)| "#{key}='#{value.gsub("'", ''')}'" end.join('') end end end |