Added table of contents to wiki page.

This commit is contained in:
Benjamin Meichsner 2009-05-20 14:06:40 +02:00
parent d371839eba
commit 2660d86b89
6 changed files with 35 additions and 3 deletions

View file

@ -1,7 +1,7 @@
module PagesHelper module PagesHelper
def wikified_body(body) def wikified_body(body)
r = RedCloth.new(body) r = RedCloth.new(generate_anchors(body))
r.gsub!(/\[\[(.*?)(\|(.*?))?\]\]/) { wiki_link($1, $3) } r.gsub!(/\[\[(.*?)(\|(.*?))?\]\]/) { wiki_link($1, $3) }
sanitize r.to_html sanitize r.to_html
r.to_html r.to_html
@ -15,4 +15,25 @@ module PagesHelper
link_to((link_text || wiki_words), wiki_page_url(permalink), :class => "new_wiki_link") link_to((link_text || wiki_words), wiki_page_url(permalink), :class => "new_wiki_link")
end end
end end
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 end

View file

@ -1 +1,4 @@
#toc
%h2 Inhaltsverzeichnis
= generate_toc @page.body
= wikified_body @page.body = wikified_body @page.body

View file

@ -7,7 +7,7 @@
%ul %ul
- for version in @page.versions.reverse - for version in @page.versions.reverse
%li %li
= link_to format_datetime(version.updated_at), version_page_path(@page, :version => version.version) = link_to format_time(version.updated_at), version_page_path(@page, :version => version.version)
= "(#{User.find(version.updated_by).nick})" = "(#{User.find(version.updated_by).nick})"

View file

@ -444,3 +444,6 @@ a.new_wiki_link {
#preview { #preview {
border: 1px dotted grey; border: 1px dotted grey;
padding: 0 1em; } padding: 0 1em; }
#toc {
float: right; }

View file

@ -445,6 +445,9 @@ a.new_wiki_link {
border: 1px dotted grey; border: 1px dotted grey;
padding: 0 1em; } padding: 0 1em; }
#toc {
float: right; }
#logininfo, #header, #footer { #logininfo, #header, #footer {
display: none; } display: none; }

View file

@ -491,3 +491,5 @@ a.new_wiki_link
#preview #preview
border: 1px dotted grey border: 1px dotted grey
padding: 0 1em padding: 0 1em
#toc
float: right