summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2015-06-15 21:16:42 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2015-06-15 21:16:42 +0000
commit3ae42cb32617670cb6c99a60f5cda2cf961d110c (patch)
tree2348af8e781435538aed5517b0c2087432b42c78 /lib
parent7fb35abd10e3da24188f0f4325d4ba001687ebac (diff)
downloadredmine-3ae42cb32617670cb6c99a60f5cda2cf961d110c.tar.gz
redmine-3ae42cb32617670cb6c99a60f5cda2cf961d110c.zip
Better handle html-only emails (#16962).
git-svn-id: http://svn.redmine.org/redmine/trunk@14313 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib')
-rw-r--r--lib/redmine.rb7
-rw-r--r--lib/redmine/wiki_formatting.rb18
-rw-r--r--lib/redmine/wiki_formatting/html_parser.rb54
-rw-r--r--lib/redmine/wiki_formatting/markdown/html_parser.rb40
-rw-r--r--lib/redmine/wiki_formatting/textile/html_parser.rb41
5 files changed, 154 insertions, 6 deletions
diff --git a/lib/redmine.rb b/lib/redmine.rb
index 6e3149dea..e00d089f5 100644
--- a/lib/redmine.rb
+++ b/lib/redmine.rb
@@ -267,11 +267,8 @@ Redmine::Search.map do |search|
end
Redmine::WikiFormatting.map do |format|
- format.register :textile, Redmine::WikiFormatting::Textile::Formatter, Redmine::WikiFormatting::Textile::Helper
- if Object.const_defined?(:Redcarpet)
- format.register :markdown, Redmine::WikiFormatting::Markdown::Formatter, Redmine::WikiFormatting::Markdown::Helper,
- :label => 'Markdown'
- end
+ format.register :textile
+ format.register :markdown if Object.const_defined?(:Redcarpet)
end
ActionView::Template.register_template_handler :rsb, Redmine::Views::ApiTemplateHandler
diff --git a/lib/redmine/wiki_formatting.rb b/lib/redmine/wiki_formatting.rb
index c4e4bb7c3..4fd5ea8e8 100644
--- a/lib/redmine/wiki_formatting.rb
+++ b/lib/redmine/wiki_formatting.rb
@@ -28,12 +28,19 @@ module Redmine
yield self
end
- def register(name, formatter, helper, options={})
+ def register(name, *args)
+ options = args.last.is_a?(Hash) ? args.pop : {}
name = name.to_s
raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name]
+
+ formatter, helper, parser = args.any? ?
+ args :
+ %w(Formatter Helper HtmlParser).map {|m| "Redmine::WikiFormatting::#{name.classify}::#{m}".constantize}
+
@@formatters[name] = {
:formatter => formatter,
:helper => helper,
+ :html_parser => parser,
:label => options[:label] || name.humanize
}
end
@@ -42,6 +49,10 @@ module Redmine
formatter_for(Setting.text_formatting)
end
+ def html_parser
+ html_parser_for(Setting.text_formatting)
+ end
+
def formatter_for(name)
entry = @@formatters[name.to_s]
(entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
@@ -52,6 +63,11 @@ module Redmine
(entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
end
+ def html_parser_for(name)
+ entry = @@formatters[name.to_s]
+ (entry && entry[:html_parser]) || Redmine::WikiFormatting::HtmlParser
+ end
+
def format_names
@@formatters.keys.map
end
diff --git a/lib/redmine/wiki_formatting/html_parser.rb b/lib/redmine/wiki_formatting/html_parser.rb
new file mode 100644
index 000000000..9d83497bd
--- /dev/null
+++ b/lib/redmine/wiki_formatting/html_parser.rb
@@ -0,0 +1,54 @@
+# Redmine - project management software
+# Copyright (C) 2006-2015 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+require 'loofah/helpers'
+
+module Redmine
+ module WikiFormatting
+ class HtmlParser
+
+ class_attribute :tags
+ self.tags = {
+ 'br' => {:post => "\n"}
+ }
+
+ def self.to_text(html)
+ html = html.gsub(/[\n\r]/, '').squeeze(' ')
+
+ doc = Loofah.document(html)
+ doc.scrub!(WikiTags.new(tags))
+ doc.scrub!(:newline_block_elements)
+
+ Loofah::Helpers.remove_extraneous_whitespace(doc.text).strip
+ end
+
+ class WikiTags < ::Loofah::Scrubber
+ def initialize(tags_to_text)
+ @direction = :bottom_up
+ @tags_to_text = tags_to_text || {}
+ end
+
+ def scrub(node)
+ formatting = @tags_to_text[node.name]
+ return CONTINUE unless formatting
+ node.add_next_sibling Nokogiri::XML::Text.new("#{formatting[:pre]}#{node.content}#{formatting[:post]}", node.document)
+ node.remove
+ end
+ end
+ end
+ end
+end
diff --git a/lib/redmine/wiki_formatting/markdown/html_parser.rb b/lib/redmine/wiki_formatting/markdown/html_parser.rb
new file mode 100644
index 000000000..14f89373a
--- /dev/null
+++ b/lib/redmine/wiki_formatting/markdown/html_parser.rb
@@ -0,0 +1,40 @@
+# Redmine - project management software
+# Copyright (C) 2006-2015 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+module Redmine
+ module WikiFormatting
+ module Markdown
+ class HtmlParser < Redmine::WikiFormatting::HtmlParser
+
+ self.tags = {
+ 'b' => {:pre => '**', :post => '**'},
+ 'strong' => {:pre => '**', :post => '**'},
+ 'i' => {:pre => '_', :post => '_'},
+ 'em' => {:pre => '_', :post => '_'},
+ 'strike' => {:pre => '~~', :post => '~~'},
+ 'br' => {:post => "\n"},
+ 'h1' => {:pre => "\n\n# ", :post => "\n\n"},
+ 'h2' => {:pre => "\n\n## ", :post => "\n\n"},
+ 'h3' => {:pre => "\n\n### ", :post => "\n\n"},
+ 'h4' => {:pre => "\n\n#### ", :post => "\n\n"},
+ 'h5' => {:pre => "\n\n##### ", :post => "\n\n"},
+ 'h6' => {:pre => "\n\n###### ", :post => "\n\n"}
+ }
+ end
+ end
+ end
+end
diff --git a/lib/redmine/wiki_formatting/textile/html_parser.rb b/lib/redmine/wiki_formatting/textile/html_parser.rb
new file mode 100644
index 000000000..201e69c64
--- /dev/null
+++ b/lib/redmine/wiki_formatting/textile/html_parser.rb
@@ -0,0 +1,41 @@
+# Redmine - project management software
+# Copyright (C) 2006-2015 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+module Redmine
+ module WikiFormatting
+ module Textile
+ class HtmlParser < Redmine::WikiFormatting::HtmlParser
+
+ self.tags = {
+ 'b' => {:pre => '*', :post => '*'},
+ 'strong' => {:pre => '*', :post => '*'},
+ 'i' => {:pre => '_', :post => '_'},
+ 'em' => {:pre => '_', :post => '_'},
+ 'u' => {:pre => '+', :post => '+'},
+ 'strike' => {:pre => '-', :post => '-'},
+ 'br' => {:post => "\n"},
+ 'h1' => {:pre => "\n\nh1. ", :post => "\n\n"},
+ 'h2' => {:pre => "\n\nh2. ", :post => "\n\n"},
+ 'h3' => {:pre => "\n\nh3. ", :post => "\n\n"},
+ 'h4' => {:pre => "\n\nh4. ", :post => "\n\n"},
+ 'h5' => {:pre => "\n\nh5. ", :post => "\n\n"},
+ 'h6' => {:pre => "\n\nh6. ", :post => "\n\n"}
+ }
+ end
+ end
+ end
+end