Change wiki-parser again. Now wikitext is used.
This commit is contained in:
parent
eddf20b063
commit
434fc4b2ff
6 changed files with 43 additions and 30 deletions
|
@ -20,7 +20,7 @@ class PagesController < ApplicationController
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@page = Page.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|
|
respond_to do |format|
|
||||||
format.html # new.html.erb
|
format.html # new.html.erb
|
||||||
|
|
|
@ -1,15 +1,7 @@
|
||||||
module PagesHelper
|
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)
|
def wiki_link(wiki_words, link_text = nil)
|
||||||
permalink = wiki_words.downcase.gsub(' ', '-')
|
permalink = Page.permalink(wiki_words)
|
||||||
if Page.exists?(:permalink => permalink)
|
if Page.exists?(:permalink => permalink)
|
||||||
link_to((link_text || wiki_words), wiki_page_url(permalink))
|
link_to((link_text || wiki_words), wiki_page_url(permalink))
|
||||||
else
|
else
|
||||||
|
@ -17,27 +9,43 @@ module PagesHelper
|
||||||
end
|
end
|
||||||
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)
|
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
|
end
|
||||||
|
|
||||||
def generate_toc(body)
|
def generate_toc(body)
|
||||||
toc = ""
|
toc = ""
|
||||||
body.gsub(/^\s*h([1-6])\.\s+(.*)/) do
|
body.gsub(/<h(\d{1})>(.+)<\/h\d{1}>/) do
|
||||||
number = $1
|
number = $1.to_i - 1
|
||||||
name = $2
|
name = $2
|
||||||
header = name.downcase.gsub(' ', '-')
|
|
||||||
toc << '#' * number.to_i + ' "' + name + '":#' + header + "\n"
|
|
||||||
end
|
|
||||||
RedCloth.new(toc).to_html
|
|
||||||
end
|
|
||||||
|
|
||||||
def generate_anchors(body)
|
toc << "#" * number + " #{name}\n"
|
||||||
body.gsub(/^\s*h([1-6])\.\s+(.*)/) do
|
end
|
||||||
number = $1
|
logger.debug("TOC: #{toc}")
|
||||||
name = $2
|
toc = Wikitext::Parser.new.parse toc
|
||||||
header = name.downcase.gsub(' ', '-')
|
|
||||||
"\nh#{number}. #{name}<a name ='#{header}'> </a>"
|
toc.gsub(/<li>([^<>\n]*)/) do
|
||||||
|
name = $1
|
||||||
|
token = name.downcase.gsub(' ', '-')
|
||||||
|
|
||||||
|
"<li><a href='##{token}'>#{name}</a>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -25,9 +25,13 @@ class Page < ActiveRecord::Base
|
||||||
|
|
||||||
before_validation_on_create :set_permalink
|
before_validation_on_create :set_permalink
|
||||||
|
|
||||||
|
def self.permalink(title)
|
||||||
|
Wikitext::Parser.new.parse "[[#{title}]]"
|
||||||
|
end
|
||||||
|
|
||||||
def set_permalink
|
def set_permalink
|
||||||
if self.permalink.blank?
|
unless self.permalink.blank?
|
||||||
self.permalink = Page.count == 0 ? "home" : "#{title.downcase.strip.gsub(/ |\.|@/, '-')}"
|
self.permalink = Page.count == 0 ? "Home" : Page.permalink(title)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
|
- content = wikified_body @page.body
|
||||||
#toc
|
#toc
|
||||||
%h2 Inhaltsverzeichnis
|
%h2 Inhaltsverzeichnis
|
||||||
= generate_toc @page.body
|
= generate_toc content
|
||||||
= wikified_body @page.body
|
#wiki_content= content
|
|
@ -68,7 +68,7 @@ Rails::Initializer.run do |config|
|
||||||
config.gem "fastercsv"
|
config.gem "fastercsv"
|
||||||
config.gem "prawn"
|
config.gem "prawn"
|
||||||
config.gem "haml", :version => '>=2.0.6'
|
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.
|
# 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.
|
# All files from config/locales/*.rb,yml are added automatically.
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
ActionController::Routing::Routes.draw do |map|
|
ActionController::Routing::Routes.draw do |map|
|
||||||
map.resources :pages, :collection => { :all => :get }, :member => {:version => :get, :revert => :get}
|
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.wiki "/wiki", :controller => 'pages', :action => 'show', :permalink => 'home'
|
||||||
|
|
||||||
map.logout '/logout', :controller => 'login', :action => 'logout'
|
map.logout '/logout', :controller => 'login', :action => 'logout'
|
||||||
|
|
Loading…
Reference in a new issue