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 %w(welcome_to_your_git_blog.markdown .gitignore).each do |file|
19 cp GitBlog::Location / :defaults / file, 'posts'
21 %w(main.css post.haml index.haml).each do |file|
22 cp GitBlog::Location / :defaults / file, 'design'
26 blog.commit_all("A bird... no, a plane... no, a git-blog!")
29 desc 'Attach the blog to a GitHub repository'
30 task :github, :user_name, :repo_name do |_, params|
31 path = File.expand_path '.'
32 user_name = params[:user_name].nil? ? %x(whoami).chomp : params[:user_name]
33 repo_name = params[:repo_name].nil? ? File.basename(path) : params[:repo_name]
37 github = blog.add_remote 'github', "git@github.com:#{user_name.downcase}/#{repo_name.downcase}.git"
41 desc 'Prepare the blog to be served (configures hooks)'
43 is_initialized? File.expand_path('.')
44 cp GitBlog::Location / :defaults / 'post-recieve', '.git' / :hooks
47 desc 'Create and open for editing a new post'
50 temporary_post = :post_in_progress.markdown
52 is_initialized? File.expand_path('.')
54 if File.file? temporary_post
55 puts '** You have an unfinished post from before,'
56 puts 'do you want to r)esume it or overwrite it with a n)ew one? '
61 # do nothing, go on to overwriting it
63 raise 'Invalid entry, exiting out'
68 File.open temporary_post, File::RDWR|File::TRUNC|File::CREAT do |post|
69 post.puts 'Replace this text with your title!'
70 post.puts '=================================='
71 post.print "\n"; post.close
75 system "#{ENV['VISUAL']} #{temporary_post}"
77 first_line = File.open(temporary_post, File::RDWR|File::CREAT).gets.chomp
78 markup = case first_line
79 when /^\</ then 'xhtml'
80 when /^\%/ then 'haml'
81 when /^\#|h\d\./ then 'textile'
85 title = (first_line.match GitBlog::TitleRegexen[markup])[1]
86 path = :posts / title.slugize.send(markup.to_sym)
87 mv temporary_post, path
89 blog = Git.open File.expand_path('.')
91 blog.commit "New post: #{title}"
93 puts "Post '#{title.slugize}' comitted."
97 task :push, :remote_name do |_, params|
98 is_initialized? File.expand_path('.')
100 remote_name = params[:remote_name].nil? ? 'github' : params[:remote_name]
102 remote = blog.remote remote_name
104 puts "Changes pushed to #{remote_name}."
107 desc 'Remove all generated xhtml'
109 Dir['posts/*.xhtml'].each do |path|
114 desc 'Generate xhtml files from posts and design'
115 task :deploy => :clobber do
116 is_initialized? File.expand_path('.')
118 Dir['posts/*.*'].each do |path|
119 markup = File.extname(path).downcase.gsub(/^\./,'')
120 content = IO.read path
121 first_line = content.match(/^(.*)\n/)[1]
123 post_title = (first_line.match GitBlog::TitleRegexen[markup])[1]
125 parser = GitBlog::Parsers.const_get(markup.gsub(/\b\w/){$&.upcase})
126 out = parser.send :parse, content
130 template = IO.read :design / :post.haml
132 completed = Haml::Engine.new(template, :filename => :design / :post.haml).
133 to_html(Object.new, {:content => parsed, :title => post_title})
135 destination = path.gsub /.#{markup}$/, '.xhtml'
136 file = File.open destination, File::RDWR|File::TRUNC|File::CREAT
139 puts "#{path} -> #{destination} (as #{markup})"
143 def is_initialized?(path)
144 raise "** You haven't used `rake initialize` yet." unless
145 File.directory? File.join(path, 'posts') and
146 (blog = Git.open path rescue false)