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
Search Repo:
Merge branch 'master' of git://github.com/schacon/git-wiki
al3x (author)
Sun Apr 20 18:52:18 -0700 2008
commit  f767ccaf5da293ed9cb34380bb988bd625881724
tree    dc7ae2d52dc4d20057f45d0b6230b0e2ba7b6c01
parent  c14f8a96904bf47cdc2bcdf96bf6d13f4b7f04b8 parent  a34c1151386501b52161ef02b548e68ac422fd50
0
...
4
5
6
7
8
9
 
10
11
12
13
14
 
15
16
17
...
4
5
6
 
7
8
9
10
 
11
12
13
14
15
16
17
0
@@ -4,14 +4,14 @@
0
   * pushing repo from web interface
0
 
0
 = CHACON IDEAS
0
- * inter-branch links
0
   * tagging
0
   * file attachments
0
+ * inter-branch links
0
   * merge conflict resolution
0
- * cherry-picked branches (read-tree/write-tree)
0
   * git-less version (read-only)
0
   * track which branched from which
0
   * push?
0
+ * cherry-picked branches (read-tree/write-tree)
0
   * users (email/name/ip - use for commits)
0
   
0
 = LATER/MAYBE
...
1
2
 
3
4
5
...
58
59
60
61
 
 
62
63
64
...
139
140
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
143
144
...
160
161
162
163
164
 
...
1
2
3
4
5
6
...
59
60
61
 
62
63
64
65
66
...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
...
186
187
188
 
189
190
0
@@ -1,5 +1,6 @@
0
 #!/usr/bin/env ruby
0
 
0
+require 'fileutils'
0
 require 'environment'
0
 require 'sinatra/lib/sinatra'
0
 
0
@@ -58,7 +59,8 @@ end
0
 # application paths (/a/ namespace)
0
 
0
 get '/a/list' do
0
- @pages = $repo.log.first.gtree.children.map { |name, blob| Page.new(name) } rescue []
0
+ pages = $repo.log.first.gtree.children
0
+ @pages = pages.select { |f,bl| f[0,1] != '_'}.sort.map { |name, blob| Page.new(name) } rescue []
0
   show(:list, 'Listing pages')
0
 end
0
 
0
@@ -139,6 +141,30 @@ get '/a/search' do
0
   show :search, 'Search Results'
0
 end
0
 
0
+# file upload attachments
0
+
0
+get '/a/file/upload/:page' do
0
+ @page = Page.new(params[:page])
0
+ show :attach, 'Attach File for ' + @page.name
0
+end
0
+
0
+post '/a/file/upload/:page' do
0
+ @page = Page.new(params[:page])
0
+ @page.save_file(params[:file], params[:name])
0
+ redirect '/e/' + @page.name
0
+end
0
+
0
+get '/a/file/delete/:page/:file.:ext' do
0
+ @page = Page.new(params[:page])
0
+ @page.delete_file(params[:file] + '.' + params[:ext])
0
+ redirect '/e/' + @page.name
0
+end
0
+
0
+get '/_attachment/:page/:file.:ext' do
0
+ @page = Page.new(params[:page])
0
+ send_file(File.join(@page.attach_dir, params[:file] + '.' + params[:ext]))
0
+end
0
+
0
 # support methods
0
 
0
 def page_url(page)
0
@@ -160,4 +186,4 @@ private
0
       f.close
0
       $repo.add('.meta')
0
     end
0
- end
0
\ No newline at end of file
0
+ end
...
1
2
 
3
4
5
6
7
 
 
 
 
 
8
9
10
...
42
43
44
45
46
47
48
49
 
50
51
52
...
88
89
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
92
...
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
47
48
49
 
 
 
 
 
50
51
52
53
...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
0
@@ -1,10 +1,15 @@
0
 class Page
0
- attr_reader :name
0
+ attr_reader :name, :attach_dir
0
 
0
   def initialize(name, rev=nil)
0
     @name = name
0
     @rev = rev
0
     @filename = File.join(GIT_REPO, @name)
0
+ @attach_dir = File.join(GIT_REPO, '_attachments', unwiki(@name))
0
+ end
0
+
0
+ def unwiki(string)
0
+ string.downcase
0
   end
0
 
0
   def body
0
@@ -42,11 +47,7 @@ class Page
0
   end
0
 
0
   def tracked?
0
- begin
0
- $repo.gtree('HEAD').children.keys.include?(@name)
0
- rescue
0
- false
0
- end
0
+ $repo.ls_files.keys.include?(@name)
0
   end
0
 
0
   def history
0
@@ -88,4 +89,90 @@ class Page
0
   def blob
0
     @blob ||= ($repo.gblob(@rev + ':' + @name))
0
   end
0
+
0
+ # save a file into the _attachments directory
0
+ def save_file(file, name = '')
0
+ if name.size > 0
0
+ filename = name + File.extname(file[:filename])
0
+ else
0
+ filename = file[:filename]
0
+ end
0
+ FileUtils.mkdir_p(@attach_dir) if !File.exists?(@attach_dir)
0
+ new_file = File.join(@attach_dir, filename)
0
+
0
+ f = File.new(new_file, 'w')
0
+ f.write(file[:tempfile].read)
0
+ f.close
0
+
0
+ commit_message = "uploaded #{filename} for #{@name}"
0
+ begin
0
+ $repo.add(new_file)
0
+ $repo.commit(commit_message)
0
+ rescue
0
+ nil
0
+ end
0
+ end
0
+
0
+ def delete_file(file)
0
+ file_path = File.join(@attach_dir, file)
0
+ if File.exists?(file_path)
0
+ File.unlink(file_path)
0
+
0
+ commit_message = "removed #{file} for #{@name}"
0
+ begin
0
+ $repo.remove(file_path)
0
+ $repo.commit(commit_message)
0
+ rescue
0
+ nil
0
+ end
0
+
0
+ end
0
+ end
0
+
0
+ def attachments
0
+ if File.exists?(@attach_dir)
0
+ return Dir.glob(File.join(@attach_dir, '*')).map { |f| Attachment.new(f, unwiki(@name)) }
0
+ else
0
+ false
0
+ end
0
+ end
0
+
0
+ class Attachment
0
+ attr_accessor :path, :page_name
0
+ def initialize(file_path, name)
0
+ @path = file_path
0
+ @page_name = name
0
+ end
0
+
0
+ def name
0
+ File.basename(@path)
0
+ end
0
+
0
+ def link_path
0
+ File.join('/_attachment', @page_name, name)
0
+ end
0
+
0
+ def delete_path
0
+ File.join('/a/file/delete', @page_name, name)
0
+ end
0
+
0
+ def image?
0
+ ext = File.extname(@path)
0
+ case ext
0
+ when '.png', '.jpg', '.jpeg', '.gif'; return true
0
+ else; return false
0
+ end
0
+ end
0
+
0
+ def size
0
+ size = File.size(@path).to_i
0
+ case
0
+ when size.to_i == 1; "1 Byte"
0
+ when size < 1024; "%d Bytes" % size
0
+ when size < (1024*1024); "%.2f KB" % (size / 1024.0)
0
+ else "%.2f MB" % (size / (1024 * 1024.0))
0
+ end.sub(/([0-9])\.?0+ /, '\1 ' )
0
+ end
0
+ end
0
+
0
 end
0
\ No newline at end of file
...
157
158
159
160
 
 
 
 
 
 
 
 
 
 
161
162
163
...
157
158
159
 
160
161
162
163
164
165
166
167
168
169
170
171
172
0
@@ -157,7 +157,16 @@ ul {
0
   font-size: .85em;
0
   margin-left: 0.3em;
0
 }
0
-
0
+span.detail {
0
+  color: #888;
0
+  font-size: .85em;
0
+}
0
+div.attach-options {
0
+  margin-left: 30px;
0
+  margin-bottom: 10px;
0
+  font-size: .85em;
0
+  color: #888;
0
+}
0
 /* ids */
0
 
0
 #container {
...
1
 
...
 
1
0
@@ -1 +1 @@
0
-Subproject commit 898b36eab4cbd0384d23000ee738c9d451558ce0
0
+Subproject commit f80203adb6bc5756bdcf2388c937891c756db5a6
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
0
@@ -0,0 +1,17 @@
0
+<h1>Attach File</h1>
0
+
0
+<div class="content">
0
+ <h2>New File Upload</h2>
0
+ <form method="post" action="/a/file/upload/<%= @page.name %>"
0
+ class="niceform" enctype="multipart/form-data">
0
+ <label for="name">name</label>
0
+ <input type="text" name="name" />
0
+ <br/>
0
+ <label for="name">file</label>
0
+ <input type="file" name="file" />
0
+ <br />
0
+ <label for="submit"></label>
0
+ <input type="submit" value="upload" class="submit" />
0
+ </form>
0
+</div>
0
+
...
21
22
23
 
 
24
 
25
26
27
...
29
30
31
32
 
 
33
34
35
...
21
22
23
24
25
26
27
28
29
30
...
32
33
34
 
35
36
37
38
39
0
@@ -21,7 +21,10 @@
0
     <label for="name">name</label>
0
     <input type="text" name="branch" />
0
     <br />
0
+
0
+ <input type="hidden" name="type" value="derive" />
0
     
0
+ <!--
0
     <label for="derive_radio">copy this branch</label>
0
     <input type="radio" name="type" value="derive" />
0
     <br />
0
@@ -29,7 +32,8 @@
0
     <label for="blank_radio">empty branch</label>
0
     <input type="radio" name="type" value="blank" />
0
     <br />
0
-
0
+ -->
0
+
0
     <label for="submit"></label>
0
     <input type="submit" value="create" class="submit" />
0
   </form>
...
2
3
4
 
5
6
7
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
10
11
...
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
0
@@ -2,10 +2,29 @@
0
 
0
 <div class="sub_nav">
0
   <a href="/<%= @page.name %>" class="nav_link">back</a>
0
+ &bull; <a href="/a/file/upload/<%= @page.name %>" class="nav_link">attach</a>
0
   
0
   <% if @page.tracked? %>
0
     &bull; <a href="/h/<%= @page.name %>" class="nav_link">history</a>
0
   <% end %>
0
+
0
+ <% if files = @page.attachments %>
0
+ <h3>Attachments</h3>
0
+ <% files.each do |file| %>
0
+ <li><a href="<%= file.link_path %>"><%= file.name %></a>
0
+ <span class="detail">(<%= file.size %>)</span>
0
+ <div class="attach-options">
0
+ <a href="<%= file.delete_path %>">delete</a>
0
+ &bull; <a href="<%= file.link_path %>">download</a>
0
+ <% if file.image? %>
0
+ &bull; <a href="#" onClick="$(edit_textarea).html($(edit_textarea).html() + '!<%= file.link_path %>!');">insert &#187;</a>
0
+ <% else %>
0
+ &bull; <a href="#" onClick="$(edit_textarea).html($(edit_textarea).html() + '[<%= file.name %>](<%= file.link_path %>)');">insert &#187;</a>
0
+ <% end %>
0
+ </div>
0
+ </span>
0
+ <% end %>
0
+ <% end %>
0
 </div>
0
 
0
 <div class="content">
...
34
35
36
37
 
38
39
40
...
42
43
44
 
45
46
47
48
...
34
35
36
 
37
38
39
40
...
42
43
44
45
46
47
48
49
0
@@ -34,7 +34,7 @@
0
   <% if @page.next_commit %>
0
     &bull; <a href="/h/<%= @page.name %>/<%= @page.next_commit %>" class="nav_link">newer</a>
0
   <% end %>
0
-
0
+
0
   <div class="sub_nav details">
0
     <script type="text/javascript">
0
       document.write(time_ago_in_words(<%= @page.updated_at.for_time_ago_in_words %>) + ' ago');
0
@@ -42,6 +42,7 @@
0
     <br />
0
     <%= $repo.current_branch %> branch
0
   </div>
0
+
0
 </div>
0
 
0
 <div class="content edit_area"><%= @page.body %></div>
0
\ No newline at end of file

Comments

    No one has commented yet.