Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Half done with the basics - repo setup and post creation working (-:

  • Loading branch information...
commit fedc78c87d5b9561b8442ed37051e68d30a67d87 1 parent f505f3b
@ELLIOTTCABLE authored
View
1  .gitignore
@@ -0,0 +1 @@
+blog
View
1  README.mkdn
@@ -40,6 +40,7 @@ Requirements
To run git-blog, you need the following gems:
+* Ruby/Git
* haml (if you wish to create your templates in haml)
* redcloth (if you wish to write your posts in Markdown or Textile)
View
0  Rakefile.rb → defaults/index.haml
File renamed without changes
View
0  defaults/main.css
No changes.
View
0  defaults/post.haml
No changes.
View
4 defaults/welcome_to_your_git_blog.markdown
@@ -0,0 +1,4 @@
+Welcome to your git-blog
+========================
+
+Welcome to your [git-blog](http://github.com/elliottcable/git-blog "elliottcable's git-blog – GitHub") powered blog! Type `rake -T` in your local blog directory to get started *(-:*
View
105 lib/git-blog.rb
@@ -0,0 +1,105 @@
+GB_LOC = File.expand_path(File.dirname(__FILE__))
+require 'rubygems'
+require 'git' # Ruby/Git
+require 'logger'
+require 'fileutils'
+include FileUtils
+
+require 'git-blog/core_ext'
+
+desc 'Setup the blog repository'
+task :initialize do
+ path = File.expand_path '.'
+
+ # Will create the repo if it doesn't exist
+ until (blog = Git.open path rescue false)
+ Git.init path
+ end
+
+ cd path
+ mkdir 'posts'
+ mkdir 'design'
+ cp File.join(GB_LOC, 'defaults', 'welcome_to_your_git_blog.markdown'), 'posts'
+ %w(main.css post.haml index.haml).each do |file|
+ cp File.join(GB_LOC, 'defaults', file), 'design'
+ end
+
+ blog.add
+ blog.commit_all("A bird... no, a plane... no, a git-blog!")
+end
+
+desc 'Attach the blog to a GitHub repository'
+task :github, :user_name, :repo_name do |_, params|
+ path = File.expand_path '.'
+ user_name = params[:user_name].nil? ? %x(whoami).chomp : params[:user_name]
+ repo_name = params[:repo_name].nil? ? File.basename(path) : params[:repo_name]
+
+ raise "** You haven't used `rake initialize` yet." unless
+ File.directory? File.join(path, 'posts') and
+ (blog = Git.open path rescue false)
+
+ github = blog.add_remote 'github', "git@github.com:#{user_name.downcase}/#{repo_name.downcase}.git"
+ blog.push github
+end
+
+desc 'Create and open for editing a new post'
+task :post do
+ @resume = false
+ temporary_post = :post_in_progress.markdown
+
+ if File.file? temporary_post
+ puts '** You have an unfinished post from before,'
+ puts 'do you want to r)esume it or overwrite it with a n)ew one? '
+ case STDIN.gets
+ when /r/i
+ @resume = true
+ when /n/i
+ # do nothing, go on to overwriting it
+ else
+ raise 'Invalid entry, exiting out'
+ end
+ end
+
+ unless @resume
+ File.open temporary_post, File::RDWR|File::TRUNC|File::CREAT do |post|
+ post.puts 'Title here'
+ post.puts '=========='
+ post.print "\n"
+ post.close
+ end
+ end
+
+ system "#{ENV['EDITOR']} #{temporary_post}"
+
+ first_line = File.open(temporary_post, File::RDWR|File::CREAT).gets.chomp
+ markup = case first_line
+ when /^\</ then 'html'
+ when /^\%/ then 'haml'
+ when /^\#|h\d\./ then 'textile'
+ else 'markdown'
+ end
+ title_regex = case markup
+ when 'html' then /^<[^>]+>(.*)<[^>]+>$/
+ when 'haml' then /^%[^\s\{]+(?:\{[^\}]\})? (.*)$/
+ when 'textile' then /^h\d(?:[^\s\{]|\{[^\}]*\})*\. (.*)$/
+ else /^(.*)$/
+ end
+ title = (first_line.match title_regex)[1]
+ path = :posts / title.slugize.send(markup.to_sym)
+ mv temporary_post, path
+
+ blog = Git.open File.expand_path('.')
+ blog.add path
+ blog.commit "New post: #{title}"
+
+ puts "Post '#{title.slugize}' comitted."
+end
+
+desc 'Push changes'
+task :push, :remote_name do |_, params|
+ remote_name = params[:remote_name].nil? ? 'github' : params[:remote_name]
+ blog = Git.open '.'
+ remote = blog.remote remote_name
+ blog.push remote
+ puts "Changes pushed to #{remote_name}."
+end
View
3  lib/git-blog/core_ext.rb
@@ -0,0 +1,3 @@
+require 'git-blog/core_ext/file'
+require 'git-blog/core_ext/string'
+require 'git-blog' / 'core_ext' / 'symbol'
View
12 lib/git-blog/core_ext/file.rb
@@ -0,0 +1,12 @@
+class File
+ Extensions = %r=^(markdown|textile|haml|sass|css|html|xhtml)$=i
+ module Extension
+ def method_missing(meth, *args)
+ if Extensions =~ meth.to_s
+ [self, '.', meth.to_s].join
+ else
+ super
+ end # if
+ end # method_missing
+ end # Extension
+end # File
View
15 lib/git-blog/core_ext/string.rb
@@ -0,0 +1,15 @@
+class String
+ include File::Extension
+
+ def slugize
+ string = self
+ string = string.downcase
+ string = string.strip
+ string = string.gsub /[^a-z0-9_]/, '_'
+ string = string.gsub /_+/, '_'
+ end
+
+ def /(o)
+ File.join(self, o.to_s)
+ end
+end
View
7 lib/git-blog/core_ext/symbol.rb
@@ -0,0 +1,7 @@
+class Symbol
+ include File::Extension
+
+ def /(o)
+ File.join(self.to_s, o.to_s)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.