To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / lib / redmine / wiki_formatting.rb @ 912:5e80956cc792
History | View | Annotate | Download (3.55 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 909:cbb26bc654de | Chris | # Copyright (C) 2006-2011 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 4 | # This program is free software; you can redistribute it and/or
|
||
| 5 | # modify it under the terms of the GNU General Public License
|
||
| 6 | # as published by the Free Software Foundation; either version 2
|
||
| 7 | # of the License, or (at your option) any later version.
|
||
| 8 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # This program is distributed in the hope that it will be useful,
|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 12 | # GNU General Public License for more details.
|
||
| 13 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # You should have received a copy of the GNU General Public License
|
| 15 | # along with this program; if not, write to the Free Software
|
||
| 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
| 17 | |||
| 18 | module Redmine |
||
| 19 | module WikiFormatting |
||
| 20 | 909:cbb26bc654de | Chris | class StaleSectionError < Exception; end |
| 21 | |||
| 22 | 0:513646585e45 | Chris | @@formatters = {}
|
| 23 | |||
| 24 | class << self |
||
| 25 | def map |
||
| 26 | yield self |
||
| 27 | end
|
||
| 28 | 909:cbb26bc654de | Chris | |
| 29 | 0:513646585e45 | Chris | def register(name, formatter, helper) |
| 30 | raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name.to_s] |
||
| 31 | @@formatters[name.to_s] = {:formatter => formatter, :helper => helper} |
||
| 32 | end
|
||
| 33 | 909:cbb26bc654de | Chris | |
| 34 | def formatter |
||
| 35 | formatter_for(Setting.text_formatting)
|
||
| 36 | end
|
||
| 37 | |||
| 38 | 0:513646585e45 | Chris | def formatter_for(name) |
| 39 | entry = @@formatters[name.to_s]
|
||
| 40 | (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter |
||
| 41 | end
|
||
| 42 | 909:cbb26bc654de | Chris | |
| 43 | 0:513646585e45 | Chris | def helper_for(name) |
| 44 | entry = @@formatters[name.to_s]
|
||
| 45 | (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper |
||
| 46 | end
|
||
| 47 | 909:cbb26bc654de | Chris | |
| 48 | 0:513646585e45 | Chris | def format_names |
| 49 | @@formatters.keys.map
|
||
| 50 | end
|
||
| 51 | 909:cbb26bc654de | Chris | |
| 52 | def to_html(format, text, options = {}) |
||
| 53 | 0:513646585e45 | Chris | text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, options[:object], options[:attribute]) |
| 54 | # Text retrieved from the cache store may be frozen
|
||
| 55 | # We need to dup it so we can do in-place substitutions with gsub!
|
||
| 56 | cache_store.fetch cache_key do
|
||
| 57 | formatter_for(format).new(text).to_html |
||
| 58 | end.dup
|
||
| 59 | else
|
||
| 60 | formatter_for(format).new(text).to_html |
||
| 61 | end
|
||
| 62 | text |
||
| 63 | end
|
||
| 64 | |||
| 65 | 909:cbb26bc654de | Chris | # Returns true if the text formatter supports single section edit
|
| 66 | def supports_section_edit? |
||
| 67 | (formatter.instance_methods & ['update_section', :update_section]).any? |
||
| 68 | end
|
||
| 69 | |||
| 70 | 0:513646585e45 | Chris | # Returns a cache key for the given text +format+, +object+ and +attribute+ or nil if no caching should be done
|
| 71 | def cache_key_for(format, object, attribute) |
||
| 72 | if object && attribute && !object.new_record? && object.respond_to?(:updated_on) && !format.blank? |
||
| 73 | "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{object.updated_on.to_s(:number)}"
|
||
| 74 | end
|
||
| 75 | end
|
||
| 76 | 909:cbb26bc654de | Chris | |
| 77 | 0:513646585e45 | Chris | # Returns the cache store used to cache HTML output
|
| 78 | def cache_store |
||
| 79 | ActionController::Base.cache_store |
||
| 80 | end
|
||
| 81 | end
|
||
| 82 | 909:cbb26bc654de | Chris | |
| 83 | 0:513646585e45 | Chris | # Default formatter module
|
| 84 | module NullFormatter |
||
| 85 | class Formatter |
||
| 86 | include ActionView::Helpers::TagHelper |
||
| 87 | include ActionView::Helpers::TextHelper |
||
| 88 | include ActionView::Helpers::UrlHelper |
||
| 89 | 909:cbb26bc654de | Chris | |
| 90 | 0:513646585e45 | Chris | def initialize(text) |
| 91 | @text = text
|
||
| 92 | end
|
||
| 93 | 909:cbb26bc654de | Chris | |
| 94 | 0:513646585e45 | Chris | def to_html(*args) |
| 95 | simple_format(auto_link(CGI::escapeHTML(@text))) |
||
| 96 | end
|
||
| 97 | end
|
||
| 98 | 909:cbb26bc654de | Chris | |
| 99 | 0:513646585e45 | Chris | module Helper |
| 100 | def wikitoolbar_for(field_id) |
||
| 101 | end
|
||
| 102 | 909:cbb26bc654de | Chris | |
| 103 | 0:513646585e45 | Chris | def heads_for_wiki_formatter |
| 104 | end
|
||
| 105 | 909:cbb26bc654de | Chris | |
| 106 | 0:513646585e45 | Chris | def initial_page_content(page) |
| 107 | page.pretty_title.to_s |
||
| 108 | end
|
||
| 109 | end
|
||
| 110 | end
|
||
| 111 | end
|
||
| 112 | end |