diff --git a/Gemfile b/Gemfile index 4c39b70..097a39a 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,5 @@ gem 'builder' gem 'json' gem 'mustache' gem 'rdiscount' +gem 'nokogiri' +gem 'css_parser' diff --git a/Gemfile.lock b/Gemfile.lock index 057874f..cd45153 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,9 +1,15 @@ GEM remote: https://rubygems.org/ specs: + addressable (2.3.5) builder (3.0.0) + css_parser (1.3.5) + addressable json (1.6.1) + mini_portile (0.5.2) mustache (0.99.4) + nokogiri (1.6.1) + mini_portile (~> 0.5.0) rdiscount (1.6.8) PLATFORMS @@ -11,6 +17,8 @@ PLATFORMS DEPENDENCIES builder + css_parser json mustache + nokogiri rdiscount diff --git a/bin/compile.sh b/bin/compile.sh index 35f970c..74cc371 100755 --- a/bin/compile.sh +++ b/bin/compile.sh @@ -18,6 +18,9 @@ function main() { echo "* munge html files to make them available without an extension" munge_html + echo "* inline CSS" + $DIR/inline-css.rb "$TARGET" + echo "* minify js" minify_js } diff --git a/bin/inline-css.rb b/bin/inline-css.rb new file mode 100755 index 0000000..95d1243 --- /dev/null +++ b/bin/inline-css.rb @@ -0,0 +1,53 @@ +#!/usr/bin/env ruby -w -rrubygems + +require 'bundler/setup' +require 'nokogiri' +require 'css_parser' + +# Styles are so small, inline them all. + +def main + root_dir = ARGV.shift + CSSInliner.new(root_dir).inline_all_css +end + +class CSSInliner + + def initialize(root_dir) + @root_dir = root_dir + end + + def inline_all_css + Dir[File.join(@root_dir, '**/*.html')].each do |html_path| + next if html_path =~ /\/Chalk\/|\/tweets\// + inline_css(html_path) + end + end + + def inline_css(html_path) + html = File.read(html_path) + doc = Nokogiri::HTML.parse(html) + css_parser = CssParser::Parser.new + + doc.css('link').each do |link| + if link['rel'] == 'stylesheet' + path = File.join(@root_dir, link['href']) + css = File.read(path) + css_parser.add_block!(css) + + style_node = Nokogiri::HTML.parse(" + + ").css('style') + + link.replace(style_node) + end + end + + File.open(html_path, 'w') { |f| f.puts(doc.to_html) } + end + +end + +main if $0 == __FILE__ diff --git a/bin/rss.rb b/bin/rss.rb index 2c6d260..fbe1a48 100755 --- a/bin/rss.rb +++ b/bin/rss.rb @@ -1,8 +1,7 @@ -#!/usr/bin/env ruby +#!/usr/bin/env ruby -w -rrubygems # encoding: utf-8 require 'time' -require 'rubygems' require 'bundler/setup' require 'builder' require 'json' diff --git a/public/_data.json b/public/_data.json index 60d238c..f767cbe 100644 --- a/public/_data.json +++ b/public/_data.json @@ -3,7 +3,6 @@ "subtitle": "words mean things", "url": "http://samhuri.net", "styles": [ - "/css/font-awesome.min.css", "/css/normalize.css", "/css/style.css" ], diff --git a/public/_layout.ejs b/public/_layout.ejs index acc87a0..2712054 100644 --- a/public/_layout.ejs +++ b/public/_layout.ejs @@ -132,6 +132,16 @@ © 2006 - <%= new Date().getFullYear() %> <%= author %> + + <% for (var i in allScripts) { -%> <% } -%>