Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/schacon/git-wiki
Browse files Browse the repository at this point in the history
  • Loading branch information
al3x committed Apr 21, 2008
2 parents c14f8a9 + a34c115 commit f767cca
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 14 deletions.
4 changes: 2 additions & 2 deletions TODO
Expand Up @@ -4,14 +4,14 @@
* pushing repo from web interface

= CHACON IDEAS
* inter-branch links
* tagging
* file attachments
* inter-branch links
* merge conflict resolution
* cherry-picked branches (read-tree/write-tree)
* git-less version (read-only)
* track which branched from which
* push?
* cherry-picked branches (read-tree/write-tree)
* users (email/name/ip - use for commits)

= LATER/MAYBE
Expand Down
30 changes: 28 additions & 2 deletions git-wiki.rb
@@ -1,5 +1,6 @@
#!/usr/bin/env ruby

require 'fileutils'
require 'environment'
require 'sinatra/lib/sinatra'

Expand Down Expand Up @@ -58,7 +59,8 @@
# application paths (/a/ namespace)

get '/a/list' do
@pages = $repo.log.first.gtree.children.map { |name, blob| Page.new(name) } rescue []
pages = $repo.log.first.gtree.children
@pages = pages.select { |f,bl| f[0,1] != '_'}.sort.map { |name, blob| Page.new(name) } rescue []
show(:list, 'Listing pages')
end

Expand Down Expand Up @@ -139,6 +141,30 @@
show :search, 'Search Results'
end

# file upload attachments

get '/a/file/upload/:page' do
@page = Page.new(params[:page])
show :attach, 'Attach File for ' + @page.name
end

post '/a/file/upload/:page' do
@page = Page.new(params[:page])
@page.save_file(params[:file], params[:name])
redirect '/e/' + @page.name
end

get '/a/file/delete/:page/:file.:ext' do
@page = Page.new(params[:page])
@page.delete_file(params[:file] + '.' + params[:ext])
redirect '/e/' + @page.name
end

get '/_attachment/:page/:file.:ext' do
@page = Page.new(params[:page])
send_file(File.join(@page.attach_dir, params[:file] + '.' + params[:ext]))
end

# support methods

def page_url(page)
Expand All @@ -160,4 +186,4 @@ def touchfile
f.close
$repo.add('.meta')
end
end
end
99 changes: 93 additions & 6 deletions page.rb
@@ -1,10 +1,15 @@
class Page
attr_reader :name
attr_reader :name, :attach_dir

def initialize(name, rev=nil)
@name = name
@rev = rev
@filename = File.join(GIT_REPO, @name)
@attach_dir = File.join(GIT_REPO, '_attachments', unwiki(@name))
end

def unwiki(string)
string.downcase
end

def body
Expand Down Expand Up @@ -42,11 +47,7 @@ def update(content, message=nil)
end

def tracked?
begin
$repo.gtree('HEAD').children.keys.include?(@name)
rescue
false
end
$repo.ls_files.keys.include?(@name)
end

def history
Expand Down Expand Up @@ -88,4 +89,90 @@ def version(rev)
def blob
@blob ||= ($repo.gblob(@rev + ':' + @name))
end

# save a file into the _attachments directory
def save_file(file, name = '')
if name.size > 0
filename = name + File.extname(file[:filename])
else
filename = file[:filename]
end
FileUtils.mkdir_p(@attach_dir) if !File.exists?(@attach_dir)
new_file = File.join(@attach_dir, filename)

f = File.new(new_file, 'w')
f.write(file[:tempfile].read)
f.close

commit_message = "uploaded #{filename} for #{@name}"
begin
$repo.add(new_file)
$repo.commit(commit_message)
rescue
nil
end
end

def delete_file(file)
file_path = File.join(@attach_dir, file)
if File.exists?(file_path)
File.unlink(file_path)

commit_message = "removed #{file} for #{@name}"
begin
$repo.remove(file_path)
$repo.commit(commit_message)
rescue
nil
end

end
end

def attachments
if File.exists?(@attach_dir)
return Dir.glob(File.join(@attach_dir, '*')).map { |f| Attachment.new(f, unwiki(@name)) }
else
false
end
end

class Attachment
attr_accessor :path, :page_name
def initialize(file_path, name)
@path = file_path
@page_name = name
end

def name
File.basename(@path)
end

def link_path
File.join('/_attachment', @page_name, name)
end

def delete_path
File.join('/a/file/delete', @page_name, name)
end

def image?
ext = File.extname(@path)
case ext
when '.png', '.jpg', '.jpeg', '.gif'; return true
else; return false
end
end

def size
size = File.size(@path).to_i
case
when size.to_i == 1; "1 Byte"
when size < 1024; "%d Bytes" % size
when size < (1024*1024); "%.2f KB" % (size / 1024.0)
else "%.2f MB" % (size / (1024 * 1024.0))
end.sub(/([0-9])\.?0+ /, '\1 ' )
end
end

end
11 changes: 10 additions & 1 deletion public/style.css
Expand Up @@ -157,7 +157,16 @@ ul {
font-size: .85em;
margin-left: 0.3em;
}

span.detail {
color: #888;
font-size: .85em;
}
div.attach-options {
margin-left: 30px;
margin-bottom: 10px;
font-size: .85em;
color: #888;
}
/* ids */

#container {
Expand Down
17 changes: 17 additions & 0 deletions views/attach.erb
@@ -0,0 +1,17 @@
<h1>Attach File</h1>

<div class="content">
<h2>New File Upload</h2>
<form method="post" action="/a/file/upload/<%= @page.name %>"
class="niceform" enctype="multipart/form-data">
<label for="name">name</label>
<input type="text" name="name" />
<br/>
<label for="name">file</label>
<input type="file" name="file" />
<br />
<label for="submit"></label>
<input type="submit" value="upload" class="submit" />
</form>
</div>

6 changes: 5 additions & 1 deletion views/branches.erb
Expand Up @@ -21,15 +21,19 @@
<label for="name">name</label>
<input type="text" name="branch" />
<br />

<input type="hidden" name="type" value="derive" />

<!--
<label for="derive_radio">copy this branch</label>
<input type="radio" name="type" value="derive" />
<br />
<label for="blank_radio">empty branch</label>
<input type="radio" name="type" value="blank" />
<br />

-->

<label for="submit"></label>
<input type="submit" value="create" class="submit" />
</form>
Expand Down
19 changes: 19 additions & 0 deletions views/edit.erb
Expand Up @@ -2,10 +2,29 @@

<div class="sub_nav">
<a href="/<%= @page.name %>" class="nav_link">back</a>
&bull; <a href="/a/file/upload/<%= @page.name %>" class="nav_link">attach</a>

<% if @page.tracked? %>
&bull; <a href="/h/<%= @page.name %>" class="nav_link">history</a>
<% end %>
<% if files = @page.attachments %>
<h3>Attachments</h3>
<% files.each do |file| %>
<li><a href="<%= file.link_path %>"><%= file.name %></a>
<span class="detail">(<%= file.size %>)</span>
<div class="attach-options">
<a href="<%= file.delete_path %>">delete</a>
&bull; <a href="<%= file.link_path %>">download</a>
<% if file.image? %>
&bull; <a href="#" onClick="$(edit_textarea).html($(edit_textarea).html() + '!<%= file.link_path %>!');">insert &#187;</a>
<% else %>
&bull; <a href="#" onClick="$(edit_textarea).html($(edit_textarea).html() + '[<%= file.name %>](<%= file.link_path %>)');">insert &#187;</a>
<% end %>
</div>
</span>
<% end %>
<% end %>
</div>

<div class="content">
Expand Down
3 changes: 2 additions & 1 deletion views/show.erb
Expand Up @@ -34,14 +34,15 @@
<% if @page.next_commit %>
&bull; <a href="/h/<%= @page.name %>/<%= @page.next_commit %>" class="nav_link">newer</a>
<% end %>

<div class="sub_nav details">
<script type="text/javascript">
document.write(time_ago_in_words(<%= @page.updated_at.for_time_ago_in_words %>) + ' ago');
</script>
<br />
<%= $repo.current_branch %> branch
</div>

</div>

<div class="content edit_area"><%= @page.body %></div>

0 comments on commit f767cca

Please sign in to comment.