Skip to content

Commit

Permalink
Moved tasks from Rakefile to ecstatic lib.
Browse files Browse the repository at this point in the history
Now the Rakefile for a standard site can be a two-liner.
  • Loading branch information
jgm committed Jul 21, 2009
1 parent 5bdd4c7 commit 4140c26
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 80 deletions.
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -15,6 +15,7 @@ begin
gemspec.authors = ["John MacFarlane"]
gemspec.test_files = []
gemspec.add_dependency("activesupport", [">= 1.1"])
gemspec.add_dependency("rake", [">= 0.8.0"])
gemspec.add_dependency("rpeg-markdown", [">= 0.2"])
gemspec.add_dependency("tenjin", [">= 0.6.1"])
end
Expand Down
7 changes: 6 additions & 1 deletion ecstatic.gemspec
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["John MacFarlane"]
s.date = %q{2009-07-20}
s.date = %q{2009-07-21}
s.default_executable = %q{ecstatic}
s.description = %q{Ecstatic is a framework for maintaining a static website from templates and data in YAML files.}
s.email = %q{jgm@berkeley.edu}
Expand All @@ -32,6 +32,8 @@ Gem::Specification.new do |s|
"samplesite/files/css/screen.css",
"samplesite/models/models.rb",
"samplesite/siteindex.yaml",
"samplesite/sitenav.rbhtml",
"samplesite/sitenav.yaml",
"samplesite/standard.rbhtml"
]
s.has_rdoc = true
Expand All @@ -47,15 +49,18 @@ Gem::Specification.new do |s|

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<activesupport>, [">= 1.1"])
s.add_runtime_dependency(%q<rake>, [">= 0.8.0"])
s.add_runtime_dependency(%q<rpeg-markdown>, [">= 0.2"])
s.add_runtime_dependency(%q<tenjin>, [">= 0.6.1"])
else
s.add_dependency(%q<activesupport>, [">= 1.1"])
s.add_dependency(%q<rake>, [">= 0.8.0"])
s.add_dependency(%q<rpeg-markdown>, [">= 0.2"])
s.add_dependency(%q<tenjin>, [">= 0.6.1"])
end
else
s.add_dependency(%q<activesupport>, [">= 1.1"])
s.add_dependency(%q<rake>, [">= 0.8.0"])
s.add_dependency(%q<rpeg-markdown>, [">= 0.2"])
s.add_dependency(%q<tenjin>, [">= 0.6.1"])
end
Expand Down
102 changes: 88 additions & 14 deletions lib/ecstatic.rb
Expand Up @@ -4,11 +4,85 @@
require 'yaml'
require 'markdown'
require 'activesupport'
require 'rake'
require 'rake/clean'
require 'find'

module Ecstatic
class Tasks
def self.website(paths = {})
sitedir = paths[:sitedir] || "site"
layout = paths[:layoutfile] || "standard.rbhtml"
navfile = paths[:navfile] || "sitenav.yaml"
index = paths[:indexfile] || "siteindex.yaml"
filesdir = paths[:filesdir] || "files"
modelsdir = paths[:modelsdir] || "models"

CLEAN.include(sitedir)

siteindex = YAML::load File.read(index)

# load user-defined data models
Find.find(modelsdir) do |f|
require f unless f == modelsdir
end

# construct list of pages
pages = {}
siteindex.each do |p|
dest = File.join sitedir, p['url']
pages[dest] = {
:url => p['url'],
:title => p['title'],
:template => p['template'],
:data => if p['data'].class == Array
p['data']
elsif p['data'].class == String
[p['data']]
else
[]
end }
end

# construct hash of files
files = {}
Find.find(filesdir) do |f|
if f != filesdir
base = f.gsub(/^[^\/]*\//,"")
files[File.join(sitedir, base)] = f
end
end

directory sitedir

files.each_pair do |dest,src|
file dest => src do
d = File.dirname dest
if ! File.exists?(d)
mkdir_p d
end
if ! File.directory? dest
cp src, dest
end
end
end

pages.each_pair do |dest,page|
file dest => ([page[:template], layout, navfile] + page[:data]) do
output = Ecstatic::Page.new(page[:template], page[:data], layout, navfile, page[:url]).to_html
File.open(dest, 'w').write(output)
end
end

desc "Build website in '#{sitedir}' directory."
task :website => [sitedir] + pages.keys + files.keys

end
end

class Page
attr_accessor :contexthash, :layoutfile, :templatefile, :navhash, :url

def initialize(templatefile = nil, datafiles = [], layoutfile = nil, navfile = nil, url = nil)
@templatefile = templatefile
@layoutfile = layoutfile
Expand Down Expand Up @@ -38,7 +112,7 @@ def initialize(templatefile = nil, datafiles = [], layoutfile = nil, navfile = n
end
end
end

def escapefun(format)
case
when format == :html
Expand All @@ -49,10 +123,10 @@ def escapefun(format)
return 'escape'
end
end

def to_format(format)
engine = Tenjin::Engine.new(:cache => false, :escapefunc => escapefun(format))

if File.extname(self.templatefile) == '.markdown'
contents = File.open(self.templatefile).read
output = case
Expand All @@ -67,46 +141,46 @@ def to_format(format)
context = Tenjin::Context.new(self.contexthash)
output = engine.render(self.templatefile, context)
end

if self.layoutfile
return engine.render(self.layoutfile, {'_contents' => output, '_nav' => self.navhash, '_url' => self.url})
else
return output
end
end

def to_html
self.to_format(:html)
end

def to_latex
self.to_format(:latex)
end

def to_plain
self.to_format(:plain)
end
end

# escape functions

def markdown_to_html(str)
Markdown.new(str, :smart).to_html
end

def markdown_to_compact_html(str)
res = markdown_to_html(str)
if (res =~ /<p>.*<p>/)
return res
return res
else # only one paragraph
return res.gsub(/<\/?p>/,"")
end
end

def markdown_to_latex(str)
Markdown.new(str, :smart).to_latex
end

alias m markdown_to_html
module_function :markdown_to_html, :markdown_to_compact_html, :markdown_to_latex, :m
end
Expand Down
71 changes: 6 additions & 65 deletions samplesite/Rakefile
@@ -1,69 +1,10 @@
require 'rake/clean'
require 'rake'
require 'ecstatic'
require 'find'

# load user-defined data models
Find.find('models') do |f|
require f unless f == 'models'
end

SITEDIR = "site"
CLEAN.include(SITEDIR)

LAYOUT = "standard.rbhtml"
NAVFILE = "sitenav.yaml"

siteindex = YAML::load File.read("siteindex.yaml")

# construct list of pages
PAGES = {}
siteindex.each do |p|
dest = File.join SITEDIR, p['url']
PAGES[dest] = {
:url => p['url'],
:title => p['title'],
:template => p['template'],
:data => if p['data'].class == Array
p['data']
elsif p['data'].class == String
[p['data']]
else
[]
end }
end

# construct hash of files
FILESDIR = "files"
FILES = {}
Find.find(FILESDIR) do |f|
if f != FILESDIR
base = f.gsub(/^[^\/]*\//,"")
FILES[File.join(SITEDIR, base)] = f
end
end

task :default => :all
task :all => [SITEDIR] + PAGES.keys + FILES.keys

directory SITEDIR

FILES.each_pair do |dest,src|
file dest => src do
d = File.dirname dest
if ! File.exists?(d)
mkdir_p d
end
if ! File.directory? dest
cp src, dest
end
end
end

PAGES.each_pair do |dest,page|

file dest => ([page[:template], LAYOUT, NAVFILE] + page[:data]) do
output = Ecstatic::Page.new(page[:template], page[:data], LAYOUT, NAVFILE, page[:url]).to_html
File.open(dest, 'w').write(output)
end
Ecstatic::Tasks.website

desc "Upload website to server."
task :upload => :website do
puts "Not implemented!"
# Insert your own uploading code here, using rsync or whatever...
end

0 comments on commit 4140c26

Please sign in to comment.