Class: WikiCloth::WikiBuffer::Var

Inherits:
WikiCloth::WikiBuffer show all
Defined in:
lib/wikicloth/wiki_buffer/var.rb

Instance Method Summary collapse

Methods inherited from WikiCloth::WikiBuffer

#add_char, #buffer_type, #buffers, #check_globals, #data, #debug, #get_param, #in_template?, #params, #run_globals?

Constructor Details

#initialize(data = "", options = {}) ⇒ Var

Returns a new instance of Var.



7
8
9
10
11
12
13
14
15
# File 'lib/wikicloth/wiki_buffer/var.rb', line 7

def initialize(data="",options={})
  super(data,options)
  self.buffer_type = "var"
  @in_quotes = false
  @tag_start = true
  @tag_size = 2
  @close_size = 2
  @fname = nil
end

Instance Method Details

#default_functions(name, params) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
# File 'lib/wikicloth/wiki_buffer/var.rb', line 77

def default_functions(name,params)
  case name
  when "#if"
    params.first.blank? ? params[2] : params[1]
  when "#switch"
    match = params.first
    default = nil
    for p in params[1..-1]
      temp = p.split("=")
      if temp.length == 1 && p == params.last
        return p
      elsif temp.instance_of?(Array) && temp.length > 1
        test = temp.first.strip
        default = temp[1].strip if test == "#default"
        return temp[1].strip if test == match
      end
    end
    default.nil? ? "" : default
  when "#expr"
    begin
      ExpressionParser::Parser.new.parse(params.first)
    rescue RuntimeError
      "Expression error: #{$!}"
    end
  when "#ifeq"
    if params[0] =~ /^[0-9A-Fa-f]+$/ && params[1] =~ /^[0-9A-Fa-f]+$/
      params[0].to_i == params[1].to_i ? params[2] : params[3]
    else
      params[0] == params[1] ? params[2] : params[3]
    end
  when "#len"
    params.first.length
  when "#sub"
    params.first[params[1].to_i,params[2].to_i]
  when "#pad"
    case params[3]
    when "right"
      params[0].ljust(params[1].to_i,params[2])
    when "center"
      params[0].center(params[1].to_i,params[2])
    else
      params[0].rjust(params[1].to_i,params[2])
    end
  when "#iferror"
    params.first =~ /error/ ? params[1] : params[2]
  when "#capture"
    @options[:params][params.first] = params[1]
    ""
  when "lc"
    params.first.downcase
  when "uc"
    params.first.upcase
  when "ucfirst"
    params.first.capitalize
  when "lcfirst"
    params.first[0,1].downcase + params.first[1,-1]
  when "plural"
    params.first.to_i > 1 ? params[1] : params[2]
  when "debug"
    ret = nil
    case params.first
    when "param"
      @options[:buffer].buffers.reverse.each do |b|
        if b.instance_of?(WikiBuffer::HTMLElement) && b.element_name == "template"
           ret = b.get_param(params[1])
        end
      end
      ret
    when "buffer"
      ret = "<pre>"
      buffer = @options[:buffer].buffers
      buffer.each do |b|
        ret += " --- #{b.class}"
        ret += b.instance_of?(WikiBuffer::HTMLElement) ? " -- #{b.element_name}\n" : " -- #{b.data}\n"
      end
      "#{ret}</pre>"
    end
  end
end

#function_nameObject



37
38
39
# File 'lib/wikicloth/wiki_buffer/var.rb', line 37

def function_name
  @fname
end

#is_function?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/wikicloth/wiki_buffer/var.rb', line 161

def is_function?
  self.function_name.nil? || self.function_name.blank? ? false : true
end

#is_param?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/wikicloth/wiki_buffer/var.rb', line 157

def is_param?
  @tag_size == 3 ? true : false
end

#skip_html?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/wikicloth/wiki_buffer/var.rb', line 25

def skip_html?
  false
end

#tag_sizeObject



17
18
19
# File 'lib/wikicloth/wiki_buffer/var.rb', line 17

def tag_size
  @tag_size
end

#tag_size=(val) ⇒ Object



21
22
23
# File 'lib/wikicloth/wiki_buffer/var.rb', line 21

def tag_size=(val)
  @tag_size = val
end

#tag_startObject



29
30
31
# File 'lib/wikicloth/wiki_buffer/var.rb', line 29

def tag_start
  @tag_start
end

#tag_start=(val) ⇒ Object



33
34
35
# File 'lib/wikicloth/wiki_buffer/var.rb', line 33

def tag_start=(val)
  @tag_start = val
end

#to_sObject



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
67
68
69
70
71
72
73
74
75
# File 'lib/wikicloth/wiki_buffer/var.rb', line 41

def to_s
  if self.is_function?
    ret = default_functions(function_name,params.collect { |p| p.strip })
    ret ||= @options[:link_handler].function(function_name, params.collect { |p| p.strip })
    ret.to_s
  elsif self.is_param?
    ret = nil
    @options[:buffer].buffers.reverse.each do |b|
      ret = b.get_param(params[0],params[1]) if b.instance_of?(WikiBuffer::HTMLElement) && b.element_name == "template"
    end
    ret.to_s
  else
    # put template at beginning of buffer
    template_stack = @options[:buffer].buffers.collect { |b| b.get_param("__name") if b.instance_of?(WikiBuffer::HTMLElement) && 
      b.element_name == "template" }.compact
    if template_stack.last == params[0]
      debug_tree = @options[:buffer].buffers.collect { |b| b.debug }.join("-->")
      "<span class=\"error\">Template loop detected: &#123;&#123;#{debug_tree}&#125;&#125;</span>"
    else
      ret = @options[:link_handler].include_resource("#{params[0]}".strip,params[1..-1]).to_s
      ret.gsub!(/<!--(.|\s)*?-->/,"")
      count = 0
      tag_attr = self.params[1..-1].collect { |p|
        if p.instance_of?(Hash)
          "#{p[:name]}=\"#{p[:value]}\""
        else
          count += 1
          "#{count}=\"#{p}\""
        end
      }.join(" ")
      self.data = ret.blank? ? "" : "<template __name=\"#{params[0]}\" #{tag_attr}>#{ret}</template>"
      ""
    end
  end
end