diff options
author | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-07-18 00:46:16 +0000 |
---|---|---|
committer | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-07-18 00:46:16 +0000 |
commit | fd25f74d64c69d636764ea11aa5a809b85e58f69 (patch) | |
tree | 40585659bf4b9665ad0d258c415a6765a056d35d /lib/rdoc/markup/to_html.rb | |
parent | 0af4a490b48bb6fef8d4f392d0c0b215db8e06f9 (diff) |
Import RDoc r101.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18121 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rdoc/markup/to_html.rb')
-rw-r--r-- | lib/rdoc/markup/to_html.rb | 64 |
1 files changed, 55 insertions, 9 deletions
diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb index 3c08d7bf6a..ca29373db1 100644 --- a/lib/rdoc/markup/to_html.rb +++ b/lib/rdoc/markup/to_html.rb @@ -1,7 +1,6 @@ require 'rdoc/markup/formatter' require 'rdoc/markup/fragments' require 'rdoc/markup/inline' -require 'rdoc/generator' require 'cgi' @@ -21,6 +20,11 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter def initialize super + # @in_tt - tt nested levels count + # @tt_bit - cache + @in_tt = 0 + @tt_bit = RDoc::Markup::Attribute.bitmap_for :TT + # external hyperlinks @markup.add_special(/((link:|https?:|mailto:|ftp:|www\.)\S+\w)/, :HYPERLINK) @@ -31,6 +35,27 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter end ## + # 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 "/" + + 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 + + ## # Generate a hyperlink for url, labeled with text. Handle the # special cases for img: and link: described under handle_special_HYPEDLINK @@ -48,7 +73,7 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter url = if path[0, 1] == '#' then # is this meaningful? path else - RDoc::Generator.gen_url @from_path, path + self.class.gen_relative_url @from_path, path end end @@ -88,6 +113,20 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter end ## + # are we currently inside <tt> tags? + + def in_tt? + @in_tt > 0 + end + + ## + # is +tag+ a <tt> tag? + + def tt?(tag) + tag.bit == @tt_bit + end + + ## # Set up the standard mapping of attributes to HTML tags def init_tags @@ -216,6 +255,7 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter @attr_tags.each do |tag| if attr_mask & tag.bit != 0 res << annotate(tag.on) + @in_tt += 1 if tt?(tag) end end end @@ -226,6 +266,7 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter @attr_tags.reverse_each do |tag| if attr_mask & tag.bit != 0 + @in_tt -= 1 if tt?(tag) res << annotate(tag.off) end end @@ -251,27 +292,33 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter res end + def convert_string(item) + in_tt? ? convert_string_simple(item) : convert_string_fancy(item) + end + + def convert_string_simple(item) + CGI.escapeHTML item + end + ## # some of these patterns are taken from SmartyPants... - def convert_string(item) - CGI.escapeHTML(item). - + def convert_string_fancy(item) # convert -- to em-dash, (-- to en-dash) - gsub(/---?/, '—'). #gsub(/--/, '–'). + item.gsub(/---?/, '—'). #gsub(/--/, '–'). # convert ... to elipsis (and make sure .... becomes .<elipsis>) gsub(/\.\.\.\./, '.…').gsub(/\.\.\./, '…'). # convert single closing quote - gsub(%r{([^ \t\r\n\[\{\(])\'}, '\1’'). + gsub(%r{([^ \t\r\n\[\{\(])\'}, '\1’'). # } gsub(%r{\'(?=\W|s\b)}, '’'). # convert single opening quote gsub(/'/, '‘'). # convert double closing quote - gsub(%r{([^ \t\r\n\[\{\(])\'(?=\W)}, '\1”'). + gsub(%r{([^ \t\r\n\[\{\(])\'(?=\W)}, '\1”'). # } # convert double opening quote gsub(/'/, '“'). @@ -281,7 +328,6 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter # convert and registered trademark gsub(/\(r\)/, '®') - end def convert_special(special) |