ruby-changes:26367
From: drbrain <ko1@a...>
Date: Mon, 17 Dec 2012 08:08:30 +0900 (JST)
Subject: [ruby-changes:26367] drbrain:r38418 (trunk): * lib/rdoc.rb: Updated VERSION.
drbrain 2012-12-17 08:07:49 +0900 (Mon, 17 Dec 2012) New Revision: 38418 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38418 Log: * lib/rdoc.rb: Updated VERSION. * lib/rdoc/markup/attribute_manager.rb: Removed useless empty check. * lib/rdoc/markup/to_markdown.rb: Support links from other formats. * lib/rdoc/markup/formatter.rb: ditto. * lib/rdoc/markup/to_html.rb: ditto. * test/rdoc/test_rdoc_markup_formatter.rb: Test for above. * test/rdoc/test_rdoc_markup_to_html.rb: ditto. * test/rdoc/test_rdoc_markup_to_markdown.rb: ditto. * lib/rdoc/rd/block_parser.rb: Improved footnote display. Worked around bug in footnote conversion to Markdown. * test/rdoc/test_rdoc_rd_block_parser.rb: Test for above. * lib/rdoc/rd/inline_parser.rb: Fixed bug with emphasis inside verbatim. * test/rdoc/test_rdoc_rd_inline_parser.rb: Test for above. * test/rdoc/test_rdoc_parser_rd.rb: Use mu_pp, use shortcut methods. Modified files: trunk/lib/rdoc/markup/attribute_manager.rb trunk/lib/rdoc/markup/formatter.rb trunk/lib/rdoc/markup/to_html.rb trunk/lib/rdoc/markup/to_markdown.rb trunk/lib/rdoc/rd/block_parser.rb trunk/lib/rdoc/rd/inline_parser.rb trunk/lib/rdoc.rb trunk/test/rdoc/test_rdoc_markup_formatter.rb trunk/test/rdoc/test_rdoc_markup_to_html.rb trunk/test/rdoc/test_rdoc_markup_to_markdown.rb trunk/test/rdoc/test_rdoc_parser_rd.rb trunk/test/rdoc/test_rdoc_rd_block_parser.rb trunk/test/rdoc/test_rdoc_rd_inline_parser.rb Index: lib/rdoc.rb =================================================================== --- lib/rdoc.rb (revision 38417) +++ lib/rdoc.rb (revision 38418) @@ -64,7 +64,7 @@ module RDoc https://github.com/ruby/ruby/blob/trunk/lib/rdoc.rb#L64 ## # RDoc version you are using - VERSION = '4.0.0.preview2' + VERSION = '4.0.0.preview2.1' ## # Method visibilities Index: lib/rdoc/markup/attribute_manager.rb =================================================================== --- lib/rdoc/markup/attribute_manager.rb (revision 38417) +++ lib/rdoc/markup/attribute_manager.rb (revision 38418) @@ -168,15 +168,13 @@ class RDoc::Markup::AttributeManager https://github.com/ruby/ruby/blob/trunk/lib/rdoc/markup/attribute_manager.rb#L168 # Converts special sequences to RDoc attributes def convert_specials str, attrs - unless @special.empty? - @special.each do |regexp, attribute| - str.scan(regexp) do - capture = $~.size == 1 ? 0 : 1 + @special.each do |regexp, attribute| + str.scan(regexp) do + capture = $~.size == 1 ? 0 : 1 - s, e = $~.offset capture + s, e = $~.offset capture - attrs.set_attrs s, e - s, attribute | @attributes.special - end + attrs.set_attrs s, e - s, attribute | @attributes.special end end end Index: lib/rdoc/markup/to_html.rb =================================================================== --- lib/rdoc/markup/to_html.rb (revision 38417) +++ lib/rdoc/markup/to_html.rb (revision 38418) @@ -36,30 +36,6 @@ class RDoc::Markup::ToHtml < RDoc::Marku https://github.com/ruby/ruby/blob/trunk/lib/rdoc/markup/to_html.rb#L36 attr_accessor :from_path - ## - # Converts a target url to one that is relative to a given path - - def self.gen_relative_url(path, target) - from = File.dirname path - to, to_file = File.split target - - from = from.split "/" - to = to.split "/" - - from.delete '.' - to.delete '.' - - while from.size > 0 and to.size > 0 and from[0] == to[0] do - from.shift - to.shift - end - - from.fill ".." - from.concat to - from << to_file - File.join(*from) - end - # :section: ## @@ -79,17 +55,8 @@ class RDoc::Markup::ToHtml < RDoc::Marku https://github.com/ruby/ruby/blob/trunk/lib/rdoc/markup/to_html.rb#L55 @markup.add_special(/(?:link:|https?:|mailto:|ftp:|irc:|www\.)\S+\w/, :HYPERLINK) - # internal links - @markup.add_special(/rdoc-[a-z]+:\S+/, :RDOCLINK) - - # and links of the form <text>[<url>] - @markup.add_special(/(?: - \{.*?\} | # multi-word label - \b[^\s{}]+? # single-word label - ) - - \[\S+?\] # link target - /x, :TIDYLINK) + add_special_RDOCLINK + add_special_TIDYLINK init_tags end @@ -325,32 +292,13 @@ class RDoc::Markup::ToHtml < RDoc::Marku https://github.com/ruby/ruby/blob/trunk/lib/rdoc/markup/to_html.rb#L292 # for img: and link: described under handle_special_HYPERLINK def gen_url url, text - if url =~ /^rdoc-label:([^:]*)(?::(.*))?/ then - type = "link" - path = "##{$1}" - id = " id=\"#{$2}\"" if $2 - elsif url =~ /([A-Za-z]+):(.*)/ then - type = $1 - path = $2 - else - type = "http" - path = url - url = "http://#{url}" - end - - if type == "link" then - url = if path[0, 1] == '#' then # is this meaningful? - path - else - self.class.gen_relative_url @from_path, path - end - end + scheme, url, id = parse_url url - if (type == "http" or type == "https" or type == "link") and + if %w[http https link].include?(scheme) and url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then "<img src=\"#{url}\" />" else - "<a#{id} href=\"#{url}\">#{text.sub(%r{^#{type}:/*}, '')}</a>" + "<a#{id} href=\"#{url}\">#{text.sub(%r{^#{scheme}:/*}i, '')}</a>" end end Index: lib/rdoc/markup/to_markdown.rb =================================================================== --- lib/rdoc/markup/to_markdown.rb (revision 38417) +++ lib/rdoc/markup/to_markdown.rb (revision 38418) @@ -18,6 +18,9 @@ class RDoc::Markup::ToMarkdown < RDoc::M https://github.com/ruby/ruby/blob/trunk/lib/rdoc/markup/to_markdown.rb#L18 @headings[5] = ['##### ', ''] @headings[6] = ['###### ', ''] + add_special_RDOCLINK + add_special_TIDYLINK + @hard_break = " \n" end @@ -130,5 +133,57 @@ class RDoc::Markup::ToMarkdown < RDoc::M https://github.com/ruby/ruby/blob/trunk/lib/rdoc/markup/to_markdown.rb#L133 @res << "\n" unless @res =~ /\n\z/ end + ## + # Creates a Markdown-style URL from +url+ with +text+. + + def gen_url url, text + scheme, url, = parse_url url + + "[#{text.sub(%r{^#{scheme}:/*}i, '')}](#{url})" + end + + ## + # Handles <tt>rdoc-</tt> type links for footnotes. + + def handle_rdoc_link url + case url + when /\Ardoc-ref:/ then + $' + when /\Ardoc-label:footmark-(\d+)/ then + "[^#{$1}]:" + when /\Ardoc-label:foottext-(\d+)/ then + "[^#{$1}]" + when /\Ardoc-label:label-/ then + gen_url url, $' + when /\Ardoc-[a-z]+:/ then + $' + end + end + + ## + # Converts the RDoc markup tidylink into a Markdown.style link. + + def handle_special_TIDYLINK special + text = special.text + + return text unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/ + + label = $1 + url = $2 + + if url =~ /^rdoc-label:foot/ then + handle_rdoc_link url + else + gen_url url, label + end + end + + ## + # Converts the rdoc-...: links into a Markdown.style links. + + def handle_special_RDOCLINK special + handle_rdoc_link special.text + end + end Index: lib/rdoc/markup/formatter.rb =================================================================== --- lib/rdoc/markup/formatter.rb (revision 38417) +++ lib/rdoc/markup/formatter.rb (revision 38418) @@ -18,6 +18,30 @@ class RDoc::Markup::Formatter https://github.com/ruby/ruby/blob/trunk/lib/rdoc/markup/formatter.rb#L18 InlineTag = Struct.new(:bit, :on, :off) ## + # Converts a target url to one that is relative to a given path + + def self.gen_relative_url path, target + from = File.dirname path + to, to_file = File.split target + + from = from.split "/" + to = to.split "/" + + from.delete '.' + to.delete '.' + + while from.size > 0 and to.size > 0 and from[0] == to[0] do + from.shift + to.shift + end + + from.fill ".." + from.concat to + from << to_file + File.join(*from) + end + + ## # Creates a new Formatter def initialize options, markup = nil @@ -35,6 +59,7 @@ class RDoc::Markup::Formatter https://github.com/ruby/ruby/blob/trunk/lib/rdoc/markup/formatter.rb#L59 @tt_bit = @attributes.bitmap_for :TT @hard_break = '' + @from_path = '.' end ## @@ -52,6 +77,26 @@ class RDoc::Markup::Formatter https://github.com/ruby/ruby/blob/trunk/lib/rdoc/markup/formatter.rb#L77 end ## + # Adds a special for links of the form rdoc-...: + + def add_special_RDOCLINK + @markup.add_special(/rdoc-[a-z]+:\S+/, :RDOCLINK) + end + + ## + # Adds a special for links of the form {<text>}[<url>] and <word>[<url>] + + def add_special_TIDYLINK + @markup.add_special(/(?: + \{.*?\} | # multi-word label + \b[^\s{}]+? # single-word label + ) + + \[\S+?\] # link target + /x, :TIDYLINK) + end + + ## # Add a new set of tags for an attribute. We allow separate start and end # tags for flexibility @@ -179,6 +224,36 @@ class RDoc::Markup::Formatter https://github.com/ruby/ruby/blob/trunk/lib/rdoc/markup/formatter.rb#L224 end ## + # Extracts and a scheme, url and an anchor id from +url+ and returns them. + + def parse_url url + case url + when /^rdoc-label:([^:]*)(?::(.*))?/ then + scheme = 'link' + path = "##{$1}" + id = " id=\"#{$2}\"" if $2 + when /([A-Za-z]+):(.*)/ then + scheme = $1.downcase + path = $2 + when /^#/ then + else + scheme = 'http' + path = url + url = "http://#{url}" + end + + if scheme == 'link' then + url = if path[0, 1] == '#' then # is this meaningful? + path + else + self.class.gen_relative_url @from_path, path + end + end + + [scheme, url, id] + end + + ## # Is +tag+ a tt tag? def tt? tag Index: lib/rdoc/rd/block_parser.rb =================================================================== --- lib/rdoc/rd/block_parser.rb (revision 38417) +++ lib/rdoc/rd/block_parser.rb (revision 38418) @@ -394,11 +394,12 @@ end https://github.com/ruby/ruby/blob/trunk/lib/rdoc/rd/block_parser.rb#L394 # Adds footnote +content+ to the document def add_footnote content - index = @footnotes.length + 1 + index = @footnotes.length / 2 + 1 footmark_link = "{^#{index}}[rdoc-label:footmark-#{index}:foottext-#{index}]" - @footnotes << RDoc::Markup::Paragraph.new(footmark_link, *content) + @footnotes << RDoc::Markup::Paragraph.new(footmark_link, ' ', *content) + @footnotes << RDoc::Markup::BlankLine.new index end Index: lib/rdoc/rd/inline_parser.rb =================================================================== --- lib/rdoc/rd/inline_parser.rb (revision 38417) +++ lib/rdoc/rd/inline_parser.rb (revision 38418) @@ -1104,7 +1104,7 @@ def _reduce_101(val, _values, result) https://github.com/ruby/ruby/blob/trunk/lib/rdoc/rd/inline_parser.rb#L1104 end def _reduce_102(val, _values, result) - result = "<tt>#{val[1]}</tt>" + result = inline "<tt>#{val[1]}</tt>", val[1] result end Index: test/rdoc/test_rdoc_parser_rd.rb =================================================================== --- test/rdoc/test_rdoc_parser_rd.rb (revision 38417) +++ test/rdoc/test_rdoc_parser_rd.rb (revision 38418) @@ -22,6 +22,13 @@ class TestRDocParserRd < RDoc::TestCase https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_parser_rd.rb#L22 @tempfile.close end + def mu_pp obj + s = '' + s = PP.pp obj, s + s = s.force_encoding Encoding.default_external if defined? Encoding + s.chomp + end + def test_file assert_kind_of RDoc::Parser::Text, util_parser('') end @@ -34,9 +41,7 @@ class TestRDocParserRd < RDoc::TestCase https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_parser_rd.rb#L41 def test_scan parser = util_parser 'it ((*really*)) works' - expected = - @RM::Document.new( - @RM::Paragraph.new('it <em>really</em> works')) + expected = doc(para('it <em>really</em> works')) expected.file = @top_level parser.scan Index: test/rdoc/test_rdoc_markup_to_markdown.rb =================================================================== --- test/rdoc/test_rdoc_markup_to_markdown.rb (revision 38417) +++ test/rdoc/test_rdoc_markup_to_markdown.rb (revision 38418) @@ -348,5 +348,36 @@ words words words words https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_markup_to_markdown.rb#L348 assert_equal expected, @to.end_accepting end + def test_convert_RDOCLINK + result = @to.convert 'rdoc-garbage:C' + + assert_equal "C\n", result + end + + def test_convert_TIDYLINK + result = @to.convert \ + '{DSL}[http://en.wikipedia.org/wiki/Domain-specific_language]' + + expected = "[DSL](http://en.wikipedia.org/wiki/Domain-specific_language)\n" + + assert_equal expected, result + end + + def test_handle_rdoc_link_label_footmark + assert_equal '[^1]:', @to.handle_rdoc_link('rdoc-label:footmark-1:x') + end + + def test_handle_rdoc_link_label_foottext + assert_equal '[^1]', @to.handle_rdoc_link('rdoc-label:foottext-1:x') + end + + def test_handle_rdoc_link_label_label + assert_equal '[x](#label-x)', @to.handle_rdoc_link('rdoc-label:label-x') + end + + def test_handle_rdoc_link_ref + assert_equal 'x', @to.handle_rdoc_link('rdoc-ref:x') + end + end Index: test/rdoc/test_rdoc_rd_inline_parser.rb =================================================================== --- test/rdoc/test_rdoc_rd_inline_parser.rb (revision 38417) +++ test/rdoc/test_rdoc_rd_inline_parser.rb (revision 38418) @@ -26,7 +26,8 @@ class TestRDocRdInlineParser < RDoc::Tes https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_rd_inline_parser.rb#L26 assert_equal '{*1}[rdoc-label:foottext-1:footmark-1]', parse('((-text-))') expected = [ - @RM::Paragraph.new('{^1}[rdoc-label:footmark-1:foottext-1]', 'text') + @RM::Paragraph.new('{^1}[rdoc-label:footmark-1:foottext-1]', ' ', 'text'), + blank_line, ] assert_equal expected, @block_parser.footnotes @@ -161,6 +162,10 @@ class TestRDocRdInlineParser < RDoc::Tes https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_rd_inline_parser.rb#L162 assert_equal '<tt>text "</tt>', parse("(('text \\\"'))") end + def test_parse_verb_emphasis + assert_equal '<tt>((*emphasis*))</tt>', parse("(('((*emphasis*))'))") + end + def test_parse_verb_multiple assert_equal '<tt>((*text*))</tt>', parse("(('((*text*))'))") end Index: test/rdoc/test_rdoc_markup_formatter.rb =================================================================== --- test/rdoc/test_rdoc_markup_formatter.rb (revision 38417) +++ test/rdoc/test_rdoc_markup_formatter.rb (revision 38418) @@ -48,6 +48,97 @@ class TestRDocMarkupFormatter < RDoc::Te https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_markup_formatter.rb#L48 @tt = @attributes.bitmap_for :TT end + def test_class_gen_relative_url + def gen(from, to) + RDoc::Markup::ToHtml.gen_relative_url from, to + end + + assert_equal 'a.html', gen('a.html', 'a.html') + assert_equal 'b.html', gen('a.html', 'b.html') + + assert_equal 'd.html', gen('a/c.html', 'a/d.html') + assert_equal '../a.html', gen('a/c.html', 'a.html') + assert_equal 'a/c.html', gen('a.html', 'a/c.html') + end + + def special_names + @attribute_manager.special.map do |_, mask| + @attributes.as_string mask + end + end + + def test_add_special_RDOCLINK + @to.add_special_RDOCLINK + + assert_includes special_names, 'RDOCLINK' + end + + def test_add_special_TIDYLINK + @to.add_special_TIDYLINK + + assert_includes special_names, 'TIDYLINK' + end + + def test_parse_url + scheme, url, id = @to.parse_url 'example/foo' + + assert_equal 'http', scheme + assert_equal 'http://example/foo', url + assert_equal nil, id + end + + def test_parse_url_anchor + scheme, url, id = @to.parse_url '#foottext-1' + + assert_equal nil, scheme + assert_equal '#foottext-1', url + assert_equal nil, id + end + + def test_parse_url_link + scheme, url, id = @to.parse_url 'link:README.txt' + + assert_equal 'link', scheme + assert_equal 'README.txt', url + assert_equal nil, id + end + + def test_parse_url_link_id + scheme, url, id = @to.parse_url 'link:README.txt#label-foo' + + assert_equal 'link', scheme + assert_equal 'README.txt#label-foo', url + assert_equal nil, id + end + + def test_parse_url_rdoc_label + scheme, url, id = @to.parse_url 'rdoc-label:foo' + + assert_equal 'link', scheme + assert_equal '#foo', url + assert_equal nil, id + + scheme, url, id = @to.parse_url 'rdoc-label:foo:bar' + + assert_equal 'link', scheme + assert_equal '#foo', url + assert_equal ' id="bar"', id + end + + def test_parse_url_scheme + scheme, url, id = @to.parse_url 'http://example/foo' + + assert_equal 'http', scheme + assert_equal 'http://example/foo', url + assert_equal nil, id + + scheme, url, id = @to.parse_url 'https://example/foo' + + assert_equal 'https', scheme + assert_equal 'https://example/foo', url + assert_equal nil, id + end + def test_convert_tt_special converted = @to.convert '<code>AAA</code>' Index: test/rdoc/test_rdoc_markup_to_html.rb =================================================================== --- test/rdoc/test_rdoc_markup_to_html.rb (revision 38417) +++ test/rdoc/test_rdoc_markup_to_html.rb (revision 38418) @@ -10,19 +10,6 @@ class TestRDocMarkupToHtml < RDoc::Marku https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_markup_to_html.rb#L10 @to = RDoc::Markup::ToHtml.new @options end - def test_class_gen_relative_url - def gen(from, to) - RDoc::Markup::ToHtml.gen_relative_url from, to - end - - assert_equal 'a.html', gen('a.html', 'a.html') - assert_equal 'b.html', gen('a.html', 'b.html') - - assert_equal 'd.html', gen('a/c.html', 'a/d.html') - assert_equal '../a.html', gen('a/c.html', 'a.html') - assert_equal 'a/c.html', gen('a.html', 'a/c.html') - end - def accept_blank_line assert_empty @to.res.join end Index: test/rdoc/test_rdoc_rd_block_parser.rb =================================================================== --- test/rdoc/test_rdoc_rd_block_parser.rb (revision 38417) +++ test/rdoc/test_rdoc_rd_block_parser.rb (revision 38418) @@ -15,6 +15,23 @@ class TestRDocRdBlockParser < RDoc::Test https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_rd_block_parser.rb#L15 s.chomp end + def test_add_footnote + index = @block_parser.add_footnote 'context' + + assert_equal 1, index + + expected = [ + para('{^1}[rdoc-label:footmark-1:foottext-1]', ' ', 'context'), + blank_line, + ] + + assert_equal expected, @block_parser.footnotes + + index = @block_parser.add_footnote 'other' + + assert_equal 2, index + end + def test_parse_desclist list = <<-LIST :one @@ -25,9 +42,9 @@ class TestRDocRdBlockParser < RDoc::Test https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_rd_block_parser.rb#L42 expected = doc( - @RM::List.new(:NOTE, - @RM::ListItem.new("one", @RM::Paragraph.new("desc one")), - @RM::ListItem.new("two", @RM::Paragraph.new("desc two")))) + list(:NOTE, + item("one", para("desc one")), + item("two", para("desc two")))) assert_equal expected, parse(list) end @@ -40,9 +57,9 @@ class TestRDocRdBlockParser < RDoc::Test https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_rd_block_parser.rb#L57 expected = doc( - @RM::List.new(:NUMBER, - @RM::ListItem.new(nil, @RM::Paragraph.new("one")), - @RM::ListItem.new(nil, @RM::Paragraph.new("two")))) + list(:NUMBER, + item(nil, para("one")), + item(nil, para("two")))) assert_equal expected, parse(list) end @@ -56,10 +73,10 @@ class TestRDocRdBlockParser < RDoc::Test https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_rd_block_parser.rb#L73 expected = doc( - @RM::List.new(:NUMBER, - @RM::ListItem.new(nil, - @RM::Paragraph.new("one"), - @RM::Paragraph.new("two")))) + list(:NUMBER, + item(nil, + para("one"), + para("two")))) assert_equal expected, parse(list) end @@ -74,8 +91,8 @@ class TestRDocRdBlockParser < RDoc::Test https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_rd_block_parser.rb#L91 expected = doc( - @RM::List.new(:NUMBER, - @RM::ListItem.new(nil, @RM::Paragraph.new(*contents)))) + list(:NUMBER, + item(nil, para(*contents)))) assert_equal expected, parse(list) end @@ -88,10 +105,10 @@ class TestRDocRdBlockParser < RDoc::Test https://github.com/ruby/rub (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/