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 link img}
- STEAL_PARAMS =
{ 'link' => [:href, :charset, :rel, :type, :media, :rev, :target], 'a' => [:title, :onclick, :target], 'script' => [:type, :charset, :defer], :other => [: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.
- #steal_keys ⇒ Object
-
#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_attribute(text_to_append) ⇒ Object
- #append_dyn_param(key, value, conditional = false) ⇒ Object
- #append_param(key, value) ⇒ Object
-
#compile_params(helper) ⇒ Object
Compile dynamic parameters as ERB.
-
#dup ⇒ Object
Duplicate markup and make sure params and dyn_params are duplicated as well.
-
#has_param?(key) ⇒ Boolean
Return true if the given key exists in params or dyn_params.
-
#initialize(tag, params = nil) ⇒ Markup
constructor
A new instance of Markup.
-
#pre_wrap ⇒ Object
Store some text to insert at the beggining of the tag content on wrap.
- #prepend_dyn_param(key, value, conditional = false) ⇒ Object
- #prepend_param(key, value) ⇒ Object
-
#set_dyn_param(key, value) ⇒ Object
Set dynamic html parameters.
-
#set_dyn_params(hash) ⇒ Object
Set dynamic html parameters.
-
#set_id(erb_id) ⇒ Object
Define the DOM id from a node context.
-
#set_param(key, value) ⇒ Object
Set static html parameters.
-
#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).
- #to_s ⇒ Object
-
#wrap(text) ⇒ Object
Wrap the given text with our tag.
Constructor Details
#initialize(tag, params = nil) ⇒ Markup
Returns a new instance of Markup.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/zafu/markup.rb', line 67 def initialize(tag, params = nil) @done = false @tag = tag if params self.params = params else @params = Zafu::OrderedHash.new end @dyn_params = Zafu::OrderedHash.new end |
Instance Attribute Details
#done ⇒ Object
Ensure wrap is not called more then once unless this attribute has been reset in between
20 21 22 |
# File 'lib/zafu/markup.rb', line 20 def done @done end |
#dyn_params ⇒ Object
Dynamic tag parameters that should not be escaped. For example: (.. class=‘<%= @node.klass %>’)
18 19 20 |
# File 'lib/zafu/markup.rb', line 18 def dyn_params @dyn_params end |
#params ⇒ Object
Tag parameters (.. class=‘xxx’ id=‘yyy’)
16 17 18 |
# File 'lib/zafu/markup.rb', line 16 def params @params end |
#space_after ⇒ Object
Space to insert after tag
24 25 26 |
# File 'lib/zafu/markup.rb', line 24 def space_after @space_after end |
#space_before ⇒ Object
Space to insert before tag
22 23 24 |
# File 'lib/zafu/markup.rb', line 22 def space_before @space_before end |
#steal_keys ⇒ Object
257 258 259 |
# File 'lib/zafu/markup.rb', line 257 def steal_keys @steal_keys || (STEAL_PARAMS[@tag] || []) + STEAL_PARAMS[:other] end |
#tag ⇒ Object
Tag used (“li” for example). The tag can be nil (no tag).
14 15 16 |
# File 'lib/zafu/markup.rb', line 14 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’
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 58 59 60 61 62 63 64 |
# File 'lib/zafu/markup.rb', line 32 def parse_params(text) return Zafu::OrderedHash.new unless text return text if text.kind_of?(Hash) params = Zafu::OrderedHash.new 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_attribute(text_to_append) ⇒ Object
187 188 189 |
# File 'lib/zafu/markup.rb', line 187 def append_attribute(text_to_append) (@append ||= '') << text_to_append end |
#append_dyn_param(key, value, conditional = false) ⇒ Object
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/zafu/markup.rb', line 191 def append_dyn_param(key, value, conditional = false) spacer = conditional ? '' : ' ' if prev_value = @params.delete(key) @dyn_params[key] = "#{prev_value}#{spacer}#{value}" elsif prev_value = @dyn_params[key] @dyn_params[key] = "#{prev_value}#{spacer}#{value}" else @dyn_params[key] = value end end |
#append_param(key, value) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/zafu/markup.rb', line 166 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 “#…”
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/zafu/markup.rb', line 107 def compile_params(helper) @params.each do |key, value| if value =~ /^(.*)\#\{(.*)\}(.*)$/ @params.delete(key) if $1 == '' && $3 == '' code = RubyLess.translate(helper, $2) if code.literal append_dyn_param(key, helper.form_quote(code.literal.to_s)) else append_dyn_param(key, "<%= #{code} %>") end else code = RubyLess.translate_string(helper, value) if code.literal append_dyn_param(key, helper.form_quote(code.literal.to_s)) else append_dyn_param(key, "<%= #{code} %>") end end end end end |
#dup ⇒ Object
Duplicate markup and make sure params and dyn_params are duplicated as well.
214 215 216 217 218 219 220 |
# File 'lib/zafu/markup.rb', line 214 def dup markup = super markup.instance_variable_set(:@params, @params.dup) markup.instance_variable_set(:@dyn_params, @dyn_params.dup) markup.instance_variable_set(:@pre_wrap, @pre_wrap.dup) if @pre_wrap markup end |
#has_param?(key) ⇒ Boolean
Return true if the given key exists in params or dyn_params.
209 210 211 |
# File 'lib/zafu/markup.rb', line 209 def has_param?(key) params[key] || dyn_params[key] end |
#pre_wrap ⇒ Object
Store some text to insert at the beggining of the tag content on wrap. Inserted elements are indexed in a hash but only values are shown.
224 225 226 |
# File 'lib/zafu/markup.rb', line 224 def pre_wrap @pre_wrap ||= {} end |
#prepend_dyn_param(key, value, conditional = false) ⇒ Object
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/zafu/markup.rb', line 176 def prepend_dyn_param(key, value, conditional = false) spacer = conditional ? '' : ' ' if prev_value = @params.delete(key) @dyn_params[key] = "#{value}#{spacer}#{prev_value}" elsif prev_value = @dyn_params[key] @dyn_params[key] = "#{value}#{spacer}#{prev_value}" else @dyn_params[key] = value end end |
#prepend_param(key, value) ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/zafu/markup.rb', line 156 def prepend_param(key, value) if prev_value = @dyn_params[key] @dyn_params[key] = "#{value} #{prev_value}" elsif prev_value = @params[key] @params[key] = "#{value} #{prev_value}" else @params[key] = value end end |
#set_dyn_param(key, value) ⇒ Object
Set dynamic html parameters.
138 139 140 141 |
# File 'lib/zafu/markup.rb', line 138 def set_dyn_param(key, value) @params.delete(key) @dyn_params[key] = value end |
#set_dyn_params(hash) ⇒ Object
Set dynamic html parameters.
131 132 133 134 135 |
# File 'lib/zafu/markup.rb', line 131 def set_dyn_params(hash) hash.each do |k,v| set_dyn_param(k, v) end end |
#set_id(erb_id) ⇒ Object
Define the DOM id from a node context
203 204 205 206 |
# File 'lib/zafu/markup.rb', line 203 def set_id(erb_id) params[:id] = nil dyn_params[:id] = erb_id end |
#set_param(key, value) ⇒ Object
Set static html parameters.
151 152 153 154 |
# File 'lib/zafu/markup.rb', line 151 def set_param(key, value) @dyn_params.delete(key) @params[key] = value end |
#set_params(hash) ⇒ Object
Set static html parameters.
144 145 146 147 148 |
# File 'lib/zafu/markup.rb', line 144 def set_params(hash) hash.each do |k,v| set_param(k, v) end end |
#steal_html_params_from(p) ⇒ Object
Steal html parameters from an existing hash (the stolen parameters are removed from the argument)
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/zafu/markup.rb', line 94 def steal_html_params_from(p) p.delete_if do |k,v| if steal_keys.include?(k) || k =~ /^data-/ @params[k] = v true else false end end end |
#to_s ⇒ Object
253 254 255 |
# File 'lib/zafu/markup.rb', line 253 def to_s wrap(nil) end |
#wrap(text) ⇒ 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>.
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/zafu/markup.rb', line 230 def wrap(text) return text if @done text = "#{@pre_wrap.values}#{text}" if @pre_wrap if dyn_params[:id] @tag ||= 'div' end if @tag if text.blank? && EMPTY_TAGS.include?(@tag) res = "#{@pre_wrap}<#{@tag}#{params_to_html}#{@append}/>" else res = "<#{@tag}#{params_to_html}#{@append}>#{text}</#{@tag}>" end else res = text end @done = true (@space_before || '') + res + (@space_after || '') end |