Module: Redmine::WikiFormatting

Defined in:
lib/redmine/wiki_formatting.rb,
lib/redmine/wiki_formatting/macros.rb,
lib/redmine/wiki_formatting/textile/helper.rb,
lib/redmine/wiki_formatting/markdown/helper.rb,
lib/redmine/wiki_formatting/textile/formatter.rb,
lib/redmine/wiki_formatting/markdown/formatter.rb

Defined Under Namespace

Modules: LinksHelper, Macros, Markdown, NullFormatter, Textile Classes: StaleSectionError

Constant Summary collapse

@@formatters =
{}

Class Method Summary collapse

Class Method Details

.cache_key_for(format, text, object, attribute) ⇒ Object

Returns a cache key for the given text format, text, object and attribute or nil if no caching should be done


82
83
84
85
86
# File 'lib/redmine/wiki_formatting.rb', line 82

def cache_key_for(format, text, object, attribute)
  if object && attribute && !object.new_record? && format.present?
    "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{Digest::MD5.hexdigest text}"
  end
end

.cache_storeObject

Returns the cache store used to cache HTML output


89
90
91
# File 'lib/redmine/wiki_formatting.rb', line 89

def cache_store
  ActionController::Base.cache_store
end

.format_namesObject


55
56
57
# File 'lib/redmine/wiki_formatting.rb', line 55

def format_names
  @@formatters.keys.map
end

.formats_for_selectObject


59
60
61
# File 'lib/redmine/wiki_formatting.rb', line 59

def formats_for_select
  @@formatters.map {|name, options| [options[:label], name]}
end

.formatterObject


41
42
43
# File 'lib/redmine/wiki_formatting.rb', line 41

def formatter
  formatter_for(Setting.text_formatting)
end

.formatter_for(name) ⇒ Object


45
46
47
48
# File 'lib/redmine/wiki_formatting.rb', line 45

def formatter_for(name)
  entry = @@formatters[name.to_s]
  (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
end

.helper_for(name) ⇒ Object


50
51
52
53
# File 'lib/redmine/wiki_formatting.rb', line 50

def helper_for(name)
  entry = @@formatters[name.to_s]
  (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
end

.map {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:


27
28
29
# File 'lib/redmine/wiki_formatting.rb', line 27

def map
  yield self
end

.register(name, formatter, helper, options = {}) ⇒ Object

Raises:

  • (ArgumentError)

31
32
33
34
35
36
37
38
39
# File 'lib/redmine/wiki_formatting.rb', line 31

def register(name, formatter, helper, options={})
  name = name.to_s
  raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name]
  @@formatters[name] = {
    :formatter => formatter,
    :helper => helper,
    :label => options[:label] || name.humanize
  }
end

.supports_section_edit?Boolean

Returns true if the text formatter supports single section edit


77
78
79
# File 'lib/redmine/wiki_formatting.rb', line 77

def supports_section_edit?
  (formatter.instance_methods & ['update_section', :update_section]).any?
end

.to_html(format, text, options = {}) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/redmine/wiki_formatting.rb', line 63

def to_html(format, text, options = {})
  text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, text, options[:object], options[:attribute])    # Text retrieved from the cache store may be frozen
    # We need to dup it so we can do in-place substitutions with gsub!

    cache_store.fetch cache_key do
      formatter_for(format).new(text).to_html
    end.dup
  else
    formatter_for(format).new(text).to_html
  end
  text
end