4 require 'git-blog/core'
6 desc 'Setup the blog repository'
8 path = File.expand_path '.'
10 # Will create the repo if it doesn't exist
11 until (blog = Git.open path rescue false)
18 cp GitBlog::Scope / :prepped / '.gitignore', '.'
19 %w(welcome_to_your_git_blog.markdown .gitignore).each do |file|
20 cp GitBlog::Scope / :prepped / :posts / file, 'posts'
22 %w(main.css post.haml index.haml).each do |file|
23 cp GitBlog::Scope / :prepped / :design / file, 'design'
26 Dir['**/**'].each do |file|
27 if File.directory? file
34 chmod 0664, '.gitignore'
35 chmod 0664, :posts / '.gitignore'
38 blog.commit_all("A bird... no, a plane... no, a git-blog!")
41 desc 'Attach the blog to a GitHub repository - can also clone an existing git-blog repository here'
42 task :github, :user_name, :repo_name do |_, params|
43 path = File.expand_path '.'
44 user_name = params[:user_name].nil? ? %x(whoami).chomp : params[:user_name]
45 repo_name = params[:repo_name].nil? ? File.basename(path) : params[:repo_name]
47 github_url = "git@github.com:#{user_name.downcase}/#{repo_name.downcase}.git"
49 if File.directory? File.join(path, 'posts') and
50 (blog = Git.open path rescue false)
52 github = blog.add_remote 'github', github_url
56 system "git-remote add -f github #{github_url}"
57 system "git checkout -b master github/master"
61 desc 'Prepare the blog to be served (configures hooks)'
63 should_be_initialized File.expand_path('.')
65 mv '.git' / :hooks / 'post-receive', '.git' / :hooks / 'post-receive.old'
66 cp GitBlog::Scope / :prepped / 'post-receive.hook', '.git' / :hooks / 'post-receive'
67 chmod 0775, '.git' / :hooks / 'post-receive'
69 puts '** git-blog is prepared for serving (git post-recieve hook prepared)'
72 desc 'Create and open for editing a new post'
75 temporary_post = :post_in_progress.markdown
77 should_be_initialized File.expand_path('.')
79 if File.file? temporary_post
80 puts '** You have an unfinished post from before,'
81 puts 'do you want to r)esume it or overwrite it with a n)ew one? '
86 # do nothing, go on to overwriting it
88 raise 'Invalid entry, exiting out'
93 File.open temporary_post, File::RDWR|File::TRUNC|File::CREAT, 0664 do |post|
94 post.puts 'Replace this text with your title!'
95 post.puts '=================================='
96 post.print "\n"; post.close
100 system "#{ENV['VISUAL']} #{temporary_post}"
102 first_line = File.open(temporary_post, File::RDONLY).gets.chomp
103 markup = case first_line
104 when /^\</ then 'xhtml'
105 when /^\%/ then 'haml'
106 when /^\#|h\d\./ then 'textile'
110 title = (first_line.match GitBlog::TitleRegexen[markup])[1]
111 path = :posts / title.slugize.send(markup.to_sym)
112 mv temporary_post, path
114 blog = Git.open File.expand_path('.')
116 blog.commit "New post: #{title}"
118 puts "Post '#{title.slugize}' comitted."
122 task :push, :remote_name do |_, params|
123 should_be_initialized File.expand_path('.')
125 remote_name = params[:remote_name].nil? ? 'github' : params[:remote_name]
127 remote = blog.remote remote_name
129 puts "Changes pushed to #{remote_name}."
132 desc 'Remove all generated xhtml'
134 Dir['posts/*.xhtml'].each do |path|
139 # desc 'Invisible task, generates index.xhtml'
141 path = File.expand_path('.')
142 repo = should_be_initialized path
144 commits = repo.log.select {|c| c.message =~ /New post: /}
146 commits.map do |commit|
147 title = commit.message.match(/New post: (.*)$/)[1]
148 posts << {:title => title, :slug => title.slugize}
151 template = IO.read :design / :index.haml
153 completed = Haml::Engine.new(template, :filename => :design / :index.haml).
154 to_html Object.new, :posts => posts
156 destination = :index.xhtml
157 file = File.open destination, File::RDWR|File::TRUNC|File::CREAT, 0664
160 puts "index.xhtml compiled"
163 desc 'Generate xhtml files from posts and design'
164 task :deploy => [:clobber, :index] do
165 should_be_initialized File.expand_path('.')
167 Dir['posts/*.*'].each do |path|
168 markup = File.extname(path).downcase.gsub(/^\./,'')
169 content = IO.read path
170 first_line = content.match(/^(.*)\n/)[1]
172 post_title = (first_line.match GitBlog::TitleRegexen[markup])[1]
174 require "git-blog/parser/#{markup.downcase}"
175 parser = GitBlog::Parsers.const_get(markup.gsub(/\b\w/){$&.upcase})
176 parser.send :parse, content
179 template = IO.read :design / :post.haml
181 completed = Haml::Engine.new(template, :filename => :design / :post.haml).
182 to_html Object.new, {:content => parsed, :title => post_title}
184 completed = GitBlog::Parsers::fix_pres(completed)
186 destination = path.gsub /.#{markup}$/, '.xhtml'
187 file = File.open destination, File::RDWR|File::TRUNC|File::CREAT, 0664
190 puts "#{path} -> #{destination} (as #{markup})"
195 # desc 'Invisible task, forces checkout (for the post-receive hook)'
196 task :force_checkout do
197 puts 'Forcing update of working copy...'
198 system 'git-checkout -f master'
201 # desc 'Invisible task, run as the post-receive hook'
202 task :post_receive => [:force_checkout, :deploy]
204 def should_be_initialized path
205 raise "** You haven't used `rake initialize` yet." unless
206 File.directory? File.join(path, 'posts') and
207 (blog = Git.open path rescue false)