Skip to content

Commit

Permalink
reject all dotfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyan committed Jan 11, 2012
1 parent 736433b commit aa1b987
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
12 changes: 6 additions & 6 deletions page.rb
Expand Up @@ -8,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
Expand All @@ -25,8 +25,8 @@ 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
Expand Down
8 changes: 4 additions & 4 deletions spec/wiki_spec.rb
Expand Up @@ -15,7 +15,7 @@
end

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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
11 changes: 5 additions & 6 deletions wiki.rb
Expand Up @@ -13,7 +13,6 @@ def link_to(page)
class SimpleWiki < Sinatra::Base
configure do
config_file File.join(File.dirname(__FILE__),'config.yml')
$excl = ['.', '..', '.gitkeep']
set :markdown, :layout_engine => :erb
end

Expand All @@ -27,7 +26,7 @@ class SimpleWiki < Sinatra::Base
end

get '/contents' do
contents = Page.list($excl).each_with_object([]) do |p,arr|
contents = Page.list.each_with_object([]) do |p,arr|
arr << "<li>#{link_to(p)}</li>"
end.join
erb '<h1>Table of Contents</h1><ul>' + contents + '</ul>'
Expand All @@ -39,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 << "<li>#{link_to(p)}</li>" if page.raw.match %r/#{params[:q]}/i
end.join
Expand All @@ -56,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
Expand All @@ -77,7 +76,7 @@ class SimpleWiki < Sinatra::Base

get '/:page' do |page|
@page, @edit = Page.new(page), true
redirect "/new/#{page}" unless @page.exists?($excl)
redirect "/new/#{page}" unless @page.exists?
erb '<%= @page.to_html %>'
end

Expand Down

0 comments on commit aa1b987

Please sign in to comment.