GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of sr/git-wiki
Description: A wiki engine that uses a Git repository as its data store.
Homepage: http://atonie.org/2008/02/git-wiki
Clone URL: git://github.com/al3x/git-wiki.git
al3x (author)
Tue Mar 11 17:32:56 -0700 2008
commit  1c1ec6e80bd5c29cde200fe9fe5809b2937012e8
tree    fff304d38538eea8ef370dfc631a67b674b1ea65
parent  3a6f793043cfa05dc91d3ad3e0a0396c4c616f68
git-wiki / page.rb
100644 79 lines (65 sloc) 1.815 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require 'uv'
 
class Page
  attr_reader :name
 
  def initialize(name, rev=nil)
    @name = name
    @rev = rev
    @filename = File.join(GIT_REPO, @name)
  end
 
  def body
    ext = File.extname(@filename)
    unless ext.empty?
      @body ||= Uv.parse(raw_body, "xhtml", Uv.syntax_for_file_extension(ext), false, UV_THEME)
    else
      @body ||= RubyPants.new(RedCloth.new(raw_body).to_html).to_html.wiki_linked
    end
  end
 
  def updated_at
    commit.committed_date
  end
 
  def raw_body
    if @rev
       @raw_body ||= blob.data
    else
      @raw_body ||= File.exists?(@filename) ? File.read(@filename) : ''
    end
  end
 
  def body=(content)
    File.open(@filename, 'w') { |f| f << content }
    message = tracked? ? "edited #{@name}" : "created #{@name}"
    `cd #{GIT_REPO} && git add #{@name} && git commit -m "#{message}"`
  end
 
  def tracked?
    return false if $repo.commits.empty?
    $repo.commits.first.tree.contents.map { |b| b.name }.include?(@name)
  end
 
  def history
    return nil unless tracked?
    @history ||= $repo.log('master', @name)
  end
 
  def delta(rev)
    $repo.diff(previous_commit, rev, @name)
  end
  
  def commit
    @commit ||= $repo.log(@rev || 'master', @name, {"max-count" => 1}).first
  end
 
  def previous_commit
    @previous_commit ||= $repo.log(@rev || 'master', @name, {"max-count" => 2})[1]
  end
 
  def next_commit
    if self.history[0].to_s == self.commit.to_s
      @next_commit ||= nil
    else
      matching_index = nil
      history.each_with_index { |c, i| matching_index = i if c.to_s == self.commit.to_s }
      @next_commit ||= history[matching_index - 1]
    end
  end
 
  def version(rev)
    data = blob.data
    RubyPants.new(RedCloth.new(data).to_html).to_html.wiki_linked
  end
 
  def blob
    @blob ||= ($repo.tree(@rev)/@name)
  end
end