From f7b2941a302875b1738589add2cd83e118ed273f Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Thu, 15 Mar 2012 21:48:26 -0600 Subject: [PATCH] adding posts --- Gemfile | 1 + Gemfile.lock | 4 ++++ Rakefile | 18 ++++++++++++++++++ config.ru | 2 ++ posts/hello-world.md | 9 +++++++++ resources/blog_resource.rb | 30 ++++++++++++++++++++++++++++++ views/post.html.haml | 5 +++++ 7 files changed, 69 insertions(+) create mode 100644 Rakefile create mode 100644 posts/hello-world.md create mode 100644 resources/blog_resource.rb create mode 100644 views/post.html.haml diff --git a/Gemfile b/Gemfile index 0b29463..ecbe9f1 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,4 @@ gem 'webmachine', :git => "https://github.com/seancribbs/webmachine-ruby.git" gem 'rack' gem 'haml' +gem 'metadown' diff --git a/Gemfile.lock b/Gemfile.lock index 608d48b..ec5fed8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -10,12 +10,16 @@ GEM specs: haml (3.1.4) i18n (0.6.0) + metadown (1.0.1) + redcarpet rack (1.4.1) + redcarpet (2.1.0) PLATFORMS ruby DEPENDENCIES haml + metadown rack webmachine! diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..d002ba8 --- /dev/null +++ b/Rakefile @@ -0,0 +1,18 @@ +desc "Make a new post" +task :"new_post", :title do |t, args| + args.with_defaults(:title => 'new-post') + title = args.title + filename = "posts/#{title.downcase.gsub(/\W/, "-")}.md" + + puts "Creating new posts: #{filename}" + open(filename, 'w') do |post| + post.puts "---" + post.puts "title: \"#{title.gsub(/&/,'&')}\"" + post.puts "slug: \"#{title.gsub(/\W/, '-').downcase}\"" + post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}" + post.puts "---" + post.puts "" + post.puts "CONTENT HERE **IN MARKDOWN**" + end +end + diff --git a/config.ru b/config.ru index c59afe4..2f31c72 100644 --- a/config.ru +++ b/config.ru @@ -8,6 +8,8 @@ Dir["resources/*"].each {|f| require "./#{f}" } Shoes = Webmachine::Application.new do |app| app.routes do add [], ShoesHomepage + add ["blog"], BlogResource + add ["blog", :slug], BlogResource add ['*'], StaticResource, :root => "public" end app.configure do |config| diff --git a/posts/hello-world.md b/posts/hello-world.md new file mode 100644 index 0000000..e476705 --- /dev/null +++ b/posts/hello-world.md @@ -0,0 +1,9 @@ +--- +title: "Hello World" +slug: "hello-world" +date: 2012-03-15 21:47 +--- + +Hello, world! + +**MARKDOWN**!!!! diff --git a/resources/blog_resource.rb b/resources/blog_resource.rb new file mode 100644 index 0000000..24e7b97 --- /dev/null +++ b/resources/blog_resource.rb @@ -0,0 +1,30 @@ +require 'metadown' + +$posts = Dir["posts/*"].inject({}) do |hsh, file| + data = Metadown.render(File.read(file)) + + hsh[data.metadata["slug"]] = data + + hsh +end + +class BlogResource < Webmachine::Resource + def resource_exists? + return true unless request.path_info[:slug] #index + + @post = $posts[request.path_info[:slug]] if request.path_info[:slug] + !@post.nil? + end + + def to_html + return "INDEX" unless @post + + Haml::Engine.new(File.read("views/layout.html.haml")).render do + Haml::Engine.new(File.read("views/post.html.haml")).render(Object.new, + :contents => @post.output, + :title => @post.metadata["title"], + :date => @post.metadata["date"] + ) + end + end +end diff --git a/views/post.html.haml b/views/post.html.haml new file mode 100644 index 0000000..8704efb --- /dev/null +++ b/views/post.html.haml @@ -0,0 +1,5 @@ +%h1= title +%h3 Posted on #{date} +%hr/ + += contents