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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object (private)

catch special tagz methods



126
127
128
129
130
131
132
133
134
135
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
# File 'lib/tagz.rb', line 126

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

.descriptionObject



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



391
392
393
# File 'lib/tagz.rb', line 391

def Tagz.escape(*strings)
  CGI.escapeHTML(strings.join)
end

.escape!(options = {}) ⇒ Object

configure tagz escaping



442
443
444
445
446
447
448
449
450
451
452
# File 'lib/tagz.rb', line 442

def Tagz.escape!(options = {})
  options = {:keys => options, :values => options, :content => options} unless options.is_a?(Hash)

  escape_keys = options[:keys]||options['keys']||options[:key]||options['key']
  escape_values = options[:values]||options['values']||options[:value]||options['value']
  escape_contents = options[:contents]||options['contents']||options[:content]||options['content']

  Tagz.escape_keys!(!!escape_keys)
  Tagz.escape_values!(!!escape_values)
  Tagz.escape_contents!(!!escape_contents)
end

.escapeAttribute(*strings) ⇒ Object



394
395
396
# File 'lib/tagz.rb', line 394

def Tagz.escapeAttribute(*strings)
  CGI.escapeHTML(strings.join)
end

.escapeHTML(*strings) ⇒ Object

escape utils



388
389
390
# File 'lib/tagz.rb', line 388

def Tagz.escapeHTML(*strings)
  CGI.escapeHTML(strings.join)
end

.html_mode!Object



466
467
468
469
470
471
472
# File 'lib/tagz.rb', line 466

def Tagz.html_mode!
  Tagz.escape!(
    :keys => true,
    :values => false,
    :content => false
  )
end

.html_safe(*args, &block) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/tagz.rb', line 227

def Tagz.html_safe(*args, &block)
  if args.size == 1 and args.first.respond_to?(:html_safe?)
    return args.first
  end

  if args.empty? and block.nil?
    Tagz.namespace(:HtmlSafe)
  else
    string = args.join
    string += block.call.to_s if block
    HtmlSafe.new(string)
  end
end

.i_do_not_know_what_the_hell_i_am_doing!Object



456
457
458
# File 'lib/tagz.rb', line 456

def Tagz.i_do_not_know_what_the_hell_i_am_doing!
  escape!(true)
end

.i_know_what_the_hell_i_am_doing!Object



453
454
455
# File 'lib/tagz.rb', line 453

def Tagz.i_know_what_the_hell_i_am_doing!
  escape!(false)
end

.raw(*args, &block) ⇒ Object

raw utils



400
401
402
# File 'lib/tagz.rb', line 400

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



180
181
182
183
184
185
186
187
# File 'lib/tagz.rb', line 180

def Tagz.singleton_class(&block)
  @singleton_class ||= (
    class << Tagz
      self
    end
  )
  block ? @singleton_class.module_eval(&block) : @singleton_class
end

.versionObject



9
10
11
# File 'lib/tagz.rb', line 9

def Tagz.version()
  '9.7.0'
end

.xml_mode!Object



459
460
461
462
463
464
465
# File 'lib/tagz.rb', line 459

def Tagz.xml_mode!
  Tagz.escape!(
    :keys => true,
    :values => true,
    :contents => true
  )
end