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/links_helper.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/common_mark/helper.rb,
lib/redmine/wiki_formatting/markdown/formatter.rb,
lib/redmine/wiki_formatting/textile/html_parser.rb,
lib/redmine/wiki_formatting/markdown/html_parser.rb,
lib/redmine/wiki_formatting/common_mark/formatter.rb,
lib/redmine/wiki_formatting/common_mark/html_parser.rb,
lib/redmine/wiki_formatting/common_mark/markdown_filter.rb,
lib/redmine/wiki_formatting/common_mark/sanitization_filter.rb,
lib/redmine/wiki_formatting/common_mark/external_links_filter.rb,
lib/redmine/wiki_formatting/common_mark/append_spaces_to_lines.rb,
lib/redmine/wiki_formatting/common_mark/fixup_auto_links_filter.rb,
lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter.rb

Defined Under Namespace

Modules: CommonMark, 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



107
108
109
110
111
# File 'lib/redmine/wiki_formatting.rb', line 107

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



114
115
116
# File 'lib/redmine/wiki_formatting.rb', line 114

def cache_store
  ActionController::Base.cache_store
end

.format_namesObject



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

def format_names
  @@formatters.keys.map
end

.formats_for_selectObject



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

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

.formatterObject



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

def formatter
  formatter_for(Setting.text_formatting)
end

.formatter_for(name) ⇒ Object



63
64
65
66
# File 'lib/redmine/wiki_formatting.rb', line 63

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

.helper_for(name) ⇒ Object



68
69
70
71
# File 'lib/redmine/wiki_formatting.rb', line 68

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

.html_parserObject



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

def html_parser
  html_parser_for(Setting.text_formatting)
end

.html_parser_for(name) ⇒ Object



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

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:



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

def map
  yield self
end

.register(name, *args) ⇒ Object

Raises:

  • (ArgumentError)


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

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)


102
103
104
# File 'lib/redmine/wiki_formatting.rb', line 102

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

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/redmine/wiki_formatting.rb', line 86

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