1 # == Dependencies ==============================================================
8 # == Configuration =============================================================
10 # Set "rake watch" as default task
11 task :default => :watch
13 # Load the configuration file
14 CONFIG = YAML.load_file("_rakeconfig.yml")
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
25 # == Helpers ===================================================================
27 # Execute a system command
33 def check_title(title)
34 if title.nil? or title.empty?
35 raise "Please add a title to your file."
39 # Transform the filename and date to a slug
40 def transform_to_slug(title, extension)
41 characters = /("|'|!|\?|:|\s\z)/
43 "#{title.gsub(characters,"").gsub(whitespace,"-").downcase}.#{extension}"
46 # Read the template file
47 def read_file(template)
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}'."
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."
65 write_file(content, title, directory, filename)
66 if editor && !editor.nil?
68 execute("#{editor} #{directory}/#{filename}")
73 # Get the "open" command
75 if RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
77 elsif RbConfig::CONFIG["host_os"] =~ /darwin/
84 # == Tasks =====================================================================
87 desc "Create a post in _posts"
88 task :post, :title do |t, args|
90 template = CONFIG["post"]["template"]
91 extension = CONFIG["post"]["extension"]
92 editor = CONFIG["editor"]
94 filename = "#{DATE}-#{transform_to_slug(title, extension)}"
95 content = read_file(template)
96 create_file(POSTS, filename, content, title, editor)
100 desc "Create a post in _drafts"
101 task :draft, :title do |t, args|
103 template = CONFIG["post"]["template"]
104 extension = CONFIG["post"]["extension"]
105 editor = CONFIG["editor"]
107 filename = transform_to_slug(title, extension)
108 content = read_file(template)
109 create_file(DRAFTS, filename, content, title, editor)
113 desc "Move a post from _drafts to _posts"
115 extension = CONFIG["post"]["extension"]
116 files = Dir["#{DRAFTS}/*.#{extension}"]
117 files.each_with_index do |file, index|
118 puts "#{index + 1}: #{file}".sub("#{DRAFTS}/", "")
123 filename = files[number.to_i - 1].sub("#{DRAFTS}/", "")
124 FileUtils.mv("#{DRAFTS}/#{filename}", "#{POSTS}/#{DATE}-#{filename}")
125 puts "#{filename} was moved to '#{POSTS}'."
127 puts "Please choose a draft by the assigned number."
132 # rake page["Title","Path/to/folder"]
133 desc "Create a page (optional filepath)"
134 task :page, :title, :path do |t, args|
136 template = CONFIG["page"]["template"]
137 extension = CONFIG["page"]["extension"]
138 editor = CONFIG["editor"]
139 directory = args[:path]
140 if directory.nil? or directory.empty?
143 FileUtils.mkdir_p("#{directory}")
146 filename = transform_to_slug(title, extension)
147 content = read_file(template)
148 create_file(directory, filename, content, title, editor)
152 desc "Build the site, optionally with changed destination path"
153 task :build, :option do |t, args|
154 option = args[:option]
155 if option.nil? or option.empty?
156 execute("jekyll build")
158 execute("jekyll build -d #{option}")
164 # rake watch["drafts"]
165 desc "Serve and watch the site (with post limit or drafts)"
166 task :watch, :option do |t, args|
167 option = args[:option]
169 trap("INT") { interrupted = true }
170 if option.nil? or option.empty?
171 execute("jekyll serve --incremental --watch")
173 if option == "drafts"
174 execute("jekyll serve --watch --drafts")
176 execute("jekyll serve --watch --limit_posts #{option}")
180 execute("rm -rf _site/")
185 desc "Launch a preview of the site in the browser"
187 port = CONFIG["port"]
188 if port.nil? or port.empty?
192 puts "Launching browser for preview..."
194 execute("#{open_command} http://localhost:#{port}/")
196 Rake::Task[:watch].invoke
199 # rake deploy["Commit message"]
200 desc "Deploy the site to a remote git repo"
201 task :deploy, :message do |t, args|
202 message = args[:message]
203 branch = CONFIG["git"]["branch"]
204 if message.nil? or message.empty?
205 raise "Please add a commit message."
207 if branch.nil? or branch.empty?
208 raise "Please add a branch."
210 Rake::Task[:build].invoke
212 execute("git commit -m \"#{message}\"")
213 execute("git push origin #{branch}")