Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit of ground-up rewrite

  • Loading branch information...
commit 8bbf9f0188012d16cad4fd0b417b797717b681e7 0 parents
@ddemaree authored
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in soundwave.gemspec
+gemspec
4 bin/soundwave
@@ -0,0 +1,4 @@
+#!/usr/bin/env ruby
+
+require 'soundwave'
+Soundwave::CLI.run
133 lib/soundwave.rb
@@ -0,0 +1,133 @@
+require 'hike'
+require 'mustache'
+require 'multi_json'
+
+module Soundwave
+
+ module CLI
+ extend self
+
+ def run
+ source = ARGV.shift || "./"
+
+ if File.directory?(source)
+ site = Site.new(source)
+ destination = ARGV.shift || site.source.join("_site")
+ site.generate(destination)
+ else
+ site = Site.new("./")
+ page = Page.new(site, source)
+ puts page.render
+ end
+ end
+ end
+
+ class Mustache < ::Mustache
+ attr_reader :page
+
+ def initialize(page=nil)
+ @page = page
+ end
+
+ def site
+ @page.site
+ end
+
+ def template
+ @page.path.read
+ end
+
+ def partial_path(name)
+ name = name.to_s
+ dirname = File.dirname(name)
+ basename = File.basename(name)
+ partialname = "_#{basename}"
+ File.join(dirname, partialname)
+ end
+
+ def partial(name)
+ @paths ||= [@page.path.dirname, site.source.join("includes"), Dir.pwd].map(&:to_s).uniq
+ @trail ||= Hike::Trail.new(@page.site.source).tap do |t|
+ t.append_extension ".mustache"
+ t.append_paths *@paths
+ end
+
+ if path = @trail.find(partial_path(name))
+ File.read(path)
+ end
+ end
+ end
+
+ class Site
+ attr_accessor :source, :destination
+
+ def initialize(source="./", destination="./_site")
+ @source = Pathname(source).expand_path
+ # @destination = Pathname(destination).expand_path
+ end
+
+ def generate(destination)
+ destination = Pathname(destination)
+ Dir[source.join("**","*.mustache")].each do |path|
+ next if File.basename(path).to_s =~ /^_/
+ page = Page.new(self, path)
+ page.write(destination.join(page.output_path))
+ end
+ end
+
+ def data_trail
+ @_data_path ||= Hike::Trail.new(source.join("_data")).tap do |t|
+ t.append_path "."
+ t.append_extension ".json"
+ t.append_extension ".yml", ".yaml"
+ 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
22 lib/soundwave/rake.rb
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/tasklib'
+
+module Soundwave
+ class RakeTask < ::Rake::TaskLib
+ attr_accessor :name, :site, :destination
+
+ def initialize(name=:pages, source="./", destination="./_site")
+ @name = name
+ @site = Site.new(source)
+ @destination = destination
+ define
+ end
+
+ def define
+ desc "Build pages in #{@site.source}"
+ task(name) do
+ @site.generate(@destination)
+ end
+ end
+ end
+end
3  lib/soundwave/version.rb
@@ -0,0 +1,3 @@
+module Soundwave
+ VERSION = "0.0.3"
+end
28 soundwave.gemspec
@@ -0,0 +1,28 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/soundwave/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["David Demaree"]
+ gem.email = ["ddemaree@gmail.com"]
+ gem.description = %q{Processes Mustache templates and YAML/JSON data into static web pages}
+ # gem.summary = %q{A simple static website generator based on Tilt and Mustache}
+ gem.homepage = "http://github.com/ddemaree/soundwave"
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = "soundwave"
+ gem.require_paths = ["lib"]
+ gem.version = Soundwave::VERSION
+
+ gem.executables = ["soundwave"]
+
+ 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 "annotations"
+ gem.add_development_dependency "bundler"
+ gem.add_development_dependency "rspec", "~> 2.8.0"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.