Class: Zafu::Markup
- Inherits:
-
Object
- Object
- Zafu::Markup
- Defined in:
- lib/zafu/markup.rb
Overview
A Markup object is used to hold information on the tag used (<li>), it’s parameters (.. class=‘xxx’) and indentation.
Constant Summary collapse
- EMPTY_TAGS =
%w{meta input}
- STEAL_PARAMS =
[:class, :id, :style]
Instance Attribute Summary collapse
-
#done ⇒ Object
Ensure wrap is not called more then once unless this attribute has been reset in between.
-
#dyn_params ⇒ Object
Dynamic tag parameters that should not be escaped.
-
#params ⇒ Object
Tag parameters (.. class=‘xxx’ id=‘yyy’).
-
#space_after ⇒ Object
Space to insert after tag.
-
#space_before ⇒ Object
Space to insert before tag.
-
#tag ⇒ Object
Tag used (“li” for example).
Class Method Summary collapse
-
.parse_params(text) ⇒ Object
Parse parameters into a hash.
Instance Method Summary collapse
- #append_dyn_param(key, value) ⇒ Object
- #append_param(key, value) ⇒ Object
-
#compile_params(helper) ⇒ Object
Compile dynamic parameters as ERB.
-
#initialize(tag) ⇒ Markup
constructor
A new instance of Markup.
-
#set_dyn_params(hash) ⇒ Object
Set dynamic html parameters.
-
#set_id(erb_id) ⇒ Object
Define the DOM id from a node context.
-
#set_params(hash) ⇒ Object
Set static html parameters.
-
#steal_html_params_from(p) ⇒ Object
Steal html parameters from an existing hash (the stolen parameters are removed from the argument).
-
#wrap(text, *append) ⇒ Object
Wrap the given text with our tag.
Constructor Details
#initialize(tag) ⇒ Markup
Returns a new instance of Markup.
60 61 62 63 64 65 |
# File 'lib/zafu/markup.rb', line 60 def initialize(tag) @done = false @tag = tag @params = {} @dyn_params = {} end |
Instance Attribute Details
#done ⇒ Object
Ensure wrap is not called more then once unless this attribute has been reset in between
15 16 17 |
# File 'lib/zafu/markup.rb', line 15 def done @done end |
#dyn_params ⇒ Object
Dynamic tag parameters that should not be escaped. For example: (.. class=‘<%= @node.klass %>’)
13 14 15 |
# File 'lib/zafu/markup.rb', line 13 def dyn_params @dyn_params end |
#params ⇒ Object
Tag parameters (.. class=‘xxx’ id=‘yyy’)
11 12 13 |
# File 'lib/zafu/markup.rb', line 11 def params @params end |
#space_after ⇒ Object
Space to insert after tag
19 20 21 |
# File 'lib/zafu/markup.rb', line 19 def space_after @space_after end |
#space_before ⇒ Object
Space to insert before tag
17 18 19 |
# File 'lib/zafu/markup.rb', line 17 def space_before @space_before end |
#tag ⇒ Object
Tag used (“li” for example). The tag can be nil (no tag).
9 10 11 |
# File 'lib/zafu/markup.rb', line 9 def tag @tag end |
Class Method Details
.parse_params(text) ⇒ Object
Parse parameters into a hash. This parsing supports multiple values for one key by creating additional keys: <tag do=‘hello’ or=‘goodbye’ or=‘gotohell’> creates the hash :or=>‘goodbye’, :or1=>‘gotohell’
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 |
# File 'lib/zafu/markup.rb', line 25 def parse_params(text) return {} unless text return text if text.kind_of?(Hash) params = {} rest = text.strip while (rest != '') if rest =~ /(.+?)=/ key = $1.strip.to_sym rest = rest[$&.length..-1].strip if rest =~ /('|")(|[^\1]*?[^\\])\1/ rest = rest[$&.length..-1].strip key_counter = 1 while params[key] key = "#{key}#{key_counter}".to_sym key_counter += 1 end if $1 == "'" params[key] = $2.gsub("\\'", "'") else params[key] = $2.gsub('\\"', '"') end else # error, bad format, return found params. break end else # error, bad format break end end params end |
Instance Method Details
#append_dyn_param(key, value) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/zafu/markup.rb', line 127 def append_dyn_param(key, value) if prev_value = @params.delete(key) @dyn_params[key] = "#{prev_value} #{value}" elsif prev_value = @dyn_params[key] @dyn_params[key] = "#{prev_value} #{value}" else @dyn_params[key] = value end end |
#append_param(key, value) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/zafu/markup.rb', line 117 def append_param(key, value) if prev_value = @dyn_params[key] @dyn_params[key] = "#{prev_value} #{value}" elsif prev_value = @params[key] @params[key] = "#{prev_value} #{value}" else @params[key] = value end end |
#compile_params(helper) ⇒ Object
Compile dynamic parameters as ERB. A parameter is considered dynamic if it contains the string eval “#…”
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/zafu/markup.rb', line 88 def compile_params(helper) @params.each do |key, value| if value =~ /^(.*)\#\{(.*)\}(.*)$/ @params.delete(key) if $1 == '' && $3 == '' append_dyn_param(key, "<%= #{RubyLess.translate($2, helper)} %>") else append_dyn_param(key, "<%= #{RubyLess.translate_string(value, helper)} %>") end end end end |
#set_dyn_params(hash) ⇒ Object
Set dynamic html parameters.
102 103 104 105 106 107 |
# File 'lib/zafu/markup.rb', line 102 def set_dyn_params(hash) hash.keys.each do |k| @params.delete(k) end @dyn_params.merge!(hash) end |
#set_id(erb_id) ⇒ Object
Define the DOM id from a node context
138 139 140 141 |
# File 'lib/zafu/markup.rb', line 138 def set_id(erb_id) params[:id] = nil dyn_params[:id] = erb_id end |
#set_params(hash) ⇒ Object
Set static html parameters.
110 111 112 113 114 115 |
# File 'lib/zafu/markup.rb', line 110 def set_params(hash) hash.keys.each do |k| @dyn_params.delete(k) end @params.merge!(hash) end |
#steal_html_params_from(p) ⇒ Object
Steal html parameters from an existing hash (the stolen parameters are removed from the argument)
78 79 80 81 82 83 84 |
# File 'lib/zafu/markup.rb', line 78 def steal_html_params_from(p) @params ||= {} STEAL_PARAMS.each do |k| next unless p[k] @params[k] = p.delete(k) end end |
#wrap(text, *append) ⇒ Object
Wrap the given text with our tag. If ‘append’ is not empty, append the text after the tag parameters: <li class=‘foo’[APPEND HERE]>text</li>.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/zafu/markup.rb', line 145 def wrap(text, *append) return text if @done append ||= [] if @tag if text.blank? && EMPTY_TAGS.include?(@tag) res = "<#{@tag}#{params_to_html}#{append.join('')}/>" else res = "<#{@tag}#{params_to_html}#{append.join('')}>#{text}</#{@tag}>" end else res = text end @done = true (@space_before || '') + res + (@space_after || '') end |