From 6874288c8ac3c356dfd13ce57b5bb656fb8446b0 Mon Sep 17 00:00:00 2001 From: Luke Andrew Date: Sat, 12 Feb 2011 09:56:00 +0100 Subject: [PATCH] [GH-28]: Descriptive section names. Header sections will get a descriptive hash anchor: rather than `section-1`, the section will be given an id containing the header text with spaces stripped and replaced with `_`. `section-Header_Goes_Here` for instance. Thanks to [Luke Andrew][1] for the initial work on this patch. [1]: https://github.com/zyx --- lib/rocco/layout.mustache | 4 ++-- lib/rocco/layout.rb | 7 +++++-- test/test_descriptive_section_names.rb | 29 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 test/test_descriptive_section_names.rb diff --git a/lib/rocco/layout.mustache b/lib/rocco/layout.mustache index c611875..f76bb0e 100644 --- a/lib/rocco/layout.mustache +++ b/lib/rocco/layout.mustache @@ -29,10 +29,10 @@ {{#sections}} - +
- +
{{{ docs }}} diff --git a/lib/rocco/layout.rb b/lib/rocco/layout.rb index a2a59c4..25bfd7e 100644 --- a/lib/rocco/layout.rb +++ b/lib/rocco/layout.rb @@ -18,16 +18,19 @@ class Rocco::Layout < Mustache def sections num = 0 @doc.sections.map do |docs,code| + is_header = /^(.+)<\/h.>$/.match( docs ) + header_text = is_header && is_header[1].split.join("_") + num += 1 { :docs => docs, :docs? => !docs.empty?, - :header? => /^.+<\/h.>$/.match( docs ), + :header? => is_header, :code => code, :code? => !code.empty?, :empty? => ( code.empty? && docs.empty? ), - :num => (num += 1) + :section_id => is_header ? header_text : num } end end diff --git a/test/test_descriptive_section_names.rb b/test/test_descriptive_section_names.rb new file mode 100644 index 0000000..21c42ce --- /dev/null +++ b/test/test_descriptive_section_names.rb @@ -0,0 +1,29 @@ +require File.dirname(__FILE__) + '/helper' + +class RoccoDescriptiveSectionNamesTests < Test::Unit::TestCase + def test_section_name + r = roccoize( "filename.rb", "# # Comment 1\ndef codeblock\nend\n" ) + html = r.to_html + assert( + html.include?( "" ), + "The first section should be named" + ) + assert( + html.include?( '' ), + "The rendered HTML should link to a named section" + ) + end + def test_section_numbering + r = roccoize( "filename.rb", "# # Header 1\ndef codeblock\nend\n# Comment 1\ndef codeblock1\nend\n# # Header 2\ndef codeblock2\nend" ) + html = r.to_html + assert( + html.include?( '' ) && + html.include?( '' ), + "First and second headers should be named sections" + ) + assert( + html.include?( '' ), + "Sections should continue numbering as though headers were counted." + ) + end +end