Sharpie
Quick and easy static site generation with a familiar API.
Usage
class App < Sharpie::Base
get "/" do
"Hello, world."
end
# get posts from ActiveRecord or elsewhere
def self.posts
Post.all
end
posts.each do |post|
get "/posts/#{post.id}" do
erb :post
end
get "/posts/#{post.id}.json" do
post.to_json
end
end
end
App.build!("_site")
Or use with an existing Sinatra application:
class App < Sinatra::Base
register Sinatra::AdvancedRoutes # this is required
def self.posts
Post.all
end
posts.each do |post|
get "/posts/#{post['id']}.json" do
post.to_json
end
end
get "/" do
erb :index
end
end
builder = Sharpie::Builder.new(App)
builder.build!("_site")
Sharpie::Base
subclasses from Sinatra::Base
.
Installation
Add this line to your application's Gemfile:
gem 'sharpie'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sharpie
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Why
Sinatra routes make web app and API creation dead-simple. The syntax is straightforward and allows for your site to feel more like Ruby and less like a bloated framework. And there's also no reason why this powerful API should be limited to dynamic web applications. Sinatra routes simply describe a path and a response. Whether the response is evaluated ahead of time or on fly should not matter.
Why not Jekyll?
Jekyll is great for blogs and other types of sites where content is "hand created." That is, a human creates a file in a folder and writes some Markdown. Sharpie is more geared towards use-cases where the original content is already in a machine-readable format. For example, if you have a database whose contents you want to expose via REST API. You also don't want to maintain a server and you don't want to worry about scaling. With Sharpie, you can write such an API with very few (yet straightfoward) lines of code.
Fork
This software is mostly a fork of the sinatra-static gem by Paul Asmuth. Paul's code is very well-written. I'd rather copy and attribute it than re-invent a well-made wheel. I'm focusing on building an interface that suits my needs. More specifically, I want Sharpie to be part of a toolchain for rapidly developing static file-backed REST APIs.
License
This software, which is a fork of MIT-licensed software, is also MIT-licensed. See LICENSE.txt
.