Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pengwynn committed Apr 12, 2010
0 parents commit b5e2cf8
Show file tree
Hide file tree
Showing 352 changed files with 5,034 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .bundle/config
@@ -0,0 +1,2 @@
---
BUNDLE_WITHOUT: ""
7 changes: 7 additions & 0 deletions .components
@@ -0,0 +1,7 @@
---
:test: rspec
:renderer: haml
:script: jquery
:stylesheet: sass
:orm: none
:mock: none
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
.DS_Store
log/**/*
tmp/**/*
bin/*
vendor/gems/*
!vendor/gems/cache/
.sass-cache/*
23 changes: 23 additions & 0 deletions Gemfile
@@ -0,0 +1,23 @@
source :rubygems

# Project requirements
gem 'rake'
gem 'rack-flash'
gem 'thin' # or mongrel
gem "builder", "2.1.2"
gem "rdiscount", "1.5.8"
gem "RedCloth", "4.2.2"

# Component requirements
gem 'haml'

# Test requirements
gem 'rspec', :require => "spec", :group => "test"
gem 'rack-test', :require => 'rack/test', :group => 'test'

gem "hpricot", "0.8.2", :group => "test"
gem "rspec_hpricot_matchers", "1.0", :group => "test"
gem "test-unit", "1.2.3"

# Padrino
gem 'padrino', "0.9.9"
41 changes: 41 additions & 0 deletions app/app.rb
@@ -0,0 +1,41 @@
class Presto < Padrino::Application
configure do
register SassInitializer

##
# Application-specific configuration options
#
# set :raise_errors, true # Show exceptions (default for development)
# set :public, "foo/bar" # Location for static assets (default root/public)
# set :sessions, false # Enabled by default
# set :reload, false # Reload application files (default in development)
# set :default_builder, "foo" # Set a custom form builder (default 'StandardFormBuilder')
# set :locale_path, "bar" # Set path for I18n translations (default your_app/locales)
# disable :padrino_helpers # Disables padrino markup helpers (enabled by default if present)
# disable :padrino_mailer # Disables padrino mailer (enabled by default if present)
# disable :flash # Disables rack-flash (enabled by default)
# enable :authentication # Enable padrino-admin authentication (disabled by default)
# layout :my_layout # Layout can be in views/layouts/foo.ext or views/foo.ext (default :application)
#

set :haml, {:format => :html5 }

end

##
# You can configure for a specified environment like:
#
# configure :development do
# set :foo, :bar
# end
#

##
# You can manage errors like:
#
# error 404 do
# render 'errors/404'
# end
#

end
72 changes: 72 additions & 0 deletions app/controllers/pages.rb
@@ -0,0 +1,72 @@
Presto.controllers :pages do
# get :index, :map => "/foo/bar" do
# session[:foo] = "bar"
# render 'index'
# end

# get :sample, :map => "/sample/url", :respond_to => [:any, :js] do
# case content_type
# when :js then ...
# else ...
# end

# get :foo, :with => :id do
# "Maps to url '/foo/#{params[:id]}'"
# end

# get "/example" do
# "Hello world!"
# end

before do
@menu_items = Page.menu_items
@title = Nesta::Config.title
@subtitle = Nesta::Config.subtitle
@keywords = Nesta::Config.keywords
@description = Nesta::Config.description
@author = Nesta::Config.author
@google_analytics_code = Nesta::Config.google_analytics_code
@heading = @title

end

get :index, :map => '/' do

@title = "#{@title} - #{@subtitle}"
@articles = Page.find_articles[0..7]
@body_class = "home"
render 'pages/index'
end

get :feed, :map => '/feed' do
content_type :xml, :charset => "utf-8"
@articles = Page.find_articles.select { |a| a.date }[0..9]
render 'pages/atom'
end

get :sitemap, :map => "/sitemap.xml" do
content_type :xml, :charset => "utf-8"
@pages = Page.find_all
@last = @pages.map { |page| page.last_modified }.inject do |latest, page|
(page > latest) ? page : latest
end
render 'pages/sitemap'
end

get :attachments, :map => '/attachments/{:filename,(\w|\-|\.)}' do
puts params[:filename]
file = File.join(
Nesta::Config.attachment_path, params[:filename])
send_file(file, :disposition => nil)
end

get :catchall, :map => '*splat' do
@page = Page.find_by_path(File.join(params[:splat]))
raise Sinatra::NotFound if @page.nil?
@title = @page.title
@description = @page.description
@keywords = @page.keywords
render 'pages/page'
end

end
36 changes: 36 additions & 0 deletions app/helpers/pages_helper.rb
@@ -0,0 +1,36 @@
# Helper methods defined here can be accessed in any controller or view in the application

Presto.helpers do

def nesta_atom_id_for_page(page)
published = page.date.strftime('%Y-%m-%d')
"tag:#{request.host},#{published}:#{page.abspath}"
end

def atom_id(page = nil)
if page
page.atom_id || nesta_atom_id_for_page(page)
else
"tag:#{request.host},2009:/"
end
end

def url_for(page)
File.join(base_url, page.path)
end

def base_url
url = "http://#{request.host}"
request.port == 80 ? url : url + ":#{request.port}"
end

def absolute_urls(text)
text.gsub!(/(<a href=['"])\//, '\1' + base_url + '/')
text
end

def format_date(date)
date.strftime("%d %B %Y")
end

end
109 changes: 109 additions & 0 deletions app/models/file_model.rb
@@ -0,0 +1,109 @@
class FileModel
FORMATS = [:mdown, :haml, :textile]
@@cache = {}

attr_reader :filename, :mtime

def self.model_path(basename = nil)
Nesta::Config.content_path(basename)
end

def self.find_all
file_pattern = File.join(model_path, "**", "*.{#{FORMATS.join(',')}}")
Dir.glob(file_pattern).map do |path|
relative = path.sub("#{model_path}/", "")
load(relative.sub(/\.(#{FORMATS.join('|')})/, ""))
end
end

def self.needs_loading?(path, filename)
@@cache[path].nil? || File.mtime(filename) > @@cache[path].mtime
end

def self.load(path)
FORMATS.each do |format|
filename = model_path("#{path}.#{format}")
if File.exist?(filename) && needs_loading?(path, filename)
@@cache[path] = self.new(filename)
break
end
end
@@cache[path]
end

def self.purge_cache
@@cache = {}
end

def initialize(filename)
@filename = filename
@format = filename.split(".").last.to_sym
parse_file
@mtime = File.mtime(filename)
end

def permalink
File.basename(@filename, ".*")
end

def path
abspath.sub(/^\//, "")
end

def abspath
prefix = File.dirname(@filename).sub(Nesta::Config.page_path, "")
File.join(prefix, permalink)
end

def to_html
case @format
when :mdown, :markdown, :md
RDiscount.new(markup).to_html
when :haml
Haml::Engine.new(markup).to_html
when :textile
RedCloth.new(markup).to_html
end
end

def last_modified
@last_modified ||= File.stat(@filename).mtime
end

def description
metadata("description")
end

def keywords
metadata("keywords")
end

private
def markup
@markup
end

def metadata(key)
@metadata[key]
end

def paragraph_is_metadata(text)
text.split("\n").first =~ /^[\w ]+:/
end

def parse_file
first_para, remaining = File.open(@filename).read.split(/\r?\n\r?\n/, 2)
@metadata = {}
if paragraph_is_metadata(first_para)
@markup = remaining
for line in first_para.split("\n") do
key, value = line.split(/\s*:\s*/, 2)
@metadata[key.downcase] = value.chomp
end
else
@markup = [first_para, remaining].join("\n\n")
end
rescue Errno::ENOENT # file not found
raise Sinatra::NotFound
end
end

0 comments on commit b5e2cf8

Please sign in to comment.