Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'develop' into separate_content_dir
  • Loading branch information
stoyan committed Jan 11, 2012
2 parents 446654b + 4c1e1b5 commit 640040f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,6 +9,7 @@ backup
.bundle
.rsync_cache
config.yml
content/*
tmp/**/*
tmp/restart.txt
bin
Expand Down
2 changes: 1 addition & 1 deletion content/sample.md
@@ -1 +1 @@
This is a page
This **is** a page
17 changes: 11 additions & 6 deletions page.rb
@@ -1,4 +1,5 @@
# encoding: utf-8
require 'rdiscount'

class Page
attr_reader :name
Expand All @@ -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
Expand All @@ -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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 7 additions & 10 deletions wiki.rb
Expand Up @@ -2,7 +2,6 @@

require 'sinatra'
require 'sinatra/config_file'
require 'rdiscount'
require File.join(File.dirname(__FILE__),'page')

module WikiHelpers
Expand All @@ -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
Expand All @@ -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 << "<li>#{link_to(p)}</li>"
end.join
erb '<h1>Table of Contents</h1><ul>' + contents + '</ul>'
Expand All @@ -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 << "<li>#{link_to(p)}</li>" if page.raw.match %r/#{params[:q]}/i
end.join
Expand All @@ -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
Expand All @@ -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|
Expand Down

0 comments on commit 640040f

Please sign in to comment.