Better handle html-only emails (#16962).

git-svn-id: http://svn.redmine.org/redmine/trunk@14313 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang
2015-06-15 21:16:42 +00:00
parent 7fb35abd10
commit 3ae42cb326
14 changed files with 1312 additions and 19 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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