Format code

This commit is contained in:
bootstraponline
2014-04-05 12:48:31 -04:00
parent 452d825e66
commit 2ae75978dd
12 changed files with 307 additions and 270 deletions
+4 -2
View File
@@ -151,7 +151,9 @@ if options['irb']
begin begin
require 'gollum-lib' require 'gollum-lib'
wiki = Gollum::Wiki.new(gollum_path, wiki_options) wiki = Gollum::Wiki.new(gollum_path, wiki_options)
if !wiki.exist? then raise Gollum::InvalidGitRepositoryError end if !wiki.exist? then
raise Gollum::InvalidGitRepositoryError
end
puts "Loaded Gollum wiki at #{File.expand_path(gollum_path).inspect}." puts "Loaded Gollum wiki at #{File.expand_path(gollum_path).inspect}."
puts puts
puts %( page = wiki.page('page-name')) puts %( page = wiki.page('page-name'))
@@ -193,7 +195,7 @@ else
def initialize base_path def initialize base_path
@mg = Rack::Builder.new do @mg = Rack::Builder.new do
map '/' do map '/' do
run Proc.new { [ 302, {'Location'=> "/#{base_path}" }, [] ] } run Proc.new { [302, { 'Location' => "/#{base_path}" }, []] }
end end
map "/#{base_path}" do map "/#{base_path}" do
+3 -2
View File
@@ -13,7 +13,7 @@ require File.expand_path('../gollum/uri_encode_component', __FILE__)
# Set ruby to UTF-8 mode # Set ruby to UTF-8 mode
# This is required for Ruby 1.8.7 which gollum still supports. # This is required for Ruby 1.8.7 which gollum still supports.
$KCODE = 'U' if RUBY_VERSION[0,3] == '1.8' $KCODE = 'U' if RUBY_VERSION[0, 3] == '1.8'
module Gollum module Gollum
VERSION = '3.0.0' VERSION = '3.0.0'
@@ -22,7 +22,8 @@ module Gollum
::File.expand_path('gollum/public', ::File.dirname(__FILE__)) ::File.expand_path('gollum/public', ::File.dirname(__FILE__))
end end
class Error < StandardError; end class Error < StandardError;
end
class DuplicatePageError < Error class DuplicatePageError < Error
attr_accessor :dir attr_accessor :dir
+5 -5
View File
@@ -56,7 +56,7 @@ module Precious
def supported_useragent?(user_agent) def supported_useragent?(user_agent)
ua = UserAgent.parse(user_agent) ua = UserAgent.parse(user_agent)
@@min_ua.detect {|min| ua >= min } @@min_ua.detect { |min| ua >= min }
end end
# We want to serve public assets for now # We want to serve public assets for now
@@ -216,7 +216,7 @@ module Precious
end end
committer = Gollum::Committer.new(wiki, commit_message) committer = Gollum::Committer.new(wiki, commit_message)
commit = {:committer => committer} commit = { :committer => committer }
success = wiki.rename_page(page, rename, commit) success = wiki.rename_page(page, rename, commit)
if !success if !success
@@ -239,7 +239,7 @@ module Precious
page = wiki.paged(page_name, path, exact = true) page = wiki.paged(page_name, path, exact = true)
return if page.nil? return if page.nil?
committer = Gollum::Committer.new(wiki, commit_message) committer = Gollum::Committer.new(wiki, commit_message)
commit = {:committer => committer} commit = { :committer => committer }
update_wiki_page(wiki, page, params[:content], commit, page.name, params[:format]) update_wiki_page(wiki, page, params[:content], commit, page.name, params[:format])
update_wiki_page(wiki, page.header, params[:header], commit) if params[:header] update_wiki_page(wiki, page.header, params[:header], commit) if params[:header]
@@ -309,7 +309,7 @@ module Precious
@path = wikip.path @path = wikip.path
@name = wikip.name @name = wikip.name
wiki = wikip.wiki wiki = wikip.wiki
@page = wiki.paged(@name,@path) @page = wiki.paged(@name, @path)
sha1 = params[:sha1] sha1 = params[:sha1]
sha2 = params[:sha2] sha2 = params[:sha2]
@@ -405,7 +405,7 @@ module Precious
@query = params[:q] @query = params[:q]
wiki = wiki_new wiki = wiki_new
# Sort wiki search results by count (desc) and then by name (asc) # Sort wiki search results by count (desc) and then by name (asc)
@results = wiki.search(@query).sort{ |a, b| (a[:count] <=> b[:count]).nonzero? || b[:name] <=> a[:name] }.reverse @results = wiki.search(@query).sort { |a, b| (a[:count] <=> b[:count]).nonzero? || b[:name] <=> a[:name] }.reverse
@name = @query @name = @query
mustache :search mustache :search
end end
+3 -3
View File
@@ -22,13 +22,13 @@ module Precious
end end
def sanitize_empty_params(param) def sanitize_empty_params(param)
[nil,''].include?(param) ? nil : CGI.unescape(param) [nil, ''].include?(param) ? nil : CGI.unescape(param)
end end
# Ensure path begins with a single leading slash # Ensure path begins with a single leading slash
def clean_path(path) def clean_path(path)
if path if path
(path[0] != '/' ? path.insert(0, '/') : path).gsub(/\/{2,}/,'/') (path[0] != '/' ? path.insert(0, '/') : path).gsub(/\/{2,}/, '/')
end end
end end
@@ -36,7 +36,7 @@ module Precious
# Remove all double slashes # Remove all double slashes
def clean_url url def clean_url url
return url if url.nil? return url if url.nil?
url.gsub('%2F','/').gsub(/^\/+/,'').gsub('//','/') url.gsub('%2F', '/').gsub(/^\/+/, '').gsub('//', '/')
end end
end end
+66 -34
View File
@@ -49,38 +49,58 @@ class String
end end
end end
module URI; class << self module URI
;
class << self
# Does the char code correspond to an alpha-numeric char. # Does the char code correspond to an alpha-numeric char.
# isAlphaNumeric('a'.ord) => true # isAlphaNumeric('a'.ord) => true
# isAlphaNumeric(''.ord) => false # isAlphaNumeric(''.ord) => false
def isAlphaNumeric(cc) def isAlphaNumeric(cc)
# a - z # a - z
if (97 <= cc && cc <= 122); return true end if (97 <= cc && cc <= 122);
return true
end
# A - Z # A - Z
if (65 <= cc && cc <= 90); return true end if (65 <= cc && cc <= 90);
return true
end
# 0 - 9 # 0 - 9
if (48 <= cc && cc <= 57); return true end if (48 <= cc && cc <= 57);
return true
end
return false return false
end end
def unescapePredicate(cc) def unescapePredicate(cc)
if (isAlphaNumeric(cc)); return true end if (isAlphaNumeric(cc));
return true
end
# ! # !
if (cc == 33); return true end if (cc == 33);
return true
end
# '()* # '()*
if (39 <= cc && cc <= 42); return true end if (39 <= cc && cc <= 42);
return true
end
# -. # -.
if (45 <= cc && cc <= 46); return true end if (45 <= cc && cc <= 46);
return true
end
# _ # _
if (cc == 95); return true end if (cc == 95);
return true
end
# ~ # ~
if (cc == 126); return true end if (cc == 126);
return true
end
return false return false
end end
def URIEncodeSingle(cc, result, index) def URIEncodeSingle(cc, result, index)
x = (cc >> 12) & 0xF; x = (cc >> 12) & 0xF;
y = (cc >> 6) & 63; y = (cc >> 6) & 63;
z = cc & 63; z = cc & 63;
@@ -96,12 +116,12 @@ def URIEncodeSingle(cc, result, index)
octets[2] = z + 128; octets[2] = z + 128;
end end
return URIEncodeOctets(octets, result, index); return URIEncodeOctets(octets, result, index);
end end
# Lazily initialized. # Lazily initialized.
@@hexCharCodeArray = 0; @@hexCharCodeArray = 0;
def URIAddEncodedOctetToBuffer(octet, result, index) def URIAddEncodedOctetToBuffer(octet, result, index)
result[index] = 37; # Char code of '%'. result[index] = 37; # Char code of '%'.
index += 1 index += 1
result[index] = @@hexCharCodeArray[octet >> 4]; result[index] = @@hexCharCodeArray[octet >> 4];
@@ -109,21 +129,27 @@ def URIAddEncodedOctetToBuffer(octet, result, index)
result[index] = @@hexCharCodeArray[octet & 0x0F]; result[index] = @@hexCharCodeArray[octet & 0x0F];
index += 1 index += 1
return index; return index;
end end
def URIEncodeOctets(octets, result, index) def URIEncodeOctets(octets, result, index)
if (@@hexCharCodeArray == 0) if (@@hexCharCodeArray == 0)
@@hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, @@hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
65, 66, 67, 68, 69, 70]; 65, 66, 67, 68, 69, 70];
end end
index = URIAddEncodedOctetToBuffer(octets[0], result, index); index = URIAddEncodedOctetToBuffer(octets[0], result, index);
if (octets[1]); index = URIAddEncodedOctetToBuffer(octets[1], result, index) end if (octets[1]);
if (octets[2]); index = URIAddEncodedOctetToBuffer(octets[2], result, index) end index = URIAddEncodedOctetToBuffer(octets[1], result, index)
if (octets[3]); index = URIAddEncodedOctetToBuffer(octets[3], result, index) end end
if (octets[2]);
index = URIAddEncodedOctetToBuffer(octets[2], result, index)
end
if (octets[3]);
index = URIAddEncodedOctetToBuffer(octets[3], result, index)
end
return index; return index;
end end
def URIEncodePair(cc1 , cc2, result, index) def URIEncodePair(cc1, cc2, result, index)
u = ((cc1 >> 6) & 0xF) + 1; u = ((cc1 >> 6) & 0xF) + 1;
w = (cc1 >> 2) & 0xF; w = (cc1 >> 2) & 0xF;
x = cc1 & 3; x = cc1 & 3;
@@ -135,15 +161,15 @@ def URIEncodePair(cc1 , cc2, result, index)
octets[2] = ((x << 4) | y) + 128; octets[2] = ((x << 4) | y) + 128;
octets[3] = z + 128; octets[3] = z + 128;
return URIEncodeOctets(octets, result, index); return URIEncodeOctets(octets, result, index);
end end
# component must be String # component must be String
def URIEncodeComponent(componentString) def URIEncodeComponent(componentString)
Encode(componentString, :unescapePredicate); Encode(componentString, :unescapePredicate);
end end
# ECMA-262, section 15.1.3 # ECMA-262, section 15.1.3
def Encode(uri, unescape) def Encode(uri, unescape)
uriLength = uri.length; uriLength = uri.length;
# We are going to pass result to %StringFromCharCodeArray # We are going to pass result to %StringFromCharCodeArray
# which does not expect any getters/setters installed # which does not expect any getters/setters installed
@@ -158,14 +184,20 @@ def Encode(uri, unescape)
result[index] = cc1; result[index] = cc1;
index += 1 index += 1
else else
if (cc1 >= 0xDC00 && cc1 <= 0xDFFF); throw("URI malformed") end if (cc1 >= 0xDC00 && cc1 <= 0xDFFF);
throw("URI malformed")
end
if (cc1 < 0xD800 || cc1 > 0xDBFF) if (cc1 < 0xD800 || cc1 > 0xDBFF)
index = URIEncodeSingle(cc1, result, index); index = URIEncodeSingle(cc1, result, index);
else else
k+=1; k+=1;
if (k == uriLength); throw("URI malformed") end if (k == uriLength);
throw("URI malformed")
end
cc2 = uri.charCodeAt(k); cc2 = uri.charCodeAt(k);
if (cc2 < 0xDC00 || cc2 > 0xDFFF); throw("URI malformed") end if (cc2 < 0xDC00 || cc2 > 0xDFFF);
throw("URI malformed")
end
index = URIEncodePair(cc1, cc2, result, index); index = URIEncodePair(cc1, cc2, result, index);
end end
end end
@@ -175,6 +207,6 @@ def Encode(uri, unescape)
# 'c' = 8 bit signed char # 'c' = 8 bit signed char
# http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-pack # http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-pack
return result.compact.pack 'c*' return result.compact.pack 'c*'
end end
end # class << self end # class << self
end # module end # module
+2
View File
@@ -47,6 +47,7 @@ module Precious
end end
@left_diff_line_number = nil @left_diff_line_number = nil
def left_diff_line_number(id, line) def left_diff_line_number(id, line)
if line =~ /^@@/ if line =~ /^@@/
m, li = *line.match(/\-(\d+)/) m, li = *line.match(/\-(\d+)/)
@@ -68,6 +69,7 @@ module Precious
end end
@right_diff_line_number = nil @right_diff_line_number = nil
def right_diff_line_number(id, line) def right_diff_line_number(id, line)
if line =~ /^@@/ if line =~ /^@@/
m, ri = *line.match(/\+(\d+)/) m, ri = *line.match(/\+(\d+)/)
+1 -1
View File
@@ -4,7 +4,7 @@ module Precious
Gollum::Markup.formats.map do |key, val| Gollum::Markup.formats.map do |key, val|
{ :name => val[:name], { :name => val[:name],
:id => key.to_s, :id => key.to_s,
:selected => selected == key} :selected => selected == key }
end.sort do |a, b| end.sort do |a, b|
a[:name].downcase <=> b[:name].downcase a[:name].downcase <=> b[:name].downcase
end end
+2 -2
View File
@@ -22,7 +22,7 @@ module Precious
:date => v.authored_date.strftime("%B %d, %Y"), :date => v.authored_date.strftime("%B %d, %Y"),
:gravatar => Digest::MD5.hexdigest(v.author.email.strip.downcase), :gravatar => Digest::MD5.hexdigest(v.author.email.strip.downcase),
:identicon => self._identicon_code(v.author.email), :identicon => self._identicon_code(v.author.email),
:date_full=> v.authored_date, :date_full => v.authored_date,
} }
end end
end end
@@ -36,7 +36,7 @@ module Precious
def string_to_code string def string_to_code string
# sha bytes # sha bytes
b = [Digest::SHA1.hexdigest(string)[0,20]].pack('H*').bytes.to_a b = [Digest::SHA1.hexdigest(string)[0, 20]].pack('H*').bytes.to_a
# Thanks donpark's IdenticonUtil.java for this. # Thanks donpark's IdenticonUtil.java for this.
# Match the following Java code # Match the following Java code
# ((b[0] & 0xFF) << 24) | ((b[1] & 0xFF) << 16) | # ((b[0] & 0xFF) << 24) | ((b[1] & 0xFF) << 16) |
+1 -1
View File
@@ -166,7 +166,7 @@ module Precious
title = find_header_node(doc) title = find_header_node(doc)
title.remove unless title.empty? title.remove unless title.empty?
# .inner_html will cause href escaping on UTF-8 # .inner_html will cause href escaping on UTF-8
doc.css("div#gollum-root").children.to_xml( @@to_xml ) doc.css("div#gollum-root").children.to_xml(@@to_xml)
end end
end end
end end
+1 -1
View File
@@ -34,7 +34,7 @@ module Precious
folder_links = [] folder_links = []
@results.map { |page| @results.map { |page|
page_path = page.path.sub(/^#{@path}\//,'') page_path = page.path.sub(/^#{@path}\//, '')
if page_path.include?('/') if page_path.include?('/')
folder = page_path.split('/').first folder = page_path.split('/').first