2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-04-22 20:40:25 +00:00
|
|
|
# HTML filter that adds an anchor child element to all Headers in a
|
|
|
|
# document, so that they can be linked to.
|
|
|
|
#
|
|
|
|
# Generates the Table of Contents with links to each header. See Results.
|
|
|
|
#
|
|
|
|
# Based on HTML::Pipeline::TableOfContentsFilter.
|
|
|
|
#
|
|
|
|
# Context options:
|
|
|
|
# :no_header_anchors - Skips all processing done by this filter.
|
|
|
|
#
|
|
|
|
# Results:
|
|
|
|
# :toc - String containing Table of Contents data as a `ul` element with
|
|
|
|
# `li` child elements.
|
|
|
|
class TableOfContentsFilter < HTML::Pipeline::Filter
|
|
|
|
PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u
|
|
|
|
|
|
|
|
def call
|
|
|
|
return doc if context[:no_header_anchors]
|
|
|
|
|
|
|
|
result[:toc] = ""
|
|
|
|
|
|
|
|
headers = Hash.new(0)
|
2017-08-29 18:08:59 +00:00
|
|
|
header_root = current_header = HeaderNode.new
|
2017-08-18 07:09:10 +00:00
|
|
|
|
2015-04-22 20:40:25 +00:00
|
|
|
doc.css('h1, h2, h3, h4, h5, h6').each do |node|
|
|
|
|
if header_content = node.children.first
|
2017-08-29 18:08:59 +00:00
|
|
|
id = node
|
|
|
|
.text
|
|
|
|
.downcase
|
|
|
|
.gsub(PUNCTUATION_REGEXP, '') # remove punctuation
|
|
|
|
.tr(' ', '-') # replace spaces with dash
|
|
|
|
.squeeze('-') # replace multiple dashes with one
|
2017-12-08 09:50:22 +00:00
|
|
|
.gsub(/\A(\d+)\z/, 'anchor-\1') # digits-only hrefs conflict with issue refs
|
2017-08-29 18:08:59 +00:00
|
|
|
|
|
|
|
uniq = headers[id] > 0 ? "-#{headers[id]}" : ''
|
|
|
|
headers[id] += 1
|
2015-04-22 20:40:25 +00:00
|
|
|
href = "#{id}#{uniq}"
|
2017-08-18 07:09:10 +00:00
|
|
|
|
2017-08-29 18:08:59 +00:00
|
|
|
current_header = HeaderNode.new(node: node, href: href, previous_header: current_header)
|
|
|
|
|
|
|
|
header_content.add_previous_sibling(anchor_tag(href))
|
2015-04-22 20:40:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-01 19:04:32 +00:00
|
|
|
push_toc(header_root.children, root: true)
|
2015-04-22 20:40:25 +00:00
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-29 18:08:59 +00:00
|
|
|
def anchor_tag(href)
|
|
|
|
%Q{<a id="user-content-#{href}" class="anchor" href="##{href}" aria-hidden="true"></a>}
|
2015-04-22 20:40:25 +00:00
|
|
|
end
|
|
|
|
|
2017-09-01 19:04:32 +00:00
|
|
|
def push_toc(children, root: false)
|
|
|
|
return if children.empty?
|
2017-08-29 18:08:59 +00:00
|
|
|
|
2017-09-01 19:04:32 +00:00
|
|
|
klass = ' class="section-nav"' if root
|
2017-08-29 18:08:59 +00:00
|
|
|
|
2017-09-01 19:04:32 +00:00
|
|
|
result[:toc] << "<ul#{klass}>"
|
|
|
|
children.each { |child| push_anchor(child) }
|
|
|
|
result[:toc] << '</ul>'
|
|
|
|
end
|
2017-08-29 18:08:59 +00:00
|
|
|
|
2017-09-01 19:04:32 +00:00
|
|
|
def push_anchor(header_node)
|
|
|
|
result[:toc] << %Q{<li><a href="##{header_node.href}">#{header_node.text}</a>}
|
|
|
|
push_toc(header_node.children)
|
2017-08-29 18:08:59 +00:00
|
|
|
result[:toc] << '</li>'
|
|
|
|
end
|
|
|
|
|
|
|
|
class HeaderNode
|
|
|
|
attr_reader :node, :href, :parent, :children
|
|
|
|
|
|
|
|
def initialize(node: nil, href: nil, previous_header: nil)
|
|
|
|
@node = node
|
|
|
|
@href = href
|
|
|
|
@children = []
|
|
|
|
|
2017-09-01 18:58:40 +00:00
|
|
|
@parent = find_parent(previous_header)
|
|
|
|
@parent.children.push(self) if @parent
|
2017-08-29 18:08:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def level
|
|
|
|
return 0 unless node
|
|
|
|
|
|
|
|
@level ||= node.name[1].to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def text
|
|
|
|
return '' unless node
|
|
|
|
|
2018-06-15 05:40:14 +00:00
|
|
|
@text ||= EscapeUtils.escape_html(node.text)
|
2017-08-29 18:08:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_parent(previous_header)
|
|
|
|
return unless previous_header
|
|
|
|
|
|
|
|
if level == previous_header.level
|
2017-09-01 18:58:40 +00:00
|
|
|
parent = previous_header.parent
|
2017-08-29 18:08:59 +00:00
|
|
|
elsif level > previous_header.level
|
2017-09-01 18:58:40 +00:00
|
|
|
parent = previous_header
|
2017-08-29 18:08:59 +00:00
|
|
|
else
|
2017-09-01 18:58:40 +00:00
|
|
|
parent = previous_header
|
|
|
|
parent = parent.parent while parent.level >= level
|
2017-08-29 18:08:59 +00:00
|
|
|
end
|
|
|
|
|
2017-09-01 18:58:40 +00:00
|
|
|
parent
|
2017-08-29 18:08:59 +00:00
|
|
|
end
|
2015-04-22 20:40:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|