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 module Gollum
class Markup class Markup
attr_accessor :toc
# Initialize a new Markup object. # Initialize a new Markup object.
# #
# page - The Gollum::Page. # page - The Gollum::Page.
@@ -17,6 +18,8 @@ module Gollum
@data = page.text_data @data = page.text_data
@version = page.version.id if page.version @version = page.version.id if page.version
@format = page.format @format = page.format
@sub_page = page.sub_page
@parent_page = page.parent_page
@dir = ::File.dirname(page.path) @dir = ::File.dirname(page.path)
@tagmap = {} @tagmap = {}
@codemap = {} @codemap = {}
@@ -57,7 +60,8 @@ module Gollum
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,toc = process_headers(doc)
@toc = @sub_page ? @parent_page.toc_data : toc
yield doc if block_given? yield doc if block_given?
data = doc.to_html data = doc.to_html
@@ -69,6 +73,7 @@ module Gollum
end end
def process_headers(doc) def process_headers(doc)
toc = nil
doc.css('h1,h2,h3,h4,h5,h6').each do |h| doc.css('h1,h2,h3,h4,h5,h6').each do |h|
id = CGI::escape(h.content.gsub(' ','-')) id = CGI::escape(h.content.gsub(' ','-'))
level = h.name.gsub(/[hH]/,'').to_i level = h.name.gsub(/[hH]/,'').to_i
@@ -98,7 +103,7 @@ module Gollum
node.add_child("<a href='##{id}'>#{h.content}</a>") node.add_child("<a href='##{id}'>#{h.content}</a>")
tail.add_child(node) tail.add_child(node)
end end
toc ||= Nokogiri::XML::DocumentFragment.parse('<!--No TOC headers found-->') toc = toc.to_xhtml if toc != nil
[doc, toc] [doc, toc]
end end
@@ -368,7 +373,7 @@ module Gollum
# #
# Returns the marked up String data. # Returns the marked up String data.
def process_toc_tags(data) def process_toc_tags(data)
data.gsub!("[[_TOC_]]", @toc.nil? ? '' : @toc.to_html) data.gsub!("[[_TOC_]]", @toc.nil? ? '' : @toc)
data data
end end
+21 -6
View File
@@ -20,6 +20,11 @@ module Gollum
# Returns nothing. # Returns nothing.
attr_writer :historical 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. # Checks if a filename has a valid extension understood by GitHub::Markup.
# #
# filename - String filename, like "Home.md". # filename - String filename, like "Home.md".
@@ -101,6 +106,7 @@ module Gollum
@wiki = wiki @wiki = wiki
@blob = @header = @footer = @sidebar = nil @blob = @header = @footer = @sidebar = nil
@doc = nil @doc = nil
@parent_page = nil
end end
# Public: The on-disk filename of the page including extension. # Public: The on-disk filename of the page including extension.
@@ -134,6 +140,14 @@ module Gollum
Sanitize.clean(name).strip Sanitize.clean(name).strip
end 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. # Public: The path of the page within the repo.
# #
# Returns the String path. # Returns the String path.
@@ -177,11 +191,9 @@ module Gollum
# #
# Returns the String data. # Returns the String data.
def toc_data() def toc_data()
formatted_data if @doc == nil return @parent_page.toc_data if @parent_page and @sub_page
toc = Toc.new(@doc) formatted_data if markup_class.toc == nil
if (toc_content = toc.generate) markup_class.toc
toc.generate.to_xhtml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XHTML)
end
end end
# Public: The format of the page. # Public: The format of the page.
@@ -417,12 +429,15 @@ module Gollum
map = @wiki.tree_map_for(self.version.id) map = @wiki.tree_map_for(self.version.id)
while !dirs.empty? while !dirs.empty?
if page = find_page_in_tree(map, name, dirs.join('/')) if page = find_page_in_tree(map, name, dirs.join('/'))
page.parent_page = self
return page return page
end end
dirs.pop dirs.pop
end end
find_page_in_tree(map, name, '') page = find_page_in_tree(map, name, '')
page.parent_page = self
page
end end
def inspect def inspect