You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiki_formatting.rb 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative 'wiki_formatting/textile/redcloth3'
  19. module Redmine
  20. module WikiFormatting
  21. class StaleSectionError < StandardError; end
  22. @@formatters = {}
  23. class << self
  24. def map
  25. yield self
  26. end
  27. def register(name, *args)
  28. options = args.last.is_a?(Hash) ? args.pop : {}
  29. name = name.to_s
  30. formatter, helper, parser =
  31. if args.any?
  32. args
  33. else
  34. %w(Formatter Helper HtmlParser).map {|m| "Redmine::WikiFormatting::#{name.classify}::#{m}".constantize rescue nil}
  35. end
  36. raise "A formatter class is required" if formatter.nil?
  37. entry = {
  38. :formatter => formatter,
  39. :helper => helper,
  40. :html_parser => parser,
  41. :label => options[:label] || name.humanize
  42. }
  43. if @@formatters[name] && @@formatters[name] != entry
  44. raise ArgumentError, "format name '#{name}' is already taken"
  45. end
  46. @@formatters[name] = entry
  47. end
  48. def formatter
  49. formatter_for(Setting.text_formatting)
  50. end
  51. def html_parser
  52. html_parser_for(Setting.text_formatting)
  53. end
  54. def formatter_for(name)
  55. entry = @@formatters[name.to_s]
  56. (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
  57. end
  58. def helper_for(name)
  59. entry = @@formatters[name.to_s]
  60. (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
  61. end
  62. def html_parser_for(name)
  63. entry = @@formatters[name.to_s]
  64. (entry && entry[:html_parser]) || Redmine::WikiFormatting::HtmlParser
  65. end
  66. def format_names
  67. @@formatters.keys.map
  68. end
  69. def formats_for_select
  70. @@formatters.map {|name, options| [options[:label], name]}
  71. end
  72. def to_html(format, text, options = {})
  73. text =
  74. if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store &&
  75. cache_key = cache_key_for(format, text, options[:object], options[:attribute])
  76. # Text retrieved from the cache store may be frozen
  77. # We need to dup it so we can do in-place substitutions with gsub!
  78. cache_store.fetch cache_key do
  79. formatter_for(format).new(text).to_html
  80. end.dup
  81. else
  82. formatter_for(format).new(text).to_html
  83. end
  84. text
  85. end
  86. # Returns true if the text formatter supports single section edit
  87. def supports_section_edit?
  88. (formatter.instance_methods & ['update_section', :update_section]).any?
  89. end
  90. # Returns a cache key for the given text +format+, +text+, +object+ and +attribute+ or nil if no caching should be done
  91. def cache_key_for(format, text, object, attribute)
  92. if object && attribute && !object.new_record? && format.present?
  93. "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{ActiveSupport::Digest.hexdigest text}"
  94. end
  95. end
  96. # Returns the cache store used to cache HTML output
  97. def cache_store
  98. ActionController::Base.cache_store
  99. end
  100. end
  101. # Default formatter module
  102. module NullFormatter
  103. class Formatter
  104. include ActionView::Helpers::TagHelper
  105. include ActionView::Helpers::TextHelper
  106. include ActionView::Helpers::UrlHelper
  107. include Redmine::WikiFormatting::LinksHelper
  108. def initialize(text)
  109. @text = text
  110. end
  111. def to_html(*args)
  112. t = CGI::escapeHTML(@text)
  113. auto_link!(t)
  114. auto_mailto!(t)
  115. restore_redmine_links(t)
  116. simple_format(t, {}, :sanitize => false)
  117. end
  118. end
  119. module Helper
  120. def wikitoolbar_for(field_id, preview_url = preview_text_path)
  121. end
  122. def heads_for_wiki_formatter
  123. end
  124. def initial_page_content(page)
  125. page.pretty_title.to_s
  126. end
  127. end
  128. end
  129. end
  130. end