module Tagz

core tagz functions

supporting code

Public Class Methods

description() click to toggle source
# File lib/tagz.rb, line 12
    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) click to toggle source
# File lib/tagz.rb, line 407
def Tagz.escape(*strings)
  Tagz.escape_html(strings.join)
end
escape!(options = {}) click to toggle source

configure tagz escaping

# File lib/tagz.rb, line 490
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) click to toggle source
# File lib/tagz.rb, line 411
def Tagz.escapeAttribute(*strings)
  Tagz.escape_html(strings.join)
end
escapeHTML(*strings) click to toggle source
# File lib/tagz.rb, line 403
def Tagz.escapeHTML(*strings)
  Tagz.escape_html(strings.join)
end
escape_html(s) click to toggle source
# File lib/tagz.rb, line 387
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() click to toggle source

escape utils

# File lib/tagz.rb, line 379
def Tagz.escape_html_map
  @escape_html_map ||= { '&' => '&amp;',  '>' => '&gt;',   '<' => '&lt;', '"' => '&quot;', "'" => '&#39;' }
end
escape_html_once(s) click to toggle source
# File lib/tagz.rb, line 397
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() click to toggle source
# File lib/tagz.rb, line 383
def Tagz.escape_html_once_regexp
  @escape_html_once_regexp ||= /["><']|&(?!([a-zA-Z]+|(#\d+));)/
end
h(string) click to toggle source
# File lib/tagz.rb, line 448
def Tagz.h(string)
  Tagz.escape_html(string)
end
html_mode!() click to toggle source
# File lib/tagz.rb, line 514
def Tagz.html_mode!
  Tagz.escape!(
    :keys => true,
    :values => false,
    :content => false
  )
end
html_safe(*args, &block) click to toggle source

raw utils

# File lib/tagz.rb, line 417
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) click to toggle source
# File lib/tagz.rb, line 440
def Tagz.html_safe?(string)
  string.html_safe? rescue false
end
i_do_not_know_what_the_hell_i_am_doing!() click to toggle source
# File lib/tagz.rb, line 504
def Tagz.i_do_not_know_what_the_hell_i_am_doing!
  escape!(true)
end
i_know_what_the_hell_i_am_doing!() click to toggle source
# File lib/tagz.rb, line 501
def Tagz.i_know_what_the_hell_i_am_doing!
  escape!(false)
end
raw(*args, &block) click to toggle source
# File lib/tagz.rb, line 444
def Tagz.raw(*args, &block)
  Tagz.html_safe(*args, &block)
end
singleton_class(&block) click to toggle source

singleton_class access for ad-hoc method adding from inside namespace

# 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() click to toggle source
# File lib/tagz.rb, line 8
def Tagz.version()
  '9.10.0'
end
xml_mode!() click to toggle source
# File lib/tagz.rb, line 507
def Tagz.xml_mode!
  Tagz.escape!(
    :keys => true,
    :values => true,
    :contents => true
  )
end

Private Instance Methods

__tagz(tag, *a, &b) click to toggle source

close_tag

# File lib/tagz.rb, line 129
def __tagz(tag, *a, &b)
  tagz.push "</#{ tag }>"
end
method_missing(m, *a, &b) click to toggle source

catch special tagz methods

Calls superclass method
# File lib/tagz.rb, line 135
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
      :do_nothing_until_strange_core_dump_in_ruby_2_5_is_fixed
      # $!.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
tagz(document = nil, &block) click to toggle source

access tagz doc and enclose tagz operations

# File lib/tagz.rb, line 31
def tagz(document = nil, &block)
  @tagz ||= nil ## shut wornings up
  previous = @tagz

  if block
    @tagz ||= (Tagz.document.for(document) || Tagz.document.new)

    begin
      previous_size = @tagz.size

      content = instance_eval(&block)

      current_size = @tagz.size

      content_was_added = current_size > previous_size

      unless content_was_added
        @tagz << content
      end

      @tagz
    ensure
      @tagz = previous
    end
  else
    document ? Tagz.document.for(document) : @tagz
  end
end
tagz__(name, *argv, &block) click to toggle source

open_tag

# File lib/tagz.rb, line 63
def tagz__(name, *argv, &block)
  options = argv.last.is_a?(Hash) ? argv.pop : {}
  content = argv
  attributes = ''

  unless options.empty?
    booleans = []
    options.each do |key, value|
      if key.to_s =~ Tagz.namespace(:Boolean)
        value = value.to_s =~ %r/\Atrue\Z/imox ? nil : "\"#{ key.to_s.downcase.strip }\""
        booleans.push([key, value].compact)
        next
      end

      key = Tagz.escape_key(key)
      value = Tagz.escape_value(value)

      if value =~ %r/"/
        raise ArgumentError, value if value =~ %r/'/
        value = "'#{ value }'"
      else
        raise ArgumentError, value if value =~ %r/"/
        value = "\"#{ value }\""
      end

      attributes << ' ' << [key, value].join('=')
    end
    booleans.each do |kv|
      attributes << ' ' << kv.compact.join('=')
    end
  end

  tagz.push "<#{ name }#{ attributes }>"

  if content.empty?
    if block
      size = tagz.size
      value = block.call(tagz)

      if value.nil?
        unless(tagz.size > size)
          tagz[-1] = "/>"
        else
          tagz.push "</#{ name }>"
        end
      else
        tagz << value.to_s unless(tagz.size > size)
        tagz.push "</#{ name }>"
      end

    end
  else
    tagz << content.join
    if block
      size = tagz.size
      value = block.arity.abs >= 1 ? block.call(tagz) : block.call()
      tagz << value.to_s unless(tagz.size > size)
    end
    tagz.push "</#{ name }>"
  end

  tagz
end