Module: Redmine::WikiFormatting

Defined in:
lib/redmine/wiki_formatting.rb,
lib/redmine/wiki_formatting/macros.rb,
lib/redmine/wiki_formatting/html_parser.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,
lib/redmine/wiki_formatting/textile/html_parser.rb,
lib/redmine/wiki_formatting/markdown/html_parser.rb

Defined Under Namespace

Modules: LinksHelper, Macros, Markdown, NullFormatter, Textile Classes: HtmlParser, 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


104
105
106
107
108
# File 'lib/redmine/wiki_formatting.rb', line 104

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


111
112
113
# File 'lib/redmine/wiki_formatting.rb', line 111

def cache_store
  ActionController::Base.cache_store
end

.format_namesObject


75
76
77
# File 'lib/redmine/wiki_formatting.rb', line 75

def format_names
  @@formatters.keys.map
end

.formats_for_selectObject


79
80
81
# File 'lib/redmine/wiki_formatting.rb', line 79

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

.formatterObject


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

def formatter
  formatter_for(Setting.text_formatting)
end

.formatter_for(name) ⇒ Object


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

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

.helper_for(name) ⇒ Object


65
66
67
68
# File 'lib/redmine/wiki_formatting.rb', line 65

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

.html_parserObject


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

def html_parser
  html_parser_for(Setting.text_formatting)
end

.html_parser_for(name) ⇒ Object


70
71
72
73
# File 'lib/redmine/wiki_formatting.rb', line 70

def html_parser_for(name)
  entry = @@formatters[name.to_s]
  (entry && entry[:html_parser]) || Redmine::WikiFormatting::HtmlParser
end

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

Yields:

  • (_self)

Yield Parameters:


29
30
31
# File 'lib/redmine/wiki_formatting.rb', line 29

def map
  yield self
end

.register(name, *args) ⇒ Object

Raises:

  • (ArgumentError)

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/redmine/wiki_formatting.rb', line 33

def register(name, *args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  name = name.to_s
  raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name]
  formatter, helper, parser =
    if args.any?
      args
    else
      %w(Formatter Helper HtmlParser).map {|m| "Redmine::WikiFormatting::#{name.classify}::#{m}".constantize rescue nil}
    end
  raise "A formatter class is required" if formatter.nil?
  @@formatters[name] = {
    :formatter => formatter,
    :helper => helper,
    :html_parser => parser,
    :label => options[:label] || name.humanize
  }
end

.supports_section_edit?Boolean

Returns true if the text formatter supports single section edit

Returns:

  • (Boolean)

99
100
101
# File 'lib/redmine/wiki_formatting.rb', line 99

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

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


83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/redmine/wiki_formatting.rb', line 83

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