smeagol 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/smeagol +4 -59
- data/lib/smeagol.rb +1 -0
- data/lib/smeagol/app.rb +21 -0
- data/lib/smeagol/option_parser.rb +123 -0
- data/lib/smeagol/version.rb +1 -1
- metadata +5 -4
data/bin/smeagol
CHANGED
@@ -6,78 +6,23 @@ require 'ostruct'
|
|
6
6
|
require File.expand_path(File.dirname(__FILE__) + '/../lib/smeagol')
|
7
7
|
|
8
8
|
# Parse options
|
9
|
-
options =
|
10
|
-
options.port = 4567
|
11
|
-
options.auto_update = false
|
12
|
-
options.cache_enabled = true
|
13
|
-
opts = OptionParser.new do |opts|
|
14
|
-
opts.banner = 'usage: smeagol [OPTIONS] [PATH]\n\n'
|
15
|
-
|
16
|
-
opts.on('--port [PORT]', 'Bind port (default 4567).') do |port|
|
17
|
-
options.port = port.to_i
|
18
|
-
end
|
19
|
-
|
20
|
-
opts.on('--git [GIT]', 'Path to Git binary.') do |path|
|
21
|
-
options.git = path
|
22
|
-
end
|
23
|
-
|
24
|
-
opts.on('--autoupdate', 'Updates the repository on a daily basis.') do |flag|
|
25
|
-
options.auto_update = flag
|
26
|
-
end
|
27
|
-
|
28
|
-
opts.on('--[no-]cache', 'Enables page caching.') do |flag|
|
29
|
-
options.cache_enabled = flag
|
30
|
-
end
|
31
|
-
|
32
|
-
opts.on('-v', '--version', 'Display current version.') do
|
33
|
-
puts "Smeagol #{Smeagol::VERSION}"
|
34
|
-
exit 0
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
# Read command line options into `options` hash
|
39
|
-
begin
|
40
|
-
opts.parse!
|
41
|
-
rescue OptionParser::InvalidOption
|
42
|
-
puts "smeagol: #{$!.message}"
|
43
|
-
puts "smeagol: try 'smeagol --help' for more information"
|
44
|
-
exit
|
45
|
-
end
|
46
|
-
|
47
|
-
# Attempt to find the git binary
|
48
|
-
if options.git.nil?
|
49
|
-
['/usr/bin', '/usr/sbin', '/usr/local/bin', '/opt/local/bin'].each do |path|
|
50
|
-
file = "#{path}/git"
|
51
|
-
options.git = file if File.executable?(file)
|
52
|
-
break if options.git
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
# Alert user that updates are unavailable if git is not found
|
57
|
-
if options.git.nil? || !File.executable?(options.git)
|
58
|
-
puts "warning: git executable could not be found."
|
59
|
-
else
|
60
|
-
puts "git found: #{options.git}"
|
61
|
-
end
|
62
|
-
|
63
|
-
# Set the path to the Gollum repository
|
64
|
-
gollum_path = ARGV[0] || Dir.pwd
|
9
|
+
options = Smeagol::OptionParser.parse(ARGV)
|
65
10
|
|
66
11
|
# Run the auto update process
|
67
12
|
if options.git && options.auto_update
|
68
13
|
Thread.new do
|
69
14
|
while true do
|
70
15
|
sleep 86400
|
71
|
-
Smeagol::Updater.update(options.git, gollum_path)
|
16
|
+
Smeagol::Updater.update(options.git, options.gollum_path)
|
72
17
|
end
|
73
18
|
end
|
74
19
|
end
|
75
20
|
|
76
21
|
# Clear the cache
|
77
|
-
Smeagol::Cache.new(Gollum::Wiki.new(gollum_path)).clear()
|
22
|
+
Smeagol::Cache.new(Gollum::Wiki.new(options.gollum_path)).clear()
|
78
23
|
|
79
24
|
# Run the web server
|
80
|
-
Smeagol::App.set(:gollum_path, gollum_path)
|
25
|
+
Smeagol::App.set(:gollum_path, options.gollum_path)
|
81
26
|
Smeagol::App.set(:git, options.git)
|
82
27
|
Smeagol::App.set(:cache_enabled, options.cache_enabled)
|
83
28
|
Smeagol::App.run!(:port => options.port)
|
data/lib/smeagol.rb
CHANGED
data/lib/smeagol/app.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'gollum'
|
2
|
+
require 'rack/file'
|
2
3
|
require 'sinatra'
|
3
4
|
require 'mustache'
|
4
5
|
require 'tmpdir'
|
@@ -34,7 +35,9 @@ module Smeagol
|
|
34
35
|
get '/*' do
|
35
36
|
name = params[:splat].first
|
36
37
|
name = "Home" if name == ""
|
38
|
+
name = name.gsub(/\/+$/, '')
|
37
39
|
name = File.sanitize_path(name)
|
40
|
+
file_path = "#{settings.gollum_path}/#{name}"
|
38
41
|
|
39
42
|
# Load the wiki settings
|
40
43
|
wiki = Smeagol::Wiki.new(settings.gollum_path)
|
@@ -48,8 +51,12 @@ module Smeagol
|
|
48
51
|
content = Mustache.render(page_template, Smeagol::Views::Page.new(page))
|
49
52
|
cache.set_page(name, content) if settings.cache_enabled
|
50
53
|
content
|
54
|
+
# If it is a directory, redirect to the index page
|
55
|
+
elsif File.directory?(file_path)
|
56
|
+
redirect "/#{name}/index.html"
|
51
57
|
# If it is not a wiki page then try to find the file
|
52
58
|
elsif file = wiki.file(name)
|
59
|
+
content_type get_mime_type(name)
|
53
60
|
file.raw_data
|
54
61
|
# Otherwise return a 404 error
|
55
62
|
else
|
@@ -77,5 +84,19 @@ module Smeagol
|
|
77
84
|
IO.read(File.join(File.dirname(__FILE__), 'templates/page.mustache'))
|
78
85
|
end
|
79
86
|
end
|
87
|
+
|
88
|
+
# Retrieves the mime type for a filename based on its extension.
|
89
|
+
#
|
90
|
+
# file - The filename.
|
91
|
+
#
|
92
|
+
# Returns the mime type for a file.
|
93
|
+
def get_mime_type(file)
|
94
|
+
if !file.nil?
|
95
|
+
extension = file.slice(file.rindex('.')..-1) if file.rindex('.')
|
96
|
+
return Rack::Mime::MIME_TYPES[extension] || 'text/plain'
|
97
|
+
end
|
98
|
+
|
99
|
+
return 'text/plain'
|
100
|
+
end
|
80
101
|
end
|
81
102
|
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
module Smeagol
|
2
|
+
class OptionParser
|
3
|
+
# Parses the command line options.
|
4
|
+
#
|
5
|
+
# args - The arguments to parse. Typically use `ARGV`.
|
6
|
+
#
|
7
|
+
# Returns an OpenStruct of the options.
|
8
|
+
def self.parse(args)
|
9
|
+
# Set parse options
|
10
|
+
options = {}
|
11
|
+
parser = ::OptionParser.new do |parser|
|
12
|
+
parser.banner = 'usage: smeagol [OPTIONS] [PATH]\n\n'
|
13
|
+
|
14
|
+
parser.on('--port [PORT]', 'Bind port (default 4567).') do |port|
|
15
|
+
options['port'] = port.to_i
|
16
|
+
end
|
17
|
+
|
18
|
+
parser.on('--git [GIT]', 'Path to Git binary.') do |path|
|
19
|
+
options['git'] = path
|
20
|
+
end
|
21
|
+
|
22
|
+
parser.on('-c', '--config [PATH]', 'Loads the config file. (default /etc/smeagol/config.yml)') do |path|
|
23
|
+
puts "Cannot find configuration file: #{path}" unless File.exists?(path)
|
24
|
+
puts "Cannot read configuration file: #{path}" unless File.readable?(path)
|
25
|
+
options['config_path'] = path
|
26
|
+
end
|
27
|
+
|
28
|
+
parser.on('--autoupdate', 'Updates the repository on a daily basis.') do |flag|
|
29
|
+
options['auto_update'] = flag
|
30
|
+
end
|
31
|
+
|
32
|
+
parser.on('--[no-]cache', 'Enables page caching.') do |flag|
|
33
|
+
options['cache_enabled'] = flag
|
34
|
+
end
|
35
|
+
|
36
|
+
parser.on('-v', '--version', 'Display current version.') do
|
37
|
+
puts "Smeagol #{Smeagol::VERSION}"
|
38
|
+
exit 0
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Read command line options into `options` hash
|
43
|
+
begin
|
44
|
+
parser.parse!
|
45
|
+
rescue ::OptionParser::InvalidOption
|
46
|
+
puts "smeagol: #{$!.message}"
|
47
|
+
puts "smeagol: try 'smeagol --help' for more information"
|
48
|
+
exit
|
49
|
+
end
|
50
|
+
|
51
|
+
# Load config
|
52
|
+
config_path = options['config_path'] || '/etc/smeagol/config.yml'
|
53
|
+
config = load_config(config_path)
|
54
|
+
opts = default_options.merge(config).merge(options)
|
55
|
+
opts = opts.to_ostruct()
|
56
|
+
|
57
|
+
# Attempt to find the git binary
|
58
|
+
update_git_path(opts)
|
59
|
+
|
60
|
+
# Set the path to the Gollum repository if not already set
|
61
|
+
opts.gollum_path ||= args[0] || Dir.pwd
|
62
|
+
|
63
|
+
# Merge all options
|
64
|
+
return opts
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
###########################################################################
|
69
|
+
#
|
70
|
+
# Private Methods
|
71
|
+
#
|
72
|
+
###########################################################################
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
# The default options for Smeagol.
|
77
|
+
def self.default_options
|
78
|
+
options = Hash.new
|
79
|
+
options['port'] = 4567
|
80
|
+
options['auto_update'] = false
|
81
|
+
options['cache_enabled'] = true
|
82
|
+
options
|
83
|
+
end
|
84
|
+
|
85
|
+
# Loads a configuration file.
|
86
|
+
#
|
87
|
+
# Returns a hash of options from the config file.
|
88
|
+
def self.load_config(path)
|
89
|
+
config = {}
|
90
|
+
|
91
|
+
if File.exists?(path)
|
92
|
+
# Notify the user if the config file exists but is not readable.
|
93
|
+
if !File.readable?(path)
|
94
|
+
puts "Config file not readable: #{path}"
|
95
|
+
exit
|
96
|
+
end
|
97
|
+
|
98
|
+
config = YAML.load(IO.read(path))
|
99
|
+
p config
|
100
|
+
end
|
101
|
+
|
102
|
+
return config
|
103
|
+
end
|
104
|
+
|
105
|
+
# Locates the git binary in common places in the file system.
|
106
|
+
def self.update_git_path(options)
|
107
|
+
if options.git.nil?
|
108
|
+
['/usr/bin', '/usr/sbin', '/usr/local/bin', '/opt/local/bin'].each do |path|
|
109
|
+
file = "#{path}/git"
|
110
|
+
options.git = file if File.executable?(file)
|
111
|
+
break if options.git
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# Alert user that updates are unavailable if git is not found
|
116
|
+
if options.git.nil? || !File.executable?(options.git)
|
117
|
+
puts "warning: git executable could not be found."
|
118
|
+
else
|
119
|
+
puts "git found: #{options.git}"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
data/lib/smeagol/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smeagol
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 4
|
10
|
+
version: 0.2.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ben Johnson
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-30 00:00:00 -06:00
|
19
19
|
default_executable: smeagol
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -251,6 +251,7 @@ files:
|
|
251
251
|
- lib/smeagol/app.rb
|
252
252
|
- lib/smeagol/cache.rb
|
253
253
|
- lib/smeagol/hash.rb
|
254
|
+
- lib/smeagol/option_parser.rb
|
254
255
|
- lib/smeagol/public/smeagol/main.css
|
255
256
|
- lib/smeagol/public/smeagol/pygment.css
|
256
257
|
- lib/smeagol/templates/page.mustache
|