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::Location / :prepped / '.gitignore', '.'
19 %w(welcome_to_your_git_blog.markdown .gitignore).each do |file|
20 cp GitBlog::Location / :prepped / :posts / file, 'posts'
22 %w(main.css post.haml index.haml).each do |file|
23 cp GitBlog::Location / :prepped / :design / file, 'design'
26 Dir['**/**'].each do |file|
27 if File.directory? file
35 blog.commit_all("A bird... no, a plane... no, a git-blog!")
38 desc 'Attach the blog to a GitHub repository - can also clone an existing git-blog repository here'
39 task :github, :user_name, :repo_name do |_, params|
40 path = File.expand_path '.'
41 user_name = params[:user_name].nil? ? %x(whoami).chomp : params[:user_name]
42 repo_name = params[:repo_name].nil? ? File.basename(path) : params[:repo_name]
44 github_url = "git@github.com:#{user_name.downcase}/#{repo_name.downcase}.git"
46 if File.directory? File.join(path, 'posts') and
47 (blog = Git.open path rescue false)
49 github = blog.add_remote 'github', github_url
53 system "git-remote add -f github #{github_url}"
54 system "git checkout -b master github/master"
58 desc 'Prepare the blog to be served (configures hooks)'
60 should_be_initialized File.expand_path('.')
61 mv_f '.git' / :hooks / 'post-receive', '.git' / :hooks / 'post-receive.old'
62 cp GitBlog::Location / :prepped / 'post-receive.hook', '.git' / :hooks / 'post-receive'
63 chmod 0775, '.git' / :hooks / 'post-receive'
64 puts '** git-blog is prepared for serving (git post-recieve hook prepared)'
67 desc 'Create and open for editing a new post'
70 temporary_post = :post_in_progress.markdown
72 should_be_initialized File.expand_path('.')
74 if File.file? temporary_post
75 puts '** You have an unfinished post from before,'
76 puts 'do you want to r)esume it or overwrite it with a n)ew one? '
81 # do nothing, go on to overwriting it
83 raise 'Invalid entry, exiting out'
88 File.open temporary_post, File::RDWR|File::TRUNC|File::CREAT, 0664 do |post|
89 post.puts 'Replace this text with your title!'
90 post.puts '=================================='
91 post.print "\n"; post.close
95 system "#{ENV['VISUAL']} #{temporary_post}"
97 first_line = File.open(temporary_post, File::RDONLY).gets.chomp
98 markup = case first_line
99 when /^\</ then 'xhtml'
100 when /^\%/ then 'haml'
101 when /^\#|h\d\./ then 'textile'
105 title = (first_line.match GitBlog::TitleRegexen[markup])[1]
106 path = :posts / title.slugize.send(markup.to_sym)
107 mv temporary_post, path
109 blog = Git.open File.expand_path('.')
111 blog.commit "New post: #{title}"
113 puts "Post '#{title.slugize}' comitted."
117 task :push, :remote_name do |_, params|
118 should_be_initialized File.expand_path('.')
120 remote_name = params[:remote_name].nil? ? 'github' : params[:remote_name]
122 remote = blog.remote remote_name
124 puts "Changes pushed to #{remote_name}."
127 desc 'Remove all generated xhtml'
129 Dir['posts/*.xhtml'].each do |path|
134 desc 'Generate xhtml files from posts and design'
135 task :deploy => :clobber do
136 should_be_initialized File.expand_path('.')
138 Dir['posts/*.*'].each do |path|
139 markup = File.extname(path).downcase.gsub(/^\./,'')
140 content = IO.read path
141 first_line = content.match(/^(.*)\n/)[1]
143 post_title = (first_line.match GitBlog::TitleRegexen[markup])[1]
145 parser = GitBlog::Parsers.const_get(markup.gsub(/\b\w/){$&.upcase})
146 out = parser.send :parse, content
150 template = IO.read :design / :post.haml
152 completed = Haml::Engine.new(template, :filename => :design / :post.haml).
153 to_html(Object.new, {:content => parsed, :title => post_title})
155 destination = path.gsub /.#{markup}$/, '.xhtml'
156 file = File.open destination, File::RDWR|File::TRUNC|File::CREAT, 0664
159 puts "#{path} -> #{destination} (as #{markup})"
163 def should_be_initialized path
164 raise "** You haven't used `rake initialize` yet." unless
165 File.directory? File.join(path, 'posts') and
166 (blog = Git.open path rescue false)