Module: Tagz
- Included in:
- Namespace::Globally, Namespace::Privately
- Defined in:
- lib/tagz.rb,
lib/tagz.rb
Overview
supporting code
Defined Under Namespace
Modules: Namespace
Class Method Summary collapse
- .description ⇒ Object
- .escape(*strings) ⇒ Object
-
.escape!(options = {}) ⇒ Object
configure tagz escaping.
- .escape_html(s) ⇒ Object
-
.escape_html_map ⇒ Object
escape utils.
- .escape_html_once(s) ⇒ Object
- .escape_html_once_regexp ⇒ Object
- .escapeAttribute(*strings) ⇒ Object
- .escapeHTML(*strings) ⇒ Object
- .h(string) ⇒ Object
- .html_mode! ⇒ Object
-
.html_safe(*args, &block) ⇒ Object
raw utils.
- .html_safe?(string) ⇒ Boolean
- .i_do_not_know_what_the_hell_i_am_doing! ⇒ Object
- .i_know_what_the_hell_i_am_doing! ⇒ Object
- .raw(*args, &block) ⇒ Object
-
.singleton_class(&block) ⇒ Object
singleton_class access for ad-hoc method adding from inside namespace.
- .version ⇒ Object
- .xml_mode! ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *a, &b) ⇒ Object (private)
catch special tagz methods
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/tagz.rb', line 136 def method_missing(m, *a, &b) strategy = case m.to_s when %r/^(.*[^_])_(!)?$/o :open_tag when %r/^_([^_].*)$/o :close_tag when 'e' :element when '__', '___' :puts else nil end if(strategy.nil? or (tagz.nil? and Tagz.privately===self)) begin return super ensure $!.set_backtrace caller(1) if $! end end case strategy when :open_tag m, bang = $1, $2 b ||= lambda{} if bang tagz{ tagz__(m, *a, &b) } when :close_tag m = $1 tagz{ __tagz(m, *a, &b) } when :element Tagz.element.new(*a, &b) when :puts tagz do tagz.push("\n") unless a.empty? tagz.push(a.join) tagz.push("\n") end end end end |
Class Method Details
.description ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/tagz.rb', line 13 def Tagz.description <<-____ tagz.rb is generates html, xml, or any sgml variant like a small ninja running across the backs of a herd of giraffes swatting of heads like a mark-up weedwacker. weighing in at less than 300 lines of code tagz.rb adds an html/xml/sgml syntax to ruby that is both unobtrusive, safe, and available globally to objects without the need for any builder or superfluous objects. tagz.rb is designed for applications that generate html to be able to do so easily in any context without heavyweight syntax or scoping issues, like a ninja sword through butter. ____ end |
.escape(*strings) ⇒ Object
403 404 405 |
# File 'lib/tagz.rb', line 403 def Tagz.escape(*strings) Tagz.escape_html(strings.join) end |
.escape!(options = {}) ⇒ Object
configure tagz escaping
486 487 488 489 490 491 492 493 494 495 496 |
# File 'lib/tagz.rb', line 486 def Tagz.escape!( = {}) = {:keys => , :values => , :content => } unless .is_a?(Hash) escape_keys = [:keys]||['keys']||[:key]||['key'] escape_values = [:values]||['values']||[:value]||['value'] escape_contents = [:contents]||['contents']||[:content]||['content'] Tagz.escape_keys!(!!escape_keys) Tagz.escape_values!(!!escape_values) Tagz.escape_contents!(!!escape_contents) end |
.escape_html(s) ⇒ Object
383 384 385 386 387 388 389 390 391 |
# File 'lib/tagz.rb', line 383 def Tagz.escape_html(s) s = s.to_s if Tagz.html_safe?(s) s else Tagz.html_safe(s.gsub(/[&"'><]/, Tagz.escape_html_map)) end end |
.escape_html_map ⇒ Object
escape utils
375 376 377 |
# File 'lib/tagz.rb', line 375 def Tagz.escape_html_map @escape_html_map ||= { '&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => ''' } end |
.escape_html_once(s) ⇒ Object
393 394 395 396 397 |
# File 'lib/tagz.rb', line 393 def Tagz.escape_html_once(s) result = s.to_s.gsub(Tagz.html_escape_once_regexp){|_| Tagz.escape_html_map[_]} Tagz.html_safe?(s) ? Tagz.html_safe(result) : result end |
.escape_html_once_regexp ⇒ Object
379 380 381 |
# File 'lib/tagz.rb', line 379 def Tagz.escape_html_once_regexp @escape_html_once_regexp ||= /["><']|&(?!([a-zA-Z]+|(#\d+));)/ end |
.escapeAttribute(*strings) ⇒ Object
407 408 409 |
# File 'lib/tagz.rb', line 407 def Tagz.escapeAttribute(*strings) Tagz.escape_html(strings.join) end |
.escapeHTML(*strings) ⇒ Object
399 400 401 |
# File 'lib/tagz.rb', line 399 def Tagz.escapeHTML(*strings) Tagz.escape_html(strings.join) end |
.h(string) ⇒ Object
444 445 446 |
# File 'lib/tagz.rb', line 444 def Tagz.h(string) Tagz.escape_html(string) end |
.html_mode! ⇒ Object
510 511 512 513 514 515 516 |
# File 'lib/tagz.rb', line 510 def Tagz.html_mode! Tagz.escape!( :keys => true, :values => false, :content => false ) end |
.html_safe(*args, &block) ⇒ Object
raw utils
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/tagz.rb', line 413 def Tagz.html_safe(*args, &block) html_safe = namespace(:HTMLSafe) if args.empty? and block.nil? return html_safe end first = args.first case when first.is_a?(html_safe) return first when args.size == 1 string = first html_safe.new(string) else string = [args, (block ? block.call : nil)].flatten.compact.join(' ') html_safe.new(string) end end |
.html_safe?(string) ⇒ Boolean
436 437 438 |
# File 'lib/tagz.rb', line 436 def Tagz.html_safe?(string) string.html_safe? rescue false end |
.i_do_not_know_what_the_hell_i_am_doing! ⇒ Object
500 501 502 |
# File 'lib/tagz.rb', line 500 def Tagz.i_do_not_know_what_the_hell_i_am_doing! escape!(true) end |
.i_know_what_the_hell_i_am_doing! ⇒ Object
497 498 499 |
# File 'lib/tagz.rb', line 497 def Tagz.i_know_what_the_hell_i_am_doing! escape!(false) end |
.raw(*args, &block) ⇒ Object
440 441 442 |
# File 'lib/tagz.rb', line 440 def Tagz.raw(*args, &block) Tagz.html_safe(*args, &block) end |
.singleton_class(&block) ⇒ Object
singleton_class access for ad-hoc method adding from inside namespace
190 191 192 193 194 195 196 197 |
# File 'lib/tagz.rb', line 190 def Tagz.singleton_class(&block) @singleton_class ||= ( class << Tagz self end ) block ? @singleton_class.module_eval(&block) : @singleton_class end |
.version ⇒ Object
9 10 11 |
# File 'lib/tagz.rb', line 9 def Tagz.version() '9.9.1' end |