Added --page-file-dir option to gollum command

This commit is contained in:
Hiroshi Saito
2010-10-07 17:55:40 +09:00
parent 33aad801bd
commit fcdffc39ff
4 changed files with 65 additions and 12 deletions
+7 -1
View File
@@ -19,6 +19,7 @@ require 'gollum'
exec = {}
options = { 'port' => 4567, 'bind' => '127.0.0.1' }
wiki_options = {}
opts = OptionParser.new do |opts|
opts.banner = help
@@ -38,6 +39,10 @@ opts = OptionParser.new do |opts|
opts.on("--irb", "Start an irb process with gollum loaded for the current wiki.") do
options['irb'] = true
end
opts.on("--page-file-dir [PATH]", "Specify the sub directory for all page files (default: repository root).") do |path|
wiki_options[:page_file_dir] = path
end
end
# Read command line options into `options` hash
@@ -77,7 +82,7 @@ if options['irb']
end
begin
wiki = Gollum::Wiki.new(gollum_path)
wiki = Gollum::Wiki.new(gollum_path, wiki_options)
if !wiki.exist? then raise Grit::InvalidGitRepositoryError end
puts "Loaded Gollum wiki at #{File.expand_path(gollum_path).inspect}."
puts
@@ -99,5 +104,6 @@ if options['irb']
else
require 'gollum/frontend/app'
Precious::App.set(:gollum_path, gollum_path)
Precious::App.set(:wiki_options, wiki_options)
Precious::App.run!(options)
end
+9 -9
View File
@@ -42,7 +42,7 @@ module Precious
get '/edit/*' do
@name = params[:splat].first
wiki = Gollum::Wiki.new(settings.gollum_path)
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
if page = wiki.page(@name)
@page = page
@content = page.raw_data
@@ -54,7 +54,7 @@ module Precious
post '/edit/*' do
name = params[:splat].first
wiki = Gollum::Wiki.new(settings.gollum_path)
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
page = wiki.page(name)
format = params[:format].intern
name = params[:rename] if params[:rename]
@@ -66,7 +66,7 @@ module Precious
post '/create/*' do
name = params[:page]
wiki = Gollum::Wiki.new(settings.gollum_path)
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
format = params[:format].intern
@@ -82,13 +82,13 @@ module Precious
post '/preview' do
format = params['wiki_format']
data = params['text']
wiki = Gollum::Wiki.new(settings.gollum_path)
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
wiki.preview_page("Preview", data, format).formatted_data
end
get '/history/:name' do
@name = params[:name]
wiki = Gollum::Wiki.new(settings.gollum_path)
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
@page = wiki.page(@name)
@page_num = [params[:page].to_i, 1].max
@versions = @page.versions :page => @page_num
@@ -110,7 +110,7 @@ module Precious
get '/compare/:name/:version_list' do
@name = params[:name]
@versions = params[:version_list].split(/\.{2,3}/)
wiki = Gollum::Wiki.new(settings.gollum_path)
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
@page = wiki.page(@name)
diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path)
@diff = diffs.first
@@ -119,7 +119,7 @@ module Precious
get %r{/(.+?)/([0-9a-f]{40})} do
name = params[:captures][0]
wiki = Gollum::Wiki.new(settings.gollum_path)
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
if page = wiki.page(name, params[:captures][1])
@page = page
@name = name
@@ -132,7 +132,7 @@ module Precious
get '/search' do
@query = params[:q]
wiki = Gollum::Wiki.new(settings.gollum_path)
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
@results = wiki.search @query
mustache :search
end
@@ -142,7 +142,7 @@ module Precious
end
def show_page_or_file(name)
wiki = Gollum::Wiki.new(settings.gollum_path)
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
if page = wiki.page(name)
@page = page
@name = name
+13 -2
View File
@@ -46,6 +46,8 @@ module Gollum
# to "/".
attr_reader :base_path
attr_reader :page_file_dir
# Public: Initialize a new Gollum Repo.
#
# repo - The String path to the Git repository that holds the Gollum
@@ -55,6 +57,7 @@ module Gollum
# Default: "/"
# :page_class - The page Class. Default: Gollum::Page
# :file_class - The file Class. Default: Gollum::File
# :page_file_dir - String the directory in which all page files reside
#
# Returns a fresh Gollum::Repo.
def initialize(path, options = {})
@@ -63,6 +66,7 @@ module Gollum
@base_path = options[:base_path] || "/"
@page_class = options[:page_class] || self.class.page_class
@file_class = options[:file_class] || self.class.file_class
@page_file_dir = options[:page_file_dir]
clear_cache
end
@@ -339,6 +343,9 @@ module Gollum
else
::File.join(dir, page_file_name(name, format))
end
if @page_file_dir
path = ::File.join(@page_file_dir, path)
end
Dir.chdir(::File.join(@repo.path, '..')) do
if file_path_scheduled_for_deletion?(index.tree, path)
@@ -433,7 +440,7 @@ module Gollum
dir = '/' if dir.strip.empty?
fullpath = ::File.join(dir, path)
fullpath = ::File.join(*[@page_file_dir, dir, path].compact)
fullpath = fullpath[1..-1] if fullpath =~ /^\//
if index.current_tree && tree = index.current_tree / dir
@@ -507,7 +514,11 @@ module Gollum
tree.split("\0").each do |line|
items << parse_tree_line(line)
end
items
if dir = @page_file_dir
items.select{|i| i.path =~ /^#{dir}\// }
else
items
end
end
# Parses a line of output from the `ls-tree` command.
+36
View File
@@ -282,3 +282,39 @@ context "Wiki sync with working directory" do
FileUtils.rm_r(@path)
end
end
context "page_file_dir option" do
setup do
@path = testpath('examples/pfdtest')
@repo = Grit::Repo.init(@path)
@page_file_dir = 'docs'
Dir.chdir(@path) do
Dir.mkdir(@page_file_dir)
File.open("docs/foo.md", "w"){|f| f.print "Hello foo" }
@repo.add("docs/foo.md")
File.open("bar.md", "w"){|f| f.print "Hello bar" }
@repo.add("bar.md")
@repo.commit_index("Added docs/foo.md and bar.md")
end
@wiki = Gollum::Wiki.new(@path, :page_file_dir => @page_file_dir)
end
test "write a page in sub directory" do
@wiki.write_page("New Page", :markdown, "Hi", commit_details)
assert_equal "Hi", File.read(File.join(@path, @page_file_dir, "New-Page.md"))
assert !File.exist?(File.join(@path, "New-Page.md"))
end
test "a file in page file dir should be found" do
assert @wiki.page("foo")
end
test "a file out of page file dir should not be found" do
assert !@wiki.page("bar")
end
teardown do
FileUtils.rm_r(@path)
end
end