diff options
author | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-02-10 03:59:08 +0000 |
---|---|---|
committer | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-02-10 03:59:08 +0000 |
commit | 455b051a008aa859a7b0d6fca56de69a38cc1f8d (patch) | |
tree | d6669b04050c3ac4281651be447dd7d5f57463ea /lib/rdoc/markup | |
parent | d44f24c47cc0d03135fabb7c764ab23e9d5d1152 (diff) | |
download | ruby-455b051a008aa859a7b0d6fca56de69a38cc1f8d.tar.gz |
* lib/rdoc/code_objects.rb: Make some attributes accessible for reuse.
* lib/rdoc/generator/html.rb: Pull out ContextUser classes and related
methods for reuse.
* lib/rdoc/generator.rb: Move ContextUser classes to
RDoc::Generator::Context for reuse.
* lib/rdoc/rdoc.rb: Make RDoc::RDoc initialization a little easier.
* lib/rdoc/options.rb: Make RDoc::Options easier to use without
parsing an ARGV.
* lib/rdoc/markup/to_*.rb: Subclass RDoc::Markup::Formatter.
* lib/rdoc/markup/formatter.rb: Add RDoc::Markup::Formatter to make
RDoc markup conversion easier.
* lib/rdoc/markup/fragments.rb: Make RDoc::Markup::ListItem easier to
test.
* lib/rdoc/markup/to_html_hyperlink.rb: Pulled out of the HTML
generator for easier reusability.
* lib/rdoc/markup.rb: Fix bug with labeled lists containing bullet
lists.
* lib/rdoc/generators/html/html.rb: Fix Constant display.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15421 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rdoc/markup')
-rw-r--r-- | lib/rdoc/markup/formatter.rb | 14 | ||||
-rw-r--r-- | lib/rdoc/markup/fragments.rb | 19 | ||||
-rw-r--r-- | lib/rdoc/markup/inline.rb | 28 | ||||
-rw-r--r-- | lib/rdoc/markup/lines.rb | 4 | ||||
-rw-r--r-- | lib/rdoc/markup/to_flow.rb | 5 | ||||
-rw-r--r-- | lib/rdoc/markup/to_html.rb | 20 | ||||
-rw-r--r-- | lib/rdoc/markup/to_html_hyperlink.rb | 149 | ||||
-rw-r--r-- | lib/rdoc/markup/to_latex.rb | 3 | ||||
-rw-r--r-- | lib/rdoc/markup/to_test.rb | 3 |
9 files changed, 213 insertions, 32 deletions
diff --git a/lib/rdoc/markup/formatter.rb b/lib/rdoc/markup/formatter.rb new file mode 100644 index 0000000000..14cbae59f9 --- /dev/null +++ b/lib/rdoc/markup/formatter.rb @@ -0,0 +1,14 @@ +require 'rdoc/markup' + +class RDoc::Markup::Formatter + + def initialize + @markup = RDoc::Markup.new + end + + def convert(content) + @markup.convert content, self + end + +end + diff --git a/lib/rdoc/markup/fragments.rb b/lib/rdoc/markup/fragments.rb index 39b63cae22..1765861ad0 100644 --- a/lib/rdoc/markup/fragments.rb +++ b/lib/rdoc/markup/fragments.rb @@ -83,10 +83,16 @@ class RDoc::Markup class ListItem < ListBase type_name :LIST - # def label - # am = AttributeManager.new(@param) - # am.flow - # end + def to_s + text = if [:NOTE, :LABELED].include? type then + "#{@param}: #{@txt}" + else + @txt + end + + "L#@level: #{type} #{self.class.name.split('::')[-1]}\n#{text}" + end + end class ListStart < ListBase @@ -311,9 +317,8 @@ class RDoc::Markup def tidy_blank_lines (@fragments.size - 1).times do |i| - if @fragments[i].kind_of?(BlankLine) and - @fragments[i+1].kind_of?(ListEnd) - @fragments[i], @fragments[i+1] = @fragments[i+1], @fragments[i] + if BlankLine === @fragments[i] and ListEnd === @fragments[i+1] then + @fragments[i], @fragments[i+1] = @fragments[i+1], @fragments[i] end end diff --git a/lib/rdoc/markup/inline.rb b/lib/rdoc/markup/inline.rb index cbf5032a68..8945e14b83 100644 --- a/lib/rdoc/markup/inline.rb +++ b/lib/rdoc/markup/inline.rb @@ -12,9 +12,9 @@ class RDoc::Markup @@name_to_bitmap = { :_SPECIAL_ => SPECIAL } @@next_bitmap = 2 - def Attribute.bitmap_for(name) + def self.bitmap_for(name) bitmap = @@name_to_bitmap[name] - if !bitmap + unless bitmap then bitmap = @@next_bitmap @@next_bitmap <<= 1 @@name_to_bitmap[name] = bitmap @@ -22,7 +22,7 @@ class RDoc::Markup bitmap end - def Attribute.as_string(bitmap) + def self.as_string(bitmap) return "none" if bitmap.zero? res = [] @@name_to_bitmap.each do |name, bit| @@ -31,7 +31,7 @@ class RDoc::Markup res.join(",") end - def Attribute.each_name_of(bitmap) + def self.each_name_of(bitmap) @@name_to_bitmap.each do |name, bit| next if bit == SPECIAL yield name.to_s if (bitmap & bit) != 0 @@ -85,14 +85,15 @@ class RDoc::Markup self.text == o.text && self.type == o.type end - def to_s - "Special: type=#{type}, name=#{RDoc::Markup::Attribute.as_string type}, text=#{text.dump}" - end - def inspect "#<RDoc::Markup::Special:0x%x @type=%p, name=%p @text=%p>" % [ object_id, @type, RDoc::Markup::Attribute.as_string(type), text.dump] end + + def to_s + "Special: type=#{type}, name=#{RDoc::Markup::Attribute.as_string type}, text=#{text.dump}" + end + end class AttributeManager @@ -165,8 +166,10 @@ class RDoc::Markup def convert_attrs(str, attrs) # first do matching ones tags = MATCHING_WORD_PAIRS.keys.join("") + re = "(^|\\W)([#{tags}])([A-Za-z_]+?)\\2(\\W|\$)" # re = "(^|\\W)([#{tags}])(\\S+?)\\2(\\W|\$)" + 1 while str.gsub!(Regexp.new(re)) { attr = MATCHING_WORD_PAIRS[$2]; attrs.set_attrs($`.length + $1.length + $2.length, $3.length, attr) @@ -185,9 +188,9 @@ class RDoc::Markup end def convert_html(str, attrs) - tags = HTML_TAGS.keys.join("|") - re = "<(#{tags})>(.*?)</\\1>" - 1 while str.gsub!(Regexp.new(re, Regexp::IGNORECASE)) { + tags = HTML_TAGS.keys.join '|' + + 1 while str.gsub!(/<(#{tags})>(.*?)<\/\1>/i) { attr = HTML_TAGS[$1.downcase] html_length = $1.length + 2 seq = NULL * html_length @@ -210,7 +213,7 @@ class RDoc::Markup # A \ in front of a character that would normally be processed turns off # processing. We do this by turning \< into <#{PROTECT} - PROTECTABLE = [ "<" << "\\" ] #" + PROTECTABLE = [ "<" << "\\" ] def mask_protected_sequences @@ -300,7 +303,6 @@ class RDoc::Markup end def split_into_flow - display_attributes if $DEBUG_RDOC res = [] diff --git a/lib/rdoc/markup/lines.rb b/lib/rdoc/markup/lines.rb index 985304c225..069492122f 100644 --- a/lib/rdoc/markup/lines.rb +++ b/lib/rdoc/markup/lines.rb @@ -59,8 +59,8 @@ class RDoc::Markup end # Return true if this line is blank - def isBlank? - @text.length.zero? + def blank? + @text.empty? end # stamp a line with a type, a level, a prefix, and a flag diff --git a/lib/rdoc/markup/to_flow.rb b/lib/rdoc/markup/to_flow.rb index cb7da5fa88..3d87b3e9c3 100644 --- a/lib/rdoc/markup/to_flow.rb +++ b/lib/rdoc/markup/to_flow.rb @@ -1,3 +1,4 @@ +require 'rdoc/markup/formatter' require 'rdoc/markup/fragments' require 'rdoc/markup/inline' require 'cgi' @@ -22,7 +23,7 @@ class RDoc::Markup H = Struct.new(:level, :text) end - class ToFlow + class ToFlow < RDoc::Markup::Formatter LIST_TYPE_TO_HTML = { :BULLET => [ "<ul>", "</ul>" ], :NUMBER => [ "<ol>", "</ol>" ], @@ -35,6 +36,8 @@ class RDoc::Markup InlineTag = Struct.new(:bit, :on, :off) def initialize + super + init_tags end diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb index 0238d5ae67..da42312ff5 100644 --- a/lib/rdoc/markup/to_html.rb +++ b/lib/rdoc/markup/to_html.rb @@ -1,22 +1,25 @@ +require 'rdoc/markup/formatter' require 'rdoc/markup/fragments' require 'rdoc/markup/inline' require 'cgi' -class RDoc::Markup::ToHtml +class RDoc::Markup::ToHtml < RDoc::Markup::Formatter LIST_TYPE_TO_HTML = { - :BULLET => [ "<ul>", "</ul>" ], - :NUMBER => [ "<ol>", "</ol>" ], - :UPPERALPHA => [ "<ol>", "</ol>" ], - :LOWERALPHA => [ "<ol>", "</ol>" ], - :LABELED => [ "<dl>", "</dl>" ], - :NOTE => [ "<table>", "</table>" ], + :BULLET => %w[<ul> </ul>], + :NUMBER => %w[<ol> </ol>], + :UPPERALPHA => %w[<ol> </ol>], + :LOWERALPHA => %w[<ol> </ol>], + :LABELED => %w[<dl> </dl>], + :NOTE => %w[<table> </table>], } InlineTag = Struct.new(:bit, :on, :off) def initialize + super + init_tags end @@ -94,8 +97,11 @@ class RDoc::Markup::ToHtml if tag = @in_list_entry.last @res << annotate(tag) << "\n" end + @res << list_item_start(am, fragment) + @res << wrap(convert_flow(am.flow(fragment.txt))) << "\n" + @in_list_entry[-1] = list_end_for(fragment.type) end diff --git a/lib/rdoc/markup/to_html_hyperlink.rb b/lib/rdoc/markup/to_html_hyperlink.rb new file mode 100644 index 0000000000..58adaa8cb0 --- /dev/null +++ b/lib/rdoc/markup/to_html_hyperlink.rb @@ -0,0 +1,149 @@ +require 'rdoc/markup/to_html' + +## +# Subclass of the RDoc::Markup::ToHtml class that supports looking up words in +# the AllReferences list. Those that are found (like AllReferences in this +# comment) will be hyperlinked + +class RDoc::Markup::ToHtmlHyperlink < RDoc::Markup::ToHtml + + attr_accessor :context + + ## + # We need to record the html path of our caller so we can generate + # correct relative paths for any hyperlinks that we find + + def initialize(from_path, context, show_hash) + super() + + # class names, variable names, or instance variables + @markup.add_special(/( + # A::B.meth(**) (for operator in Fortran95) + \w+(::\w+)*[.\#]\w+(\([\.\w+\*\/\+\-\=\<\>]+\))? + # meth(**) (for operator in Fortran95) + | \#\w+(\([.\w\*\/\+\-\=\<\>]+\))? + | \b([A-Z]\w*(::\w+)*[.\#]\w+) # A::B.meth + | \b([A-Z]\w+(::\w+)*) # A::B + | \#\w+[!?=]? # #meth_name + | \\?\b\w+([_\/\.]+\w+)*[!?=]? # meth_name + )/x, + :CROSSREF) + + # external hyperlinks + @markup.add_special(/((link:|https?:|mailto:|ftp:|www\.)\S+\w)/, :HYPERLINK) + + # and links of the form <text>[<url>] + @markup.add_special(/(((\{.*?\})|\b\S+?)\[\S+?\.\S+?\])/, :TIDYLINK) + + @from_path = from_path + @context = context + @show_hash = show_hash + + @seen = {} + end + + ## + # We're invoked when any text matches the CROSSREF pattern + # (defined in MarkUp). If we fine the corresponding reference, + # generate a hyperlink. If the name we're looking for contains + # no punctuation, we look for it up the module/class chain. For + # example, HyperlinkHtml is found, even without the Generator:: + # prefix, because we look for it in module Generator first. + + def handle_special_CROSSREF(special) + name = special.text + + return @seen[name] if @seen.include? name + + if name[0,1] == '#' then + lookup = name[1..-1] + name = lookup unless @show_hash + else + lookup = name + end + + # Find class, module, or method in class or module. + if /([A-Z]\w*)[.\#](\w+[!?=]?)/ =~ lookup then + container = $1 + method = $2 + ref = @context.find_symbol container, method + elsif /([A-Za-z]\w*)[.\#](\w+(\([\.\w+\*\/\+\-\=\<\>]+\))?)/ =~ lookup then + container = $1 + method = $2 + ref = @context.find_symbol container, method + else + ref = @context.find_symbol lookup + end + + out = if lookup =~ /^\\/ then + $' + elsif ref and ref.document_self then + "<a href=\"#{ref.as_href(@from_path)}\">#{name}</a>" + else + name + end + + @seen[name] = out + + out + end + + ## + # Generate a hyperlink for url, labeled with text. Handle the + # special cases for img: and link: described under handle_special_HYPEDLINK + + def gen_url(url, text) + if 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 + HTML.gen_url @from_path, path + end + end + + if (type == "http" or type == "link") and + url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then + "<img src=\"#{url}\" />" + else + "<a href=\"#{url}\">#{text.sub(%r{^#{type}:/*}, '')}</a>" + end + end + + ## + # And we're invoked with a potential external hyperlink mailto: + # just gets inserted. http: links are checked to see if they + # reference an image. If so, that image gets inserted using an + # <img> tag. Otherwise a conventional <a href> is used. We also + # support a special type of hyperlink, link:, which is a reference + # to a local file whose path is relative to the --op directory. + + def handle_special_HYPERLINK(special) + url = special.text + gen_url url, url + end + + ## + # Here's a hypedlink where the label is different to the URL + # <label>[url] + + def handle_special_TIDYLINK(special) + text = special.text + + return text unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/ + + label = $1 + url = $2 + gen_url url, label + end + +end + diff --git a/lib/rdoc/markup/to_latex.rb b/lib/rdoc/markup/to_latex.rb index 8b7e33719b..a679b3b06e 100644 --- a/lib/rdoc/markup/to_latex.rb +++ b/lib/rdoc/markup/to_latex.rb @@ -1,3 +1,4 @@ +require 'rdoc/markup/formatter' require 'rdoc/markup/fragments' require 'rdoc/markup/inline' @@ -6,7 +7,7 @@ require 'cgi' ## # Convert SimpleMarkup to basic LaTeX report format. -class RDoc::Markup::ToLaTeX +class RDoc::Markup::ToLaTeX < RDoc::Markup::Formatter BS = "\020" # \ OB = "\021" # { diff --git a/lib/rdoc/markup/to_test.rb b/lib/rdoc/markup/to_test.rb index eb183e5ba4..ce6aff6e9a 100644 --- a/lib/rdoc/markup/to_test.rb +++ b/lib/rdoc/markup/to_test.rb @@ -1,9 +1,10 @@ require 'rdoc/markup' +require 'rdoc/markup/formatter' ## # This Markup outputter is used for testing purposes. -class RDoc::Markup::ToTest +class RDoc::Markup::ToTest < RDoc::Markup::Formatter def start_accepting @res = [] |