Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

wiki_formatting.rb 4.7KB

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