Skip to content

Commit

Permalink
Implemented topic-based TOC.
Browse files Browse the repository at this point in the history
* Closes #143.
  • Loading branch information
h3rald committed Aug 2, 2010
1 parent 221398d commit e98a81f
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 26 deletions.
3 changes: 1 addition & 2 deletions lib/glyph/document.rb
Expand Up @@ -149,9 +149,8 @@ def finalize
@topics.each do |t|
t[:contents].gsub! key_s, value_s
end
else
@output.gsub! key_s, value_s
end
@output.gsub! key_s, value_s
rescue Glyph::MacroError => e
e.macro.macro_warning e.message, e
rescue Exception => e
Expand Down
6 changes: 3 additions & 3 deletions lib/glyph/macro_helpers.rb
Expand Up @@ -103,6 +103,7 @@ def toc_element_for(depth, title, procs={})
added_headers ||= []
n1.descend do |n2, level|
if n2.is_a?(Glyph::MacroNode) && Glyph['system.structure.headers'].include?(n2[:name]) then
next if Glyph.multiple_output_files? && !n2.attribute(:src) # Only consider topics when building TOC for web/web5
next if n2.find_parent{|node| Glyph['system.structure.special'].include? node[:name] }
header_hash = n2[:header]
next if depth && header_hash && (header_hash.level-1 > depth.to_i) || header_hash && !header_hash.toc?
Expand Down Expand Up @@ -141,7 +142,7 @@ def section_element_for(procs={})
end
end
ident = (attr(:id) || "h_#{@node[:document].headers.length+1}").to_sym
bmk = header :title => attr(:title), :level => level, :id => ident, :toc => !attr(:notoc), :file => @source_file
bmk = header :title => attr(:title), :level => level, :id => ident, :toc => !attr(:notoc), :file => attr(:src) || @source_file
@node[:header] = bmk
h = procs[:title].call level, bmk, attr(:title)
end
Expand All @@ -152,11 +153,10 @@ def section_element_for(procs={})
layout = attr(:layout) || Glyph['document.topic_layout'] || :topic
layout_name = "layout:#{layout}".to_sym
macro_error "Layout '#{layout}' not found" unless Glyph::MACROS[layout_name]
@node[:change_topic] = true
result = interpret %{#{layout_name}[
@title[#{attr(:title)}]
@id[#{topic_id}]
@contents[include[#{attr(:src)}]]
@contents[include[@topic[true]#{attr(:src)}]]
]}
# Fix file for topic bookmark
@node[:document].bookmark?(topic_id).file = attr(:src)
Expand Down
2 changes: 1 addition & 1 deletion macros/core.rb
Expand Up @@ -78,7 +78,7 @@
end
begin
folder = Glyph.lite? ? "" : "text/"
topic = @node[:change_topic] ? folder+v : nil
topic = (attr(:topic) && Glyph.multiple_output_files?) ? folder+v : nil
update_source v, folder+v, topic
interpret contents
rescue Glyph::MutualInclusionError => e
Expand Down
14 changes: 4 additions & 10 deletions macros/html/structure.rb
Expand Up @@ -3,6 +3,7 @@
macro :section do
max_parameters 1
level = (raw_attribute(:src) && Glyph.multiple_output_files?) ? :error : :warning
within :contents if Glyph.multiple_output_files?
required_attribute :title, :level => level
procs = {}
procs[:title] = lambda do |level, ident, title|
Expand Down Expand Up @@ -159,7 +160,7 @@
</div>}
end
toc_item_proc = lambda do |classes, header|
"<li class=\"#{classes.join(" ")}\">#{header}</li>"
"<li class=\"#{classes.join(" ").strip}\">#{header}</li>"
end
toc_sublist_proc = lambda do |contents|
"<li><ol>#{contents}</ol></li>\n"
Expand All @@ -172,15 +173,8 @@
end

macro :contents do
interpret(@node.parameter(0).to_s)
end

macro :topic do
within :contents
not_within :topic
required_attribute :src
required_attribute :title
interpret "include[#{attr(:src)}]"
result = interpret(@node.parameter(0).to_s)
Glyph.multiple_output_files? ? "" : result
end

# See:
Expand Down
1 change: 1 addition & 0 deletions spec/files/web_doc.glyph
Expand Up @@ -4,6 +4,7 @@ document[
style[test.sass]
]
body[
toc[]
contents[
section[
@title[Web Document]
Expand Down
21 changes: 13 additions & 8 deletions spec/lib/macro_validators_spec.rb
Expand Up @@ -54,20 +54,25 @@
it "should validate required attributes" do
Glyph['document.output'] = 'web'
Glyph.run! 'load:macros'
lambda { output_for("contents[topic[test]]") }.should raise_error(Glyph::MacroError, "Macro 'topic' requires a 'src' attribute")
lambda { output_for("contents[section[@src[test]]]") }.should raise_error(Glyph::MacroError, "Macro 'section' requires a 'title' attribute")
end

it "should validate if a macro is within another one" do
Glyph['document.output'] = 'web'
Glyph.run! 'load:macros'
lambda { output_for("topic[@src[test]@title[test]]") }.should raise_error(Glyph::MacroError, "Macro 'topic' must be within a 'contents' macro")
define_em_macro
Glyph.macro :within_m do
within :em
"---"
end
lambda { output_for("within_m[test]") }.should raise_error(Glyph::MacroError, "Macro 'within_m' must be within a 'em' macro")
end

it "should validate if a macro is not within another one" do
Glyph['document.output'] = 'web'
Glyph.run! 'load:macros'
lambda { output_for("contents[topic[@src[test]@title[test]topic[@src[test]@title[test]]]]") }.should
raise_error(Glyph::MacroError, "Macro 'topic' must not be within a 'topic' macro")
define_em_macro
Glyph.macro :within_m do
not_within :em
"---"
end
lambda { output_for("em[within_m[test]]") }.should raise_error(Glyph::MacroError, "Macro 'within_m' must not be within a 'em' macro")
end

end
Expand Down
4 changes: 2 additions & 2 deletions spec/macros/macros_spec.rb
Expand Up @@ -99,8 +99,8 @@
<div class="contents">
<h2 class="toc-header" id="toc">Table of Contents</h2>
<ol class="toc">
<li class=" section"><a href="#h_1">Container section</a></li>
<li class=" section"><a href="#md">Markdown</a></li>
<li class="section"><a href="#h_1">Container section</a></li>
<li class="section"><a href="#md">Markdown</a></li>
</ol>
</div>
}.gsub(/\n|\t/, '')
Expand Down
20 changes: 20 additions & 0 deletions spec/macros/web_spec.rb
Expand Up @@ -18,6 +18,7 @@

it "section (topic)" do
lambda { output_for("contents[section[@src[test]]]") }.should raise_error(Glyph::MacroError, "Macro 'section' requires a 'title' attribute")
lambda { output_for("section[@src[test]]") }.should raise_error(Glyph::MacroError, "Macro 'section' must be within a 'contents' macro")
interpret("contents[section[@src[a/web1.glyph]@title[Test]]]")
topic = @p.document.topics[0]
topic.blank?.should == false
Expand All @@ -39,4 +40,23 @@
web2.match(%{<ul class="navigation"><li><a href="a/web1.html">&larr; Previous</a></li><li><a href="index.html">Contents</a></li></ul>}).blank?.should == false
end

it "toc should only list topics" do
Glyph.run! 'generate:web'
index = Glyph.file_load(Glyph::PROJECT/'output/web/index.html')
index.match(%{<li class="section"><a href="#h_1">Web Document</a></li>}).blank?.should == true
index.match(%{href="a/web1.html#h_2"}).blank?.should == false
index.match(%{href="a/b/web2.html#h_6"}).blank?.should == false
delete_project
reset_quiet
create_web_project
Glyph['document.output'] = 'html'
Glyph.run! 'generate:html'
index = Glyph.file_load(Glyph::PROJECT/'output/html/test_project.html')
index.match(%{href="a/web1.html#h_2"}).blank?.should == true
index.match(%{href="a/b/web2.html#h_6"}).blank?.should == true
index.match(%{<li class="section"><a href="#h_1">Web Document</a></li>}).blank?.should == false
index.match(%{href="#h_2"}).blank?.should == false
index.match(%{href="#h_7"}).blank?.should == false # Header numbers are different...
end

end

0 comments on commit e98a81f

Please sign in to comment.