PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/redmine/wiki_formatting.rb

https://bitbucket.org/eimajenthat/redmine
Ruby | 173 lines | 127 code | 21 blank | 25 comment | 16 complexity | 810df05d7eec605cc8aae88124009de4 MD5 | raw file
Possible License(s): GPL-2.0
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require 'digest/md5'
  18. module Redmine
  19. module WikiFormatting
  20. class StaleSectionError < Exception; end
  21. @@formatters = {}
  22. class << self
  23. def map
  24. yield self
  25. end
  26. def register(name, formatter, helper)
  27. raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name.to_s]
  28. @@formatters[name.to_s] = {:formatter => formatter, :helper => helper}
  29. end
  30. def formatter
  31. formatter_for(Setting.text_formatting)
  32. end
  33. def formatter_for(name)
  34. entry = @@formatters[name.to_s]
  35. (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
  36. end
  37. def helper_for(name)
  38. entry = @@formatters[name.to_s]
  39. (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
  40. end
  41. def format_names
  42. @@formatters.keys.map
  43. end
  44. def to_html(format, text, options = {})
  45. text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, text, options[:object], options[:attribute])
  46. # Text retrieved from the cache store may be frozen
  47. # We need to dup it so we can do in-place substitutions with gsub!
  48. cache_store.fetch cache_key do
  49. formatter_for(format).new(text).to_html
  50. end.dup
  51. else
  52. formatter_for(format).new(text).to_html
  53. end
  54. text
  55. end
  56. # Returns true if the text formatter supports single section edit
  57. def supports_section_edit?
  58. (formatter.instance_methods & ['update_section', :update_section]).any?
  59. end
  60. # Returns a cache key for the given text +format+, +text+, +object+ and +attribute+ or nil if no caching should be done
  61. def cache_key_for(format, text, object, attribute)
  62. if object && attribute && !object.new_record? && format.present?
  63. "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{Digest::MD5.hexdigest text}"
  64. end
  65. end
  66. # Returns the cache store used to cache HTML output
  67. def cache_store
  68. ActionController::Base.cache_store
  69. end
  70. end
  71. module LinksHelper
  72. AUTO_LINK_RE = %r{
  73. ( # leading text
  74. <\w+.*?>| # leading HTML tag, or
  75. [^=<>!:'"/]| # leading punctuation, or
  76. ^ # beginning of line
  77. )
  78. (
  79. (?:https?://)| # protocol spec, or
  80. (?:s?ftps?://)|
  81. (?:www\.) # www.*
  82. )
  83. (
  84. (\S+?) # url
  85. (\/)? # slash
  86. )
  87. ((?:&gt;)?|[^[:alnum:]_\=\/;\(\)]*?) # post
  88. (?=<|\s|$)
  89. }x unless const_defined?(:AUTO_LINK_RE)
  90. # Destructively remplaces urls into clickable links
  91. def auto_link!(text)
  92. text.gsub!(AUTO_LINK_RE) do
  93. all, leading, proto, url, post = $&, $1, $2, $3, $6
  94. if leading =~ /<a\s/i || leading =~ /![<>=]?/
  95. # don't replace URL's that are already linked
  96. # and URL's prefixed with ! !> !< != (textile images)
  97. all
  98. else
  99. # Idea below : an URL with unbalanced parethesis and
  100. # ending by ')' is put into external parenthesis
  101. if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
  102. url=url[0..-2] # discard closing parenth from url
  103. post = ")"+post # add closing parenth to post
  104. end
  105. content = proto + url
  106. href = "#{proto=="www."?"http://www.":proto}#{url}"
  107. %(#{leading}<a class="external" href="#{ERB::Util.html_escape href}">#{ERB::Util.html_escape content}</a>#{post}).html_safe
  108. end
  109. end
  110. end
  111. # Destructively remplaces email addresses into clickable links
  112. def auto_mailto!(text)
  113. text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
  114. mail = $1
  115. if text.match(/<a\b[^>]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
  116. mail
  117. else
  118. %(<a class="email" href="mailto:#{ERB::Util.html_escape mail}">#{ERB::Util.html_escape mail}</a>).html_safe
  119. end
  120. end
  121. end
  122. end
  123. # Default formatter module
  124. module NullFormatter
  125. class Formatter
  126. include ActionView::Helpers::TagHelper
  127. include ActionView::Helpers::TextHelper
  128. include ActionView::Helpers::UrlHelper
  129. include Redmine::WikiFormatting::LinksHelper
  130. def initialize(text)
  131. @text = text
  132. end
  133. def to_html(*args)
  134. t = CGI::escapeHTML(@text)
  135. auto_link!(t)
  136. auto_mailto!(t)
  137. simple_format(t, {}, :sanitize => false)
  138. end
  139. end
  140. module Helper
  141. def wikitoolbar_for(field_id)
  142. end
  143. def heads_for_wiki_formatter
  144. end
  145. def initial_page_content(page)
  146. page.pretty_title.to_s
  147. end
  148. end
  149. end
  150. end
  151. end