Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
add compatibility with nanoc 3.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jingoro committed Feb 12, 2012
1 parent b3dc136 commit b9fb6fe
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 73 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
.rvmrc
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source :rubygems

gemspec
28 changes: 18 additions & 10 deletions README.md
Expand Up @@ -29,7 +29,9 @@ install this gem:

Then load it via your project Gemfile or in `./lib/default.rb`:

require 'nanoc3/cachebuster'
```ruby
require 'nanoc/cachebuster'
```

Usage
=====
Expand All @@ -41,21 +43,27 @@ replace any references to the regularly-named file to the rewritten one.

So, when you include a stylesheet:

<link rel="stylesheet" href="styles.css">
```html
<link rel="stylesheet" href="styles.css">
```

And you rewrite the output of the file to include a fingerprint:

# in your ./lib/default.rb
include Nanoc3::Helpers::CacheBusting
# in ./Rules
route '/styles/' do
fp = fingerprint(item[:filename])
item.identifier.chop + fp + '.css'
end
```ruby
# in your ./lib/default.rb
include Nanoc::Helpers::CacheBusting
# in ./Rules
route '/styles/' do
fp = fingerprint(item[:filename])
item.identifier.chop + fp + '.css'
end
```

The filter will change your HTML on compilation to:

<link rel="stylesheet" href="styles-cb7a4bb98ef.css">
```html
<link rel="stylesheet" href="styles-cb7a4bb98ef.css">
```

You get simple, content-based cachebusters for free. All that is left for you
to do is set some far-future expires header in your server configuration.
Expand Down
38 changes: 13 additions & 25 deletions Rakefile
@@ -1,19 +1,12 @@
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
require 'nanoc3/cachebuster/version'

task :build do
sh 'gem build nanoc-cachebuster.gemspec'
end
require 'bundler/setup'
require 'bundler/gem_tasks'

desc 'Install the gem locally'
task :install => :build do
sh "gem install nanoc-cachebuster-#{Nanoc3::Cachebuster::VERSION}.gem"
end
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec

task :push do
sh 'git push origin master'
sh 'git push --tags'
end
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
require 'nanoc/cachebuster/version'

task :log do
changes = `git log --oneline $(git describe --abbrev=0 2>/dev/null)..HEAD`
Expand All @@ -23,22 +16,17 @@ task :log do
path = File.expand_path('../HISTORY.md', __FILE__)

original_content = File.read(path)
addition = "# #{Nanoc3::Cachebuster::VERSION}\n\n#{changes}"
addition = "# #{Nanoc::Cachebuster::VERSION}\n\n#{changes}"
puts addition

File.open(path, 'w') do |f|
f.write "#{addition}\n#{original_content}"
end
end

desc 'Tag the code, push upstream, build and push the gem'
task :release => [:install, :push] do
sh "gem push nanoc-cachebuster-#{Nanoc3::Cachebuster::VERSION}.gem"
end

desc 'Print current version number'
task :version do
puts Nanoc3::Cachebuster::VERSION
puts Nanoc::Cachebuster::VERSION
end

class Version
Expand All @@ -60,7 +48,7 @@ class Version
end

def write
file = File.expand_path('../lib/nanoc3/cachebuster/version.rb', __FILE__)
file = File.expand_path('../lib/nanoc/cachebuster/version.rb', __FILE__)
original_contents = File.read(file)
File.open(file, 'w') do |f|
f.write original_contents.gsub(/VERSION = ('|")\d+\.\d+\.\d+\1/, "VERSION = '#{to_s}'")
Expand All @@ -74,17 +62,17 @@ namespace :version do
namespace :bump do
desc 'Bump a major version'
task :major do
Version.new(Nanoc3::Cachebuster::VERSION).bump(:major).write
Version.new(Nanoc::Cachebuster::VERSION).bump(:major).write
end

desc 'Bump a minor version'
task :minor do
Version.new(Nanoc3::Cachebuster::VERSION).bump(:minor).write
Version.new(Nanoc::Cachebuster::VERSION).bump(:minor).write
end

desc 'Bump a patch version'
task :patch do
Version.new(Nanoc3::Cachebuster::VERSION).bump(:patch).write
Version.new(Nanoc::Cachebuster::VERSION).bump(:patch).write
end
end
end
4 changes: 2 additions & 2 deletions lib/nanoc3/cachebuster.rb → lib/nanoc/cachebuster.rb
@@ -1,7 +1,7 @@
require 'nanoc3'
require 'nanoc'
require 'digest'

module Nanoc3
module Nanoc
module Cachebuster
autoload :VERSION, 'cachebuster/version'

Expand Down
@@ -1,6 +1,6 @@
require 'pathname'

module Nanoc3
module Nanoc
module Cachebuster
# The Strategy is a way to deal with an input file. The Cache busting filter
# will use a strategy to process all references. You may want to use different
Expand All @@ -17,7 +17,7 @@ def self.inherited(subclass)

def self.for(kind, site, item)
klass = @subclasses[kind]
raise Nanoc3::Cachebuster::NoSuchStrategy.new "No strategy found for #{kind}" unless klass
raise Nanoc::Cachebuster::NoSuchStrategy.new "No strategy found for #{kind}" unless klass
klass.new(site, item)
end

Expand All @@ -28,12 +28,12 @@ def self.for(kind, site, item)
# future portability we might as well carry the entire site object
# over.
#
# @return <Nanoc3::Site>
# @return <Nanoc::Site>
attr_reader :site

# The Nanoc item we are currently filtering.
#
# @return <Nanoc3::Item>
# @return <Nanoc::Item>
attr_reader :current_item

def initialize(site, current_item)
Expand Down Expand Up @@ -70,21 +70,21 @@ def output_filename(input_path)

matching_item = site.items.find do |i|
next unless i.path # some items don't have an output path. Ignore those.
i.path.sub(/#{Nanoc3::Cachebuster::CACHEBUSTER_PREFIX}[a-zA-Z0-9]{9}(?=\.)/o, '') == path
i.path.sub(/#{Nanoc::Cachebuster::CACHEBUSTER_PREFIX}[a-zA-Z0-9]{9}(?=\.)/o, '') == path
end

# Raise an exception to indicate we should leave this reference alone
unless matching_item
raise Nanoc3::Cachebuster::NoSuchSourceFile, 'No source file found matching ' + input_path
raise Nanoc::Cachebuster::NoSuchSourceFile, 'No source file found matching ' + input_path
end

# keep using an absolute path if the input reference did so...
return matching_item.path if input_path =~ /^\//

# ... if not, recreate the relative path to referenced file from
# the current file path.
current_path = Pathname.new(File.dirname(current_item.path))
target_path = Pathname.new(File.dirname(matching_item.path))
current_path = Pathname.new(File.dirname(current_item.path.sub(/^\//, '')))
target_path = Pathname.new(File.dirname(matching_item.path.sub(/^\//, '')))
output_reference = target_path.relative_path_from(current_path).join(File.basename(matching_item.path))
end

Expand Down Expand Up @@ -124,7 +124,7 @@ class Css < Strategy
('|"|) # Then either a single, double or no quote at all
(
([^'")]+) # The file basename, and below the extension
\.(#{Nanoc3::Cachebuster::FILETYPES_TO_FINGERPRINT.join('|')})
\.(#{Nanoc::Cachebuster::FILETYPES_TO_FINGERPRINT.join('|')})
)
\1 # Repeat the same quote as at the start
\) # And cose the url()
Expand All @@ -146,7 +146,7 @@ class Html < Strategy
( # Capture the entire reference
[^'"]+ # Anything but something that would close the attribute
# And then the extension:
(\.(?:#{Nanoc3::Cachebuster::FILETYPES_TO_FINGERPRINT.join('|')}))
(\.(?:#{Nanoc::Cachebuster::FILETYPES_TO_FINGERPRINT.join('|')}))
)
\2 # Repeat the opening quote
/ix
Expand Down
@@ -1,4 +1,4 @@
module Nanoc3
module Nanoc
module Cachebuster
VERSION = '0.1.2'
end
Expand Down
4 changes: 4 additions & 0 deletions lib/nanoc/filters.rb
@@ -0,0 +1,4 @@
module Nanoc::Filters
autoload 'CacheBuster', 'nanoc/filters/cache_buster'
Nanoc::Filter.register '::Nanoc::Filters::CacheBuster', :cache_buster
end
@@ -1,15 +1,15 @@
module Nanoc3
module Nanoc
module Filters
class CacheBuster < Nanoc3::Filter
class CacheBuster < Nanoc::Filter
identifier :cache_buster

def run(content, options = {})
kind = options[:strategy] || (stylesheet? ? :css : :html)
strategy = Nanoc3::Cachebuster::Strategy.for(kind , site, item)
strategy = Nanoc::Cachebuster::Strategy.for(kind , site, item)
content.gsub(strategy.class::REGEX) do |m|
begin
strategy.apply m, $1, $2, $3, $4
rescue Nanoc3::Cachebuster::NoSuchSourceFile
rescue Nanoc::Cachebuster::NoSuchSourceFile
m
end
end
Expand All @@ -22,10 +22,10 @@ def run(content, options = {})
# This is a simple check for filetypes, but you can override what strategy to use
# with the filter options. This provides a default.
#
# @see Nanoc3::Cachebuster::FILETYPES_CONSIDERED_CSS
# @see Nanoc::Cachebuster::FILETYPES_CONSIDERED_CSS
# @return <Bool>
def stylesheet?
Nanoc3::Cachebuster::FILETYPES_CONSIDERED_CSS.include?(item[:extension].to_s)
Nanoc::Cachebuster::FILETYPES_CONSIDERED_CSS.include?(item[:extension].to_s)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/nanoc/helpers.rb
@@ -0,0 +1,3 @@
module Nanoc::Helpers
autoload 'CacheBusting', 'nanoc/helpers/cache_busting'
end
@@ -1,4 +1,4 @@
module Nanoc3
module Nanoc
module Helpers
module CacheBusting

Expand All @@ -15,7 +15,7 @@ module CacheBusting
# @param <Item> item is the item to test
# @return <Boolean>
def cachebust?(item)
Nanoc3::Cachebuster.should_apply_fingerprint_to_file?(item)
Nanoc::Cachebuster.should_apply_fingerprint_to_file?(item)
end

# Get a unique fingerprint for a file's content. This currently uses
Expand All @@ -25,7 +25,7 @@ def cachebust?(item)
# @param <String> filename is the path to the file to fingerprint.
# @return <String> file fingerprint
def fingerprint(filename)
Nanoc3::Cachebuster.fingerprint_file(filename)
Nanoc::Cachebuster.fingerprint_file(filename)
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions lib/nanoc3/filters.rb

This file was deleted.

3 changes: 0 additions & 3 deletions lib/nanoc3/helpers.rb

This file was deleted.

8 changes: 6 additions & 2 deletions nanoc-cachebuster.gemspec
@@ -1,10 +1,10 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path('../lib', __FILE__)
require 'nanoc3/cachebuster/version'
require 'nanoc/cachebuster/version'

Gem::Specification.new do |s|
s.name = 'nanoc-cachebuster'
s.version = Nanoc3::Cachebuster::VERSION
s.version = Nanoc::Cachebuster::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ['Arjan van der Gaag']
s.email = ['arjan@arjanvandergaag.nl']
Expand All @@ -31,4 +31,8 @@ EOS
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ['lib']

s.add_runtime_dependency 'nanoc', '>= 3.3.0'
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec'
end
Expand Up @@ -48,12 +48,12 @@ def [](k)
end
end

describe Nanoc3::Filters::CacheBuster do
describe Nanoc::Filters::CacheBuster do
before(:each) do
Digest::MD5.stub!(:hexdigest).and_return('123456789')
end

let(:subject) { Nanoc3::Filters::CacheBuster.new context }
let(:subject) { Nanoc::Filters::CacheBuster.new context }
let(:content) { item.content }
let(:item) { MockItem.css_file }
let(:target) { MockItem.image_file }
Expand All @@ -69,7 +69,7 @@ def [](k)
end

describe 'filter interface' do
it { should be_kind_of(Nanoc3::Filter) }
it { should be_kind_of(Nanoc::Filter) }
it { should respond_to(:run) }

it 'should accept a string and an options Hash' do
Expand Down
@@ -1,7 +1,7 @@
describe Nanoc3::Helpers::CacheBusting do
describe Nanoc::Helpers::CacheBusting do
let(:subject) do
o = Object.new
o.extend Nanoc3::Helpers::CacheBusting
o.extend Nanoc::Helpers::CacheBusting
end

describe '#should_cachebust?' do
Expand Down
3 changes: 1 addition & 2 deletions spec/spec_helper.rb
@@ -1,2 +1 @@
require 'nanoc3/cachebuster'

require 'nanoc/cachebuster'

0 comments on commit b9fb6fe

Please sign in to comment.