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
+10 -8
View File
@@ -17,10 +17,10 @@ require 'optparse'
require 'rubygems' require 'rubygems'
require 'gollum' require 'gollum'
exec = {} exec = {}
options = { 'port' => 4567, 'bind' => '0.0.0.0' } options = { 'port' => 4567, 'bind' => '0.0.0.0' }
wiki_options = { wiki_options = {
:live_preview => false, :live_preview => false,
:allow_uploads => false, :allow_uploads => false,
} }
@@ -85,7 +85,7 @@ opts = OptionParser.new do |opts|
end end
opts.on("--allow-uploads [MODE]", [:dir, :page], "Allows file uploads. Modes: dir (default, store all uploads in the same directory), page (store each upload at the same location as the page).") do |mode| opts.on("--allow-uploads [MODE]", [:dir, :page], "Allows file uploads. Modes: dir (default, store all uploads in the same directory), page (store each upload at the same location as the page).") do |mode|
wiki_options[:allow_uploads] = true wiki_options[:allow_uploads] = true
wiki_options[:per_page_uploads] = true if mode == :page wiki_options[:per_page_uploads] = true if mode == :page
end end
@@ -120,8 +120,8 @@ end
# --gollum-path wins over ARGV[0] # --gollum-path wins over ARGV[0]
gollum_path = wiki_options[:gollum_path] ? gollum_path = wiki_options[:gollum_path] ?
wiki_options[:gollum_path] : wiki_options[:gollum_path] :
ARGV[0] || Dir.pwd ARGV[0] || Dir.pwd
if options['irb'] if options['irb']
require 'irb' require 'irb'
@@ -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
+100 -100
View File
@@ -43,7 +43,7 @@ module Precious
register Mustache::Sinatra register Mustache::Sinatra
include Precious::Helpers include Precious::Helpers
dir = File.dirname(File.expand_path(__FILE__)) dir = File.dirname(File.expand_path(__FILE__))
# Detect unsupported browsers. # Detect unsupported browsers.
Browser = Struct.new(:browser, :version) Browser = Struct.new(:browser, :version)
@@ -56,23 +56,23 @@ 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
set :public_folder, "#{dir}/public/gollum" set :public_folder, "#{dir}/public/gollum"
set :static, true set :static, true
set :default_markup, :markdown set :default_markup, :markdown
set :mustache, { set :mustache, {
# Tell mustache where the Views constant lives # Tell mustache where the Views constant lives
:namespace => Precious, :namespace => Precious,
# Mustache templates live here # Mustache templates live here
:templates => "#{dir}/templates", :templates => "#{dir}/templates",
# Tell mustache where the views are # Tell mustache where the views are
:views => "#{dir}/views" :views => "#{dir}/views"
} }
# Sinatra error handling # Sinatra error handling
@@ -90,7 +90,7 @@ module Precious
# above will detect base_path when it's used with map in a config.ru # above will detect base_path when it's used with map in a config.ru
settings.wiki_options.merge!({ :base_path => @base_url }) settings.wiki_options.merge!({ :base_path => @base_url })
@css = settings.wiki_options[:css] @css = settings.wiki_options[:css]
@js = settings.wiki_options[:js] @js = settings.wiki_options[:js]
end end
get '/' do get '/' do
@@ -139,9 +139,9 @@ module Precious
end end
redirect to(live_preview_url) redirect to(live_preview_url)
else else
@page = page @page = page
@page.version = wiki.repo.log(wiki.ref, @page.path).first @page.version = wiki.repo.log(wiki.ref, @page.path).first
@content = page.text_data @content = page.text_data
mustache :edit mustache :edit
end end
else else
@@ -163,9 +163,9 @@ module Precious
tempfile = params[:file][:tempfile] tempfile = params[:file][:tempfile]
end end
dir = wiki.per_page_uploads ? params[:upload_dest] : 'uploads' dir = wiki.per_page_uploads ? params[:upload_dest] : 'uploads'
ext = ::File.extname(fullname) ext = ::File.extname(fullname)
format = ext.split('.').last || 'txt' format = ext.split('.').last || 'txt'
filename = ::File.basename(fullname, ext) filename = ::File.basename(fullname, ext)
contents = ::File.read(tempfile) contents = ::File.read(tempfile)
reponame = filename + '.' + format reponame = filename + '.' + format
@@ -173,10 +173,10 @@ module Precious
head = wiki.repo.head head = wiki.repo.head
options = { options = {
:message => "Uploaded file to #{dir}/#{reponame}", :message => "Uploaded file to #{dir}/#{reponame}",
:parent => wiki.repo.head.commit, :parent => wiki.repo.head.commit,
} }
author = session['gollum.author'] author = session['gollum.author']
unless author.nil? unless author.nil?
options.merge! author options.merge! author
end end
@@ -197,37 +197,37 @@ module Precious
end end
post '/rename/*' do post '/rename/*' do
wikip = wiki_page(params[:splat].first) wikip = wiki_page(params[:splat].first)
halt 500 if wikip.nil? halt 500 if wikip.nil?
wiki = wikip.wiki wiki = wikip.wiki
page = wiki.paged(wikip.name, wikip.path, exact = true) page = wiki.paged(wikip.name, wikip.path, exact = true)
rename = params[:rename] rename = params[:rename]
halt 500 if page.nil? halt 500 if page.nil?
halt 500 if rename.nil? or rename.empty? halt 500 if rename.nil? or rename.empty?
# Fixup the rename if it is a relative path # Fixup the rename if it is a relative path
# In 1.8.7 rename[0] != rename[0..0] # In 1.8.7 rename[0] != rename[0..0]
if rename[0..0] != '/' if rename[0..0] != '/'
source_dir = ::File.dirname(page.path) source_dir = ::File.dirname(page.path)
source_dir = '' if source_dir == '.' source_dir = '' if source_dir == '.'
(target_dir, target_name) = ::File.split(rename) (target_dir, target_name) = ::File.split(rename)
target_dir = target_dir == '' ? source_dir : "#{source_dir}/#{target_dir}" target_dir = target_dir == '' ? source_dir : "#{source_dir}/#{target_dir}"
rename = "#{target_dir}/#{target_name}" rename = "#{target_dir}/#{target_name}"
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
# This occurs on NOOPs, for example renaming A => A # This occurs on NOOPs, for example renaming A => A
redirect to("/#{page.escaped_url_path}") redirect to("/#{page.escaped_url_path}")
return return
end end
committer.commit committer.commit
wikip = wiki_page(rename) wikip = wiki_page(rename)
page = wiki.paged(wikip.name, wikip.path, exact = true) page = wiki.paged(wikip.name, wikip.path, exact = true)
return if page.nil? return if page.nil?
redirect to("/#{page.escaped_url_path}") redirect to("/#{page.escaped_url_path}")
end end
@@ -239,11 +239,11 @@ 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]
update_wiki_page(wiki, page.footer, params[:footer], commit) if params[:footer] update_wiki_page(wiki, page.footer, params[:footer], commit) if params[:footer]
update_wiki_page(wiki, page.sidebar, params[:sidebar], commit) if params[:sidebar] update_wiki_page(wiki, page.sidebar, params[:sidebar], commit) if params[:sidebar]
committer.commit committer.commit
@@ -252,9 +252,9 @@ module Precious
get '/delete/*' do get '/delete/*' do
wikip = wiki_page(params[:splat].first) wikip = wiki_page(params[:splat].first)
name = wikip.name name = wikip.name
wiki = wikip.wiki wiki = wikip.wiki
page = wikip.page page = wikip.page
unless page.nil? unless page.nil?
wiki.delete_page(page, { :message => "Destroyed #{name} (#{page.format})" }) wiki.delete_page(page, { :message => "Destroyed #{name} (#{page.format})" })
end end
@@ -286,10 +286,10 @@ module Precious
end end
post '/create' do post '/create' do
name = params[:page].to_url name = params[:page].to_url
path = sanitize_empty_params(params[:path]) || '' path = sanitize_empty_params(params[:path]) || ''
format = params[:format].intern format = params[:format].intern
wiki = wiki_new wiki = wiki_new
path.gsub!(/^\//, '') path.gsub!(/^\//, '')
@@ -305,15 +305,15 @@ module Precious
end end
post '/revert/*/:sha1/:sha2' do post '/revert/*/:sha1/:sha2' do
wikip = wiki_page(params[:splat].first) wikip = wiki_page(params[:splat].first)
@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]
commit = commit_message commit = commit_message
commit[:message] = "Revert commit #{sha1.chars.take(7).join}" commit[:message] = "Revert commit #{sha1.chars.take(7).join}"
if wiki.revert_page(@page, sha1, sha2, commit) if wiki.revert_page(@page, sha1, sha2, commit)
redirect to("/#{@page.escaped_url_path}") redirect to("/#{@page.escaped_url_path}")
@@ -328,23 +328,23 @@ module Precious
end end
post '/preview' do post '/preview' do
wiki = wiki_new wiki = wiki_new
@name = params[:page] || "Preview" @name = params[:page] || "Preview"
@page = wiki.preview_page(@name, params[:content], params[:format]) @page = wiki.preview_page(@name, params[:content], params[:format])
@content = @page.formatted_data @content = @page.formatted_data
@toc_content = wiki.universal_toc ? @page.toc_data : nil @toc_content = wiki.universal_toc ? @page.toc_data : nil
@mathjax = wiki.mathjax @mathjax = wiki.mathjax
@h1_title = wiki.h1_title @h1_title = wiki.h1_title
@editable = false @editable = false
@allow_uploads = wiki.allow_uploads @allow_uploads = wiki.allow_uploads
mustache :page mustache :page
end end
get '/history/*' do get '/history/*' do
@page = wiki_page(params[:splat].first).page @page = wiki_page(params[:splat].first).page
@page_num = [params[:page].to_i, 1].max @page_num = [params[:page].to_i, 1].max
unless @page.nil? unless @page.nil?
@versions = @page.versions :page => @page_num @versions = @page.versions :page => @page_num
mustache :history mustache :history
else else
redirect to("/") redirect to("/")
@@ -358,10 +358,10 @@ module Precious
redirect to("/history/#{@file}") redirect to("/history/#{@file}")
else else
redirect to("/compare/%s/%s...%s" % [ redirect to("/compare/%s/%s...%s" % [
@file, @file,
@versions.last, @versions.last,
@versions.first] @versions.first]
) )
end end
end end
@@ -373,14 +373,14 @@ module Precious
\.{2,3} # match .. or ... \.{2,3} # match .. or ...
(.+) # match the second SHA1 (.+) # match the second SHA1
}x do |path, start_version, end_version| }x do |path, start_version, end_version|
wikip = wiki_page(path) wikip = wiki_page(path)
@path = wikip.path @path = wikip.path
@name = wikip.name @name = wikip.name
@versions = [start_version, end_version] @versions = [start_version, end_version]
wiki = wikip.wiki wiki = wikip.wiki
@page = wikip.page @page = wikip.page
diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path) diffs = wiki.repo.diff(@versions.first, @versions.last, @page.path)
@diff = diffs.first @diff = diffs.first
mustache :compare mustache :compare
end end
@@ -391,8 +391,8 @@ module Precious
name = wikip.name name = wikip.name
path = wikip.path path = wikip.path
if page = wikip.page if page = wikip.page
@page = page @page = page
@name = name @name = name
@content = page.formatted_data @content = page.formatted_data
@version = version @version = version
mustache :page mustache :page
@@ -402,11 +402,11 @@ module Precious
end end
get '/search' do get '/search' do
@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
@@ -426,16 +426,16 @@ module Precious
end end
get '/fileview' do get '/fileview' do
wiki = wiki_new wiki = wiki_new
options = settings.wiki_options options = settings.wiki_options
content = wiki.pages content = wiki.pages
# if showing all files include wiki.files # if showing all files include wiki.files
content += wiki.files if options[:show_all] content += wiki.files if options[:show_all]
# must pass wiki_options to FileView # must pass wiki_options to FileView
# --show-all and --collapse-tree can be set. # --show-all and --collapse-tree can be set.
@results = Gollum::FileView.new(content, options).render_files @results = Gollum::FileView.new(content, options).render_files
@ref = wiki.ref @ref = wiki.ref
mustache :file_view, { :layout => false } mustache :file_view, { :layout => false }
end end
@@ -450,21 +450,21 @@ module Precious
path = extract_path(fullpath) || '/' path = extract_path(fullpath) || '/'
if page = wiki.paged(name, path, exact = true) if page = wiki.paged(name, path, exact = true)
@page = page @page = page
@name = name @name = name
@content = page.formatted_data @content = page.formatted_data
@upload_dest = settings.wiki_options[:allow_uploads] ? @upload_dest = settings.wiki_options[:allow_uploads] ?
(settings.wiki_options[:per_page_uploads] ? (settings.wiki_options[:per_page_uploads] ?
"#{path}/#{@name}".sub(/^\/\//, '') : 'uploads' "#{path}/#{@name}".sub(/^\/\//, '') : 'uploads'
) : '' ) : ''
# Extensions and layout data # Extensions and layout data
@editable = true @editable = true
@page_exists = !page.versions.empty? @page_exists = !page.versions.empty?
@toc_content = wiki.universal_toc ? @page.toc_data : nil @toc_content = wiki.universal_toc ? @page.toc_data : nil
@mathjax = wiki.mathjax @mathjax = wiki.mathjax
@h1_title = wiki.h1_title @h1_title = wiki.h1_title
@bar_side = wiki.bar_side @bar_side = wiki.bar_side
@allow_uploads = wiki.allow_uploads @allow_uploads = wiki.allow_uploads
mustache :page mustache :page
@@ -483,9 +483,9 @@ module Precious
def update_wiki_page(wiki, page, content, commit, name = nil, format = nil) def update_wiki_page(wiki, page, content, commit, name = nil, format = nil)
return if !page || return if !page ||
((!content || page.raw_data == content) && page.format == format) ((!content || page.raw_data == content) && page.format == format)
name ||= page.name name ||= page.name
format = (format || page.format).to_sym format = (format || page.format).to_sym
content ||= page.raw_data content ||= page.raw_data
wiki.update_page(page, name, format, content.to_s, commit) wiki.update_page(page, name, format, content.to_s, commit)
end end
@@ -499,8 +499,8 @@ module Precious
# message is sourced from the incoming request parameters # message is sourced from the incoming request parameters
# author details are sourced from the session, to be populated by rack middleware ahead of us # author details are sourced from the session, to be populated by rack middleware ahead of us
def commit_message def commit_message
msg = (params[:message].nil? or params[:message].empty?) ? "[no message]" : params[:message] msg = (params[:message].nil? or params[:message].empty?) ? "[no message]" : params[:message]
commit_message = { :message => msg } commit_message = { :message => msg }
author_parameters = session['gollum.author'] author_parameters = session['gollum.author']
commit_message.merge! author_parameters unless author_parameters.nil? commit_message.merge! author_parameters unless author_parameters.nil?
commit_message commit_message
+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
+142 -110
View File
@@ -49,132 +49,164 @@ 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);
# A - Z return true
if (65 <= cc && cc <= 90); return true end end
# 0 - 9 # A - Z
if (48 <= cc && cc <= 57); return true end if (65 <= cc && cc <= 90);
return true
end
# 0 - 9
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
if (cc == 33); return true end end
# '()* # !
if (39 <= cc && cc <= 42); return true end if (cc == 33);
# -. return true
if (45 <= cc && cc <= 46); return true end end
# _ # '()*
if (cc == 95); return true end if (39 <= cc && cc <= 42);
# ~ return true
if (cc == 126); return true end end
# -.
if (45 <= cc && cc <= 46);
return true
end
# _
if (cc == 95);
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;
octets = Array.new(3); octets = Array.new(3);
if (cc <= 0x007F) if (cc <= 0x007F)
octets[0] = cc; octets[0] = cc;
elsif (cc <= 0x07FF) elsif (cc <= 0x07FF)
octets[0] = y + 192; octets[0] = y + 192;
octets[1] = z + 128; octets[1] = z + 128;
else else
octets[0] = x + 224; octets[0] = x + 224;
octets[1] = y + 128; octets[1] = y + 128;
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];
index += 1 index += 1
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
return index; if (octets[2]);
end index = URIAddEncodedOctetToBuffer(octets[2], result, index)
end
if (octets[3]);
index = URIAddEncodedOctetToBuffer(octets[3], result, index)
end
return index;
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;
y = (cc2 >> 6) & 0xF; y = (cc2 >> 6) & 0xF;
z = cc2 & 63; z = cc2 & 63;
octets = Array.new(4); octets = Array.new(4);
octets[0] = (u >> 2) + 240; octets[0] = (u >> 2) + 240;
octets[1] = (((u & 3) << 4) | w) + 128; octets[1] = (((u & 3) << 4) | w) + 128;
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
# on the incoming array. # on the incoming array.
result = Array.new(uriLength); result = Array.new(uriLength);
index = 0; index = 0;
k = -1; k = -1;
while ((k+=1) < uriLength) do while ((k+=1) < uriLength) do
cc1 = uri.charCodeAt(k); cc1 = uri.charCodeAt(k);
next if cc1.nil? next if cc1.nil?
if (self.send(unescape, cc1)) if (self.send(unescape, cc1))
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);
if (cc1 < 0xD800 || cc1 > 0xDBFF) throw("URI malformed")
index = URIEncodeSingle(cc1, result, index); end
else if (cc1 < 0xD800 || cc1 > 0xDBFF)
k+=1; index = URIEncodeSingle(cc1, result, index);
if (k == uriLength); throw("URI malformed") end else
cc2 = uri.charCodeAt(k); k+=1;
if (cc2 < 0xDC00 || cc2 > 0xDFFF); throw("URI malformed") end if (k == uriLength);
index = URIEncodePair(cc1, cc2, result, index); throw("URI malformed")
end
cc2 = uri.charCodeAt(k);
if (cc2 < 0xDC00 || cc2 > 0xDFFF);
throw("URI malformed")
end
index = URIEncodePair(cc1, cc2, result, index);
end
end
end end
# use .compact to get rid of nils from charCodeAt
# return %StringFromCharCodeArray(result);
# 'c' = 8 bit signed char
# http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-pack
return result.compact.pack 'c*'
end end
end end # class << self
# use .compact to get rid of nils from charCodeAt
# return %StringFromCharCodeArray(result);
# 'c' = 8 bit signed char
# http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-pack
return result.compact.pack 'c*'
end
end # class << self
end # module end # module
+16 -14
View File
@@ -47,43 +47,45 @@ 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+)/)
@left_diff_line_number = li.to_i @left_diff_line_number = li.to_i
@current_line_number = @left_diff_line_number @current_line_number = @left_diff_line_number
ret = '...' ret = '...'
elsif line[0] == ?- elsif line[0] == ?-
ret = @left_diff_line_number.to_s ret = @left_diff_line_number.to_s
@left_diff_line_number += 1 @left_diff_line_number += 1
@current_line_number = @left_diff_line_number - 1 @current_line_number = @left_diff_line_number - 1
elsif line[0] == ?+ elsif line[0] == ?+
ret = ' ' ret = ' '
else else
ret = @left_diff_line_number.to_s ret = @left_diff_line_number.to_s
@left_diff_line_number += 1 @left_diff_line_number += 1
@current_line_number = @left_diff_line_number - 1 @current_line_number = @left_diff_line_number - 1
end end
ret ret
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+)/)
@right_diff_line_number = ri.to_i @right_diff_line_number = ri.to_i
@current_line_number = @right_diff_line_number @current_line_number = @right_diff_line_number
ret = '...' ret = '...'
elsif line[0] == ?- elsif line[0] == ?-
ret = ' ' ret = ' '
elsif line[0] == ?+ elsif line[0] == ?+
ret = @right_diff_line_number.to_s ret = @right_diff_line_number.to_s
@right_diff_line_number += 1 @right_diff_line_number += 1
@current_line_number = @right_diff_line_number - 1 @current_line_number = @right_diff_line_number - 1
else else
ret = @right_diff_line_number.to_s ret = @right_diff_line_number.to_s
@right_diff_line_number += 1 @right_diff_line_number += 1
@current_line_number = @right_diff_line_number - 1 @current_line_number = @right_diff_line_number - 1
end end
ret ret
end end
+4 -4
View File
@@ -6,10 +6,10 @@ module Precious
attr_reader :page, :content attr_reader :page, :content
# return path set in app.rb not @page.path # return path set in app.rb not @page.path
def path def path
@path @path
end end
def title def title
"#{@page.title}" "#{@page.title}"
+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
+14 -14
View File
@@ -13,16 +13,16 @@ module Precious
i = @versions.size + 1 i = @versions.size + 1
@versions.map do |v| @versions.map do |v|
i -= 1 i -= 1
{ :id => v.id, { :id => v.id,
:id7 => v.id[0..6], :id7 => v.id[0..6],
:num => i, :num => i,
:selected => @page.version.id == v.id, :selected => @page.version.id == v.id,
:author => v.author.name.respond_to?(:force_encoding) ? v.author.name.force_encoding('UTF-8') : v.author.name, :author => v.author.name.respond_to?(:force_encoding) ? v.author.name.force_encoding('UTF-8') : v.author.name,
:message => v.message.respond_to?(:force_encoding) ? v.message.force_encoding('UTF-8') : v.message, :message => v.message.respond_to?(:force_encoding) ? v.message.force_encoding('UTF-8') : v.message,
: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,16 +36,16 @@ 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) |
# ((b[2] & 0xFF) << 8) | (b[3] & 0xFF) # ((b[2] & 0xFF) << 8) | (b[3] & 0xFF)
return left_shift(b[0], 24) | return left_shift(b[0], 24) |
left_shift(b[1], 16) | left_shift(b[1], 16) |
left_shift(b[2], 8) | left_shift(b[2], 8) |
b[3] & 0xFF b[3] & 0xFF
end end
def _identicon_code(blob) def _identicon_code(blob)
@@ -53,7 +53,7 @@ module Precious
end end
def use_identicon def use_identicon
@page.wiki.user_icons == 'identicon' @page.wiki.user_icons == 'identicon'
end end
def partial(name) def partial(name)
+7 -7
View File
@@ -4,9 +4,9 @@ module Precious
include HasPage include HasPage
attr_reader :content, :page, :header, :footer attr_reader :content, :page, :header, :footer
DATE_FORMAT = "%Y-%m-%d %H:%M:%S" DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
DEFAULT_AUTHOR = 'you' DEFAULT_AUTHOR = 'you'
@@to_xml = { :save_with => Nokogiri::XML::Node::SaveOptions::DEFAULT_XHTML ^ 1, :indent => 0, :encoding => 'UTF-8' } @@to_xml = { :save_with => Nokogiri::XML::Node::SaveOptions::DEFAULT_XHTML ^ 1, :indent => 0, :encoding => 'UTF-8' }
def title def title
h1 = @h1_title ? page_header_from_content(@content) : false h1 = @h1_title ? page_header_from_content(@content) : false
@@ -23,14 +23,14 @@ module Precious
def author def author
page_versions = @page.versions page_versions = @page.versions
first = page_versions ? page_versions.first : false first = page_versions ? page_versions.first : false
return DEFAULT_AUTHOR unless first return DEFAULT_AUTHOR unless first
first.author.name.respond_to?(:force_encoding) ? first.author.name.force_encoding('UTF-8') : first.author.name first.author.name.respond_to?(:force_encoding) ? first.author.name.force_encoding('UTF-8') : first.author.name
end end
def date def date
page_versions = @page.versions page_versions = @page.versions
first = page_versions ? page_versions.first : false first = page_versions ? page_versions.first : false
return Time.now.strftime(DATE_FORMAT) unless first return Time.now.strftime(DATE_FORMAT) unless first
first.authored_date.strftime(DATE_FORMAT) first.authored_date.strftime(DATE_FORMAT)
end end
@@ -153,7 +153,7 @@ module Precious
# Extracts title from page if present. # Extracts title from page if present.
# #
def page_header_from_content(content) def page_header_from_content(content)
doc = build_document(content) doc = build_document(content)
title = find_header_node(doc).inner_text.strip title = find_header_node(doc).inner_text.strip
title = nil if title.empty? title = nil if title.empty?
title title
@@ -162,11 +162,11 @@ module Precious
# Returns page content without title if it was extracted. # Returns page content without title if it was extracted.
# #
def content_without_page_header(content) def content_without_page_header(content)
doc = build_document(content) doc = build_document(content)
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
+2 -2
View File
@@ -11,7 +11,7 @@ module Precious
def breadcrumb def breadcrumb
if @path if @path
path = Pathname.new(@path) path = Pathname.new(@path)
breadcrumb = [%{<a href="#{@base_url}/pages/">Home</a>}] breadcrumb = [%{<a href="#{@base_url}/pages/">Home</a>}]
path.descend do |crumb| path.descend do |crumb|
title = crumb.basename title = crumb.basename
@@ -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