gollum-lib 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of gollum-lib might be problematic. Click here for more details.

@@ -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
- :path => self.unchomp(path),
14
- :host => 'raw.github.com',
15
- :scheme => 'https',
16
- :port => 443 })
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 = Net::HTTP.new uri.host, uri.port
30
+ http = Net::HTTP.new uri.host, uri.port
31
31
  http.use_ssl = true
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'
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 = resp.code.to_i
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 = URI.parse resp.header['location']
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
@@ -7,7 +7,7 @@ module Grit
7
7
  end
8
8
 
9
9
  def symlink_target(base_path = nil)
10
- target = self.data
10
+ target = self.data
11
11
  new_path = File.expand_path(File.join('..', target), base_path)
12
12
 
13
13
  if File.file? new_path
@@ -4,8 +4,8 @@ module Gollum
4
4
 
5
5
  def trim_leading_slash url
6
6
  return url if url.nil?
7
- url.gsub!('%2F','/')
8
- return '/' + url.gsub(/^\/+/,'') if url[0,1] == '/'
7
+ url.gsub!('%2F', '/')
8
+ return '/' + url.gsub(/^\/+/, '') if url[0, 1] == '/'
9
9
  url
10
10
  end
11
11
 
@@ -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 = options[:regexp] || Regexp.new(ext.to_s)
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 :encoding
47
- attr_reader :sanitize
48
- attr_reader :format
49
- attr_reader :wiki
50
- attr_reader :name
51
- attr_reader :include_levels
52
- attr_reader :to_xml_opts
53
- attr_reader :dir
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
- @wiki = page.wiki
62
- @name = page.filename
63
- @data = page.text_data
64
- @version = page.version.id if page.version
65
- @format = page.format
66
- @sub_page = page.sub_page
67
- @parent_page = page.parent_page
68
- @dir = ::File.dirname(page.path)
69
- @metadata = nil
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
- @wiki.history_sanitizer :
84
- @wiki.sanitizer
137
+ @wiki.history_sanitizer :
138
+ @wiki.sanitizer
85
139
 
86
- @encoding = encoding
140
+ @encoding = encoding
87
141
  @include_levels = include_levels
88
142
 
89
- data = @data.dup
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
- # First we extract the data through the chain...
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.
@@ -12,4 +12,4 @@ module Gollum
12
12
  register(:pod, "Pod")
13
13
  register(:txt, "Plain Text")
14
14
  end
15
- end
15
+ end
@@ -84,9 +84,9 @@ module Gollum
84
84
  #
85
85
  # Returns a newly initialized Gollum::Page.
86
86
  def initialize(wiki)
87
- @wiki = wiki
88
- @blob = @header = @footer = @sidebar = nil
89
- @doc = nil
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
- self.path.sub(/\/[^\/]+$/, '/')
143
- else
144
- ''
145
- end
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
- def version_short
288
- version.to_s[0,7]
289
- end
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
- name.gsub(%r{\s},char_white_sub).gsub(%r{[<>+]}, char_other_sub) :
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
- version : @wiki.commit_for(version)
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 = ::File.join(checked_dir, name) if checked_dir
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 = page_to_skip(options.delete(:page))
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
- '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'
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
- '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']
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
- 'a' => {'href' => ['http', 'https', 'mailto', 'ftp', 'irc', 'apt', :relative]},
48
- 'img' => {'src' => ['http', 'https', :relative]},
49
- 'form' => {'action' => ['http', 'https', :relative]}
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 = lambda do |env, node|
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
- 'script',
64
- 'style'
65
- ].freeze
63
+ 'script',
64
+ 'style'
65
+ ].freeze
66
66
 
67
67
  # Default transformers to force @id attributes with 'wiki-' prefix
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)
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
- end
78
- if found_attrs.size > 0
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 = 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 = ''
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 => 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
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