mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-23 00:45:16 +00:00
25 lines
867 B
Ruby
25 lines
867 B
Ruby
|
# Unique header generation
|
||
|
require 'middleman-core/renderers/redcarpet'
|
||
|
require 'digest'
|
||
|
class UniqueHeadCounter < Middleman::Renderers::MiddlemanRedcarpetHTML
|
||
|
def initialize
|
||
|
super
|
||
|
@head_count = {}
|
||
|
end
|
||
|
def header(text, header_level)
|
||
|
friendly_text = text.gsub(/<[^>]*>/,"").parameterize
|
||
|
if friendly_text.strip.length == 0
|
||
|
# Looks like parameterize removed the whole thing! It removes many unicode
|
||
|
# characters like Chinese and Russian. To get a unique URL, let's just
|
||
|
# URI escape the whole header
|
||
|
friendly_text = Digest::SHA1.hexdigest(text)[0,10]
|
||
|
end
|
||
|
@head_count[friendly_text] ||= 0
|
||
|
@head_count[friendly_text] += 1
|
||
|
if @head_count[friendly_text] > 1
|
||
|
friendly_text += "-#{@head_count[friendly_text]}"
|
||
|
end
|
||
|
return "<h#{header_level} id='#{friendly_text}'>#{text}</h#{header_level}>"
|
||
|
end
|
||
|
end
|