Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Handle more HTML entities, and refactor for multiple Ruby versions

  • Loading branch information...
commit cb80fdd5edfb091d5f1341db6c654b284265e79c 1 parent b7fea82
@Jberlinsky authored
Showing with 44 additions and 2 deletions.
  1. +7 −2 lib/premailer/adapter/nokogiri.rb
  2. +37 −0 lib/premailer/premailer.rb
View
9 lib/premailer/adapter/nokogiri.rb
@@ -189,9 +189,14 @@ def load_html(input) # :nodoc:
# Handle HTML entities
if @options[:replace_html_entities] == true and thing.is_a?(String)
if RUBY_VERSION =~ /1.9/
- thing = thing.gsub("’","'")
+ html_entity_ruby_version = "1.9"
elsif RUBY_VERSION =~ /1.8/
- thing = thing.gsub("\342\200\231", "'")
+ html_entity_ruby_version = "1.8"
+ end
+ if html_entity_ruby_version
+ HTML_ENTITIES[html_entity_ruby_version].map do |entity, replacement|
+ thing.gsub! entity, replacement
+ end
end
end
# Default encoding is ASCII-8BIT (binary) per http://groups.google.com/group/nokogiri-talk/msg/0b81ef0dc180dc74
View
37 lib/premailer/premailer.rb
@@ -38,6 +38,43 @@ class Premailer
RE_UNMERGABLE_SELECTORS = /(\:(visited|active|hover|focus|after|before|selection|target|first\-(line|letter))|^\@)/i
RE_RESET_SELECTORS = /^(\:\#outlook|body.*|\.ReadMsgBody|\.ExternalClass|img|\#backgroundTable)$/
+ # list of HTMLEntities to fix
+ # source: http://stackoverflow.com/questions/2812781/how-to-convert-webpage-apostrophe-8217-to-ascii-39-in-ruby-1-
+ HTML_ENTITIES = {
+ "1.8" => {
+ "\342\200\231" => "'",
+ "\342\200\246" => "...",
+ "\342\200\176" => "'",
+ "\342\200\177" => "'",
+ "\342\200\230" => "'",
+ "\342\200\231" => "'",
+ "\342\200\232" => ',',
+ "\342\200\233" => "'",
+ "\342\200\234" => '"',
+ "\342\200\235" => '"',
+ "\342\200\041" => '-',
+ "\342\200\174" => '-',
+ "\342\200\220" => '-',
+ "\342\200\223" => '-',
+ "\342\200\224" => '--',
+ "\342\200\225" => '--',
+ "\342\200\042" => '--'
+ },
+ "1.9" => {
+ "’" => "'",
+ "…" => "...",
+ "‘" => "'",
+ "‚" => ',',
+ "‛" => "'",
+ "“" => '"',
+ "”" => '"',
+ "‐" => '-',
+ "–" => '-',
+ "—" => '--',
+ "―" => '--'
+ }
+ }
+
# list of CSS attributes that can be rendered as HTML attributes
#
# TODO: too much repetition
Please sign in to comment.
Something went wrong with that request. Please try again.