Changed wiki parser to wikicloth.

* Temporarly removing of TOC.
This commit is contained in:
Benjamin Meichsner 2009-08-11 14:30:35 +02:00
parent 55729e20f9
commit 5d9385cde4
39 changed files with 3474 additions and 64 deletions

View file

@ -0,0 +1,43 @@
module ExtendedString
def blank?
respond_to?(:empty?) ? empty? : !self
end
def to_slug
self.gsub(/\W+/, '-').gsub(/^-+/,'').gsub(/-+$/,'').downcase
end
def auto_link
url_check = Regexp.new( '(^|[\n ])([\w]+?://[\w]+[^ \"\r\n\t<]*)', Regexp::MULTILINE | Regexp::IGNORECASE )
www_check = Regexp.new( '(^|[\n ])((www)\.[^ \"\t\n\r<]*)', Regexp::MULTILINE | Regexp::IGNORECASE )
self.gsub!(url_check, '\1<a href="\2">\2</a>')
self.gsub!(www_check, '\1<a href="http://\2">\2</a>')
to_s
end
def dump()
ret = to_s
delete!(to_s)
ret
end
def smart_split(char)
ret = []
tmp = ""
inside = 0
to_s.each_char do |x|
if x == char && inside == 0
ret << tmp
tmp = ""
else
inside += 1 if x == "[" || x == "{" || x == "<"
inside -= 1 if x == "]" || x == "}" || x == ">"
tmp += x
end
end
ret << tmp unless tmp.empty?
ret
end
end

View file

@ -0,0 +1,279 @@
module WikiCloth
class WikiBuffer
def initialize(data="",options={})
@options = options
self.data = data
self.buffer_type = nil
@section_count = 0
@buffers ||= [ ]
@buffers << self
@list_data = []
end
def run_globals?
true
end
def skip_html?
false
end
def data
@data ||= ""
end
def params
@params ||= [ "" ]
end
def buffer_type
@buffer_type
end
def to_s
"<p>" + self.params.join("\n") + "</p>"
end
def check_globals()
return false if self.class != WikiBuffer
if previous_char == "\n"
if @indent == @buffers[-1].object_id && current_char != " "
# close pre tag
cc_temp = current_char
"</pre>".each_char { |c| self.add_char(c) }
# get the parser back on the right track
"\n#{cc_temp}".each_char { |c| @buffers[-1].add_char(c) }
@indent = nil
return true
end
if current_char == " " && @indent.nil? && @buffers[-1].class != WikiBuffer::HTMLElement
"<pre> ".each_char { |c| @buffers[-1].add_char(c) }
@indent = @buffers[-1].object_id
return true
end
end
if @buffers[-1].run_globals?
# new html tag
if @check_new_tag == true && current_char =~ /([a-z])/ && !@buffers[-1].skip_html?
@buffers[-1].data.chop!
parent = @buffers[-1].element_name if @buffers[-1].class == WikiBuffer::HTMLElement
@buffers << WikiBuffer::HTMLElement.new("",@options,parent)
end
@check_new_tag = current_char == '<' ? true : false
# global
case
# start variable
when previous_char == '{' && current_char == '{'
@buffers[-1].data.chop!
@buffers << WikiBuffer::Var.new("",@options)
return true
# start link
when current_char == '[' && previous_char != '['
@buffers << WikiBuffer::Link.new("",@options)
return true
# start table
when previous_char == '{' && current_char == "|"
@buffers[-1].data.chop!
@buffers << WikiBuffer::Table.new("",@options)
return true
end
end
return false
end
def add_char(c)
self.previous_char = self.current_char
self.current_char = c
if self.check_globals() == false
case
when @buffers.size == 1
return self.new_char()
when @buffers[-1].add_char(c) == false && self.class == WikiBuffer
tmp = @buffers.pop
@buffers[-1].data += tmp.to_s
# any data left in the buffer we feed into the parent
unless tmp.data.blank?
tmp.data.each_char { |c| self.add_char(c) }
end
end
end
end
protected
# only executed in the default state
def new_char()
case
when current_char == "\n"
if @options[:extended_markup] == true
self.data.gsub!(/---([^-]+)---/,"<strike>\\1</strike>")
self.data.gsub!(/_([^_]+)_/,"<u>\\1</u>")
end
self.data.gsub!(/__([a-zA-Z0-9]+)__/) { |r|
case $1
when "NOEDITSECTION"
@noeditsection = true
end
""
}
self.data.gsub!(/^([-]{4,})/) { |r| "<hr />" }
self.data.gsub!(/^([=]{1,6})\s*(.*?)\s*(\1)/) { |r|
@section_count += 1
"<h#{$1.length}>" + (@noeditsection == true ? "" :
"<span class=\"editsection\">[<a href=\"" + @options[:link_handler].section_link(@section_count) +
"\" title=\"Edit section: #{$2}\">edit</a>]</span>") +
" <span class=\"mw-headline\">#{$2}</span></h#{$1.length}>"
}
self.data.gsub!(/([\']{2,5})(.*?)(\1)/) { |r|
tmp = "<i>#{$2}</i>" if $1.length == 2
tmp = "<b>#{$2}</b>" if $1.length == 3
tmp = "<b>'#{$2}'</b>" if $1.length == 4
tmp = "<b><i>#{$2}</i></b>" if $1.length == 5
tmp
}
lines = self.data.split("\n")
self.data = ""
for line in lines
if !@list_data.empty? && (line.blank? || line =~ /^([^#\*:;]+)/)
tmp = ""
@list_data.reverse!
@list_data.each { |x| tmp += "</" + list_inner_tag_for(x) + "></#{list_tag_for(x)}>" }
line = "#{tmp} #{line}"
@list_data = []
end
line.gsub!(/^([#\*:;]+)(.*)$/) { |r|
cdata = []
tmp = ""
$1.each_char { |c| cdata << c }
if @list_data.empty?
tmp += "<#{list_tag_for(cdata[0])}>"
cdata[1..-1].each { |x| tmp += "<" + list_inner_tag_for(cdata[0]) + "><#{list_tag_for(x)}>" } if cdata.size > 1
else
case
when cdata.size > @list_data.size
i = cdata.size-@list_data.size
cdata[-i,i].each { |x| tmp += "<#{list_tag_for(x)}>" }
when cdata.size < @list_data.size
i = @list_data.size-cdata.size
nlist = @list_data[-i,i].reverse
nlist.each { |x| tmp += "</" + list_inner_tag_for(x) + "></#{list_tag_for(x)}>" }
tmp += "</#{list_inner_tag_for(cdata.last)}>"
else
if cdata != @list_data
# FIXME: this will only work if the change depth is one level
unless (@list_data.last == ';' || @list_data.last == ':') && (cdata.last == ';' || cdata.last == ':')
tmp += "</#{list_tag_for(@list_data.pop)}>"
tmp += "<#{list_tag_for(cdata.last)}>"
end
else
tmp += "</" + list_inner_tag_for(@list_data.last) + ">"
end
end
end
# FIXME: still probably does not detect the : properly
peices = cdata.last == ";" ? $2.smart_split(":") : [ $2 ]
if peices.size > 1
tmp += "<#{list_inner_tag_for(cdata.last)}>#{peices[0]}</#{list_inner_tag_for(cdata.last)}>"
tmp += "<dd>#{peices[1..-1].join(":")}</dd>"
cdata[-1] = ":"
else
tmp += "<#{list_inner_tag_for(cdata.last)}>#{peices[0]}"
end
@list_data = cdata
tmp
}
self.data += line + "\n"
end
self.data = "</p><p>" if self.data.blank?
self.params << self.data.auto_link
self.data = ""
else
self.data += current_char
end
return true
end
def name_current_param()
params[-1] = { :value => "", :name => params[-1] } unless params[-1].kind_of?(Hash) || params[-1].nil?
end
def current_param=(val)
unless self.params[-1].nil? || self.params[-1].kind_of?(String)
self.params[-1][:value] = val
else
self.params[-1] = val
end
end
def params=(val)
@params = val
end
def buffer_type=(val)
@buffer_type = val
end
def data=(val)
@data = val
end
def current_char=(val)
@current_char = val
end
def current_char
@current_char ||= ""
end
def previous_char=(val)
@previous_char = val
end
def previous_char
@previous_char
end
def current_line=(val)
@current_line = val
end
def current_line
@current_line ||= ""
end
def list_tag_for(tag)
case tag
when "#" then "ol"
when "*" then "ul"
when ";" then "dl"
when ":" then "dl"
end
end
def list_inner_tag_for(tag)
case tag
when "#" then "li"
when "*" then "li"
when ";" then "dt"
when ":" then "dd"
end
end
end
end
require File.join(File.expand_path(File.dirname(__FILE__)), "wiki_buffer", "html_element")
require File.join(File.expand_path(File.dirname(__FILE__)), "wiki_buffer", "table")
require File.join(File.expand_path(File.dirname(__FILE__)), "wiki_buffer", "var")
require File.join(File.expand_path(File.dirname(__FILE__)), "wiki_buffer", "link")

View file

@ -0,0 +1,237 @@
require 'rubygems'
require 'builder'
module WikiCloth
class WikiBuffer::HTMLElement < WikiBuffer
ALLOWED_ELEMENTS = ['a','b','i','div','span','sup','sub','strike','s','u','font','big','ref','tt','del',
'small','blockquote','strong','pre','code','references','ol','li','ul','dd','dt','dl','center',
'h2','h3','h4','h5','h6']
ALLOWED_ATTRIBUTES = ['id','name','style','class','href','start','value']
ESCAPED_TAGS = [ 'nowiki', 'pre', 'code' ]
SHORT_TAGS = [ 'meta','br','hr','img' ]
NO_NEED_TO_CLOSE = ['li','p'] + SHORT_TAGS
def initialize(d="",options={},check=nil)
super("",options)
self.buffer_type = "Element"
@in_quotes = false
@in_single_quotes = false
@start_tag = 1
@tag_check = check unless check.nil?
end
def run_globals?
return ESCAPED_TAGS.include?(self.element_name) ? false : true
end
def to_s
if NO_NEED_TO_CLOSE.include?(self.element_name)
return "<#{self.element_name} />" if SHORT_TAGS.include?(self.element_name)
return "</#{self.element_name}><#{self.element_name}>" if @tag_check == self.element_name
end
if ESCAPED_TAGS.include?(self.element_name)
# escape all html inside this element
self.element_content = self.element_content.gsub('<','&lt;').gsub('>','&gt;')
# hack to fix <code><nowiki> nested mess
self.element_content = self.element_content.gsub(/&lt;[\/]*\s*nowiki\s*&gt;/,'')
end
lhandler = @options[:link_handler]
case self.element_name
when "ref"
self.element_name = "sup"
named_ref = self.name_attribute
ref = lhandler.find_reference_by_name(named_ref) unless named_ref.nil?
if ref.nil?
lhandler.references << { :name => named_ref, :value => self.element_content, :count => 0 }
ref = lhandler.references.last
end
ref_id = (named_ref.nil? ? "" : "#{named_ref}_") + "#{lhandler.reference_index(ref)}-#{ref[:count]}"
self.params << { :name => "id", :value => "cite_ref-#{ref_id}" }
self.params << { :name => "class", :value => "reference" }
self.element_content = "[<a href=\"#cite_note-" + (named_ref.nil? ? "" : "#{named_ref}_") +
"#{lhandler.reference_index(ref)}\">#{lhandler.reference_index(ref)}</a>]"
ref[:count] += 1
when "references"
ref_count = 0
self.element_name = "ol"
self.element_content = lhandler.references.collect { |r|
ref_count += 1
ref_name = (r[:name].nil? ? "" : r[:name].to_slug + "_")
ret = "<li id=\"cite_note-#{ref_name}#{ref_count}\"><b>"
1.upto(r[:count]) { |x| ret += "<a href=\"#cite_ref-#{ref_name}#{ref_count}-#{x-1}\">" +
(r[:count] == 1 ? "^" : (x-1).to_s(26).tr('0-9a-p', 'a-z')) + "</a> " }
ret += "</b> #{r[:value]}</li>"
}.to_s
when "nowiki"
return self.element_content
end
tmp = elem.tag!(self.element_name, self.element_attributes) { |x| x << self.element_content }
unless ALLOWED_ELEMENTS.include?(self.element_name)
tmp.gsub!(/[\-!\|&"\{\}\[\]]/) { |r| self.escape_char(r) }
return tmp.gsub('<', '&lt;').gsub('>', '&gt;')
end
tmp
end
def name_attribute
params.each { |p| return p[:value].to_slug if p.kind_of?(Hash) && p[:name] == "name" }
return nil
end
def element_attributes
attr = {}
params.each { |p| attr[p[:name]] = p[:value] if p.kind_of?(Hash) }
if ALLOWED_ELEMENTS.include?(self.element_name.strip.downcase)
attr.delete_if { |key,value| !ALLOWED_ATTRIBUTES.include?(key.strip) }
end
return attr
end
def element_name
@ename ||= ""
end
def element_content
@econtent ||= ""
end
protected
def escape_char(c)
c = case c
when '-' then '&#45;'
when '!' then '&#33;'
when '|' then '&#124;'
when '&' then '&amp;'
when '"' then '&quot;'
when '{' then '&#123;'
when '}' then '&#125;'
when '[' then '&#91;'
when ']' then '&#93;'
when '*' then '&#42;'
when '#' then '&#35;'
when ':' then '&#58;'
when ';' then '&#59;'
when "'" then '&#39;'
when '=' then '&#61;'
else
c
end
return c
end
def elem
Builder::XmlMarkup.new
end
def element_name=(val)
@ename = val
end
def element_content=(val)
@econtent = val
end
def in_quotes?
@in_quotes || @in_single_quotes ? true : false
end
def new_char()
case
# tag name
when @start_tag == 1 && current_char == ' '
self.element_name = self.data.strip.downcase
self.data = ""
@start_tag = 2
# tag is closed <tag/> no attributes
when @start_tag == 1 && previous_char == '/' && current_char == '>'
self.data.chop!
self.element_name = self.data.strip.downcase
self.data = ""
@start_tag = 0
return false
# open tag
when @start_tag == 1 && previous_char != '/' && current_char == '>'
self.element_name = self.data.strip.downcase
self.data = ""
@start_tag = 0
return false if SHORT_TAGS.include?(self.element_name)
return false if self.element_name == @tag_check && NO_NEED_TO_CLOSE.include?(self.element_name)
# new tag attr
when @start_tag == 2 && current_char == ' ' && self.in_quotes? == false
self.current_param = self.data
self.data = ""
self.params << ""
# tag attribute name
when @start_tag == 2 && current_char == '=' && self.in_quotes? == false
self.current_param = self.data
self.data = ""
self.name_current_param()
# tag is now open
when @start_tag == 2 && previous_char != '/' && current_char == '>'
self.current_param = self.data
self.data = ""
@start_tag = 0
return false if SHORT_TAGS.include?(self.element_name)
return false if self.element_name == @tag_check && NO_NEED_TO_CLOSE.include?(self.element_name)
# tag is closed <example/>
when @start_tag == 2 && previous_char == '/' && current_char == '>'
self.current_param = self.data.chop
self.data = ""
@start_tag = 0
return false
# in quotes
when @start_tag == 2 && current_char == "'" && previous_char != '\\' && !@in_quotes
@in_single_quotes = !@in_single_quotes
# in quotes
when @start_tag == 2 && current_char == '"' && previous_char != '\\' && !@in_single_quotes
@in_quotes = !@in_quotes
# start of a closing tag
when @start_tag == 0 && previous_char == '<' && current_char == '/'
self.element_content += self.data.chop
self.data = ""
@start_tag = 5
when @start_tag == 5 && (current_char == '>' || current_char == ' ') && !self.data.blank?
self.data = self.data.strip.downcase
if self.data == self.element_name
self.data = ""
return false
else
if @tag_check == self.data && NO_NEED_TO_CLOSE.include?(self.element_name)
self.data = "</#{self.data}>"
return false
else
self.element_content += "&lt;/#{self.data}&gt;"
@start_tag = 0
self.data = ""
end
end
else
if @start_tag == 0 && ESCAPED_TAGS.include?(self.element_name)
self.data += self.escape_char(current_char)
else
self.data += current_char
end
end
return true
end
end
end

View file

@ -0,0 +1,70 @@
module WikiCloth
class WikiBuffer::Link < WikiBuffer
def initialize(data="",options={})
super(data,options)
@in_quotes = false
end
def internal_link
@internal_link ||= false
end
def to_s
link_handler = @options[:link_handler]
unless self.internal_link
return link_handler.external_link("#{params[0]}".strip, "#{params[1]}".strip)
else
case
when params[0] =~ /^:(.*)/
return link_handler.link_for(params[0],params[1])
when params[0] =~ /^\s*([a-zA-Z0-9-]+)\s*:(.*)$/
return link_handler.link_for_resource($1,$2,params[1..-1])
else
return link_handler.link_for(params[0],params[1])
end
end
end
protected
def internal_link=(val)
@internal_link = (val == true ? true : false)
end
def new_char()
case
# check if this link is internal or external
when previous_char.blank? && current_char == '['
self.internal_link = true
# Marks the beginning of another paramater for
# the current object
when current_char == '|' && self.internal_link == true && @in_quotes == false
self.current_param = self.data
self.data = ""
self.params << ""
# URL label
when current_char == ' ' && self.internal_link == false && params[1].nil? && !self.data.blank?
self.current_param = self.data
self.data = ""
self.params << ""
# end of link
when current_char == ']' && ((previous_char == ']' && self.internal_link == true) || self.internal_link == false) && @in_quotes == false
self.data.chop! if self.internal_link == true
self.current_param = self.data
self.data = ""
return false
else
self.data += current_char unless current_char == ' ' && self.data.blank?
end
return true
end
end
end

View file

@ -0,0 +1,159 @@
module WikiCloth
class WikiBuffer::Table < WikiBuffer
def initialize(data="",options={})
super(data,options)
self.buffer_type = "table"
@start_table = true
@start_row = false
@start_caption = false
@in_quotes = false
end
def table_caption
@caption ||= ""
return @caption.kind_of?(Hash) ? @caption[:value] : @caption
end
def table_caption_attributes
@caption.kind_of?(Hash) ? @caption[:style] : ""
end
def rows
@rows ||= [ [] ]
end
def to_s
row_count = 0
ret = "<table" + (params[0].blank? ? "" : " #{params[0].strip}") + ">"
ret += "<caption" + (self.table_caption_attributes.blank? ? "" : " #{table_caption_attributes.strip}") +
">#{table_caption.strip}</caption>" unless self.table_caption.blank?
for row in rows
row_count += 1
ret += "<tr" + (params[row_count].nil? || params[row_count].blank? ? "" : " #{params[row_count].strip}") + ">"
for cell in row
cell_attributes = cell[:style].blank? ? "" : " #{cell[:style].strip}"
ret += "<#{cell[:type]}#{cell_attributes}>\n#{cell[:value].strip}\n</#{cell[:type]}>"
end
ret += "</tr>"
end
ret += "</table>"
end
protected
def rows=(val)
@rows = val
end
def table_caption_attributes=(val)
@caption = { :style => val, :value => self.table_caption } unless @caption.kind_of?(Hash)
@caption[:style] = val if @caption.kind_of?(Hash)
end
def table_caption=(val)
@caption = val unless @caption.kind_of?(Hash)
@caption[:value] = val if @caption.kind_of?(Hash)
end
def next_row()
self.params << ""
self.rows << []
end
def next_cell(type="td")
if self.rows[-1].size == 0
self.rows[-1] = [ { :type => type, :value => "", :style => "" } ]
else
self.rows[-1][-1][:value] = self.data
self.rows[-1] << { :type => type, :value => "", :style => "" }
end
end
def new_char()
if @check_cell_data == 1
case
when current_char != '|' && @start_caption == false && self.rows[-1][-1][:style].blank?
self.rows[-1][-1][:style] = self.data
self.data = ""
when current_char != '|' && @start_caption == true && self.table_caption_attributes.blank?
self.table_caption_attributes = self.data
self.data = ""
end
@check_cell_data = 0
end
case
# Next table cell in row (TD)
when current_char == "|" && (previous_char == "\n" || previous_char == "|") && @in_quotes == false
self.data.chop!
self.next_cell() unless self.data.blank? && previous_char == "|"
self.data = ""
# Next table cell in row (TH)
when current_char == "!" && (previous_char == "\n" || previous_char == "!") && @in_quotes == false
self.data.chop!
self.next_cell('th')
self.data = ""
# End of a table
when current_char == '}' && previous_char == '|'
self.data = ""
self.rows[-1].pop
return false
# Start table caption
when current_char == '+' && previous_char == '|' && @in_quotes == false
self.data = ""
self.rows[-1].pop
@start_caption = true
# Table cell might have attributes
when current_char == '|' && previous_char != "\n" && @in_quotes == false
@check_cell_data = 1
# End table caption
when current_char == "\n" && @start_caption == true && @in_quotes == false
@start_caption = false
self.table_caption = self.data
self.data = ""
# in quotes
when current_char == '"' && previous_char != '\\'
@in_quotes = !@in_quotes
self.data += '"'
# Table params
when current_char == "\n" && @start_table == true && @in_quotes == false
@start_table = false
unless self.data.blank?
self.current_param = self.data
self.params << ""
end
self.data = ""
# Table row params
when current_char == "\n" && @start_row == true && @in_quotes == false
@start_row = false
unless self.data.blank?
self.current_param = self.data
end
self.data = ""
# Start new table row
when current_char == '-' && previous_char == '|' && @in_quotes == false
self.data.chop!
self.rows[-1].pop
self.next_row()
@start_row = true
else
self.data += current_char
end
return true
end
end
end

View file

@ -0,0 +1,77 @@
module WikiCloth
class WikiBuffer::Var < WikiBuffer
def initialize(data="",options={})
super(data,options)
self.buffer_type = "var"
@in_quotes = false
end
def skip_html?
true
end
def function_name
@fname
end
def to_s
if self.is_function?
ret = "#{buffer_type}"
ret += " function #{function_name}"
ret += "(#{params.inspect})"
ret += " [#{data}]"
else
ret = @options[:link_handler].include_resource("#{params[0]}".strip,params[1..-1])
end
ret ||= "<!-- TEMPLATE[#{params[0]}] NOT FOUND -->"
ret
end
def is_function?
self.function_name.nil? || self.function_name.blank? ? false : true
end
protected
def function_name=(val)
@fname = val
end
def new_char()
case
when current_char == '|' && @in_quotes == false
self.current_param = self.data
self.data = ""
self.params << ""
# Start of either a function or a namespace change
when current_char == ':' && @in_quotes == false && self.params.size <= 1
self.function_name = self.data
self.data = ""
puts "[found var function (#{function_name})"
# Dealing with variable names within functions
# and variables
when current_char == '=' && @in_quotes == false
self.current_param = self.data
self.data = ""
self.name_current_param()
# End of a template, variable, or function
when current_char == '}' && previous_char == '}'
self.data.chop!
self.current_param = self.data
self.data = ""
return false
else
self.data += current_char
end
return true
end
end
end

View file

@ -0,0 +1,61 @@
require 'jcode'
module WikiCloth
class WikiCloth
def initialize(opt={})
self.load(opt[:data],opt[:params]) unless opt[:data].nil? || opt[:data].blank?
self.options[:link_handler] = opt[:link_handler] unless opt[:link_handler].nil?
end
def load(data,p={})
data.gsub!(/<!--(.|\s)*?-->/,"")
self.params = p
self.html = data
end
def render(opt={})
self.options = { :output => :html, :link_handler => self.link_handler, :params => self.params }.merge(opt)
self.options[:link_handler].params = options[:params]
buffer = WikiBuffer.new("",options)
self.html.each_char { |c| buffer.add_char(c) }
buffer.to_s
end
def to_html(opt={})
self.render(opt)
end
def link_handler
self.options[:link_handler] ||= WikiLinkHandler.new
end
def html
@page_data
end
def params
@page_params ||= {}
end
protected
def options=(val)
@options = val
end
def options
@options ||= {}
end
def html=(val)
@page_data = val
end
def params=(val)
@page_params = val
end
end
end

View file

@ -0,0 +1,138 @@
require 'rubygems'
require 'builder'
module WikiCloth
class WikiLinkHandler
def references
@references ||= []
end
def section_link(section)
""
end
def params
@params ||= {}
end
def external_links
@external_links ||= []
end
def find_reference_by_name(n)
references.each { |r| return r if !r[:name].nil? && r[:name].strip == n }
return nil
end
def reference_index(h)
references.each_index { |r| return r+1 if references[r] == h }
return nil
end
def references=(val)
@references = val
end
def params=(val)
@params = val
end
def external_link(url,text)
self.external_links << url
elem.a({ :href => url }) { |x| x << (text.blank? ? url : text) }
end
def external_links=(val)
@external_links = val
end
def url_for(page)
"javascript:void(0)"
end
def link_attributes_for(page)
{ :href => url_for(page) }
end
def link_for(page, text)
ltitle = !text.nil? && text.blank? ? self.pipe_trick(page) : text
ltitle = page if text.nil?
elem.a(link_attributes_for(page)) { |x| x << ltitle.strip }
end
def include_resource(resource, options=[])
return self.params[resource] unless self.params[resource].nil?
end
def link_for_resource(prefix, resource, options=[])
ret = ""
prefix.downcase!
case
when ["image","file","media"].include?(prefix)
ret += wiki_image(resource,options)
else
title = options[0] ? options[0] : "#{prefix}:#{resource}"
ret += link_for("#{prefix}:#{resource}",title)
end
ret
end
protected
def pipe_trick(page)
t = page.split(":")
t = t[1..-1] if t.size > 1
return t.join("").split(/[,(]/)[0]
end
# this code needs some work... lots of work
def wiki_image(resource,options)
pre_img = ''
post_img = ''
css = []
loc = "right"
type = nil
w = 180
h = nil
title = nil
ffloat = false
options.each do |x|
case
when ["thumb","thumbnail","frame","border"].include?(x.strip)
type = x.strip
when ["left","right","center","none"].include?(x.strip)
ffloat = true
loc = x.strip
when x.strip =~ /^([0-9]+)\s*px$/
w = $1
css << "width:#{w}px"
when x.strip =~ /^([0-9]+)\s*x\s*([0-9]+)\s*px$/
w = $1
css << "width:#{w}px"
h = $2
css << "height:#{h}px"
else
title = x.strip
end
end
css << "float:#{loc}" if ffloat == true
css << "border:1px solid #000" if type == "border"
sane_title = title.nil? ? "" : title.gsub(/<\/?[^>]*>/, "")
if type == "thumb" || type == "thumbnail" || type == "frame"
pre_img = '<div class="thumb t' + loc + '"><div class="thumbinner" style="width: ' + w.to_s +
'px;"><a href="" class="image" title="' + sane_title + '">'
post_img = '</a><div class="thumbcaption">' + title + '</div></div></div>'
end
"#{pre_img}<img src=\"#{resource}\" alt=\"#{sane_title}\" title=\"#{sane_title}\" style=\"#{css.join(";")}\" />#{post_img}"
end
def elem
Builder::XmlMarkup.new
end
end
end