532687c9dfe262cf014f77024454859b51a45b3a
[blog.git] / Rakefile
1 # == Dependencies ==============================================================
2
3 require 'rake'
4 require 'yaml'
5 require 'fileutils'
6 require 'rbconfig'
7
8 # == Configuration =============================================================
9
10 # Set "rake watch" as default task
11 task :default => :watch
12
13 # Load the configuration file
14 CONFIG = YAML.load_file("_rakeconfig.yml")
15
16 # Get and parse the date
17 DATE = Time.now.strftime("%Y-%m-%d")
18 TIME = Time.now.strftime("%H:%M:%S")
19 POST_TIME = DATE + ' ' + TIME
20
21 # Directories
22 POSTS = "_posts"
23 DRAFTS = "_drafts"
24
25 # == Helpers ===================================================================
26
27 # Execute a system command
28 def execute(command)
29 system "#{command}"
30 end
31
32 # Chech the title
33 def check_title(title)
34 if title.nil? or title.empty?
35 raise "Please add a title to your file."
36 end
37 end
38
39 # Transform the filename and date to a slug
40 def transform_to_slug(title, extension)
41 characters = /("|'|!|\?|:|\s\z)/
42 whitespace = /\s/
43 "#{title.gsub(characters,"").gsub(whitespace,"-").downcase}.#{extension}"
44 end
45
46 # Read the template file
47 def read_file(template)
48 File.read(template)
49 end
50
51 # Save the file with the title in the YAML front matter
52 def write_file(content, title, directory, filename)
53 parsed_content = "#{content.sub("title:", "title: \"#{title}\"")}"
54 parsed_content = "#{parsed_content.sub("date:", "date: #{POST_TIME}")}"
55 File.write("#{directory}/#{filename}", parsed_content)
56 puts "#{filename} was created in '#{directory}'."
57 end
58
59 # Create the file with the slug and open the default editor
60 def create_file(directory, filename, content, title, editor)
61 FileUtils.mkdir(directory) unless File.exists?(directory)
62 if File.exists?("#{directory}/#{filename}")
63 raise "The file already exists."
64 else
65 write_file(content, title, directory, filename)
66 if editor && !editor.nil?
67 sleep 1
68 execute("#{editor} #{directory}/#{filename}")
69 end
70 end
71 end
72
73 # Get the "open" command
74 def open_command
75 if RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
76 "start"
77 elsif RbConfig::CONFIG["host_os"] =~ /darwin/
78 "open"
79 else
80 "xdg-open"
81 end
82 end
83
84 # == Tasks =====================================================================
85
86 # rake post["Title"]
87 desc "Create a post in _posts"
88 task :post, [:title] do |t, args|
89 args.with_defaults(:title => "Forgot to enter a title")
90 template = CONFIG["post"]["template"]
91 extension = CONFIG["post"]["extension"]
92 editor = CONFIG["editor"]
93 check_title(:title)
94 filename = "#{DATE}-#{transform_to_slug(:title, extension)}"
95 content = read_file(template)
96 create_file(POSTS, filename, content, :title, editor)
97 end
98
99 # rake draft["Title"]
100 desc "Create a post in _drafts"
101 task :draft, [:title, :category, :content] do |t, args|
102 if args.title == nil then
103 puts "Error! title is empty"
104 puts "Usage: draft[title,category,content]"
105 puts "Category and conten are optional"
106 puts "Category is a string; nil or empty for no category"
107 exit 1
108 end
109 template = CONFIG["post"]["template"]
110 extension = CONFIG["post"]["extension"]
111 editor = CONFIG["editor"]
112 title = args.title || "No title set"
113 filename = transform_to_slug(title, extension)
114 content = read_file(template)
115 create_file(DRAFTS, filename, content, title, editor)
116 end
117
118 # rake publish
119 desc "Move a post from _drafts to _posts"
120 task :publish do
121 extension = CONFIG["post"]["extension"]
122 files = Dir["#{DRAFTS}/*.#{extension}"]
123 files.each_with_index do |file, index|
124 puts "#{index + 1}: #{file}".sub("#{DRAFTS}/", "")
125 end
126 print "> "
127 number = $stdin.gets
128 if number =~ /\D/
129 filename = files[number.to_i - 1].sub("#{DRAFTS}/", "")
130 FileUtils.mv("#{DRAFTS}/#{filename}", "#{POSTS}/#{DATE}-#{filename}")
131 puts "#{filename} was moved to '#{POSTS}'."
132 else
133 puts "Please choose a draft by the assigned number."
134 end
135 end
136
137 # rake page["Title"]
138 # rake page["Title","Path/to/folder"]
139 desc "Create a page (optional filepath)"
140 task :page, :title, :path do |t, args|
141 title = args[:title]
142 template = CONFIG["page"]["template"]
143 extension = CONFIG["page"]["extension"]
144 editor = CONFIG["editor"]
145 directory = args[:path]
146 if directory.nil? or directory.empty?
147 directory = "./"
148 else
149 FileUtils.mkdir_p("#{directory}")
150 end
151 check_title(title)
152 filename = transform_to_slug(title, extension)
153 content = read_file(template)
154 create_file(directory, filename, content, title, editor)
155 end
156
157 # rake build
158 desc "Build the site, optionally with changed destination path"
159 task :build, :option do |t, args|
160 option = args[:option]
161 if option.nil? or option.empty?
162 execute("jekyll build")
163 else
164 execute("jekyll build -d #{option}")
165 end
166 end
167
168 # rake watch
169 # rake watch[number]
170 # rake watch["drafts"]
171 desc "Serve and watch the site (with post limit or drafts)"
172 task :watch, :option do |t, args|
173 option = args[:option]
174 interrupted = false
175 trap("INT") { interrupted = true }
176 if option.nil? or option.empty?
177 execute("jekyll serve --incremental --watch")
178 else
179 if option == "drafts"
180 execute("jekyll serve --watch --drafts")
181 else
182 execute("jekyll serve --watch --limit_posts #{option}")
183 end
184 end
185 if interrupted
186 execute("rm -rf _site/")
187 end
188 end
189
190 # rake preview
191 desc "Launch a preview of the site in the browser"
192 task :preview do
193 port = CONFIG["port"]
194 if port.nil? or port.empty?
195 port = 4000
196 end
197 Thread.new do
198 puts "Launching browser for preview..."
199 sleep 1
200 execute("#{open_command} http://localhost:#{port}/")
201 end
202 Rake::Task[:watch].invoke
203 end