Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit fedc78c87d5b9561b8442ed37051e68d30a67d87 1 parent f505f3b
authored May 16, 2008
1  .gitignore
... ...
@@ -0,0 +1 @@
  1
+blog
1  README.mkdn
Source Rendered
@@ -40,6 +40,7 @@ Requirements
40 40
 
41 41
 To run git-blog, you need the following gems:
42 42
 
  43
+* Ruby/Git
43 44
 * haml (if you wish to create your templates in haml)
44 45
 * redcloth (if you wish to write your posts in Markdown or Textile)
45 46
 
0  Rakefile.rb → defaults/index.haml
File renamed without changes
0  defaults/main.css
No changes.
0  defaults/post.haml
No changes.
4  defaults/welcome_to_your_git_blog.markdown
Source Rendered
... ...
@@ -0,0 +1,4 @@
  1
+Welcome to your git-blog
  2
+========================
  3
+
  4
+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 *(-:*
105  lib/git-blog.rb
... ...
@@ -0,0 +1,105 @@
  1
+GB_LOC = File.expand_path(File.dirname(__FILE__))
  2
+require 'rubygems'
  3
+require 'git' # Ruby/Git
  4
+require 'logger'
  5
+require 'fileutils'
  6
+include FileUtils
  7
+
  8
+require 'git-blog/core_ext'
  9
+
  10
+desc 'Setup the blog repository'
  11
+task :initialize do
  12
+  path = File.expand_path '.'
  13
+  
  14
+  # Will create the repo if it doesn't exist
  15
+  until (blog = Git.open path rescue false)
  16
+    Git.init path
  17
+  end
  18
+  
  19
+  cd path
  20
+  mkdir 'posts'
  21
+  mkdir 'design'
  22
+  cp File.join(GB_LOC, 'defaults', 'welcome_to_your_git_blog.markdown'), 'posts'
  23
+  %w(main.css post.haml index.haml).each do |file|
  24
+    cp File.join(GB_LOC, 'defaults', file), 'design'
  25
+  end
  26
+  
  27
+  blog.add
  28
+  blog.commit_all("A bird... no, a plane... no, a git-blog!")
  29
+end
  30
+
  31
+desc 'Attach the blog to a GitHub repository'
  32
+task :github, :user_name, :repo_name do |_, params|
  33
+  path = File.expand_path '.'
  34
+  user_name = params[:user_name].nil? ? %x(whoami).chomp : params[:user_name]
  35
+  repo_name = params[:repo_name].nil? ? File.basename(path) : params[:repo_name]
  36
+  
  37
+  raise "** You haven't used `rake initialize` yet." unless
  38
+    File.directory? File.join(path, 'posts') and
  39
+    (blog = Git.open path rescue false)
  40
+  
  41
+  github = blog.add_remote 'github', "git@github.com:#{user_name.downcase}/#{repo_name.downcase}.git"
  42
+  blog.push github
  43
+end
  44
+
  45
+desc 'Create and open for editing a new post'
  46
+task :post do
  47
+  @resume = false
  48
+  temporary_post = :post_in_progress.markdown
  49
+  
  50
+  if File.file? temporary_post
  51
+    puts '** You have an unfinished post from before,'
  52
+    puts 'do you want to r)esume it or overwrite it with a n)ew one? '
  53
+    case STDIN.gets
  54
+    when /r/i
  55
+      @resume = true
  56
+    when /n/i
  57
+      # do nothing, go on to overwriting it
  58
+    else
  59
+      raise 'Invalid entry, exiting out'
  60
+    end
  61
+  end
  62
+  
  63
+  unless @resume
  64
+    File.open temporary_post, File::RDWR|File::TRUNC|File::CREAT do |post|
  65
+      post.puts 'Title here'
  66
+      post.puts '=========='
  67
+      post.print "\n"
  68
+      post.close
  69
+    end
  70
+  end
  71
+  
  72
+  system "#{ENV['EDITOR']} #{temporary_post}"
  73
+  
  74
+  first_line = File.open(temporary_post, File::RDWR|File::CREAT).gets.chomp
  75
+  markup = case first_line
  76
+    when /^\</        then 'html'
  77
+    when /^\%/        then 'haml'
  78
+    when /^\#|h\d\./  then 'textile'
  79
+    else                   'markdown'
  80
+  end
  81
+  title_regex = case markup
  82
+    when 'html'     then /^<[^>]+>(.*)<[^>]+>$/
  83
+    when 'haml'     then /^%[^\s\{]+(?:\{[^\}]\})? (.*)$/
  84
+    when 'textile'  then /^h\d(?:[^\s\{]|\{[^\}]*\})*\. (.*)$/
  85
+    else                 /^(.*)$/
  86
+  end
  87
+  title = (first_line.match title_regex)[1]
  88
+  path = :posts / title.slugize.send(markup.to_sym)
  89
+  mv temporary_post, path
  90
+  
  91
+  blog = Git.open File.expand_path('.')
  92
+  blog.add path
  93
+  blog.commit "New post: #{title}"
  94
+  
  95
+  puts "Post '#{title.slugize}' comitted."
  96
+end
  97
+
  98
+desc 'Push changes'
  99
+task :push, :remote_name do |_, params|
  100
+  remote_name = params[:remote_name].nil? ? 'github' : params[:remote_name]
  101
+  blog = Git.open '.'
  102
+  remote = blog.remote remote_name
  103
+  blog.push remote
  104
+  puts "Changes pushed to #{remote_name}."
  105
+end
3  lib/git-blog/core_ext.rb
... ...
@@ -0,0 +1,3 @@
  1
+require 'git-blog/core_ext/file'
  2
+require 'git-blog/core_ext/string'
  3
+require 'git-blog' / 'core_ext' / 'symbol'
12  lib/git-blog/core_ext/file.rb
... ...
@@ -0,0 +1,12 @@
  1
+class File
  2
+  Extensions = %r=^(markdown|textile|haml|sass|css|html|xhtml)$=i
  3
+  module Extension
  4
+    def method_missing(meth, *args)
  5
+      if Extensions =~ meth.to_s
  6
+        [self, '.', meth.to_s].join
  7
+      else
  8
+        super
  9
+      end # if
  10
+    end # method_missing
  11
+  end # Extension
  12
+end # File
15  lib/git-blog/core_ext/string.rb
... ...
@@ -0,0 +1,15 @@
  1
+class String
  2
+  include File::Extension
  3
+  
  4
+  def slugize
  5
+    string = self
  6
+    string = string.downcase
  7
+    string = string.strip
  8
+    string = string.gsub /[^a-z0-9_]/, '_'
  9
+    string = string.gsub /_+/, '_'
  10
+  end
  11
+  
  12
+  def /(o)
  13
+    File.join(self, o.to_s)
  14
+  end
  15
+end
7  lib/git-blog/core_ext/symbol.rb
... ...
@@ -0,0 +1,7 @@
  1
+class Symbol
  2
+  include File::Extension
  3
+  
  4
+  def /(o)
  5
+    File.join(self.to_s, o.to_s)
  6
+  end
  7
+end

0 notes on commit fedc78c

Please sign in to comment.
Something went wrong with that request. Please try again.