4 require 'git-blog/core'
7 desc 'Setup the blog repository'
9 path = File.expand_path '.'
11 # Will create the repo if it doesn't exist
12 until (blog = Git.open path rescue false)
19 cp GitBlog::Scope / :prepped / '.gitignore', '.'
20 %w(welcome_to_your_git_blog.markdown .gitignore).each do |file|
21 cp GitBlog::Scope / :prepped / :posts / file, 'posts'
23 %w(main.css post.haml index.haml).each do |file|
24 cp GitBlog::Scope / :prepped / :design / file, 'design'
27 Dir['**/**'].each do |file|
28 if File.directory? file
35 chmod 0664, '.gitignore'
36 chmod 0664, :posts / '.gitignore'
39 blog.commit_all("A bird... no, a plane... no, a git-blog!")
42 desc 'Attach the blog to a GitHub repository - can also clone an existing git-blog repository here'
43 task :github, :user_name, :repo_name do |_, params|
44 path = File.expand_path '.'
45 user_name = params[:user_name].nil? ? %x(whoami).chomp : params[:user_name]
46 repo_name = params[:repo_name].nil? ? File.basename(path) : params[:repo_name]
48 github_url = "git@github.com:#{user_name.downcase}/#{repo_name.downcase}.git"
50 if File.directory? File.join(path, 'posts') and
51 (blog = Git.open path rescue false)
53 github = blog.add_remote 'github', github_url
57 system "git remote add -f github #{github_url}"
58 system "git checkout -b master github/master"
62 desc 'Prepare the blog to be served (configures hooks)'
64 should_be_initialized File.expand_path('.')
66 mv '.git' / :hooks / 'post-receive', '.git' / :hooks / 'post-receive.old'
67 cp GitBlog::Scope / :prepped / 'post-receive.hook', '.git' / :hooks / 'post-receive'
68 chmod 0775, '.git' / :hooks / 'post-receive'
70 puts '** git-blog is prepared for serving (git post-recieve hook prepared)'
73 desc 'Create and open for editing a new post'
76 temporary_post = :post_in_progress.markdown
78 should_be_initialized File.expand_path('.')
80 if File.file? temporary_post
81 puts '** You have an unfinished post from before,'
82 puts 'do you want to r)esume it or overwrite it with a n)ew one? '
87 # do nothing, go on to overwriting it
89 raise 'Invalid entry, exiting out'
94 File.open temporary_post, File::RDWR|File::TRUNC|File::CREAT, 0664 do |post|
95 post.puts 'Replace this text with your title!'
96 post.puts '=================================='
97 post.print "\n"; post.close
101 system "#{ENV['VISUAL']} #{temporary_post}"
103 first_line = File.open(temporary_post, File::RDONLY).gets.chomp
104 markup = case first_line
105 when /^\</ then 'xhtml'
106 when /^\%/ then 'haml'
107 when /^\#|h\d\./ then 'textile'
111 title = (first_line.match GitBlog::TitleRegexen[markup])[1]
112 path = :posts / title.slugize.send(markup.to_sym)
113 mv temporary_post, path
115 blog = Git.open File.expand_path('.')
117 blog.commit "New post: #{title}"
119 puts "Post '#{title.slugize}' comitted."
123 task :push, :remote_name do |_, params|
124 should_be_initialized File.expand_path('.')
126 remote_name = params[:remote_name].nil? ? 'github' : params[:remote_name]
128 remote = blog.remote remote_name
130 puts "Changes pushed to #{remote_name}."
133 desc 'Remove all generated xhtml'
135 Dir['posts/*.xhtml'].each do |path|
140 # desc 'Invisible task, generates index.xhtml'
142 path = File.expand_path('.')
143 repo = should_be_initialized path
145 commits = repo.log.select {|c| c.message =~ /New post: /}
147 commits.map do |commit|
148 title = commit.message.match(/New post: (.*)$/)[1]
149 posts << {:title => title, :slug => title.slugize}
152 template = IO.read :design / :index.haml
154 completed = Haml::Engine.new(template, :filename => :design / :index.haml).
155 to_html Object.new, :posts => posts
157 destination = :index.xhtml
158 file = File.open destination, File::RDWR|File::TRUNC|File::CREAT, 0664
161 puts "index.xhtml compiled"
164 desc 'Generate xhtml files from posts and design'
165 task :deploy => [:clobber, :index] do
166 should_be_initialized File.expand_path('.')
168 Dir['posts/*.*'].each do |path|
169 markup = File.extname(path).downcase.gsub(/^\./,'')
170 content = IO.read path
171 first_line = content.match(/^(.*)\n/)[1]
173 post_title = (first_line.match GitBlog::TitleRegexen[markup])[1]
175 require "git-blog/parser/#{markup.downcase}"
176 parser = GitBlog::Parsers.const_get(markup.gsub(/\b\w/){$&.upcase})
177 parser.send :parse, content
180 template = IO.read :design / :post.haml
182 completed = Haml::Engine.new(template, :filename => :design / :post.haml).
183 to_html Object.new, {:content => parsed, :title => post_title}
185 completed = GitBlog::Parsers::fix_pres(completed)
187 destination = path.gsub /.#{markup}$/, '.xhtml'
188 file = File.open destination, File::RDWR|File::TRUNC|File::CREAT, 0664
191 puts "#{path} -> #{destination} (as #{markup})"
196 # desc 'Invisible task, forces checkout (for the post-receive hook)'
197 task :force_checkout do
198 puts 'Forcing update of working copy...'
199 system 'git checkout -f master'
202 # desc 'Invisible task, run as the post-receive hook'
203 task :post_receive => [:force_checkout, :deploy]
205 def should_be_initialized path
206 raise "** You haven't used `rake initialize` yet." unless
207 File.directory? File.join(path, 'posts') and
208 (blog = Git.open path rescue false)