lib/wikicloth/wiki_buffer/var.rb in wikicloth-0.2.0 vs lib/wikicloth/wiki_buffer/var.rb in wikicloth-0.5.0
- old
+ new
@@ -1,5 +1,7 @@
+require 'expression_parser'
+
module WikiCloth
class WikiBuffer::Var < WikiBuffer
def initialize(data="",options={})
@@ -16,23 +18,89 @@
@fname
end
def to_s
if self.is_function?
-# ret = "#{buffer_type}"
-# ret += " function #{function_name}"
-# ret += "(#{params.inspect})"
-# ret += " [#{data}]"
- ret = @options[:link_handler].function(function_name, params.collect { |p| p.strip })
+ 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
else
ret = @options[:link_handler].include_resource("#{params[0]}".strip,params[1..-1])
+ # template params
+ ret = ret.to_s.gsub(/\{\{\{\s*([A-Za-z0-9]+)+(|\|+([^}]+))\s*\}\}\}/) { |match| get_param($1.strip,$3.to_s.strip) }
+ # put template at beginning of buffer
+ self.data = ret
+ ""
end
-# ret ||= "<!-- TEMPLATE[#{params[0]}] NOT FOUND -->"
- ret ||= ""
- ret
end
+ def get_param(name,default=nil)
+ ret = nil
+ # numbered params
+ if name =~ /^[0-9]+$/
+ ret = self.params[name.to_i].instance_of?(Hash) ? self.params[name.to_i][:value] : self.params[name.to_i]
+ end
+ # named params
+ self.params.each do |param|
+ ret = param[:value] if param[:name] == name
+ end
+ ret.nil? ? default : ret
+ end
+
+ def default_functions(name,params)
+ case name
+ when "#if"
+ params.first.blank? ? params[2] : params[1]
+ when "#switch"
+ params.length.times do |i|
+ temp = params[i].split("=")
+ return temp[1].strip if temp[0].strip == params[0] && i != 0
+ end
+ return ""
+ 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]
+ end
+ end
+
def is_function?
self.function_name.nil? || self.function_name.blank? ? false : true
end
protected
@@ -52,10 +120,10 @@
self.function_name = self.data
self.data = ""
# Dealing with variable names within functions
# and variables
- when current_char == '=' && @in_quotes == false
+ when current_char == '=' && @in_quotes == false && !is_function?
self.current_param = self.data
self.data = ""
self.name_current_param()
# End of a template, variable, or function