Skip to content

Commit

Permalink
remove FS-releated methods from the page object
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyan committed Jan 10, 2012
1 parent 46acc55 commit 73a2ca8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
15 changes: 4 additions & 11 deletions page.rb
Expand Up @@ -3,15 +3,13 @@
class Page
attr_reader :name

EXCLUDES = ['.', '..', 'layout.erb', 'edit.erb', 'new.erb']

def Page.dir
File.join(File.dirname(__FILE__),'content')
end

def Page.list
def Page.list(excl=[])
Dir.entries(Page.dir).map { |i|
i.chomp('.md') unless EXCLUDES.include?(i) or i.match(%r/^\./i)
i.chomp('.md') unless excl.include?(i) or i.match(%r/^\./i)
}.compact.sort_by {|c|
File.stat(File.join(Page.dir, "#{c}.md")).mtime
}.reverse
Expand All @@ -20,25 +18,20 @@ def Page.list
def initialize(name)
@name = name
@fname = File.join(Page.dir,"#{self.title}.md")
@excl = ['.', '..', 'layout.erb', 'edit.erb', 'new.erb']
end

def title
name.gsub(" ", "_").downcase
end

def exists?
File.exists?(@fname) and not EXCLUDES.include?(self.title)
def exists?(excl=[])
File.exists?(@fname) and not excl.include?(self.title)
end

def raw
File.new(@fname).read
end

def to_link
'<a href="/' + name + '">' + name + '</a>'
end

def save!(content)
File.open(@fname,"w+") { |f| f.write(content) }
end
Expand Down
11 changes: 6 additions & 5 deletions spec/wiki_spec.rb
Expand Up @@ -2,6 +2,7 @@

describe 'Wiki' do
include Rack::Test::Methods
include WikiHelpers

let(:app) { SimpleWiki }
subject { last_response.body }
Expand All @@ -14,7 +15,7 @@
end

it 'should exists' do
Page.new('homepage').exists?.should == true
Page.new('homepage').exists?($excl).should == true
end
end

Expand All @@ -31,8 +32,8 @@
context 'table of contents' do
before { get '/contents' }
it 'include all files' do
Page.list.each_with_object([]) do |p,arr|
should match %r/#{Page.new(p).to_link}/i
Page.list($excl).each_with_object([]) do |p,arr|
should match %r/#{link_to(p)}/i
end
end
end
Expand Down Expand Up @@ -92,12 +93,12 @@

it 'delete page on empty content' do
post @page.title, :content => ''
@page.exists?.should == false
@page.exists?($excl).should == false
end

it 'should not delete homepage' do
post 'homepage', :content => ''
Page.new('homepage').exists?.should == true
Page.new('homepage').exists?($excl).should == true
end
end

Expand Down
25 changes: 18 additions & 7 deletions wiki.rb
Expand Up @@ -4,20 +4,31 @@
require 'rdiscount'
require File.join(File.dirname(__FILE__),'page')

module WikiHelpers
def link_to(page)
'<a href="/' + page + '">' + page + '</a>'
end
end

class SimpleWiki < Sinatra::Base
configure do
$excl = ['.', '..', 'layout.erb', 'edit.erb', 'new.erb']
set :markdown, :layout_engine => :erb
set :views, Page.dir
end

helpers do
include WikiHelpers
end

get '/' do
@page, @edit = Page.new('homepage'), true
markdown @page.title.to_sym
end

get '/contents' do
contents = Page.list.each_with_object([]) do |p,arr|
arr << "<li>#{Page.new(p).to_link}</li>"
contents = Page.list($excl).each_with_object([]) do |p,arr|
arr << "<li>#{link_to(p)}</li>"
end.join
erb '<h1>Table of Contents</h1><ul>' + contents + '</ul>'
end
Expand All @@ -28,12 +39,12 @@ 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?
redirect to("/#{page}") if Page.new(page).exists?($excl)

#finally search through files
results = Page.list.each_with_object([]) do |p,arr|
results = Page.list($excl).each_with_object([]) do |p,arr|
page = Page.new(p)
arr << "<li>#{page.to_link}</li>" if page.raw.match %r/#{params[:q]}/i
arr << "<li>#{link_to(p)}</li>" if page.raw.match %r/#{params[:q]}/i
end.join
erb "<h1>Search results for &quot;#{params[:q]}&quot;</h1><ul>" + results + '</ul>'
end
Expand All @@ -45,7 +56,7 @@ class SimpleWiki < Sinatra::Base

get '/new/:page' do |page|
@page = Page.new(page)
if @page.exists?
if @page.exists?($excl)
redirect @page.title.to_sym
else
erb :new
Expand All @@ -66,7 +77,7 @@ class SimpleWiki < Sinatra::Base

get '/:page' do |page|
@page, @edit = Page.new(page), true
redirect "/new/#{page}" unless @page.exists?
redirect "/new/#{page}" unless @page.exists?($excl)
markdown @page.title.to_sym
end

Expand Down

0 comments on commit 73a2ca8

Please sign in to comment.