2009-03-25 19:54:04 +01:00
|
|
|
module PagesHelper
|
|
|
|
|
|
|
|
def wikified_body(body)
|
2009-06-11 17:59:21 +02:00
|
|
|
body = wiki_header(body)
|
|
|
|
r = RedCloth.new(body)
|
2009-03-25 19:54:04 +01:00
|
|
|
r.gsub!(/\[\[(.*?)(\|(.*?))?\]\]/) { wiki_link($1, $3) }
|
|
|
|
sanitize r.to_html
|
|
|
|
r.to_html
|
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_link(wiki_words, link_text = nil)
|
|
|
|
permalink = wiki_words.downcase.gsub(' ', '-')
|
|
|
|
if Page.exists?(:permalink => permalink)
|
|
|
|
link_to((link_text || wiki_words), wiki_page_url(permalink))
|
|
|
|
else
|
|
|
|
link_to((link_text || wiki_words), wiki_page_url(permalink), :class => "new_wiki_link")
|
|
|
|
end
|
|
|
|
end
|
2009-05-20 14:06:40 +02:00
|
|
|
|
2009-06-11 17:59:21 +02:00
|
|
|
def wiki_header(body)
|
|
|
|
body.gsub(/^(={2,6})\s+(.*)\s+={2,6}$/) { "h#{$1.size}. #{$2}" }
|
|
|
|
end
|
|
|
|
|
2009-05-20 14:06:40 +02:00
|
|
|
def generate_toc(body)
|
|
|
|
toc = ""
|
|
|
|
body.gsub(/^\s*h([1-6])\.\s+(.*)/) do
|
|
|
|
number = $1
|
|
|
|
name = $2
|
|
|
|
header = name.downcase.gsub(' ', '-')
|
|
|
|
toc << '#' * number.to_i + ' "' + name + '":#' + header + "\n"
|
|
|
|
end
|
|
|
|
RedCloth.new(toc).to_html
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_anchors(body)
|
|
|
|
body.gsub(/^\s*h([1-6])\.\s+(.*)/) do
|
|
|
|
number = $1
|
|
|
|
name = $2
|
|
|
|
header = name.downcase.gsub(' ', '-')
|
|
|
|
"\nh#{number}. #{name}<a name ='#{header}'> </a>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|