Skip to content

Commit

Permalink
Properly generate relevant parts of TOC for each page.
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelius committed Feb 8, 2012
1 parent d7d5cfd commit f08c164
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
2 changes: 0 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# To do

* Only show relevant part of toc for each page
* Fix display of index pages for directories
* Add code highlighting
* Add "edit" link going to the github page where the content is hosted.
* Show last modified date
Expand Down
24 changes: 19 additions & 5 deletions lib/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def process_directory dir, parent_page
page = Page.new
parent_page.add_child page
end


page.file_basename = file_basename
page.path = input_path + "/" + entry
page.content = File.read( input_path + "/" + entry )
page.file_format = file_format
Expand Down Expand Up @@ -163,11 +164,19 @@ def render_toc_section parent_page
on "<ul>"
parent_page.children.each do |page|
o "<li>"
if page.title && !page.title.empty?
o "<a href='#{relative_site_root}#{page.target}'>#{page.title}</a>"
title = page.title
if page.has_children?
title += " >"
end
if page == @page
o "<span class=\"current-page\">#{title}</span>"
else
if page.title && !page.title.empty?
o "<a href='#{relative_site_root}#{page.target}'>#{title}</a>"
end
end
on "</li>"
if page.has_children?
if @page == page || ( page.has_children? && @page.has_parent( page ) )
render_toc_section page
end
end
Expand All @@ -177,7 +186,12 @@ def render_toc_section parent_page
def relative_site_root
out = ""

@page.level.times do
effective_level = @page.level
if @page.file_basename == "index"
effective_level += 1
end

effective_level.times do
out += "../"
end

Expand Down
15 changes: 14 additions & 1 deletion lib/page.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Page

attr_accessor :children, :directory_path, :path, :target, :level,
:file_format, :output_file
:file_format, :output_file, :parent, :file_basename
attr_reader :content, :title

def initialize
Expand All @@ -11,13 +11,26 @@ def initialize

def add_child child
@children.push child
child.parent = self
child.level = self.level + 1
end

def has_children?
!@children.empty?
end

def has_parent p
if self.parent == p
return true
else
if self.parent.nil?
return false
else
return self.parent.has_parent p
end
end
end

def content= content
@content = preprocess content
end
Expand Down
27 changes: 27 additions & 0 deletions test/page_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,31 @@ def test_preprocess
assert_equal "My Title", p.title
end

def test_has_parent
parent = Page.new
child1 = Page.new
parent.add_child child1
child2 = Page.new
parent.add_child child2
grandchild11 = Page.new
child1.add_child grandchild11
grandchild12 = Page.new
child1.add_child grandchild12
grandchild21 = Page.new
child2.add_child grandchild21

assert_equal true, child1.has_parent( parent )
assert_equal true, child2.has_parent( parent )
assert_equal true, grandchild11.has_parent( child1 )
assert_equal false, grandchild11.has_parent( child2 )
assert_equal true, grandchild11.has_parent( parent )
assert_equal true, grandchild12.has_parent( child1 )
assert_equal true, grandchild12.has_parent( parent )
assert_equal false, grandchild12.has_parent( child2 )
assert_equal true, grandchild12.has_parent( child1 )
assert_equal true, grandchild21.has_parent( child2 )
assert_equal false, grandchild21.has_parent( child1 )
assert_equal true, grandchild21.has_parent( parent )
end

end
4 changes: 4 additions & 0 deletions view/public/helphelp.css
Original file line number Diff line number Diff line change
Expand Up @@ -8221,3 +8221,7 @@ div.navigation {
#aside-toc h1 {
display: none;
}

span.current-page {
font-weight: bold;
}
2 changes: 1 addition & 1 deletion view/template.haml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
%a{:href => "http://susestudio.com/logout"} Sign out

#content
%aside#aside-toc.aside-box
%aside#aside-toc
=render_toc

.content
Expand Down

0 comments on commit f08c164

Please sign in to comment.