fix index clobbering and tighten some stuff up
This commit is contained in:
+8
-1
@@ -10,11 +10,18 @@ module Gollum
|
|||||||
@blob = nil
|
@blob = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Public: The on-disk filename of the file.
|
||||||
|
#
|
||||||
|
# Returns the String name.
|
||||||
|
def name
|
||||||
|
@blob && @blob.name
|
||||||
|
end
|
||||||
|
|
||||||
# Public: The raw contents of the page.
|
# Public: The raw contents of the page.
|
||||||
#
|
#
|
||||||
# Returns the String data.
|
# Returns the String data.
|
||||||
def raw_data
|
def raw_data
|
||||||
@blob.data rescue nil
|
@blob && @blob.data
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: The Grit::Commit version of the file.
|
# Public: The Grit::Commit version of the file.
|
||||||
|
|||||||
+11
-1
@@ -8,8 +8,10 @@ module Gollum
|
|||||||
#
|
#
|
||||||
# Returns a new Gollum::Markup object, ready for rendering.
|
# Returns a new Gollum::Markup object, ready for rendering.
|
||||||
def initialize(page)
|
def initialize(page)
|
||||||
|
@wiki = page.wiki
|
||||||
@name = page.name
|
@name = page.name
|
||||||
@data = page.raw_data
|
@data = page.raw_data
|
||||||
|
@version = page.version.id
|
||||||
@tagmap = {}
|
@tagmap = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -74,7 +76,15 @@ module Gollum
|
|||||||
parts = tag.split('|')
|
parts = tag.split('|')
|
||||||
name = parts[0].strip
|
name = parts[0].strip
|
||||||
|
|
||||||
nil
|
if name =~ /^\//
|
||||||
|
if file = @wiki.file(name[1..-1], @version)
|
||||||
|
%{<img src="#{file.name}" />}
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Attempt to process the tag as a file link tag.
|
# Attempt to process the tag as a file link tag.
|
||||||
|
|||||||
+8
-3
@@ -16,7 +16,7 @@ module Gollum
|
|||||||
#
|
#
|
||||||
# Returns the String name.
|
# Returns the String name.
|
||||||
def name
|
def name
|
||||||
@blob.name rescue nil
|
@blob && @blob.name
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: The path of the page within the repo.
|
# Public: The path of the page within the repo.
|
||||||
@@ -28,14 +28,14 @@ module Gollum
|
|||||||
#
|
#
|
||||||
# Returns the String data.
|
# Returns the String data.
|
||||||
def raw_data
|
def raw_data
|
||||||
@blob.data rescue nil
|
@blob && @blob.data
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: The formatted contents of the page.
|
# Public: The formatted contents of the page.
|
||||||
#
|
#
|
||||||
# Returns the String data.
|
# Returns the String data.
|
||||||
def formatted_data
|
def formatted_data
|
||||||
Gollum::Markup.new(self).render rescue nil
|
@blob && Gollum::Markup.new(self).render
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: The format of the page.
|
# Public: The format of the page.
|
||||||
@@ -107,6 +107,11 @@ module Gollum
|
|||||||
#
|
#
|
||||||
#########################################################################
|
#########################################################################
|
||||||
|
|
||||||
|
# The underlying wiki repo.
|
||||||
|
#
|
||||||
|
# Returns the Gollum::Wiki containing the page.
|
||||||
|
attr_reader :wiki
|
||||||
|
|
||||||
# Set the Grit::Commit version of the page.
|
# Set the Grit::Commit version of the page.
|
||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
|
|||||||
+57
-7
@@ -43,11 +43,17 @@ module Gollum
|
|||||||
def write_page(name, format, data, commit = {})
|
def write_page(name, format, data, commit = {})
|
||||||
ext = Page.format_to_ext(format)
|
ext = Page.format_to_ext(format)
|
||||||
path = Gollum.canonical_name(name) + '.' + ext
|
path = Gollum.canonical_name(name) + '.' + ext
|
||||||
index = @repo.index
|
|
||||||
index.add(path, data)
|
map = {}
|
||||||
|
if pcommit = @repo.commit('master')
|
||||||
|
map = tree_map(pcommit.tree)
|
||||||
|
end
|
||||||
|
map[path] = data
|
||||||
|
index = tree_map_to_index(map)
|
||||||
|
|
||||||
|
parents = pcommit ? [pcommit] : []
|
||||||
actor = Grit::Actor.new(commit[:name], commit[:email])
|
actor = Grit::Actor.new(commit[:name], commit[:email])
|
||||||
parent = @repo.commit('master')
|
index.commit(commit[:message], parents, actor)
|
||||||
index.commit(commit[:message], [parent], actor)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: Update an existing page with new content. The location of the
|
# Public: Update an existing page with new content. The location of the
|
||||||
@@ -62,11 +68,13 @@ module Gollum
|
|||||||
#
|
#
|
||||||
# Returns the String SHA1 of the newly written version.
|
# Returns the String SHA1 of the newly written version.
|
||||||
def update_page(page, data, commit = {})
|
def update_page(page, data, commit = {})
|
||||||
index = @repo.index
|
pcommit = @repo.commit('master')
|
||||||
|
map = tree_map(pcommit.tree)
|
||||||
|
index = tree_map_to_index(map)
|
||||||
index.add(page.path, data)
|
index.add(page.path, data)
|
||||||
|
|
||||||
actor = Grit::Actor.new(commit[:name], commit[:email])
|
actor = Grit::Actor.new(commit[:name], commit[:email])
|
||||||
parent = @repo.commit('master')
|
index.commit(commit[:message], [pcommit], actor)
|
||||||
index.commit(commit[:message], [parent], actor)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
#########################################################################
|
#########################################################################
|
||||||
@@ -84,5 +92,47 @@ module Gollum
|
|||||||
#
|
#
|
||||||
# Returns the String path.
|
# Returns the String path.
|
||||||
attr_reader :path
|
attr_reader :path
|
||||||
|
|
||||||
|
# Fill an index with the existing state of the repository.
|
||||||
|
#
|
||||||
|
# tree - The Grit::Tree to start with.
|
||||||
|
#
|
||||||
|
# Returns a nested Hash of filename to content mappings.
|
||||||
|
def tree_map(tree)
|
||||||
|
map = {}
|
||||||
|
tree.contents.each do |item|
|
||||||
|
case item
|
||||||
|
when Grit::Blob
|
||||||
|
map[item.name] = item.data
|
||||||
|
when Grit::Tree
|
||||||
|
map[item.name] = tree_map(item)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
map
|
||||||
|
end
|
||||||
|
|
||||||
|
# Use a treemap to fill in the index.
|
||||||
|
#
|
||||||
|
# map - The Hash map:
|
||||||
|
# key - The String directory or filename.
|
||||||
|
# val - The Hash submap or the String contents of the file.
|
||||||
|
# index - The Grit::Index to use. Leave blank when calling from outside
|
||||||
|
# this method (default: nil).
|
||||||
|
#
|
||||||
|
# Returns the Grit::Index.
|
||||||
|
def tree_map_to_index(map, prefix = '', index = nil)
|
||||||
|
index ||= @repo.index
|
||||||
|
map.each do |k, v|
|
||||||
|
case k
|
||||||
|
when String
|
||||||
|
name = [prefix, k].join('/')[1..-1]
|
||||||
|
index.add(k, v)
|
||||||
|
when Hash
|
||||||
|
new_prefix = [prefix, k].join('/')[1..-1]
|
||||||
|
tree_map_to_index(v, new_prefix, index)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
index
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
+17
-4
@@ -7,10 +7,9 @@ context "Markup" do
|
|||||||
Grit::Repo.init_bare(@path)
|
Grit::Repo.init_bare(@path)
|
||||||
@wiki = Gollum::Wiki.new(@path)
|
@wiki = Gollum::Wiki.new(@path)
|
||||||
|
|
||||||
commit = { :message => "Bilbo page",
|
@commit = { :message => "Add stuff",
|
||||||
:name => "Tom Preston-Werner",
|
:name => "Tom Preston-Werner",
|
||||||
:email => "tom@github.com" }
|
:email => "tom@github.com" }
|
||||||
@wiki.write_page("Bilbo Baggins", :markdown, "a [[Bilbo Baggins]] b", commit)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
teardown do
|
teardown do
|
||||||
@@ -18,8 +17,22 @@ context "Markup" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "page link" do
|
test "page link" do
|
||||||
|
@wiki.write_page("Bilbo Baggins", :markdown, "a [[Bilbo Baggins]] b", @commit)
|
||||||
|
|
||||||
page = @wiki.page("Bilbo Baggins")
|
page = @wiki.page("Bilbo Baggins")
|
||||||
output = Gollum::Markup.new(page).render
|
output = Gollum::Markup.new(page).render
|
||||||
assert_equal %{<p>a <a href="Bilbo-Baggins">Bilbo Baggins</a> b</p>\n}, output
|
assert_equal %{<p>a <a href="Bilbo-Baggins">Bilbo Baggins</a> b</p>\n}, output
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "image" do
|
||||||
|
index = @wiki.repo.index
|
||||||
|
index.add("alpha.jpg", "hi")
|
||||||
|
index.commit("Add alpha.jpg")
|
||||||
|
|
||||||
|
@wiki.write_page("Bilbo Baggins", :markdown, "a [[/alpha.jpg]] b", @commit)
|
||||||
|
|
||||||
|
page = @wiki.page("Bilbo Baggins")
|
||||||
|
output = Gollum::Markup.new(page).render
|
||||||
|
assert_equal %{<p>a <img src="alpha.jpg" /> b</p>\n}, output
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -31,6 +31,12 @@ context "Wiki page writing" do
|
|||||||
assert_equal "Gollum page", @wiki.repo.commits.first.message
|
assert_equal "Gollum page", @wiki.repo.commits.first.message
|
||||||
assert_equal "Tom Preston-Werner", @wiki.repo.commits.first.author.name
|
assert_equal "Tom Preston-Werner", @wiki.repo.commits.first.author.name
|
||||||
assert_equal "tom@github.com", @wiki.repo.commits.first.author.email
|
assert_equal "tom@github.com", @wiki.repo.commits.first.author.email
|
||||||
|
assert @wiki.page("Gollum")
|
||||||
|
|
||||||
|
@wiki.write_page("Bilbo", :markdown, "# Bilbo", commit)
|
||||||
|
assert_equal 2, @wiki.repo.commits.size
|
||||||
|
assert @wiki.page("Bilbo")
|
||||||
|
assert @wiki.page("Gollum")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "update_page" do
|
test "update_page" do
|
||||||
|
|||||||
Reference in New Issue
Block a user