Cleanup wiki plugin a bit

This commit is contained in:
wvengen 2015-01-19 01:48:03 +01:00
parent b42672c06c
commit be128243fc
4 changed files with 42 additions and 37 deletions

View File

@ -1,22 +0,0 @@
class Wikilink < WikiCloth::WikiLinkHandler
def link_attributes_for(page)
permalink = Page.permalink(page)
url_options = {:host => FoodsoftConfig[:host], :protocol => FoodsoftConfig[:protocol]}
url_options.merge!({:port => FoodsoftConfig[:port]}) if FoodsoftConfig[:port]
if Page.exists?(:permalink => permalink)
{ :href => url_for(:wiki_page_path, permalink: permalink, use_route: :wiki_page) }
else
{ href: url_for(:new_page_path, title: page, parent: params[:referer]), class: 'new_wiki_link' }
end
end
def section_link(section)
""
end
def url_for(path_name, options = {})
Rails.application.routes.url_helpers.send path_name, options.merge({foodcoop: FoodsoftConfig.scope})
end
end

View File

@ -6,13 +6,12 @@ module PagesHelper
end
def wikified_body(body, title = nil)
WikiCloth.new(:data => body+"\n",
:link_handler => Wikilink.new,
:params => {:referer => title})
.to_html(wikicloth_render_options)
.html_safe
FoodsoftWiki::WikiParser.new(data: body+"\n", params: {referer: title}).to_html.html_safe
rescue => e
"<span class='alert alert-error'>#{t('.wikicloth_exception', :msg => e)}</span>".html_safe # try the following with line breaks: === one === == two == = three =
# try the following with line breaks: === one === == two == = three =
content_tag :span, class: 'alert alert-error' do
I18n.t '.wikicloth_exception', :msg => e
end.html_safe
end
def link_to_wikipage(page, text = nil)
@ -49,10 +48,7 @@ module PagesHelper
end
unless toc.blank?
WikiCloth.new(:data => toc,
:link_handler => Wikilink.new)
.to_html(wikicloth_render_options)
.gsub(/<li>([^<>\n]*)/) do
FoodsoftWiki::WikiParser.new(data: toc).to_html.gsub(/<li>([^<>\n]*)/) do
name = $1
anchor = name.gsub(/\s/, '_').gsub(/[^a-zA-Z_]/, '')
"<li><a href='##{anchor}'>#{name.truncate(20)}</a>"
@ -74,9 +70,4 @@ module PagesHelper
all_pages_url({:format => 'rss', :token => token}.merge(options))
end
private
def wikicloth_render_options
{:locale => I18n.locale} # workaround for wikicloth 0.8.0 https://github.com/nricciar/wikicloth/pull/59
end
end

View File

@ -3,6 +3,7 @@ require 'acts_as_versioned'
require 'diffy'
require 'content_for_in_controllers'
require 'foodsoft_wiki/engine'
require 'foodsoft_wiki/wiki_parser'
module FoodsoftWiki
# Return whether the wiki is used or not.

View File

@ -0,0 +1,35 @@
module FoodsoftWiki
class WikiParser < WikiCloth::Parser
url_for do |page|
url_for page
end
link_attributes_for do |page|
permalink = Page.permalink(page)
if Page.exists?(:permalink => permalink)
{ href: url_for(:wiki_page_path, permalink: permalink) }
else
{ href: url_for(:new_page_path, title: page, parent: params[:referer]), class: 'new_wiki_link' }
end
end
section_link do |section|
""
end
def to_html(render_options = {})
# workaround for wikicloth 0.8.0 https://github.com/nricciar/wikicloth/pull/59
render_options[:locale] ||= I18n.locale
super(render_options)
end
private
def url_for(path_name, options={})
Rails.application.routes.url_helpers.send path_name, options.merge({foodcoop: FoodsoftConfig.scope})
end
end
end