forked from jekyll/jekyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jekyll
executable file
·176 lines (138 loc) · 4.32 KB
/
jekyll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env ruby
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
help = <<HELP
Jekyll is a blog-aware, static site generator.
Basic Command Line Usage:
jekyll # . -> ./_site
jekyll <path to write generated site> # . -> <path>
jekyll <path to source> <path to write generated site> # <path> -> <path>
Configuration is read from '<source>/_config.yml' but can be overriden
using the following options:
HELP
require 'optparse'
require 'jekyll'
exec = {}
options = {}
opts = OptionParser.new do |opts|
opts.banner = help
opts.on("--auto", "Auto-regenerate") do
options['auto'] = true
end
opts.on("--no-auto", "No auto-regenerate") do
options['auto'] = false
end
opts.on("--no-incremental", "Disable incremental regeneration (only with auto)") do
options['incremental'] = false
end
opts.on("--server [PORT]", "Start web server (default port 4000)") do |port|
options['server'] = true
options['server_port'] = port unless port.nil?
end
opts.on("--lsi", "Use LSI for better related posts") do
options['lsi'] = true
end
opts.on("--pygments", "Use pygments to highlight code") do
options['pygments'] = true
end
opts.on("--rdiscount", "Use rdiscount gem for Markdown") do
options['markdown'] = 'rdiscount'
end
opts.on("--permalink [TYPE]", "Use 'date' (default) for YYYY/MM/DD") do |style|
options['permalink'] = style unless style.nil?
end
opts.on("--paginate [POSTS_PER_PAGE]", "Paginate a blog's posts") do |per_page|
begin
options['paginate'] = per_page.to_i
raise ArgumentError if options['paginate'] == 0
rescue
puts 'you must specify a number of posts by page bigger than 0'
exit 0
end
end
opts.on("--version", "Display current version") do
puts "Jekyll " + Jekyll.version
exit 0
end
end
# Read command line options into `options` hash
opts.parse!
# Get source and destintation from command line
case ARGV.size
when 0
when 1
options['destination'] = ARGV[0]
when 2
options['source'] = ARGV[0]
options['destination'] = ARGV[1]
else
puts "Invalid options. Run `jekyll --help` for assistance."
exit(1)
end
options = Jekyll.configuration(options)
# Get source and destination directories (possibly set by config file)
source = options['source']
destination = options['destination']
# Files to watch
def globs(source)
Dir.chdir(source) do
dirs = Dir['*'].select { |x| File.directory?(x) }
dirs -= ['_site']
dirs = dirs.map { |x| "#{x}/**/*" }
dirs += ['*']
end
end
# Create the Site
site = Jekyll::Site.new(options)
# Run the directory watcher for auto-generation, if required
if options['auto']
require 'directory_watcher'
incremental = true
incremental = options['incremental'] unless options['incremental'].nil?
puts "Auto-regenerating enabled: #{source} -> #{destination}"
puts "Incremental regeneration enabled" if incremental
dw = DirectoryWatcher.new(source)
dw.interval = 1
dw.glob = globs(source)
dw.add_observer do |*args|
t = Time.now.strftime("%Y-%m-%d %H:%M:%S")
puts "[#{t}] regeneration: #{args.size} files changed"
st = Time.now
# Check if files were only modified (not added/removed)
# to attempt incremental regeneration
mod_paths = args.collect { |e| e.path if e.type == :modified }.compact
if incremental && (args.size == mod_paths.size)
site.incremental(mod_paths)
else
site.process
end
f = Time.now
elapsed = format("%.2f",f - st)
puts "[#{f.strftime("%Y-%m-%d %H:%M:%S")}] regeneration finished: #{elapsed} seconds"
end
dw.start
unless options['server']
loop { sleep 1000 }
end
else
puts "Building site: #{source} -> #{destination}"
site.process
puts "Successfully generated site: #{source} -> #{destination}"
end
# Run the server on the specified port, if required
if options['server']
require 'webrick'
include WEBrick
FileUtils.mkdir_p(destination)
mime_types = WEBrick::HTTPUtils::DefaultMimeTypes
mime_types.store 'js', 'application/javascript'
s = HTTPServer.new(
:Port => options['server_port'],
:DocumentRoot => destination,
:MimeTypes => mime_types
)
t = Thread.new {
s.start
}
trap("INT") { s.shutdown }
t.join()
end