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, #add_word, #buffer_type, #buffers, #check_globals, #data, #debug, #eof, #get_param, #in_template?, #params, #run_globals?

Constructor Details

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

Returns a new instance of Var.



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

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



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
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
182
183
184
# File 'lib/wikicloth/wiki_buffer/var.rb', line 82

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 "#ifexpr"
    val = false
    begin
      val = ExpressionParser::Parser.new.parse(params.first)
    rescue RuntimeError
    end
    if val
      params[1]
    else
      params[2]
    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 "urlencode"
    URI.escape(params.first, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
  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 "ns"
    values = { "" => "", "0" => "", "1" => "Talk", "talk" => "Talk", "2" => "User", "user" => "User",
	"3" => "User talk", "user_talk" => "User talk", "4" => "Meta", "project" => "Meta",
	"5" => "Meta talk", "project_talk" => "Meta talk", "6" => "File", "image" => "File",
	"7" => "File talk", "image_talk" => "File talk", "8" => "MediaWiki", "mediawiki" => "MediaWiki",
	"9" => "MediaWiki talk", "mediawiki_talk" => "MediaWiki talk", "10" => "Template",
	"template" => "Template", "11" => "Template talk", "template_talk" => "Template talk",
	"12" => "Help", "help" => "Help", "13" => "Help talk", "help_talk" => "Help talk",
	"14" => "Category", "category" => "Category", "15" => "Category talk", "category_talk" => "Category talk",
	"-2" => "Media", "media" => "Media", "-1" => "Special", "special" => "Special" }
    values[params.first.gsub(/\s+/,'_').downcase]
  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



42
43
44
# File 'lib/wikicloth/wiki_buffer/var.rb', line 42

def function_name
  @fname
end

#is_function?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/wikicloth/wiki_buffer/var.rb', line 190

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

#is_param?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/wikicloth/wiki_buffer/var.rb', line 186

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

#skip_html?Boolean

Returns:

  • (Boolean)


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

def skip_html?
  false
end

#skip_links?Boolean

Returns:

  • (Boolean)


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

def skip_links?
  true
end

#tag_sizeObject



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

def tag_size
  @tag_size
end

#tag_size=(val) ⇒ Object



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

def tag_size=(val)
  @tag_size = val
end

#tag_startObject



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

def tag_start
  @tag_start
end

#tag_start=(val) ⇒ Object



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

def tag_start=(val)
  @tag_start = val
end

#to_sObject



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
76
77
78
79
80
# File 'lib/wikicloth/wiki_buffer/var.rb', line 46

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].downcase,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].downcase}=\"#{p[:value]}\""
        else
          count += 1
          "#{count}=\"#{p}\""
        end
      }.join(" ")
      self.data = ret.blank? ? "" : "<template __name=\"#{params[0]}\" #{tag_attr}>#{ret}</template>"
      ""
    end
  end
end