Permalink
Browse files

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

  • Loading branch information...
1 parent ae30b23 commit 4fa2cdf8d9d0e7c6e542e8390c4140fd3bee3647 @kristi kristi committed May 14, 2012
Showing with 29 additions and 9 deletions.
  1. +8 −3 lib/gollum/markup.rb
  2. +21 −6 lib/gollum/page.rb
View
@@ -6,6 +6,7 @@
module Gollum
class Markup
+ attr_accessor :toc
# Initialize a new Markup object.
#
# page - The Gollum::Page.
@@ -17,6 +18,8 @@ def initialize(page)
@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 @@ def render(no_follow = false, encoding = nil)
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 @@ def render(no_follow = false, encoding = nil)
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 @@ def process_headers(doc)
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 @@ def process_page_link_tag(tag)
#
# 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
View
@@ -20,6 +20,11 @@ class Page
# 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 @@ def initialize(wiki)
@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 @@ def title
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 @@ def formatted_data(encoding = nil, &block)
#
# 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 @@ def find_sub_page(name)
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

0 comments on commit 4fa2cdf

Please sign in to comment.