gollum-lib 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of gollum-lib might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Rakefile +4 -1
- data/gollum-lib.gemspec +31 -30
- data/lib/gollum-lib.rb +8 -8
- data/lib/gollum-lib/blob_entry.rb +5 -5
- data/lib/gollum-lib/committer.rb +11 -11
- data/lib/gollum-lib/file.rb +10 -9
- data/lib/gollum-lib/file_view.rb +33 -33
- data/lib/gollum-lib/filter.rb +3 -3
- data/lib/gollum-lib/filter/code.rb +30 -18
- data/lib/gollum-lib/filter/metadata.rb +5 -3
- data/lib/gollum-lib/filter/plain_text.rb +4 -3
- data/lib/gollum-lib/filter/remote_code.rb +20 -18
- data/lib/gollum-lib/filter/render.rb +4 -2
- data/lib/gollum-lib/filter/sanitize.rb +4 -2
- data/lib/gollum-lib/filter/tags.rb +36 -35
- data/lib/gollum-lib/filter/toc.rb +29 -13
- data/lib/gollum-lib/filter/wsd.rb +1 -1
- data/lib/gollum-lib/git_access.rb +11 -11
- data/lib/gollum-lib/gitcode.rb +13 -13
- data/lib/gollum-lib/grit_ext.rb +1 -1
- data/lib/gollum-lib/helpers.rb +2 -2
- data/lib/gollum-lib/markup.rb +81 -42
- data/lib/gollum-lib/markups.rb +1 -1
- data/lib/gollum-lib/page.rb +15 -15
- data/lib/gollum-lib/pagination.rb +1 -1
- data/lib/gollum-lib/sanitization.rb +74 -74
- data/lib/gollum-lib/wiki.rb +54 -54
- metadata +111 -103
data/lib/gollum-lib/gitcode.rb
CHANGED
@@ -10,10 +10,10 @@ module Gollum
|
|
10
10
|
raise(ArgumentError, 'path is nil or empty') if path.nil? or path.empty?
|
11
11
|
|
12
12
|
@uri = URI::HTTP.build({
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
:path => self.unchomp(path),
|
14
|
+
:host => 'raw.github.com',
|
15
|
+
:scheme => 'https',
|
16
|
+
:port => 443 })
|
17
17
|
end
|
18
18
|
|
19
19
|
def contents
|
@@ -27,20 +27,20 @@ module Gollum
|
|
27
27
|
|
28
28
|
def req uri, cut = 1
|
29
29
|
return "Too many redirects or retries" if cut >= 10
|
30
|
-
http
|
30
|
+
http = Net::HTTP.new uri.host, uri.port
|
31
31
|
http.use_ssl = true
|
32
|
-
resp
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
resp = http.get uri.path, {
|
33
|
+
'Accept' => 'text/plain',
|
34
|
+
'Cache-Control' => 'no-cache',
|
35
|
+
'Connection' => 'keep-alive',
|
36
|
+
'Host' => uri.host,
|
37
|
+
'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0'
|
38
38
|
}
|
39
|
-
code
|
39
|
+
code = resp.code.to_i
|
40
40
|
return resp.body if code == 200
|
41
41
|
return "Not Found" if code == 404
|
42
42
|
return "Unhandled Response Code #{code}" unless code == 304 or not resp.header['location'].nil?
|
43
|
-
loc
|
43
|
+
loc = URI.parse resp.header['location']
|
44
44
|
uri2 = loc.relative?() ? (uri + loc) : loc # overloads (+)
|
45
45
|
return req uri2, (cut + 1)
|
46
46
|
end
|
data/lib/gollum-lib/grit_ext.rb
CHANGED
data/lib/gollum-lib/helpers.rb
CHANGED
data/lib/gollum-lib/markup.rb
CHANGED
@@ -6,15 +6,22 @@ require 'base64'
|
|
6
6
|
|
7
7
|
require File.expand_path '../helpers', __FILE__
|
8
8
|
|
9
|
+
# Use pygments if it's installed
|
10
|
+
begin
|
11
|
+
require 'pygments'
|
12
|
+
Pygments.start
|
13
|
+
rescue Exception
|
14
|
+
end
|
15
|
+
|
9
16
|
module Gollum
|
10
17
|
|
11
18
|
class Markup
|
12
19
|
include Helpers
|
13
20
|
|
14
21
|
@formats = {}
|
15
|
-
|
22
|
+
|
16
23
|
class << self
|
17
|
-
|
24
|
+
|
18
25
|
# Only use the formats that are specified in config.rb
|
19
26
|
def formats
|
20
27
|
if defined? Gollum::Page::FORMAT_NAMES
|
@@ -23,7 +30,7 @@ module Gollum
|
|
23
30
|
@formats
|
24
31
|
end
|
25
32
|
end
|
26
|
-
|
33
|
+
|
27
34
|
# Register a file extension and associated markup type
|
28
35
|
#
|
29
36
|
# ext - The file extension
|
@@ -35,22 +42,21 @@ module Gollum
|
|
35
42
|
# If given a block, that block will be registered with GitHub::Markup to
|
36
43
|
# render any matching pages
|
37
44
|
def register(ext, name, options = {}, &block)
|
38
|
-
regexp
|
45
|
+
regexp = options[:regexp] || Regexp.new(ext.to_s)
|
39
46
|
@formats[ext] = { :name => name, :regexp => regexp }
|
40
|
-
GitHub::Markup.add_markup(regexp, &block) if block_given?
|
41
47
|
end
|
42
48
|
end
|
43
49
|
|
44
50
|
attr_accessor :toc
|
45
51
|
attr_accessor :metadata
|
46
|
-
attr_reader
|
47
|
-
attr_reader
|
48
|
-
attr_reader
|
49
|
-
attr_reader
|
50
|
-
attr_reader
|
51
|
-
attr_reader
|
52
|
-
attr_reader
|
53
|
-
attr_reader
|
52
|
+
attr_reader :encoding
|
53
|
+
attr_reader :sanitize
|
54
|
+
attr_reader :format
|
55
|
+
attr_reader :wiki
|
56
|
+
attr_reader :name
|
57
|
+
attr_reader :include_levels
|
58
|
+
attr_reader :to_xml_opts
|
59
|
+
attr_reader :dir
|
54
60
|
|
55
61
|
# Initialize a new Markup object.
|
56
62
|
#
|
@@ -58,18 +64,66 @@ module Gollum
|
|
58
64
|
#
|
59
65
|
# Returns a new Gollum::Markup object, ready for rendering.
|
60
66
|
def initialize(page)
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
67
|
+
if page
|
68
|
+
@wiki = page.wiki
|
69
|
+
@name = page.filename
|
70
|
+
@data = page.text_data
|
71
|
+
@version = page.version.id if page.version
|
72
|
+
@format = page.format
|
73
|
+
@sub_page = page.sub_page
|
74
|
+
@parent_page = page.parent_page
|
75
|
+
@dir = ::File.dirname(page.path)
|
76
|
+
end
|
77
|
+
@metadata = nil
|
70
78
|
@to_xml_opts = { :save_with => Nokogiri::XML::Node::SaveOptions::DEFAULT_XHTML ^ 1, :indent => 0, :encoding => 'UTF-8' }
|
71
79
|
end
|
72
80
|
|
81
|
+
# Render data using default chain in the target format.
|
82
|
+
#
|
83
|
+
# data - the data to render
|
84
|
+
# format - format to use as a symbol
|
85
|
+
# name - name using the extension of the format
|
86
|
+
#
|
87
|
+
# Returns the processed data
|
88
|
+
def render_default data, format=:markdown, name='render_default.md'
|
89
|
+
# set instance vars so we're able to render data without a wiki or page.
|
90
|
+
@format = format
|
91
|
+
@name = name
|
92
|
+
|
93
|
+
chain = [:Metadata, :PlainText, :TOC, :RemoteCode, :Code, :Sanitize, :WSD, :Tags, :Render]
|
94
|
+
|
95
|
+
filter_chain = chain.map do |r|
|
96
|
+
Gollum::Filter.const_get(r).new(self)
|
97
|
+
end
|
98
|
+
|
99
|
+
process_chain data, filter_chain
|
100
|
+
end
|
101
|
+
|
102
|
+
# Process the filter chain
|
103
|
+
#
|
104
|
+
# data - the data to send through the chain
|
105
|
+
# filter_chain - the chain to process
|
106
|
+
#
|
107
|
+
# Returns the formatted data
|
108
|
+
def process_chain data, filter_chain
|
109
|
+
# First we extract the data through the chain...
|
110
|
+
filter_chain.each do |filter|
|
111
|
+
data = filter.extract(data)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Then we process the data through the chain *backwards*
|
115
|
+
filter_chain.reverse.each do |filter|
|
116
|
+
data = filter.process(data)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Finally, a little bit of cleanup, just because
|
120
|
+
data.gsub!(/<p><\/p>/) do
|
121
|
+
''
|
122
|
+
end
|
123
|
+
|
124
|
+
data
|
125
|
+
end
|
126
|
+
|
73
127
|
# Render the content with Gollum wiki syntax on top of the file's own
|
74
128
|
# markup language.
|
75
129
|
#
|
@@ -80,13 +134,13 @@ module Gollum
|
|
80
134
|
# Returns the formatted String content.
|
81
135
|
def render(no_follow = false, encoding = nil, include_levels = 10)
|
82
136
|
@sanitize = no_follow ?
|
83
|
-
|
84
|
-
|
137
|
+
@wiki.history_sanitizer :
|
138
|
+
@wiki.sanitizer
|
85
139
|
|
86
|
-
@encoding
|
140
|
+
@encoding = encoding
|
87
141
|
@include_levels = include_levels
|
88
142
|
|
89
|
-
data
|
143
|
+
data = @data.dup
|
90
144
|
filter_chain = @wiki.filter_chain.map do |r|
|
91
145
|
Gollum::Filter.const_get(r).new(self)
|
92
146
|
end
|
@@ -98,22 +152,7 @@ module Gollum
|
|
98
152
|
yield Nokogiri::HTML::DocumentFragment.parse(data)
|
99
153
|
end
|
100
154
|
|
101
|
-
|
102
|
-
filter_chain.each do |filter|
|
103
|
-
data = filter.extract(data)
|
104
|
-
end
|
105
|
-
|
106
|
-
# Then we process the data through the chain *backwards*
|
107
|
-
filter_chain.reverse.each do |filter|
|
108
|
-
data = filter.process(data)
|
109
|
-
end
|
110
|
-
|
111
|
-
# Finally, a little bit of cleanup, just because
|
112
|
-
data.gsub!(/<p><\/p>/) do
|
113
|
-
''
|
114
|
-
end
|
115
|
-
|
116
|
-
data
|
155
|
+
process_chain data, filter_chain
|
117
156
|
end
|
118
157
|
|
119
158
|
# Find the given file in the repo.
|
data/lib/gollum-lib/markups.rb
CHANGED
data/lib/gollum-lib/page.rb
CHANGED
@@ -84,9 +84,9 @@ module Gollum
|
|
84
84
|
#
|
85
85
|
# Returns a newly initialized Gollum::Page.
|
86
86
|
def initialize(wiki)
|
87
|
-
@wiki
|
88
|
-
@blob
|
89
|
-
@doc
|
87
|
+
@wiki = wiki
|
88
|
+
@blob = @header = @footer = @sidebar = nil
|
89
|
+
@doc = nil
|
90
90
|
@parent_page = nil
|
91
91
|
end
|
92
92
|
|
@@ -139,10 +139,10 @@ module Gollum
|
|
139
139
|
# Returns the String url_path
|
140
140
|
def url_path
|
141
141
|
path = if self.path.include?('/')
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
142
|
+
self.path.sub(/\/[^\/]+$/, '/')
|
143
|
+
else
|
144
|
+
''
|
145
|
+
end
|
146
146
|
|
147
147
|
path << Page.cname(self.name, '-', '-')
|
148
148
|
path
|
@@ -180,7 +180,7 @@ module Gollum
|
|
180
180
|
#
|
181
181
|
# Returns the String url_path
|
182
182
|
def escaped_url_path
|
183
|
-
CGI.escape(self.url_path).gsub('%2F','/')
|
183
|
+
CGI.escape(self.url_path).gsub('%2F', '/')
|
184
184
|
end
|
185
185
|
|
186
186
|
# Public: The raw contents of the page.
|
@@ -284,9 +284,9 @@ module Gollum
|
|
284
284
|
# Public: The first 7 characters of the current version.
|
285
285
|
#
|
286
286
|
# Returns the first 7 characters of the current version.
|
287
|
-
|
288
|
-
|
289
|
-
|
287
|
+
def version_short
|
288
|
+
version.to_s[0, 7]
|
289
|
+
end
|
290
290
|
|
291
291
|
# Public: The header Page.
|
292
292
|
#
|
@@ -341,8 +341,8 @@ module Gollum
|
|
341
341
|
# Returns the String canonical name.
|
342
342
|
def self.cname(name, char_white_sub = '-', char_other_sub = '-')
|
343
343
|
name.respond_to?(:gsub) ?
|
344
|
-
|
345
|
-
|
344
|
+
name.gsub(%r{\s}, char_white_sub).gsub(%r{[<>+]}, char_other_sub) :
|
345
|
+
''
|
346
346
|
end
|
347
347
|
|
348
348
|
# Convert a format Symbol into an extension String.
|
@@ -380,7 +380,7 @@ module Gollum
|
|
380
380
|
map = @wiki.tree_map_for(version.to_s)
|
381
381
|
if page = find_page_in_tree(map, name, dir, exact)
|
382
382
|
page.version = version.is_a?(Grit::Commit) ?
|
383
|
-
|
383
|
+
version : @wiki.commit_for(version)
|
384
384
|
page.historical = page.version.to_s == version.to_s
|
385
385
|
page
|
386
386
|
end
|
@@ -400,7 +400,7 @@ module Gollum
|
|
400
400
|
|
401
401
|
checked_dir = BlobEntry.normalize_dir(checked_dir)
|
402
402
|
checked_dir = '' if exact && checked_dir.nil?
|
403
|
-
name
|
403
|
+
name = ::File.join(checked_dir, name) if checked_dir
|
404
404
|
|
405
405
|
map.each do |entry|
|
406
406
|
next if entry.name.to_s.empty?
|
@@ -30,7 +30,7 @@ module Gollum
|
|
30
30
|
#
|
31
31
|
# Returns Hash with :max_count and :skip keys.
|
32
32
|
def log_pagination_options(options = {})
|
33
|
-
skip
|
33
|
+
skip = page_to_skip(options.delete(:page))
|
34
34
|
options[:max_count] = [options.delete(:per_page).to_i, per_page].max
|
35
35
|
options[:skip] = skip if skip > 0
|
36
36
|
options
|
@@ -6,50 +6,50 @@ module Gollum
|
|
6
6
|
# See http://github.com/rgrove/sanitize/.
|
7
7
|
class Sanitization
|
8
8
|
# Default whitelisted elements.
|
9
|
-
ELEMENTS
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
9
|
+
ELEMENTS = [
|
10
|
+
'a', 'abbr', 'acronym', 'address', 'area', 'b', 'big',
|
11
|
+
'blockquote', 'br', 'button', 'caption', 'center', 'cite',
|
12
|
+
'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'dir',
|
13
|
+
'div', 'dl', 'dt', 'em', 'fieldset', 'font', 'form', 'h1',
|
14
|
+
'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'input',
|
15
|
+
'ins', 'kbd', 'label', 'legend', 'li', 'map', 'menu',
|
16
|
+
'ol', 'optgroup', 'option', 'p', 'pre', 'q', 's', 'samp',
|
17
|
+
'select', 'small', 'span', 'strike', 'strong', 'sub',
|
18
|
+
'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th',
|
19
|
+
'thead', 'tr', 'tt', 'u', 'ul', 'var'
|
20
20
|
].freeze
|
21
21
|
|
22
22
|
# Default whitelisted attributes.
|
23
23
|
ATTRIBUTES = {
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
24
|
+
'a' => ['href'],
|
25
|
+
'img' => ['src'],
|
26
|
+
:all => ['abbr', 'accept', 'accept-charset',
|
27
|
+
'accesskey', 'action', 'align', 'alt', 'axis',
|
28
|
+
'border', 'cellpadding', 'cellspacing', 'char',
|
29
|
+
'charoff', 'class', 'charset', 'checked', 'cite',
|
30
|
+
'clear', 'cols', 'colspan', 'color',
|
31
|
+
'compact', 'coords', 'datetime', 'dir',
|
32
|
+
'disabled', 'enctype', 'for', 'frame',
|
33
|
+
'headers', 'height', 'hreflang',
|
34
|
+
'hspace', 'id', 'ismap', 'label', 'lang',
|
35
|
+
'longdesc', 'maxlength', 'media', 'method',
|
36
|
+
'multiple', 'name', 'nohref', 'noshade',
|
37
|
+
'nowrap', 'prompt', 'readonly', 'rel', 'rev',
|
38
|
+
'rows', 'rowspan', 'rules', 'scope',
|
39
|
+
'selected', 'shape', 'size', 'span',
|
40
|
+
'start', 'summary', 'tabindex', 'target',
|
41
|
+
'title', 'type', 'usemap', 'valign', 'value',
|
42
|
+
'vspace', 'width']
|
43
43
|
}.freeze
|
44
44
|
|
45
45
|
# Default whitelisted protocols for URLs.
|
46
|
-
PROTOCOLS
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
PROTOCOLS = {
|
47
|
+
'a' => { 'href' => ['http', 'https', 'mailto', 'ftp', 'irc', 'apt', :relative] },
|
48
|
+
'img' => { 'src' => ['http', 'https', :relative] },
|
49
|
+
'form' => { 'action' => ['http', 'https', :relative] }
|
50
50
|
}.freeze
|
51
51
|
|
52
|
-
ADD_ATTRIBUTES
|
52
|
+
ADD_ATTRIBUTES = lambda do |env, node|
|
53
53
|
if add = env[:config][:add_attributes][node.name]
|
54
54
|
add.each do |key, value|
|
55
55
|
node[key] = value
|
@@ -60,34 +60,34 @@ module Gollum
|
|
60
60
|
# Default elements whose contents will be removed in addition
|
61
61
|
# to the elements themselve
|
62
62
|
REMOVE_CONTENTS = [
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
'script',
|
64
|
+
'style'
|
65
|
+
].freeze
|
66
66
|
|
67
67
|
# Default transformers to force @id attributes with 'wiki-' prefix
|
68
|
-
TRANSFORMERS
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
68
|
+
TRANSFORMERS = [
|
69
|
+
lambda do |env|
|
70
|
+
node = env[:node]
|
71
|
+
return if env[:is_whitelisted] || !node.element?
|
72
|
+
prefix = env[:config][:id_prefix]
|
73
|
+
found_attrs = %w(id name).select do |key|
|
74
|
+
if value = node[key]
|
75
|
+
node[key] = value.gsub(/\A(#{prefix})?/, prefix)
|
76
|
+
end
|
76
77
|
end
|
77
|
-
|
78
|
-
|
78
|
+
if found_attrs.size > 0
|
79
|
+
ADD_ATTRIBUTES.call(env, node)
|
80
|
+
{}
|
81
|
+
end
|
82
|
+
end,
|
83
|
+
lambda do |env|
|
84
|
+
node = env[:node]
|
85
|
+
return unless value = node['href']
|
86
|
+
prefix = env[:config][:id_prefix]
|
87
|
+
node['href'] = value.gsub(/\A\#(#{prefix})?/, '#'+prefix)
|
79
88
|
ADD_ATTRIBUTES.call(env, node)
|
80
89
|
{}
|
81
90
|
end
|
82
|
-
end,
|
83
|
-
lambda do |env|
|
84
|
-
node = env[:node]
|
85
|
-
return unless value = node['href']
|
86
|
-
prefix = env[:config][:id_prefix]
|
87
|
-
node['href'] = value.gsub(/\A\#(#{prefix})?/, '#'+prefix)
|
88
|
-
ADD_ATTRIBUTES.call(env, node)
|
89
|
-
{}
|
90
|
-
end
|
91
91
|
].freeze
|
92
92
|
|
93
93
|
# Gets an Array of whitelisted HTML elements. Default: ELEMENTS.
|
@@ -122,14 +122,14 @@ module Gollum
|
|
122
122
|
attr_writer :allow_comments
|
123
123
|
|
124
124
|
def initialize
|
125
|
-
@elements
|
126
|
-
@attributes
|
127
|
-
@protocols
|
128
|
-
@transformers
|
129
|
-
@add_attributes
|
130
|
-
@remove_contents
|
131
|
-
@allow_comments
|
132
|
-
@id_prefix
|
125
|
+
@elements = ELEMENTS.dup
|
126
|
+
@attributes = ATTRIBUTES.dup
|
127
|
+
@protocols = PROTOCOLS.dup
|
128
|
+
@transformers = TRANSFORMERS.dup
|
129
|
+
@add_attributes = {}
|
130
|
+
@remove_contents = REMOVE_CONTENTS.dup
|
131
|
+
@allow_comments = false
|
132
|
+
@id_prefix = ''
|
133
133
|
yield self if block_given?
|
134
134
|
end
|
135
135
|
|
@@ -146,7 +146,7 @@ module Gollum
|
|
146
146
|
# Returns a Sanitization instance.
|
147
147
|
def history_sanitization
|
148
148
|
self.class.new do |sanitize|
|
149
|
-
sanitize.add_attributes['a'] = {'rel' => 'nofollow'}
|
149
|
+
sanitize.add_attributes['a'] = { 'rel' => 'nofollow' }
|
150
150
|
end
|
151
151
|
end
|
152
152
|
|
@@ -154,14 +154,14 @@ module Gollum
|
|
154
154
|
#
|
155
155
|
# Returns a Hash.
|
156
156
|
def to_hash
|
157
|
-
{ :elements
|
158
|
-
:attributes
|
159
|
-
:protocols
|
160
|
-
:add_attributes
|
161
|
-
:remove_contents
|
162
|
-
:allow_comments
|
163
|
-
:transformers
|
164
|
-
:id_prefix
|
157
|
+
{ :elements => elements,
|
158
|
+
:attributes => attributes,
|
159
|
+
:protocols => protocols,
|
160
|
+
:add_attributes => add_attributes,
|
161
|
+
:remove_contents => remove_contents,
|
162
|
+
:allow_comments => allow_comments?,
|
163
|
+
:transformers => transformers,
|
164
|
+
:id_prefix => id_prefix
|
165
165
|
}
|
166
166
|
end
|
167
167
|
|