summaryrefslogtreecommitdiff
path: root/lib/rdoc
diff options
context:
space:
mode:
authordrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-16 23:07:49 +0000
committerdrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-16 23:07:49 +0000
commit810008293fd8ce3a9d3d62dcf2f2229b98c2bd49 (patch)
tree7e06c2b0a08a3b29bf6a1c1cfd44a352fb2d82e2 /lib/rdoc
parent10295ab2ff87b1189dcbe129a07a692417db53f6 (diff)
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38418 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rdoc')
-rw-r--r--lib/rdoc/markup/attribute_manager.rb12
-rw-r--r--lib/rdoc/markup/formatter.rb75
-rw-r--r--lib/rdoc/markup/to_html.rb62
-rw-r--r--lib/rdoc/markup/to_markdown.rb55
-rw-r--r--lib/rdoc/rd/block_parser.rb5
-rw-r--r--lib/rdoc/rd/inline_parser.rb2
6 files changed, 144 insertions, 67 deletions
diff --git a/lib/rdoc/markup/attribute_manager.rb b/lib/rdoc/markup/attribute_manager.rb
index 71d5e2b2cc..7ee6f423d6 100644
--- a/lib/rdoc/markup/attribute_manager.rb
+++ b/lib/rdoc/markup/attribute_manager.rb
@@ -168,15 +168,13 @@ class RDoc::Markup::AttributeManager
# 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
diff --git a/lib/rdoc/markup/formatter.rb b/lib/rdoc/markup/formatter.rb
index a5f639c28c..b668746c7a 100644
--- a/lib/rdoc/markup/formatter.rb
+++ b/lib/rdoc/markup/formatter.rb
@@ -18,6 +18,30 @@ class RDoc::Markup::Formatter
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
@tt_bit = @attributes.bitmap_for :TT
@hard_break = ''
+ @from_path = '.'
end
##
@@ -52,6 +77,26 @@ class RDoc::Markup::Formatter
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
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
diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb
index 9cd94a5945..afe35c26ca 100644
--- a/lib/rdoc/markup/to_html.rb
+++ b/lib/rdoc/markup/to_html.rb
@@ -36,30 +36,6 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter
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::Markup::Formatter
@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::Markup::Formatter
# 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
diff --git a/lib/rdoc/markup/to_markdown.rb b/lib/rdoc/markup/to_markdown.rb
index e984776399..62ad3ad13e 100644
--- a/lib/rdoc/markup/to_markdown.rb
+++ b/lib/rdoc/markup/to_markdown.rb
@@ -18,6 +18,9 @@ class RDoc::Markup::ToMarkdown < RDoc::Markup::ToRdoc
@headings[5] = ['##### ', '']
@headings[6] = ['###### ', '']
+ add_special_RDOCLINK
+ add_special_TIDYLINK
+
@hard_break = " \n"
end
@@ -130,5 +133,57 @@ class RDoc::Markup::ToMarkdown < RDoc::Markup::ToRdoc
@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
diff --git a/lib/rdoc/rd/block_parser.rb b/lib/rdoc/rd/block_parser.rb
index 1e7d872508..dd15e1262e 100644
--- a/lib/rdoc/rd/block_parser.rb
+++ b/lib/rdoc/rd/block_parser.rb
@@ -394,11 +394,12 @@ end
# 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
diff --git a/lib/rdoc/rd/inline_parser.rb b/lib/rdoc/rd/inline_parser.rb
index f330def44a..c3c1f4b030 100644
--- a/lib/rdoc/rd/inline_parser.rb
+++ b/lib/rdoc/rd/inline_parser.rb
@@ -1104,7 +1104,7 @@ def _reduce_101(val, _values, result)
end
def _reduce_102(val, _values, result)
- result = "<tt>#{val[1]}</tt>"
+ result = inline "<tt>#{val[1]}</tt>", val[1]
result
end