Allow sub pages to use the [[_TOC_]] tag

This commit is contained in:
kristi
2012-05-13 22:19:55 -07:00
parent ae30b23d34
commit 4fa2cdf8d9
2 changed files with 29 additions and 9 deletions
+8 -3
View File
@@ -6,6 +6,7 @@ require 'base64'
module Gollum
class Markup
attr_accessor :toc
# Initialize a new Markup object.
#
# page - The Gollum::Page.
@@ -17,6 +18,8 @@ module Gollum
@data = page.text_data
@version = page.version.id if page.version
@format = page.format
@sub_page = page.sub_page
@parent_page = page.parent_page
@dir = ::File.dirname(page.path)
@tagmap = {}
@codemap = {}
@@ -57,7 +60,8 @@ module Gollum
doc = Nokogiri::HTML::DocumentFragment.parse(data)
doc = sanitize.clean_node!(doc) if sanitize
doc,@toc = process_headers(doc) if @wiki.header_hashtags || @wiki.universal_toc
doc,toc = process_headers(doc)
@toc = @sub_page ? @parent_page.toc_data : toc
yield doc if block_given?
data = doc.to_html
@@ -69,6 +73,7 @@ module Gollum
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
@@ -98,7 +103,7 @@ module Gollum
node.add_child("<a href='##{id}'>#{h.content}</a>")
tail.add_child(node)
end
toc ||= Nokogiri::XML::DocumentFragment.parse('<!--No TOC headers found-->')
toc = toc.to_xhtml if toc != nil
[doc, toc]
end
@@ -368,7 +373,7 @@ module Gollum
#
# Returns the marked up String data.
def process_toc_tags(data)
data.gsub!("[[_TOC_]]", @toc.nil? ? '' : @toc.to_html)
data.gsub!("[[_TOC_]]", @toc.nil? ? '' : @toc)
data
end
+21 -6
View File
@@ -20,6 +20,11 @@ module Gollum
# Returns nothing.
attr_writer :historical
# Parent page if this is a sub page
#
# Returns a Page
attr_accessor :parent_page
# Checks if a filename has a valid extension understood by GitHub::Markup.
#
# filename - String filename, like "Home.md".
@@ -101,6 +106,7 @@ module Gollum
@wiki = wiki
@blob = @header = @footer = @sidebar = nil
@doc = nil
@parent_page = nil
end
# Public: The on-disk filename of the page including extension.
@@ -134,6 +140,14 @@ module Gollum
Sanitize.clean(name).strip
end
# Public: Determines if this is a sub-page
# Sub-pages have filenames beginning with an underscore
#
# Returns true or false.
def sub_page
filename =~ /^_/
end
# Public: The path of the page within the repo.
#
# Returns the String path.
@@ -177,11 +191,9 @@ module Gollum
#
# Returns the String data.
def toc_data()
formatted_data if @doc == nil
toc = Toc.new(@doc)
if (toc_content = toc.generate)
toc.generate.to_xhtml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XHTML)
end
return @parent_page.toc_data if @parent_page and @sub_page
formatted_data if markup_class.toc == nil
markup_class.toc
end
# Public: The format of the page.
@@ -417,12 +429,15 @@ module Gollum
map = @wiki.tree_map_for(self.version.id)
while !dirs.empty?
if page = find_page_in_tree(map, name, dirs.join('/'))
page.parent_page = self
return page
end
dirs.pop
end
find_page_in_tree(map, name, '')
page = find_page_in_tree(map, name, '')
page.parent_page = self
page
end
def inspect