From ee065e63719b0dd4f145440359c748999133e9e5 Mon Sep 17 00:00:00 2001 From: Yuki Sonoda (Yugui) Date: Sun, 12 Oct 2008 11:50:00 +0900 Subject: [PATCH] made wiki formatters pluggable. --- app/controllers/wiki_controller.rb | 9 +- app/helpers/application_helper.rb | 36 +++-- app/helpers/textile_helper.rb | 18 ++ app/views/layouts/base.rhtml | 1 + app/views/settings/_general.rhtml | 2 +- lib/redmine.rb | 5 + lib/redmine/plugin.rb | 10 + lib/redmine/wiki_formatting.rb | 203 ++------------------- lib/redmine/wiki_formatting/textile_formatter.rb | 181 +++++++++++++++++++ public/javascripts/jstoolbar/jstoolbar.js | 179 ------------------- public/javascripts/jstoolbar/textile.js | 200 +++++++++++++++++++++ 11 files changed, 465 insertions(+), 379 deletions(-) create mode 100644 app/helpers/textile_helper.rb create mode 100644 lib/redmine/wiki_formatting/textile_formatter.rb create mode 100644 public/javascripts/jstoolbar/textile.js diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 114010d..6d76485 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -63,7 +63,7 @@ class WikiController < ApplicationController @page.content = WikiContent.new(:page => @page) if @page.new_record? @content = @page.content_for_version(params[:version]) - @content.text = "h1. #{@page.pretty_title}" if @content.text.blank? + @content.text = initial_page_content(@page) if @content.text.blank? # don't keep previous comment @content.comments = nil if request.get? @@ -208,4 +208,11 @@ private def editable?(page = @page) page.editable_by?(User.current) end + + def initial_page_content(page) + helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting) + helper ||= ApplicationHelper::NullFormattingHelper + extend helper unless self.instance_of?(helper) + helper.instance_method(:initial_page_content).bind(self).call(page) + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0a4aab2..040981e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -17,10 +17,14 @@ require 'coderay' require 'coderay/helpers/file_type' +require 'forwardable' module ApplicationHelper include Redmine::WikiFormatting::Macros::Definitions + extend Forwardable + def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter + def current_role @current_role ||= User.current.role_for_project(@project) end @@ -257,9 +261,9 @@ module ApplicationHelper end end - text = (Setting.text_formatting == 'textile') ? - Redmine::WikiFormatting.to_html(text) { |macro, args| exec_macro(macro, obj, args) } : - simple_format(auto_link(h(text))) + text = (Setting.text_formatting == '0') ? + simple_format(auto_link(h(text))) : + Redmine::WikiFormatting.to_html(Setting.text_formatting, text) { |macro, args| exec_macro(macro, obj, args) } # different methods for formatting wiki links case options[:wiki_links] @@ -547,18 +551,6 @@ module ApplicationHelper end end - def wikitoolbar_for(field_id) - return '' unless Setting.text_formatting == 'textile' - - help_link = l(:setting_text_formatting) + ': ' + - link_to(l(:label_help), compute_public_path('wiki_syntax', 'help', 'html'), - :onclick => "window.open(\"#{ compute_public_path('wiki_syntax', 'help', 'html') }\", \"\", \"resizable=yes, location=no, width=300, height=640, menubar=no, status=no, scrollbars=yes\"); return false;") - - javascript_include_tag('jstoolbar/jstoolbar') + - javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language}") + - javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.setHelpLink('#{help_link}'); toolbar.draw();") - end - def content_for(name, content = nil, &block) @has_content ||= {} @has_content[name] = true @@ -568,4 +560,18 @@ module ApplicationHelper def has_content?(name) (@has_content && @has_content[name]) || false end + + private + def wiki_helper + helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting) + helper ||= NullFormattingHelper + extend helper + return self + end + + module NullFormattingHelper + def wikitoolbar_for(field_id); '' end + def heads_for_wiki_formatter; '' end + def initial_page_content(page); @page.pretty_title end + end end diff --git a/app/helpers/textile_helper.rb b/app/helpers/textile_helper.rb new file mode 100644 index 0000000..147d4b2 --- /dev/null +++ b/app/helpers/textile_helper.rb @@ -0,0 +1,18 @@ +module TextileHelper + def wikitoolbar_for(field_id) + help_link = l(:setting_text_formatting) + ': ' + + link_to(l(:label_help), compute_public_path('wiki_syntax', 'help', 'html'), + :onclick => "window.open(\"#{ compute_public_path('wiki_syntax', 'help', 'html') }\", \"\", \"resizable=yes, location=no, width=300, height=640, menubar=no, status=no, scrollbars=yes\"); return false;") + + javascript_include_tag('jstoolbar/jstoolbar') + + javascript_include_tag('jstoolbar/textile') + + javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language}") + + javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.setHelpLink('#{help_link}'); toolbar.draw();") + end + + def initial_page_content(page) + "h1. #{@page.pretty_title}" + end + + def heads_for_wiki_formatter; '' end +end diff --git a/app/views/layouts/base.rhtml b/app/views/layouts/base.rhtml index 86d23d6..27477cf 100644 --- a/app/views/layouts/base.rhtml +++ b/app/views/layouts/base.rhtml @@ -8,6 +8,7 @@ <%= stylesheet_link_tag 'application', :media => 'all' %> <%= javascript_include_tag :defaults %> <%= stylesheet_link_tag 'jstoolbar' %> +<%= heads_for_wiki_formatter %>