Last active
August 29, 2015 14:17
-
-
Save alexphelps/a0f82d184f9925f8ce76 to your computer and use it in GitHub Desktop.
Jekyll Plugin for Premailer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'premailer' | |
module Jekyll | |
class Site | |
#declare plugin properties | |
#safe true | |
#priority :low | |
# create an alias for the overriden site::write method | |
alias orig_write write | |
# we override the site::write method | |
def write | |
# first call the overriden write method, | |
# in order to be sure to find generated css | |
orig_write | |
# layout name we are looking for in pages/post front matter | |
# this can come from site config | |
@layout = 'email-base' | |
# this the css that will be inlined | |
# this can come from site config | |
@css_path = 'assets/css/email.css' | |
# look for generated css file content | |
@cssPages = pages.select{ |page| | |
page.destination(dest).include? @css_path | |
} | |
# look again in pages and posts | |
# to generate inlinedcontent with premailer | |
inlinedcontent = [posts, pages].flatten.select{ |page_or_post| | |
page_or_post.data['layout'] == @layout | |
} | |
inlinedcontent.each do |inlinedcontent| | |
inlinedcontent.output = Premailer.new( | |
inlinedcontent.output, | |
{ | |
# declare that we pass page html as a string not an url | |
:with_html_string => true, | |
# also pass css content as a string | |
:css_string => @cssPages.join, | |
} | |
).to_inline_css; | |
# rewrite the content with inlined css | |
inlinedcontent.write(dest) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Alex, this gist is really useful. I'm just wondering if you have ever integrated the creation of a text only version of each file into this workflow? I tried to get it working using premailers
.to_plain_text
but I'm not having much luck. Thanks, Dave