class Mato::AnchorBuilder
Constants
- CSS_CLASS_NAME
- DEFAULT_ANCHOR_ICON_ELEMENT
assumes use of font-awesome specify it as “<span aria-hidden="true" class="octicon octicon-link"></span>” if you use octicon
Attributes
anchor_icon_element[R]
context[R]
Public Class Methods
new(anchor_icon_element = DEFAULT_ANCHOR_ICON_ELEMENT)
click to toggle source
# File lib/mato/anchor_builder.rb, line 17 def initialize(anchor_icon_element = DEFAULT_ANCHOR_ICON_ELEMENT) @anchor_icon_element = anchor_icon_element @id_map = {} end
Public Instance Methods
make_anchor_element(hx)
click to toggle source
@param [Nokogiri::XML::Node] hx
# File lib/mato/anchor_builder.rb, line 23 def make_anchor_element(hx) id = make_anchor_id(hx) %{<a id="#{id}" href="##{id}" aria-hidden="true" class="#{CSS_CLASS_NAME}">#{anchor_icon_element}</a>} end
make_anchor_id(hx)
click to toggle source
# File lib/mato/anchor_builder.rb, line 28 def make_anchor_id(hx) prefix = make_anchor_id_prefix(hx.inner_text) "#{prefix}#{make_anchor_id_suffix(prefix)}" end
Private Instance Methods
make_anchor_id_prefix(text)
click to toggle source
# File lib/mato/anchor_builder.rb, line 46 def make_anchor_id_prefix(text) prefix = ERB::Util.url_encode(text.downcase.gsub(/[^\p{Word}\- ]/u, "").tr(" ", "-")) if prefix.empty? "user-content" # GitHub compatible else prefix end end
make_anchor_id_suffix(text)
click to toggle source
# File lib/mato/anchor_builder.rb, line 35 def make_anchor_id_suffix(text) @id_map[text] ||= -1 unique_id = @id_map[text] += 1 if unique_id > 0 "-#{unique_id}" else "" end end