Change wiki-parser again. Now wikitext is used.

This commit is contained in:
Benjamin Meichsner 2009-06-11 22:40:56 +02:00
parent eddf20b063
commit 434fc4b2ff
6 changed files with 43 additions and 30 deletions

View file

@ -20,7 +20,7 @@ class PagesController < ApplicationController
def new
@page = Page.new
@page.title = params[:title].capitalize.gsub("-", " ") if params[:title]
@page.title = params[:title].gsub("_", " ") if params[:title]
respond_to do |format|
format.html # new.html.erb

View file

@ -1,15 +1,7 @@
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(' ', '-')
permalink = Page.permalink(wiki_words)
if Page.exists?(:permalink => permalink)
link_to((link_text || wiki_words), wiki_page_url(permalink))
else
@ -17,27 +9,43 @@ module PagesHelper
end
end
def build_internal_links(body)
body.gsub(/\[\[(.*?)(\|(.*?))?\]\]/) { wiki_link($1, $3) }
end
def build_anchors(body)
body.gsub(/(<h\d{1}>(.+)<\/h\d{1}>)/) do
header = $1
token = $2.downcase.gsub(' ', '-')
"<a name='#{token}'> </a>#{header}"
end
end
def wikified_body(body)
body = Wikitext::Parser.new.parse body
build_anchors(body)
end
def wiki_header(body)
body.gsub(/^(={2,6})\s+(.*)\s+={2,6}$/) { "h#{$1.size}. #{$2}" }
body.gsub(/^(={1,6})\s*(.*)\s*={1,6}$/) { "h#{$1.size}. #{$2}" }
end
def generate_toc(body)
toc = ""
body.gsub(/^\s*h([1-6])\.\s+(.*)/) do
number = $1
body.gsub(/<h(\d{1})>(.+)<\/h\d{1}>/) do
number = $1.to_i - 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>"
toc << "#" * number + " #{name}\n"
end
logger.debug("TOC: #{toc}")
toc = Wikitext::Parser.new.parse toc
toc.gsub(/<li>([^<>\n]*)/) do
name = $1
token = name.downcase.gsub(' ', '-')
"<li><a href='##{token}'>#{name}</a>"
end
end

View file

@ -25,9 +25,13 @@ class Page < ActiveRecord::Base
before_validation_on_create :set_permalink
def self.permalink(title)
Wikitext::Parser.new.parse "[[#{title}]]"
end
def set_permalink
if self.permalink.blank?
self.permalink = Page.count == 0 ? "home" : "#{title.downcase.strip.gsub(/ |\.|@/, '-')}"
unless self.permalink.blank?
self.permalink = Page.count == 0 ? "Home" : Page.permalink(title)
end
end
end

View file

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

View file

@ -68,7 +68,7 @@ Rails::Initializer.run do |config|
config.gem "fastercsv"
config.gem "prawn"
config.gem "haml", :version => '>=2.0.6'
config.gem 'RedCloth', :lib => 'redcloth'
config.gem "wikitext"
# The internationalization framework can be changed to have another default locale (standard is :en) or more load paths.
# All files from config/locales/*.rb,yml are added automatically.

View file

@ -1,6 +1,6 @@
ActionController::Routing::Routes.draw do |map|
map.resources :pages, :collection => { :all => :get }, :member => {:version => :get, :revert => :get}
map.wiki_page "/wiki/:permalink", :controller => 'pages', :action => 'show'
map.wiki_page "/wiki/:permalink", :controller => 'pages', :action => 'show', :permalink => /[\w\._\-\%\d]+/
map.wiki "/wiki", :controller => 'pages', :action => 'show', :permalink => 'home'
map.logout '/logout', :controller => 'login', :action => 'logout'