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/textile/formatter.rb

Defined Under Namespace

Modules: Macros, NullFormatter, Textile

Constant Summary collapse

MACROS_RE =
/
  (!)?                        # escaping
  (
  \{\{                        # opening tag
  ([\w]+)                     # macro name
  (\(([^\}]*)\))?             # optional arguments
  \}\}                        # closing tag
  )
/x
@@formatters =
{}

Class Method Summary collapse

Class Method Details

.cache_key_for(format, object, attribute) ⇒ Object

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



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

def cache_key_for(format, object, attribute)
  if object && attribute && !object.new_record? && object.respond_to?(:updated_on) && !format.blank?
    "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{object.updated_on.to_s(:number)}"
  end
end

.cache_storeObject

Returns the cache store used to cache HTML output



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

def cache_store
  ActionController::Base.cache_store
end

.execute_macros(text, macros_runner) ⇒ Object

Macros substitution



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

def execute_macros(text, macros_runner)
  text.gsub!(MACROS_RE) do
    esc, all, macro = $1, $2, $3.downcase
    args = ($5 || '').split(',').each(&:strip)
    if esc.nil?
      begin
        macros_runner.call(macro, args)
      rescue => e
        "<div class=\"flash error\">Error executing the <strong>#{macro}</strong> macro (#{e})</div>"
      end || all
    else
      all
    end
  end
end

.format_namesObject



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

def format_names
  @@formatters.keys.map
end

.formatter_for(name) ⇒ Object



32
33
34
35
# File 'lib/redmine/wiki_formatting.rb', line 32

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

.helper_for(name) ⇒ Object



37
38
39
40
# File 'lib/redmine/wiki_formatting.rb', line 37

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

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

Yields:

  • (_self)

Yield Parameters:



23
24
25
# File 'lib/redmine/wiki_formatting.rb', line 23

def map
  yield self
end

.register(name, formatter, helper) ⇒ Object

Raises:

  • (ArgumentError)


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

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

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/redmine/wiki_formatting.rb', line 46

def to_html(format, text, options = {}, &block)
  text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, 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
  if block_given?
    execute_macros(text, block)
  end
  text
end