merge latest tweaks to master
This commit is contained in:
+3
-44
@@ -19,54 +19,13 @@ require 'gollum/page'
|
||||
require 'gollum/file'
|
||||
require 'gollum/markup'
|
||||
require 'gollum/albino'
|
||||
require 'gollum/sanitization'
|
||||
|
||||
module Gollum
|
||||
VERSION = '1.0.1'
|
||||
|
||||
SANITIZATION_OPTIONS = {
|
||||
:elements => [
|
||||
'a', 'abbr', 'acronym', 'address', 'area', 'b', 'big',
|
||||
'blockquote', 'br', 'button', 'caption', 'center', 'cite',
|
||||
'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'dir',
|
||||
'div', 'dl', 'dt', 'em', 'fieldset', 'font', 'form', 'h1',
|
||||
'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'input',
|
||||
'ins', 'kbd', 'label', 'legend', 'li', 'map', 'menu',
|
||||
'ol', 'optgroup', 'option', 'p', 'pre', 'q', 's', 'samp',
|
||||
'select', 'small', 'span', 'strike', 'strong', 'sub',
|
||||
'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th',
|
||||
'thead', 'tr', 'tt', 'u', 'ul', 'var'
|
||||
],
|
||||
:attributes => {
|
||||
:all => ['abbr', 'accept', 'accept-charset',
|
||||
'accesskey', 'action', 'align', 'alt', 'axis',
|
||||
'border', 'cellpadding', 'cellspacing', 'char',
|
||||
'charoff', 'charset', 'checked', 'cite',
|
||||
'class', 'clear', 'cols', 'colspan', 'color',
|
||||
'compact', 'coords', 'datetime', 'dir',
|
||||
'disabled', 'enctype', 'for', 'frame',
|
||||
'headers', 'height', 'href', 'hreflang',
|
||||
'hspace', 'id', 'ismap', 'label', 'lang',
|
||||
'longdesc', 'maxlength', 'media', 'method',
|
||||
'multiple', 'name', 'nohref', 'noshade',
|
||||
'nowrap', 'prompt', 'readonly', 'rel', 'rev',
|
||||
'rows', 'rowspan', 'rules', 'scope',
|
||||
'selected', 'shape', 'size', 'span', 'src',
|
||||
'start', 'summary', 'tabindex', 'target',
|
||||
'title', 'type', 'usemap', 'valign', 'value',
|
||||
'vspace', 'width']
|
||||
},
|
||||
:protocols => {
|
||||
'a' => {'href' => ['http', 'https', 'mailto', :relative]},
|
||||
'img' => {'href' => ['http', 'https', :relative]}
|
||||
}
|
||||
}
|
||||
HISTORY_SANITIZATION_OPTIONS = SANITIZATION_OPTIONS.merge(
|
||||
:add_attributes => {
|
||||
'a' => {'rel' => 'nofollow'}
|
||||
}
|
||||
)
|
||||
VERSION = '1.1.0'
|
||||
|
||||
class Error < StandardError; end
|
||||
|
||||
class DuplicatePageError < Error
|
||||
attr_accessor :dir
|
||||
attr_accessor :existing_path
|
||||
|
||||
@@ -11,7 +11,7 @@ module Gollum
|
||||
def initialize(page)
|
||||
@wiki = page.wiki
|
||||
@name = page.filename
|
||||
@data = page.raw_data
|
||||
@data = page.text_data
|
||||
@version = page.version.id
|
||||
@dir = ::File.dirname(page.path)
|
||||
@tagmap = {}
|
||||
@@ -27,9 +27,10 @@ module Gollum
|
||||
#
|
||||
# Returns the formatted String content.
|
||||
def render(no_follow = false)
|
||||
sanitize_options = no_follow ?
|
||||
HISTORY_SANITIZATION_OPTIONS :
|
||||
SANITIZATION_OPTIONS
|
||||
sanitize_options = no_follow ?
|
||||
@wiki.history_sanitization :
|
||||
@wiki.sanitization
|
||||
|
||||
data = extract_tex(@data)
|
||||
data = extract_code(data)
|
||||
data = extract_tags(data)
|
||||
@@ -43,7 +44,7 @@ module Gollum
|
||||
end
|
||||
data = process_tags(data)
|
||||
data = process_code(data)
|
||||
data = Sanitize.clean(data, sanitize_options)
|
||||
data = Sanitize.clean(data, sanitize_options.to_hash) if sanitize_options
|
||||
data = process_tex(data)
|
||||
data.gsub!(/<p><\/p>/, '')
|
||||
data
|
||||
@@ -284,7 +285,7 @@ module Gollum
|
||||
def process_page_link_tag(tag, no_follow = false)
|
||||
parts = tag.split('|')
|
||||
name = parts[0].strip
|
||||
cname = Page.cname((parts[1] || parts[0]).strip)
|
||||
cname = @wiki.page_class.cname((parts[1] || parts[0]).strip)
|
||||
tag = if name =~ %r{^https?://} && parts[1].nil?
|
||||
%{<a href="#{name}">#{name}</a>}
|
||||
else
|
||||
@@ -292,7 +293,7 @@ module Gollum
|
||||
link_name = cname
|
||||
page, extra = find_page_from_name(cname)
|
||||
if page
|
||||
link_name = Page.cname(page.name)
|
||||
link_name = @wiki.page_class.cname(page.name)
|
||||
presence = "present"
|
||||
end
|
||||
link = ::File.join(@wiki.base_path, CGI.escape(link_name))
|
||||
|
||||
+14
-1
@@ -116,6 +116,19 @@ module Gollum
|
||||
@blob && @blob.data
|
||||
end
|
||||
|
||||
# Public: A text data encoded in specified encoding.
|
||||
#
|
||||
# encoding - An Encoding or nil
|
||||
#
|
||||
# Returns a character encoding aware String.
|
||||
def text_data(encoding=nil)
|
||||
if raw_data.respond_to?(:encoding)
|
||||
raw_data.force_encoding(encoding || Encoding::UTF_8)
|
||||
else
|
||||
raw_data
|
||||
end
|
||||
end
|
||||
|
||||
# Public: The formatted contents of the page.
|
||||
#
|
||||
# Returns the String data.
|
||||
@@ -344,4 +357,4 @@ module Gollum
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
module Gollum
|
||||
|
||||
# Encapsulate sanitization options.
|
||||
#
|
||||
# This class does not yet support all options of Sanitize library.
|
||||
# See http://github.com/rgrove/sanitize/.
|
||||
class Sanitization
|
||||
# Default whitelisted elements.
|
||||
ELEMENTS = [
|
||||
'a', 'abbr', 'acronym', 'address', 'area', 'b', 'big',
|
||||
'blockquote', 'br', 'button', 'caption', 'center', 'cite',
|
||||
'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'dir',
|
||||
'div', 'dl', 'dt', 'em', 'fieldset', 'font', 'form', 'h1',
|
||||
'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'input',
|
||||
'ins', 'kbd', 'label', 'legend', 'li', 'map', 'menu',
|
||||
'ol', 'optgroup', 'option', 'p', 'pre', 'q', 's', 'samp',
|
||||
'select', 'small', 'span', 'strike', 'strong', 'sub',
|
||||
'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th',
|
||||
'thead', 'tr', 'tt', 'u', 'ul', 'var'
|
||||
].freeze
|
||||
|
||||
# Default whitelisted attributes.
|
||||
ATTRIBUTES = {
|
||||
'a' => ['href'],
|
||||
'img' => ['src'],
|
||||
:all => ['abbr', 'accept', 'accept-charset',
|
||||
'accesskey', 'action', 'align', 'alt', 'axis',
|
||||
'border', 'cellpadding', 'cellspacing', 'char',
|
||||
'charoff', 'class', 'charset', 'checked', 'cite',
|
||||
'clear', 'cols', 'colspan', 'color',
|
||||
'compact', 'coords', 'datetime', 'dir',
|
||||
'disabled', 'enctype', 'for', 'frame',
|
||||
'headers', 'height', 'hreflang',
|
||||
'hspace', 'ismap', 'label', 'lang',
|
||||
'longdesc', 'maxlength', 'media', 'method',
|
||||
'multiple', 'name', 'nohref', 'noshade',
|
||||
'nowrap', 'prompt', 'readonly', 'rel', 'rev',
|
||||
'rows', 'rowspan', 'rules', 'scope',
|
||||
'selected', 'shape', 'size', 'span',
|
||||
'start', 'summary', 'tabindex', 'target',
|
||||
'title', 'type', 'usemap', 'valign', 'value',
|
||||
'vspace', 'width']
|
||||
}.freeze
|
||||
|
||||
# Default whitelisted protocols for URLs.
|
||||
PROTOCOLS = {
|
||||
'a' => {'href' => ['http', 'https', 'mailto', :relative]},
|
||||
'img' => {'src' => ['http', 'https', :relative]}
|
||||
}.freeze
|
||||
|
||||
# Gets an Array of whitelisted HTML elements. Default: ELEMENTS.
|
||||
attr_reader :elements
|
||||
|
||||
# Gets a Hash describing which attributes are allowed in which HTML
|
||||
# elements. Default: ATTRIBUTES.
|
||||
attr_reader :attributes
|
||||
|
||||
# Gets a Hash describing which URI protocols are allowed in HTML
|
||||
# attributes. Default: PROTOCOLS
|
||||
attr_reader :protocols
|
||||
|
||||
# Gets a Hash describing HTML attributes that Sanitize should add.
|
||||
# Default: {}
|
||||
attr_reader :add_attributes
|
||||
|
||||
# Sets a boolean determining whether Sanitize allows HTML comments in the
|
||||
# output. Default: false.
|
||||
attr_writer :allow_comments
|
||||
|
||||
def initialize
|
||||
@elements = ELEMENTS
|
||||
@attributes = ATTRIBUTES
|
||||
@protocols = PROTOCOLS
|
||||
@add_attributes = {}
|
||||
@allow_comments = false
|
||||
yield self if block_given?
|
||||
end
|
||||
|
||||
# Determines if Sanitize should allow HTML comments.
|
||||
#
|
||||
# Returns True if comments are allowed, or False.
|
||||
def allow_comments?
|
||||
!!@allow_comments
|
||||
end
|
||||
|
||||
# Modifies the current Sanitization instance to sanitize older revisions
|
||||
# of pages.
|
||||
#
|
||||
# Returns a Sanitization instance.
|
||||
def history_sanitization
|
||||
self.class.new do |sanitize|
|
||||
sanitize.add_attributes['a'] = {'rel' => 'nofollow'}
|
||||
end
|
||||
end
|
||||
|
||||
# Builds a Hash of options suitable for Sanitize.clean.
|
||||
#
|
||||
# Returns a Hash.
|
||||
def to_hash
|
||||
{ :elements => elements,
|
||||
:attributes => attributes,
|
||||
:protocols => protocols,
|
||||
:add_attributes => add_attributes,
|
||||
:allow_comments => allow_comments?
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -18,6 +18,14 @@ module Gollum
|
||||
# Sets the default email for commits.
|
||||
attr_accessor :default_committer_email
|
||||
|
||||
# Sets sanitization options. Set to false to deactivate
|
||||
# sanitization altogether.
|
||||
attr_writer :sanitization
|
||||
|
||||
# Sets sanitization options. Set to false to deactivate
|
||||
# sanitization altogether.
|
||||
attr_writer :history_sanitization
|
||||
|
||||
# Gets the page class used by all instances of this Wiki.
|
||||
# Default: Gollum::Page.
|
||||
def page_class
|
||||
@@ -50,6 +58,26 @@ module Gollum
|
||||
::Gollum::Markup
|
||||
end
|
||||
end
|
||||
|
||||
# Gets the default sanitization options for current pages used by
|
||||
# instances of this Wiki.
|
||||
def sanitization
|
||||
if @sanitization.nil?
|
||||
@sanitization = Sanitization.new
|
||||
end
|
||||
@sanitization
|
||||
end
|
||||
|
||||
# Gets the default sanitization options for older page revisions used by
|
||||
# instances of this Wiki.
|
||||
def history_sanitization
|
||||
if @history_sanitization.nil?
|
||||
@history_sanitization = sanitization ?
|
||||
sanitization.history_sanitization :
|
||||
false
|
||||
end
|
||||
@history_sanitization
|
||||
end
|
||||
end
|
||||
|
||||
self.default_committer_name = 'Anonymous'
|
||||
@@ -60,6 +88,12 @@ module Gollum
|
||||
# to "/".
|
||||
attr_reader :base_path
|
||||
|
||||
# Gets the sanitization options for current pages used by this Wiki.
|
||||
attr_reader :sanitization
|
||||
|
||||
# Gets the sanitization options for older page revisions used by this Wiki.
|
||||
attr_reader :history_sanitization
|
||||
|
||||
# Public: Initialize a new Gollum Repo.
|
||||
#
|
||||
# repo - The String path to the Git repository that holds the Gollum
|
||||
@@ -70,6 +104,7 @@ module Gollum
|
||||
# :page_class - The page Class. Default: Gollum::Page
|
||||
# :file_class - The file Class. Default: Gollum::File
|
||||
# :markup_class - The markup Class. Default: Gollum::Markup
|
||||
# :sanitization - An instance of Sanitization.
|
||||
#
|
||||
# Returns a fresh Gollum::Repo.
|
||||
def initialize(path, options = {})
|
||||
@@ -84,6 +119,9 @@ module Gollum
|
||||
@file_class = options[:file_class] || self.class.file_class
|
||||
@markup_class = options[:markup_class] || self.class.markup_class
|
||||
@repo = @access.repo
|
||||
@sanitization = options[:sanitization] || self.class.sanitization
|
||||
@history_sanitization = options[:history_sanitization] ||
|
||||
self.class.history_sanitization
|
||||
end
|
||||
|
||||
# Public: check whether the wiki's git repo exists on the filesystem.
|
||||
|
||||
Reference in New Issue
Block a user