foodsoft/app/helpers/pages_helper.rb

45 lines
1.1 KiB
Ruby
Raw Normal View History

module PagesHelper
def wikified_body(body)
body = wiki_header(body)
r = RedCloth.new(body)
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
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
end