Add create/edit powers to frontend. Support external image URLs.

This commit is contained in:
Tom Preston-Werner
2010-07-07 16:02:26 -07:00
parent 0485c67b3d
commit 6c7fcf89c3
10 changed files with 117 additions and 3 deletions
+36 -2
View File
@@ -6,7 +6,7 @@ require 'mustache/sinatra'
require 'gollum/frontend/views/layout'
$path = "~/dev/mojombo/gollum/test/examples/lotr.git"
$path = "~/dev/sandbox/lotr2"
module Precious
class App < Sinatra::Base
@@ -41,6 +41,38 @@ module Precious
show_page_or_file('Home')
end
get '/edit/:name' do
@name = params[:name]
wiki = Gollum::Wiki.new($path)
if page = wiki.page(@name)
@content = page.raw_data
mustache :edit
else
mustache :create
end
end
post '/edit/:name' do
name = params[:name]
wiki = Gollum::Wiki.new($path)
page = wiki.page(name)
commit = { :message => 'commit message',
:name => 'Tom Preston-Werner',
:email => 'tom@github.com' }
wiki.update_page(page, params[:content], commit)
redirect "/#{name}"
end
post '/create/:page' do
page = params[:page]
wiki = Gollum::Wiki.new($path)
commit = { :message => 'commit message',
:name => 'Tom Preston-Werner',
:email => 'tom@github.com' }
wiki.write_page(page, :markdown, params[:content], commit)
redirect "/#{page}"
end
get '/*' do
show_page_or_file(params[:splat].first)
end
@@ -48,12 +80,14 @@ module Precious
def show_page_or_file(name)
wiki = Gollum::Wiki.new($path)
if page = wiki.page(name)
@name = name
@content = page.formatted_data
mustache :page
elsif file = wiki.file(name)
file.raw_data
else
halt 404
@name = name
mustache :create
end
end
end
+13
View File
@@ -34,6 +34,12 @@ p {
line-height: 22pt;
}
#nav {
width: 60em;
margin: 0 auto;
color: #666;
}
#content {
width: 60em;
margin: 0 auto;
@@ -44,6 +50,13 @@ a.absent {
color: #a00;
}
/* Forms */
textarea {
width: 100%;
height: 20em;
}
/* Images */
.frame {
@@ -0,0 +1,13 @@
<h1>Create a new page</h1>
<form method="post" action="/create/{{name}}">
<div>
<input type="text" name="page" value="{{name}}" />
</div>
<div>
<textarea name="content"></textarea>
</div>
<div>
<input type="submit" />
</div>
</form>
@@ -0,0 +1,10 @@
<h1>Editing {{name}}</h1>
<form method="post" action="/edit/{{name}}">
<div>
<textarea name="content">{{content}}</textarea>
</div>
<div>
<input type="submit" value="Save page" />
</div>
</form>
@@ -8,6 +8,9 @@
</head>
<body>
<div id="nav">
<a href="/">Home</a> | <a href="/edit/{{name}}">Edit</a>
</div>
<div id="content">
{{{yield}}}
</div>
+11
View File
@@ -0,0 +1,11 @@
module Precious
module Views
class Create < Layout
attr_reader :page
def title
"Create a new page"
end
end
end
end
+11
View File
@@ -0,0 +1,11 @@
module Precious
module Views
class Edit < Layout
attr_reader :page, :content
def title
"Edit"
end
end
end
end
+2
View File
@@ -4,6 +4,8 @@ module Precious
include Rack::Utils
alias_method :h, :escape_html
attr_reader :name
def title
"Home"
end
+8 -1
View File
@@ -85,8 +85,15 @@ module Gollum
def process_image_tag(tag)
parts = tag.split('|')
name = parts[0].strip
path = nil
if file = find_file(name)
path = "/#{file.path}"
elsif name =~ /^https?:\/\/.+(jpg|png|gif|svg|bmp)$/
path = name
end
if path
opts = parse_image_tag_options(tag)
containered = false
@@ -143,7 +150,7 @@ module Gollum
%{</span>} +
%{</span>}
else
%{<img src="/#{file.path}"#{style_string} #{attr_string}/>}
%{<img src="#{path}"#{style_string} #{attr_string}/>}
end
end
end
+10
View File
@@ -32,6 +32,16 @@ context "Markup" do
assert_equal %{<p>a <a class="internal absent" href="J.-R.-R.-Tolkien">J. R. R. Tolkien</a>'s b</p>\n}, output
end
test "image with http url" do
['http', 'https'].each do |scheme|
@wiki.write_page("Bilbo Baggins", :markdown, "a [[#{scheme}://example.com/bilbo.jpg]] b", @commit)
page = @wiki.page("Bilbo Baggins")
output = Gollum::Markup.new(page).render
assert_equal %{<p>a <img src="#{scheme}://example.com/bilbo.jpg" /> b</p>\n}, output
end
end
test "image with absolute path" do
index = @wiki.repo.index
index.add("alpha.jpg", "hi")