Permalink
Browse files

Hello, GitHub!

  • Loading branch information...
0 parents commit 9c3c44407de4225e276bd12900a9356851f562e3 @alexdunae alexdunae committed Nov 27, 2009
Showing with 922 additions and 0 deletions.
  1. +49 −0 CHANGELOG.rdoc
  2. +42 −0 LICENSE.rdoc
  3. +66 −0 README.rdoc
  4. +1 −0 init.rb
  5. +9 −0 lib/premailer.rb
  6. +58 −0 lib/premailer/html_to_plain_text.rb
  7. +393 −0 lib/premailer/premailer.rb
  8. +230 −0 misc/client_support.yaml
  9. +16 −0 premailer.gemspec
  10. +58 −0 rakefile.rb
@@ -0,0 +1,49 @@
+= Premailer CHANGELOG
+
+== Version 1.5.2
+ * released to GitHub
+ * fixed handling of mailto links
+ * various minor updates
+
+== Version 1.5.1
+ * bugfix (http://code.google.com/p/premailer/issues/detail?id=1 and http://code.google.com/p/premailer/issues/detail?id=2) thanks to Russell Norris
+ * bugfix (http://code.google.com/p/premailer/issues/detail?id=4) thanks to Dave Holmes
+
+== Version 1.5.0
+ * preview release of Ruby gem
+
+== Version 1.4
+ * incremental parsing improvements
+ * respect <tt>@media</tt> rule (http://www.w3.org/TR/CSS21/media.html#at-media-rule)
+ * better quote escaping
+
+== Version 1.3
+ * separate CSS parser into its own library
+ * handle <tt>background: red url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mP4%2F58BAAT%2FAf9jgNErAAAAAElFTkSuQmCC);</tt>
+ * preserve <tt>:hover</tt> etc... in head styles
+
+== Version 1.2
+ * respect <tt>LINK</tt> media types
+ * better style folding
+ * incremental parsing improvements
+
+== Version 1.1
+ * proper calculation of selector specificity per CSS 2.1 spec
+ * support for <tt>@import</tt>
+ * preliminary support for shorthand CSS properties (<tt>margin</tt>, <tt>padding</tt>)
+ * preliminary separation of CSS parser
+
+== Version 1.0
+ * ported web interface to eRuby
+ * incremental parsing improvements
+
+== Version 0.9
+ * initial proof-of-concept
+ * PHP web version
+
+== TODO: Future
+ * complete shorthand properties support (<tt>border-width</tt>, <tt>font</tt>, <tt>background</tt>)
+ * UTF-8 and other charsets (test page: http://kianga.kcore.de/2004/09/21/utf8_test)
+ * make warnings for <tt>border</tt> match <tt>border-left</tt>, etc...
+ * Integrate CSS validator
+ * Remove unused classes and IDs
@@ -0,0 +1,42 @@
+= Premailer License
+
+Copyright (c) 2007-09 Alex Dunae
+
+Premailer is copyrighted free software by Alex Dunae (http://dunae.ca/).
+You can redistribute it and/or modify it under the conditions below:
+
+ 1. You may make and give away verbatim copies of the source form of the
+ software without restriction, provided that you duplicate all of the
+ original copyright notices and associated disclaimers.
+
+ 2. You may modify your copy of the software in any way, provided that
+ you do at least ONE of the following:
+
+ a) place your modifications in the Public Domain or otherwise
+ make them Freely Available, such as by posting said
+ modifications to the internet or an equivalent medium, or by
+ allowing the author to include your modifications in the software.
+
+ b) use the modified software only within your corporation or
+ organization.
+
+ c) rename any non-standard executables so the names do not conflict
+ with standard executables, which must also be provided.
+
+ d) make other distribution arrangements with the author.
+
+ 3. You may modify and include the part of the software into any other
+ software (possibly commercial) as long as clear acknowledgement and
+ a link back to the original software (http://code.dunae.ca/premailer.web/)
+ is provided.
+
+ 5. The scripts and library files supplied as input to or produced as
+ output from the software do not automatically fall under the
+ copyright of the software, but belong to whomever generated them,
+ and may be sold commercially, and may be aggregated with this
+ software.
+
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ PURPOSE.
@@ -0,0 +1,66 @@
+= Premailer README
+
+=== What is this?
+
+For the best HTML e-mail delivery results, CSS should be inline. This is a
+huge pain and a simple newsletter becomes un-managable very quickly. This
+script is my solution.
+
+* CSS styles are converted to inline style attributes
+ Checks style and link[rel=stylesheet] tags and preserves existing inline attributes
+* Relative paths are converted to absolute paths
+ Checks links in href, src and CSS url('')
+* CSS properties are checked against e-mail client capabilities
+ Based on the Email Standards Project’s guides
+* A plain text version is created
+ Optional
+
+
+=== Installation
+
+Download the Premailer gem from GitHub.
+
+ gem sources -a http://gems.github.com
+ sudo gem install alexdunae-premailer
+
+=== Example
+ premailer = Premailer.new('http://example.com/myfile.html', :warn_level => Premailer::Warnings::SAFE)
+
+ # Write the HTML output
+ fout = File.open("output.html", "w")
+ fout.puts premailer.to_inline_css
+ fout.close
+
+ # Write the plain-text output
+ fout = File.open("ouput.txt", "w")
+ fout.puts premailer.to_plain_text
+ fout.close
+
+ # Output any CSS warnings
+ premailer.warnings.each do |w|
+ puts "#{w[:message]} (#{w[:level]}) may not render properly in #{w[:clients]}"
+ end
+
+=== Contributions
+
+Contributions are most welcome. Premailer was rotting away in a private SVN repository for too long and could use some TLC. Pull and patch to your heart's content.
+
+A few areas that are particularly in need of love:
+* Testing suite
+ There were unit tests but they were so funky that it was better to just strip them out.
+* Test running Premailer on local files
+* Create a binary file for easing command line use, allowing the output to be piped in *nix systems
+* Ruby 1.9 testing
+* Test with Rails
+* Move un-repeated background images defined in CSS to +<td background="">+ for Outlook
+* Correctly parse http://www.webstandards.org/files/acid2/test.html
+
+=== Credits and code
+
+Premailer is written in Ruby.
+
+The web interface can be found at http://premailer.dialect.ca/ .
+
+The source code can be found at http://github.com/alexdunae/premailer .
+
+Written by Alex Dunae (dunae.ca, e-mail 'code' at the same domain), 2008-2009.
@@ -0,0 +1 @@
+require 'premailer'
@@ -0,0 +1,9 @@
+# Premailer by Alex Dunae (dunae.ca, e-mail 'code' at the same domain), 2008-09
+
+require 'yaml'
+require 'open-uri'
+require 'hpricot'
+require 'css_parser'
+
+require File.dirname(__FILE__) + "/premailer/html_to_plain_text"
+require File.dirname(__FILE__) + "/premailer/premailer"
@@ -0,0 +1,58 @@
+require 'text/reform'
+require 'htmlentities'
+
+# Support functions for Premailer
+module HtmlToPlainText
+
+ # Returns the text in UTF-8 format with all HTML tags removed
+ #
+ # TODO:
+ # - add support for DL, OL
+ def convert_to_text(html, line_length, from_charset = 'UTF-8')
+ r = Text::Reform.new(:trim => true,
+ :squeeze => false,
+ :break => Text::Reform.break_wrap)
+
+ txt = html
+
+ he = HTMLEntities.new # decode HTML entities
+
+ txt = he.decode(txt)
+
+ txt.gsub!(/<h([0-9]+)[^>]*>(.*)<\/h[0-9]+>/i) do |s| # handle headings
+ hlevel = $1.to_i
+ htext = $2.gsub(/<\/?[^>]*>/i, '') # remove tags inside headings
+ hlength = (htext.length > line_length ?
+ line_length :
+ htext.length)
+
+ case hlevel
+ when 1 # H1
+ ('*' * hlength) + "\n" + htext + "\n" + ('*' * hlength) + "\n"
+ when 2 # H2
+ ('-' * hlength) + "\n" + htext + "\n" + ('-' * hlength) + "\n"
+ else # H3-H6 are styled the same
+ htext + "\n" + ('-' * htext.length) + "\n"
+ end
+ end
+
+ txt.gsub!(/<a.*href=\"([^\"]*)\"[^>]*>(.*)<\/a>/i) do |s| # links
+ $2 + ' [' + $1 + ']'
+ end
+
+ txt.gsub!(/(<li[\s]+[^>]*>|<li>)/i, ' * ') # unordered LIsts
+ txt.gsub!(/<\/p>/i, "\n\n") # paragraphs
+
+ txt.gsub!(/<\/?[^>]*>/, '') # strip remaining tags
+ txt.gsub!(/\A[\s]+|[\s]+\Z|^[ \t]+/m, '') # strip extra spaces
+ txt.gsub!(/[\n]{3,}/m, "\n\n") # tighten line breaks
+
+ txt = r.format(('[' * line_length), txt) # wrap text
+ txt.gsub!(/^[\*][\s]/m, ' * ') # add spaces back to lists
+
+ txt.gsub!(/^\s+$/, "\n") # \r\n and \r -> \n
+ txt.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
+ txt.gsub!(/[\n]{3,}/, "\n")
+ txt
+ end
+end
Oops, something went wrong.

0 comments on commit 9c3c444

Please sign in to comment.