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|
89 args.with_defaults(:title => "Forgot to enter a title")
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, :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"
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)
119 desc "Move a post from _drafts to _posts"
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}/", "")
129 filename = files[number.to_i - 1].sub("#{DRAFTS}/", "")
130 FileUtils.mv("#{DRAFTS}/#{filename}", "#{POSTS}/#{DATE}-#{filename}")
131 puts "#{filename} was moved to '#{POSTS}'."
133 puts "Please choose a draft by the assigned number."
138 # rake page["Title","Path/to/folder"]
139 desc "Create a page (optional filepath)"
140 task :page, :title, :path do |t, args|
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?
149 FileUtils.mkdir_p("#{directory}")
152 filename = transform_to_slug(title, extension)
153 content = read_file(template)
154 create_file(directory, filename, content, title, editor)
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")
164 execute("jekyll build -d #{option}")
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]
175 trap("INT") { interrupted = true }
176 if option.nil? or option.empty?
177 execute("jekyll serve --incremental --watch")
179 if option == "drafts"
180 execute("jekyll serve --watch --drafts")
182 execute("jekyll serve --watch --limit_posts #{option}")
186 execute("rm -rf _site/")
191 desc "Launch a preview of the site in the browser"
193 port = CONFIG["port"]
194 if port.nil? or port.empty?
198 puts "Launching browser for preview..."
200 execute("#{open_command} http://localhost:#{port}/")
202 Rake::Task[:watch].invoke