Skip to content

Instantly share code, notes, and snippets.

@alexphelps
Last active August 29, 2015 14:17
Show Gist options
  • Save alexphelps/a0f82d184f9925f8ce76 to your computer and use it in GitHub Desktop.
Save alexphelps/a0f82d184f9925f8ce76 to your computer and use it in GitHub Desktop.
Jekyll Plugin for Premailer
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
@DaveOsborn
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment