Class: Zafu::Markup

Inherits:
Object
  • Object
show all
Defined in:
lib/zafu/markup.rb

Overview

A Markup object is used to hold information on the tag used (

  • ), 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

    Class Method Summary collapse

    Instance Method Summary collapse

    Constructor Details

    #initialize(tag, params = nil) ⇒ Markup

    Returns a new instance of Markup.

    
    
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    # File 'lib/zafu/markup.rb', line 69
    
    def initialize(tag, params = nil)
      @done       = false
      @tag        = tag
      if params
        self.params = params
      else
        @params     = OrderedHash.new
      end
      @dyn_params = OrderedHash.new
    end

    Instance Attribute Details

    #doneObject

    Ensure wrap is not called more then once unless this attribute has been reset in between

    
    
    22
    23
    24
    # File 'lib/zafu/markup.rb', line 22
    
    def done
      @done
    end

    #dyn_paramsObject

    Dynamic tag parameters that should not be escaped. For example: (.. class='<%= @node.klass %>')

    
    
    20
    21
    22
    # File 'lib/zafu/markup.rb', line 20
    
    def dyn_params
      @dyn_params
    end

    #paramsObject

    Tag parameters (.. class='xxx' id='yyy')

    
    
    18
    19
    20
    # File 'lib/zafu/markup.rb', line 18
    
    def params
      @params
    end

    #space_afterObject

    Space to insert after tag

    
    
    26
    27
    28
    # File 'lib/zafu/markup.rb', line 26
    
    def space_after
      @space_after
    end

    #space_beforeObject

    Space to insert before tag

    
    
    24
    25
    26
    # File 'lib/zafu/markup.rb', line 24
    
    def space_before
      @space_before
    end

    #steal_keysObject

    
    
    255
    256
    257
    # File 'lib/zafu/markup.rb', line 255
    
    def steal_keys
      @steal_keys || (STEAL_PARAMS[@tag] || []) + STEAL_PARAMS[:other]
    end

    #tagObject

    Tag used ("li" for example). The tag can be nil (no tag).

    
    
    16
    17
    18
    # File 'lib/zafu/markup.rb', line 16
    
    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: creates the hash :or=>'goodbye', :or1=>'gotohell'

    
    
    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
    65
    66
    # File 'lib/zafu/markup.rb', line 34
    
    def parse_params(text)
      return OrderedHash.new unless text
      return text if text.kind_of?(Hash)
      params = 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

    
    
    185
    186
    187
    # File 'lib/zafu/markup.rb', line 185
    
    def append_attribute(text_to_append)
      (@append ||= '') << text_to_append
    end

    #append_dyn_param(key, value, conditional = false) ⇒ Object

    
    
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    # File 'lib/zafu/markup.rb', line 189
    
    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

    
    
    164
    165
    166
    167
    168
    169
    170
    171
    172
    # File 'lib/zafu/markup.rb', line 164
    
    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 "#..."

    
    
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    # File 'lib/zafu/markup.rb', line 105
    
    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

    #dupObject

    Duplicate markup and make sure params and dyn_params are duplicated as well.

    
    
    212
    213
    214
    215
    216
    217
    218
    # File 'lib/zafu/markup.rb', line 212
    
    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.

    Returns:

    • (Boolean)
    
    
    207
    208
    209
    # File 'lib/zafu/markup.rb', line 207
    
    def has_param?(key)
      params[key] || dyn_params[key]
    end

    #pre_wrapObject

    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.

    
    
    222
    223
    224
    # File 'lib/zafu/markup.rb', line 222
    
    def pre_wrap
      @pre_wrap ||= {}
    end

    #prepend_dyn_param(key, value, conditional = false) ⇒ Object

    
    
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    # File 'lib/zafu/markup.rb', line 174
    
    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

    
    
    154
    155
    156
    157
    158
    159
    160
    161
    162
    # File 'lib/zafu/markup.rb', line 154
    
    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.

    
    
    136
    137
    138
    139
    # File 'lib/zafu/markup.rb', line 136
    
    def set_dyn_param(key, value)
      @params.delete(key)
      @dyn_params[key] = value
    end

    #set_dyn_params(hash) ⇒ Object

    Set dynamic html parameters.

    
    
    129
    130
    131
    132
    133
    # File 'lib/zafu/markup.rb', line 129
    
    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

    
    
    201
    202
    203
    204
    # File 'lib/zafu/markup.rb', line 201
    
    def set_id(erb_id)
      params[:id] = nil
      dyn_params[:id] = erb_id
    end

    #set_param(key, value) ⇒ Object

    Set static html parameters.

    
    
    149
    150
    151
    152
    # File 'lib/zafu/markup.rb', line 149
    
    def set_param(key, value)
      @dyn_params.delete(key)
      @params[key] = value
    end

    #set_params(hash) ⇒ Object

    Set static html parameters.

    
    
    142
    143
    144
    145
    146
    # File 'lib/zafu/markup.rb', line 142
    
    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)

    
    
    96
    97
    98
    99
    100
    101
    # File 'lib/zafu/markup.rb', line 96
    
    def steal_html_params_from(p)
      steal_keys.each do |k|
        next unless p[k]
        @params[k] = p.delete(k)
      end
    end

    #to_sObject

    
    
    251
    252
    253
    # File 'lib/zafu/markup.rb', line 251
    
    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.

    
    
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    # File 'lib/zafu/markup.rb', line 228
    
    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