Permalink
Browse files

Working Tilt support

  • Loading branch information...
1 parent 3b56510 commit 4dd60e2a31d3b868bf3f7ce0681d1e2119aafadb @ddemaree committed Mar 10, 2012
View
@@ -63,50 +63,4 @@ def data_trail
end
end
end
-
- class Page
- attr_reader :site, :path
-
- def initialize(site, path)
- @site = site
- @path = Pathname(path).expand_path
- end
-
- def relative_path
- @path.relative_path_from(site.source).to_s
- end
-
- def output_path
- relative_path.sub('.mustache','.html')
- end
-
- def base_path
- relative_path.sub(".mustache",'')
- end
-
- def render
- Soundwave::Mustache.new(self).render(@path.read, self.read_data)
- end
-
- def write(destination)
- destination = Pathname(destination)
- puts "#{relative_path} => #{destination.relative_path_from(site.source)}"
- File.open(destination, "w") { |f| f.write(self.render) }
- end
-
- def read_data
- if data_file = site.data_trail.find(base_path)
- case File.extname(data_file)
- when ".yml"
- data = YAML.load(File.read(data_file))
- when ".json"
- data = MultiJson.decode(File.read(data_file))
- else
- data = {}
- end
- else
- data = {}
- end
- end
- end
end
View
@@ -0,0 +1,124 @@
+require 'soundwave'
+require 'tilt'
+
+module Soundwave
+ class Page
+ attr_reader :site, :path
+ attr_accessor :data
+
+ DEFAULT_FORMATS = {
+ ".mustache" => ".html"
+ }
+
+ ENGINES = [".mustache"] + Tilt.mappings.keys.map { |ext| ".#{ext}" }
+
+ def initialize(site, path)
+ @site = site
+ @path = Pathname(path).expand_path
+ @data = {}
+ end
+
+ # def template
+ # if engine_extension
+ # options = app.template_options[engine_extension] || nil
+ # @template ||= Tilt.new(path, options, :outvar => '@_out_buf')
+ # end
+ # end
+
+ def basename
+ @basename ||= File.basename(path.to_s)
+ end
+
+ def extensions
+ @extensions ||= basename.scan(/\.[^.]+/)
+ end
+
+ def format_extension
+ # If you don't want HTML, use multiple extensions
+ if extensions.length == 1 && DEFAULT_FORMATS[extensions[0]]
+ DEFAULT_FORMATS[extensions[0]]
+ elsif engine_extension
+ extensions[0]
+ else
+ extensions[-1]
+ end
+ end
+
+ # By convention, files that are to be processed should be named in Rails/Sprockets-like format, e.g. name.format.engine.
+ def engine_extension
+ extensions.select { |e| ENGINES.include?(e) }[-1]
+ end
+
+ def content_type
+ @content_type ||= begin
+ type = Rack::Mime.mime_type(format_extension)
+ type[/^text/] ? "#{type}; charset=utf-8" : type
+ end
+ end
+
+ # def render(env, locals = {}, &block)
+ # if template
+ # template.render(app.context_for(self, env), locals, &block)
+ # else
+ # File.read(path)
+ # end
+ # end
+
+
+ def relative_path
+ @path.relative_path_from(site.source).to_s
+ end
+
+ def logical_path
+ path = relative_path.dup
+
+ if engine_extension
+ path = path.sub(engine_extension, '')
+ path = "#{path}#{format_extension}" unless path.include?(format_extension)
+ end
+
+ path
+ end
+ alias :output_path :logical_path
+
+ def base_path
+ relative_path.sub(".mustache",'')
+ end
+
+ def render
+ if engine_extension
+ if engine_extension == ".mustache"
+ Soundwave::Mustache.new(self).render(@path.read, self.read_data)
+ else
+ template = Tilt.new(@path.to_s)
+ template.render(nil, self.read_data)
+ end
+ else
+ File.read(@path.to_s)
+ end
+ end
+
+ def write(destination)
+ destination = Pathname(destination)
+ puts "#{relative_path} => #{destination.relative_path_from(site.source)}"
+ File.open(destination, "w") { |f| f.write(self.render) }
+ end
+
+ def read_data
+ if data_file = site.data_trail.find(base_path)
+ case File.extname(data_file)
+ when ".yml"
+ YAML.load(File.read(data_file))
+ when ".json"
+ MultiJson.decode(File.read(data_file))
+ else
+ {}
+ end
+ else
+ {}
+ end
+ rescue => e
+ {}
+ end
+ end
+end
@@ -1,4 +1,5 @@
require "soundwave"
+require "soundwave/page"
require "rack"
module Soundwave
@@ -10,7 +11,7 @@ def initialize(site)
def call(env)
if page = find_page(env["PATH_INFO"])
- respond_with 200, page.render
+ respond_with 200, page.render, page.content_type
else
respond_with 404, "Not Found"
end
@@ -43,6 +44,7 @@ def respond_with(status, body, content_type = "text/html; charset=utf-8")
def template_trail
@_template_trail ||= Hike::Trail.new(@site.source).tap do |t|
+ t.extensions.replace(Soundwave::Page::ENGINES)
t.append_path "."
end
end
View
@@ -20,9 +20,11 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency "activesupport", ">= 3.1.0"
gem.add_runtime_dependency "mustache"
gem.add_runtime_dependency "hike"
+ gem.add_runtime_dependency "tilt"
gem.add_development_dependency "rack"
gem.add_development_dependency "annotations"
gem.add_development_dependency "bundler"
gem.add_development_dependency "rspec", "~> 2.8.0"
+ gem.add_development_dependency "sass", "~> 3.1.0"
end
@@ -1 +1 @@
-Hello world!
+Hello world!{{ and_goodbye }}
@@ -0,0 +1,3 @@
+function helloWorld() {
+ alert('Hello world!');
+}
@@ -0,0 +1,5 @@
+$color: #690;
+
+body {
+ background-color: $color;
+}
View
@@ -0,0 +1,54 @@
+require 'spec_helper'
+require 'soundwave/page'
+
+describe Soundwave::Page do
+
+ let(:root_path) { Pathname(File.expand_path("../fixtures/site", __FILE__)) }
+ let(:site) { Soundwave::Site.new(root_path) }
+
+ describe "Mustache template" do
+ subject { Soundwave::Page.new(site, root_path.join("index.mustache")) }
+
+ its(:format_extension) { should == ".html" }
+ its(:engine_extension) { should == ".mustache" }
+ its(:logical_path) { should == "index.html" }
+
+ it "renders the page" do
+ subject.stub!(:read_data => {:and_goodbye => " And goodbye!"})
+ subject.render.should == "Hello world! And goodbye!"
+ end
+ end
+
+ describe "Tilt template" do
+ subject { Soundwave::Page.new(site, root_path.join("styles.css.scss")) }
+
+ its(:format_extension) { should == ".css" }
+ its(:engine_extension) { should == ".scss" }
+ its(:logical_path) { should == "styles.css" }
+
+ it "renders the document" do
+ subject.render.should == "body {\n background-color: #669900; }\n"
+ end
+ end
+
+ describe "Static file" do
+ subject { Soundwave::Page.new(site, root_path.join("site.js")) }
+
+ its(:format_extension) { should == ".js" }
+ its(:engine_extension) { should be_nil }
+ its(:logical_path) { should == "site.js" }
+
+ context "with multiple extensions" do
+ subject { Soundwave::Page.new(site, root_path.join("site.min.js")) }
+
+ its(:format_extension) { should == ".js" }
+ its(:engine_extension) { should be_nil }
+ its(:logical_path) { should == "site.min.js" }
+ end
+
+ it "reads the file" do
+ subject.render.should == root_path.join("site.js").read
+ end
+ end
+
+end
View
@@ -1,9 +1,5 @@
-# This file was generated by the `rspec --init` command. Conventionally, all
-# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
-# Require this file using `require "spec_helper.rb"` to ensure that it is only
-# loaded once.
-#
-# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+require 'soundwave'
+
RSpec.configure do |config|
# config.treat_symbols_as_metadata_keys_with_true_values = true
# config.run_all_when_everything_filtered = true

0 comments on commit 4dd60e2

Please sign in to comment.