Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow prefixed @id attributes to allow internal linking #146

Merged
merged 1 commit into from May 6, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 31 additions & 1 deletion lib/gollum/sanitization.rb
Expand Up @@ -47,6 +47,25 @@ class Sanitization
'img' => {'src' => ['http', 'https', :relative]}
}.freeze

# Default transformers to force @id attributes with 'wiki-' prefix

TRANSFORMERS = [
lambda do |env|
node = env[:node]
return if env[:is_whitelisted] || !node.element? || !node['id']
prefix = env[:config][:id_prefix]
node['id'] = node['id'].gsub(/\A(#{prefix})?/, prefix)

{:node_whitelist => [node]}
end,
lambda do |env|
node = env[:node]
return unless node['href']
prefix = env[:config][:id_prefix]
node['href'] = node['href'].gsub(/\A\#(#{prefix})?/, '#'+prefix)
end
].freeze

# Gets an Array of whitelisted HTML elements. Default: ELEMENTS.
attr_reader :elements

Expand All @@ -58,6 +77,13 @@ class Sanitization
# attributes. Default: PROTOCOLS
attr_reader :protocols

# Gets a Hash describing which URI protocols are allowed in HTML
# attributes. Default: TRANSFORMERS
attr_reader :transformers

# Gets a String prefix which is added to ID attributes. Default: 'wiki-'
attr_reader :id_prefix

# Gets a Hash describing HTML attributes that Sanitize should add.
# Default: {}
attr_reader :add_attributes
Expand All @@ -70,8 +96,10 @@ def initialize
@elements = ELEMENTS
@attributes = ATTRIBUTES
@protocols = PROTOCOLS
@transformers = TRANSFORMERS
@add_attributes = {}
@allow_comments = false
@id_prefix = 'wiki-'
yield self if block_given?
end

Expand Down Expand Up @@ -100,7 +128,9 @@ def to_hash
:attributes => attributes,
:protocols => protocols,
:add_attributes => add_attributes,
:allow_comments => allow_comments?
:allow_comments => allow_comments?,
:transformers => transformers,
:id_prefix => id_prefix
}
end

Expand Down
14 changes: 14 additions & 0 deletions test/test_markup.rb
Expand Up @@ -467,6 +467,20 @@
compare(content, output, 'org')
end

test "id with prefix ok" do
content = "h2(example#wiki-foo). xxxx"
output = %(<h2 class="example" id="wiki-foo">xxxx</h2>)
compare(content, output, :textile)
end

test "id prefix added" do
content = "h2(#foo). xxxx[1]\n\nfn1.footnote"
output = "<h2 id=\"wiki-foo\">xxxx" +
"<sup class=\"footnote\"><a href=\"#wiki-fn1\">1</a></sup></h2>" +
"\n<p class=\"footnote\" id=\"wiki-fn1\"><sup>1</sup> footnote</p>"
compare(content, output, :textile)
end

#########################################################################
#
# TeX
Expand Down