Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

wiki_formatting.rb 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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 File.expand_path('../wiki_formatting/textile/redcloth3', __FILE__)
  19. require 'digest/md5'
  20. module Redmine
  21. module WikiFormatting
  22. class StaleSectionError < StandardError; end
  23. @@formatters = {}
  24. class << self
  25. def map
  26. yield self
  27. end
  28. def register(name, *args)
  29. options = args.last.is_a?(Hash) ? args.pop : {}
  30. name = name.to_s
  31. raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name]
  32. formatter, helper, parser =
  33. if args.any?
  34. args
  35. else
  36. %w(Formatter Helper HtmlParser).map {|m| "Redmine::WikiFormatting::#{name.classify}::#{m}".constantize rescue nil}
  37. end
  38. raise "A formatter class is required" if formatter.nil?
  39. @@formatters[name] = {
  40. :formatter => formatter,
  41. :helper => helper,
  42. :html_parser => parser,
  43. :label => options[:label] || name.humanize
  44. }
  45. end
  46. def formatter
  47. formatter_for(Setting.text_formatting)
  48. end
  49. def html_parser
  50. html_parser_for(Setting.text_formatting)
  51. end
  52. def formatter_for(name)
  53. entry = @@formatters[name.to_s]
  54. (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
  55. end
  56. def helper_for(name)
  57. entry = @@formatters[name.to_s]
  58. (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
  59. end
  60. def html_parser_for(name)
  61. entry = @@formatters[name.to_s]
  62. (entry && entry[:html_parser]) || Redmine::WikiFormatting::HtmlParser
  63. end
  64. def format_names
  65. @@formatters.keys.map
  66. end
  67. def formats_for_select
  68. @@formatters.map {|name, options| [options[:label], name]}
  69. end
  70. def to_html(format, text, options = {})
  71. text =
  72. if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store &&
  73. cache_key = cache_key_for(format, text, options[:object], options[:attribute])
  74. # Text retrieved from the cache store may be frozen
  75. # We need to dup it so we can do in-place substitutions with gsub!
  76. cache_store.fetch cache_key do
  77. formatter_for(format).new(text).to_html
  78. end.dup
  79. else
  80. formatter_for(format).new(text).to_html
  81. end
  82. text
  83. end
  84. # Returns true if the text formatter supports single section edit
  85. def supports_section_edit?
  86. (formatter.instance_methods & ['update_section', :update_section]).any?
  87. end
  88. # Returns a cache key for the given text +format+, +text+, +object+ and +attribute+ or nil if no caching should be done
  89. def cache_key_for(format, text, object, attribute)
  90. if object && attribute && !object.new_record? && format.present?
  91. "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{Digest::MD5.hexdigest text}"
  92. end
  93. end
  94. # Returns the cache store used to cache HTML output
  95. def cache_store
  96. ActionController::Base.cache_store
  97. end
  98. end
  99. # Default formatter module
  100. module NullFormatter
  101. class Formatter
  102. include ActionView::Helpers::TagHelper
  103. include ActionView::Helpers::TextHelper
  104. include ActionView::Helpers::UrlHelper
  105. include Redmine::WikiFormatting::LinksHelper
  106. def initialize(text)
  107. @text = text
  108. end
  109. def to_html(*args)
  110. t = CGI::escapeHTML(@text)
  111. auto_link!(t)
  112. auto_mailto!(t)
  113. restore_redmine_links(t)
  114. simple_format(t, {}, :sanitize => false)
  115. end
  116. end
  117. module Helper
  118. def wikitoolbar_for(field_id, preview_url = preview_text_path)
  119. end
  120. def heads_for_wiki_formatter
  121. end
  122. def initial_page_content(page)
  123. page.pretty_title.to_s
  124. end
  125. end
  126. end
  127. end
  128. end