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
commit  c97c066c51bbca39e09b34f4517e9c420f854140
tree    c4789c69ea8ae0aa8923a5385ee2c660d0511bdd
parent  535e37fb0a5879069a1c979af587b8d6f82c21ca
git-wiki / page.rb
100644 96 lines (81 sloc) 2.069 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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), true, UV_THEME)
    else
      @body ||= RubyPants.new(RedCloth.new(raw_body).to_html).to_html.wiki_linked
    end
  end
  
  def branch_name
    $repo.current_branch
  end
  
  def updated_at
    commit.committer_date rescue Time.now
  end
 
  def raw_body
    if @rev
       @raw_body ||= blob.contents
    else
      @raw_body ||= File.exists?(@filename) ? File.read(@filename) : ''
    end
  end
 
  def update(content, message)
    File.open(@filename, 'w') { |f| f << content }
    commit_message = tracked? ? "edited #{@name}" : "created #{@name}"
    commit_message += ' : ' + message if message.length > 0
    begin
      $repo.add(@name)
      $repo.commit(commit_message)
    rescue
      nil
    end
  end
 
  def tracked?
    begin
      $repo.gtree('HEAD').children.keys.include?(@name)
    rescue
      false
    end
  end
 
  def history
    return nil unless tracked?
    @history ||= $repo.log.path(@name)
  end
 
  def delta(rev)
    $repo.diff(previous_commit, rev).path(@name).patch
  end
  
  def commit
    @commit ||= $repo.log.object(@rev || 'master').path(@name).first
  end
 
  def previous_commit
    @previous_commit ||= $repo.log(2).object(@rev || 'master').path(@name).to_a[1]
  end
 
  def next_commit
    begin
      if (self.history.first.sha == self.commit.sha)
        @next_commit ||= nil
      else
        matching_index = nil
        history.each_with_index { |c, i| matching_index = i if c.sha == self.commit.sha }
        @next_commit ||= history.to_a[matching_index - 1]
      end
    rescue
      @next_commit ||= nil
    end
  end
 
  def version(rev)
    data = blob.contents
    RubyPants.new(RedCloth.new(data).to_html).to_html.wiki_linked
  end
 
  def blob
    @blob ||= ($repo.gblob(@rev + ':' + @name))
  end
end