create process_headers function to add anchors and create toc
This commit is contained in:
+42
-15
@@ -23,6 +23,7 @@ module Gollum
|
|||||||
@texmap = {}
|
@texmap = {}
|
||||||
@wsdmap = {}
|
@wsdmap = {}
|
||||||
@premap = {}
|
@premap = {}
|
||||||
|
@toc = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Render the content with Gollum wiki syntax on top of the file's own
|
# Render the content with Gollum wiki syntax on top of the file's own
|
||||||
@@ -53,27 +54,53 @@ module Gollum
|
|||||||
end
|
end
|
||||||
data = process_tags(data)
|
data = process_tags(data)
|
||||||
data = process_code(data, encoding)
|
data = process_code(data, encoding)
|
||||||
if sanitize || block_given? || @wiki.header_hashtags || @wiki.universal_toc
|
|
||||||
doc = Nokogiri::HTML::DocumentFragment.parse(data)
|
doc = Nokogiri::HTML::DocumentFragment.parse(data)
|
||||||
doc = sanitize.clean_node!(doc) if sanitize
|
doc = sanitize.clean_node!(doc) if sanitize
|
||||||
|
doc,@toc = process_headers(doc) if @wiki.header_hashtags || @wiki.universal_toc
|
||||||
doc.css('h1,h2,h3,h4,h5,h6').each do |h|
|
yield doc if block_given?
|
||||||
id = CGI::escape(h.content.gsub(' ','-'))
|
data = doc.to_html
|
||||||
anchor = Nokogiri::XML::Node.new('a', doc)
|
|
||||||
anchor['class'] = 'anchor'
|
|
||||||
anchor['id'] = id
|
|
||||||
anchor['href'] = '#' + id
|
|
||||||
h.child.before(anchor)
|
|
||||||
end if @wiki.header_hashtags || @wiki.universal_toc
|
|
||||||
yield doc if block_given?
|
|
||||||
data = doc.to_html
|
|
||||||
end
|
|
||||||
data = process_tex(data)
|
data = process_tex(data)
|
||||||
data = process_wsd(data)
|
data = process_wsd(data)
|
||||||
data.gsub!(/<p><\/p>/, '')
|
data.gsub!(/<p><\/p>/, '')
|
||||||
data
|
data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def process_headers(doc)
|
||||||
|
toc = nil
|
||||||
|
doc.css('h1,h2,h3,h4,h5,h6').each do |h|
|
||||||
|
id = CGI::escape(h.content.gsub(' ','-'))
|
||||||
|
level = h.name.gsub(/[hH]/,'').to_i
|
||||||
|
|
||||||
|
# Add anchors
|
||||||
|
anchor = Nokogiri::XML::Node.new('a', doc)
|
||||||
|
anchor['class'] = 'anchor'
|
||||||
|
anchor['id'] = id
|
||||||
|
anchor['href'] = '#' + id
|
||||||
|
h.child.before(anchor)
|
||||||
|
|
||||||
|
# Build TOC
|
||||||
|
toc ||= Nokogiri::XML::Node.new('ul', doc)
|
||||||
|
tail ||= toc
|
||||||
|
tail_level ||= 1
|
||||||
|
|
||||||
|
while tail_level < level do
|
||||||
|
node = Nokogiri::XML::Node.new('ul', doc)
|
||||||
|
tail = tail.add_child(node)
|
||||||
|
tail_level += 1
|
||||||
|
end
|
||||||
|
while tail_level > level
|
||||||
|
tail = tail.parent
|
||||||
|
tail_level -= 1
|
||||||
|
end
|
||||||
|
node = Nokogiri::XML::Node.new('li', doc)
|
||||||
|
node.add_child("<a href='##{id}'>#{h.content}</a>")
|
||||||
|
tail.add_child(node)
|
||||||
|
end
|
||||||
|
[doc, toc]
|
||||||
|
end
|
||||||
|
|
||||||
#########################################################################
|
#########################################################################
|
||||||
#
|
#
|
||||||
# TeX
|
# TeX
|
||||||
|
|||||||
Reference in New Issue
Block a user