diff --git a/.gitignore b/.gitignore index 5cc80b6..df26258 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ backup .bundle .rsync_cache config.yml +content/* tmp/**/* tmp/restart.txt bin diff --git a/content/sample.md b/content/sample.md index 9fc0ab5..7245d08 100644 --- a/content/sample.md +++ b/content/sample.md @@ -1 +1 @@ -This is a page \ No newline at end of file +This **is** a page \ No newline at end of file diff --git a/page.rb b/page.rb index 9d9c35f..43b1a80 100644 --- a/page.rb +++ b/page.rb @@ -1,4 +1,5 @@ # encoding: utf-8 +require 'rdiscount' class Page attr_reader :name @@ -7,10 +8,10 @@ def Page.dir File.join(File.dirname(__FILE__),'content') end - def Page.list(excl=[]) - Dir.entries(Page.dir).map { |i| - i.chomp('.md') unless excl.include?(i) or i.match(%r/^\./i) - }.compact.sort_by {|c| + def Page.list + Dir.entries(Page.dir).reject! {|f| f.start_with?('.') }.map { |i| + i.chomp('.md') + }.compact.sort_by {|c| File.stat(File.join(Page.dir, "#{c}.md")).mtime }.reverse end @@ -24,14 +25,18 @@ def title name.gsub(" ", "_").downcase end - def exists?(excl=[]) - File.exists?(@fname) and not excl.include?(self.title) + def exists? + File.exists?(@fname) and not File.basename(@fname).start_with?('.') end def raw File.new(@fname).read end + def to_html + RDiscount.new(self.raw).to_html + end + def save!(content) File.open(@fname,"w+") { |f| f.write(content) } end diff --git a/spec/wiki_spec.rb b/spec/wiki_spec.rb index 553e145..c058880 100644 --- a/spec/wiki_spec.rb +++ b/spec/wiki_spec.rb @@ -15,7 +15,7 @@ end it 'should exists' do - Page.new('homepage').exists?($excl).should == true + Page.new('homepage').exists?.should == true end end @@ -32,7 +32,7 @@ context 'table of contents' do before { get '/contents' } it 'include all files' do - Page.list($excl).each_with_object([]) do |p,arr| + Page.list.each_with_object([]) do |p,arr| should match %r/#{link_to(p)}/i end end @@ -93,12 +93,12 @@ it 'delete page on empty content' do post @page.title, :content => '' - @page.exists?($excl).should == false + @page.exists?.should == false end it 'should not delete homepage' do post 'homepage', :content => '' - Page.new('homepage').exists?($excl).should == true + Page.new('homepage').exists?.should == true end end diff --git a/content/edit.erb b/views/edit.erb similarity index 100% rename from content/edit.erb rename to views/edit.erb diff --git a/content/layout.erb b/views/layout.erb similarity index 100% rename from content/layout.erb rename to views/layout.erb diff --git a/content/new.erb b/views/new.erb similarity index 100% rename from content/new.erb rename to views/new.erb diff --git a/wiki.rb b/wiki.rb index 89f0130..c560dce 100644 --- a/wiki.rb +++ b/wiki.rb @@ -2,7 +2,6 @@ require 'sinatra' require 'sinatra/config_file' -require 'rdiscount' require File.join(File.dirname(__FILE__),'page') module WikiHelpers @@ -14,9 +13,7 @@ def link_to(page) class SimpleWiki < Sinatra::Base configure do config_file File.join(File.dirname(__FILE__),'config.yml') - $excl = ['.', '..', 'layout.erb', 'edit.erb', 'new.erb'] set :markdown, :layout_engine => :erb - set :views, Page.dir end helpers do @@ -25,11 +22,11 @@ class SimpleWiki < Sinatra::Base get '/' do @page, @edit = Page.new('homepage'), true - markdown @page.title.to_sym + erb '<%= @page.to_html %>' end get '/contents' do - contents = Page.list($excl).each_with_object([]) do |p,arr| + contents = Page.list.each_with_object([]) do |p,arr| arr << "
  • #{link_to(p)}
  • " end.join erb '

    Table of Contents

    ' @@ -41,10 +38,10 @@ class SimpleWiki < Sinatra::Base #redirect to the page if there's an exact match in the title page = params[:q].gsub(" ", "_").downcase - redirect to("/#{page}") if Page.new(page).exists?($excl) + redirect to("/#{page}") if Page.new(page).exists? #finally search through files - results = Page.list($excl).each_with_object([]) do |p,arr| + results = Page.list.each_with_object([]) do |p,arr| page = Page.new(p) arr << "
  • #{link_to(p)}
  • " if page.raw.match %r/#{params[:q]}/i end.join @@ -58,7 +55,7 @@ class SimpleWiki < Sinatra::Base get '/new/:page' do |page| @page = Page.new(page) - if @page.exists?($excl) + if @page.exists? redirect @page.title.to_sym else erb :new @@ -79,8 +76,8 @@ class SimpleWiki < Sinatra::Base get '/:page' do |page| @page, @edit = Page.new(page), true - redirect "/new/#{page}" unless @page.exists?($excl) - markdown @page.title.to_sym + redirect "/new/#{page}" unless @page.exists? + erb '<%= @page.to_html %>' end post '/:page' do |page|