[前][次][番号順一覧][スレッド一覧]

ruby-changes:26169

From: drbrain <ko1@a...>
Date: Thu, 6 Dec 2012 07:23:11 +0900 (JST)
Subject: [ruby-changes:26169] drbrain:r38226 (trunk): * lib/rdoc*: Improved display of ChangeLog files as HTML.

drbrain	2012-12-06 07:20:15 +0900 (Thu, 06 Dec 2012)

  New Revision: 38226

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38226

  Log:
    * lib/rdoc*:  Improved display of ChangeLog files as HTML.
    * test/rdoc*:  Test for above.

  Modified files:
    trunk/ChangeLog
    trunk/lib/rdoc/markup/document.rb
    trunk/lib/rdoc/markup/formatter.rb
    trunk/lib/rdoc/markup/heading.rb
    trunk/lib/rdoc/markup/to_table_of_contents.rb
    trunk/lib/rdoc/parser/changelog.rb
    trunk/test/rdoc/test_rdoc_markup_document.rb
    trunk/test/rdoc/test_rdoc_markup_to_table_of_contents.rb
    trunk/test/rdoc/test_rdoc_parser_changelog.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38225)
+++ ChangeLog	(revision 38226)
@@ -1,3 +1,8 @@
+Thu Dec  6 07:19:58 2012  Eric Hodel  <drbrain@s...>
+
+	* lib/rdoc*:  Improved display of ChangeLog files as HTML.
+	* test/rdoc*:  Test for above.
+
 Thu Dec  6 04:34:19 2012  KOSAKI Motohiro  <kosaki.motohiro@g...>
 
 	* thread.c (rb_uninterruptible): helper function for providing
Index: lib/rdoc/markup/document.rb
===================================================================
--- lib/rdoc/markup/document.rb	(revision 38225)
+++ lib/rdoc/markup/document.rb	(revision 38226)
@@ -12,6 +12,12 @@
   attr_reader :file
 
   ##
+  # If a heading is below the given level it will be omitted from the
+  # table_of_contents
+
+  attr_accessor :omit_headings_below
+
+  ##
   # The parts of the Document
 
   attr_reader :parts
@@ -24,6 +30,7 @@
     @parts.concat parts
 
     @file = nil
+    @omit_headings_from_table_of_contents_below = nil
   end
 
   ##
@@ -57,14 +64,7 @@
   def accept visitor
     visitor.start_accepting
 
-    @parts.each do |item|
-      case item
-      when RDoc::Markup::Document then # HACK
-        visitor.accept_document item
-      else
-        item.accept visitor
-      end
-    end
+    visitor.accept_document self
 
     visitor.end_accepting
   end
Index: lib/rdoc/markup/to_table_of_contents.rb
===================================================================
--- lib/rdoc/markup/to_table_of_contents.rb	(revision 38225)
+++ lib/rdoc/markup/to_table_of_contents.rb	(revision 38226)
@@ -18,15 +18,31 @@
 
   attr_reader :res
 
+  ##
+  # Omits headings with a level less than the given level.
+
+  attr_accessor :omit_headings_below
+
   def initialize # :nodoc:
     super nil
+
+    @omit_headings_below = nil
   end
 
   ##
+  # Adds +document+ to the output, using its heading cutoff if present
+
+  def accept_document document
+    @omit_headings_below = document.omit_headings_below
+
+    super
+  end
+
+  ##
   # Adds +heading+ to the table of contents
 
   def accept_heading heading
-    @res << heading
+    @res << heading unless suppressed? heading
   end
 
   ##
@@ -40,9 +56,19 @@
   # Prepares the visitor for text generation
 
   def start_accepting
+    @omit_headings_below = nil
     @res = []
   end
 
+  ##
+  # Returns true if +heading+ is below the display threshold
+
+  def suppressed? heading
+    return false unless @omit_headings_below
+
+    heading.level > @omit_headings_below
+  end
+
   # :stopdoc:
   alias accept_block_quote     ignore
   alias accept_raw             ignore
Index: lib/rdoc/markup/heading.rb
===================================================================
--- lib/rdoc/markup/heading.rb	(revision 38225)
+++ lib/rdoc/markup/heading.rb	(revision 38226)
@@ -1,7 +1,8 @@
 ##
 # A heading with a level (1-6) and text
 
-class RDoc::Markup::Heading < Struct.new :level, :text
+RDoc::Markup::Heading =
+  Struct.new :level, :text do
 
   @to_html = nil
   @to_label = nil
Index: lib/rdoc/markup/formatter.rb
===================================================================
--- lib/rdoc/markup/formatter.rb	(revision 38225)
+++ lib/rdoc/markup/formatter.rb	(revision 38226)
@@ -42,7 +42,12 @@
 
   def accept_document document
     document.parts.each do |item|
-      item.accept self
+      case item
+      when RDoc::Markup::Document then # HACK
+        accept_document item
+      else
+        item.accept self
+      end
     end
   end
 
Index: lib/rdoc/parser/changelog.rb
===================================================================
--- lib/rdoc/parser/changelog.rb	(revision 38225)
+++ lib/rdoc/parser/changelog.rb	(revision 38226)
@@ -23,13 +23,14 @@
 
   def create_document groups
     doc = RDoc::Markup::Document.new
+    doc.omit_headings_below = 2
     doc.file = @top_level
 
     doc << RDoc::Markup::Heading.new(1, File.basename(@file_name))
     doc << RDoc::Markup::BlankLine.new
 
     groups.sort_by do |day,| day end.reverse_each do |day, entries|
-      doc << RDoc::Markup::Heading.new(2, day)
+      doc << RDoc::Markup::Heading.new(2, day.dup)
       doc << RDoc::Markup::BlankLine.new
 
       doc.concat create_entries entries
@@ -55,7 +56,11 @@
     list = RDoc::Markup::List.new :NOTE
 
     items.each do |item|
-      title, body = item.split(/:\s*/, 2)
+      item =~ /\A(.*?(?:\([^)]+\))?):\s*/
+
+      title = $1
+      body = $'
+
       paragraph = RDoc::Markup::Paragraph.new body
       list_item = RDoc::Markup::ListItem.new title, paragraph
       list << list_item
Index: test/rdoc/test_rdoc_markup_document.rb
===================================================================
--- test/rdoc/test_rdoc_markup_document.rb	(revision 38225)
+++ test/rdoc/test_rdoc_markup_document.rb	(revision 38226)
@@ -191,5 +191,24 @@
     assert_equal expected, doc.table_of_contents
   end
 
+  def test_table_of_contents_omit_headings_below
+    document = doc(
+      head(1, 'A'),
+      para('B'),
+      head(2, 'C'),
+      para('D'),
+      head(1, 'E'),
+      para('F'))
+
+    document.omit_headings_below = 1
+
+    expected = [
+      head(1, 'A'),
+      head(1, 'E'),
+    ]
+
+    assert_equal expected, document.table_of_contents
+  end
+
 end
 
Index: test/rdoc/test_rdoc_parser_changelog.rb
===================================================================
--- test/rdoc/test_rdoc_parser_changelog.rb	(revision 38225)
+++ test/rdoc/test_rdoc_parser_changelog.rb	(revision 38226)
@@ -96,7 +96,17 @@
 
     expected.file = @top_level
 
-    assert_equal expected, parser.create_document(groups)
+    document = parser.create_document(groups)
+
+    assert_equal expected, document
+
+    assert_equal 2, document.omit_headings_below
+
+    headings = document.parts.select do |part|
+      RDoc::Markup::Heading === part and part.level == 2
+    end
+
+    refute headings.all? { |heading| heading.text.frozen? }
   end
 
   def test_create_entries
@@ -118,6 +128,26 @@
       list(:NOTE, item('c', para('three')), item('d', para('four'))),
     ]
 
+    entries = parser.create_entries(entries)
+    assert_equal expected, entries
+  end
+
+  def test_create_entries_colons
+    parser = util_parser
+
+    entries = [
+      ['Wed Dec  5 12:17:11 2012  Naohisa Goto  <ngotogenome@g...>',
+        ['func.rb (DL::Function#bind): log stuff [ruby-core:50562]']],
+    ]
+
+    expected = [
+      head(3,
+           'Wed Dec  5 12:17:11 2012  Naohisa Goto  <ngotogenome@g...>'),
+      blank_line,
+      list(:NOTE,
+           item('func.rb (DL::Function#bind)',
+                para('log stuff [ruby-core:50562]')))]
+
     assert_equal expected, parser.create_entries(entries)
   end
 
Index: test/rdoc/test_rdoc_markup_to_table_of_contents.rb
===================================================================
--- test/rdoc/test_rdoc_markup_to_table_of_contents.rb	(revision 38225)
+++ test/rdoc/test_rdoc_markup_to_table_of_contents.rb	(revision 38226)
@@ -91,5 +91,36 @@
   alias list_verbatim                                 empty
   alias start_accepting                               empty
 
+  def test_accept_document_omit_headings_below
+    document = doc
+    document.omit_headings_below = 2
+
+    @to.accept_document document
+
+    assert_equal 2, @to.omit_headings_below
+  end
+
+  def test_accept_heading_suppressed
+    @to.start_accepting
+    @to.omit_headings_below = 4
+
+    suppressed = head 5, 'Hello'
+
+    @to.accept_heading suppressed
+
+    assert_empty @to.res
+  end
+
+  def test_suppressed_eh
+    @to.omit_headings_below = nil
+
+    refute @to.suppressed? head(1, '')
+
+    @to.omit_headings_below = 1
+
+    refute @to.suppressed? head(1, '')
+    assert @to.suppressed? head(2, '')
+  end
+
 end
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]