summaryrefslogtreecommitdiff
path: root/lib/rdoc/markup
diff options
context:
space:
mode:
authordrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-07-18 00:46:16 +0000
committerdrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-07-18 00:46:16 +0000
commitfd25f74d64c69d636764ea11aa5a809b85e58f69 (patch)
tree40585659bf4b9665ad0d258c415a6765a056d35d /lib/rdoc/markup
parent0af4a490b48bb6fef8d4f392d0c0b215db8e06f9 (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')
-rw-r--r--lib/rdoc/markup/attribute_manager.rb2
-rw-r--r--lib/rdoc/markup/fragments.rb4
-rw-r--r--lib/rdoc/markup/preprocess.rb16
-rw-r--r--lib/rdoc/markup/to_html.rb64
-rw-r--r--lib/rdoc/markup/to_html_crossref.rb26
-rw-r--r--lib/rdoc/markup/to_texinfo.rb69
6 files changed, 157 insertions, 24 deletions
diff --git a/lib/rdoc/markup/attribute_manager.rb b/lib/rdoc/markup/attribute_manager.rb
index 72f70dadd7..ae8b77f22e 100644
--- a/lib/rdoc/markup/attribute_manager.rb
+++ b/lib/rdoc/markup/attribute_manager.rb
@@ -144,8 +144,6 @@ class RDoc::Markup::AttributeManager
add_html("b", :BOLD)
add_html("tt", :TT)
add_html("code", :TT)
-
- add_special(/<!--(.*?)-->/, :COMMENT)
end
def add_word_pair(start, stop, name)
diff --git a/lib/rdoc/markup/fragments.rb b/lib/rdoc/markup/fragments.rb
index 99780de169..b7f9b605c8 100644
--- a/lib/rdoc/markup/fragments.rb
+++ b/lib/rdoc/markup/fragments.rb
@@ -11,8 +11,8 @@ class RDoc::Markup
attr_reader :level, :param, :txt
attr_accessor :type
- ######
- # This is a simple factory system that lets us associate fragment
+ ##
+ # This is a simple factory system that lets us associate fragement
# types (a string) with a subclass of fragment
TYPE_MAP = {}
diff --git a/lib/rdoc/markup/preprocess.rb b/lib/rdoc/markup/preprocess.rb
index 760351d386..00dd4be4ad 100644
--- a/lib/rdoc/markup/preprocess.rb
+++ b/lib/rdoc/markup/preprocess.rb
@@ -14,21 +14,25 @@ class RDoc::Markup::PreProcess
##
# Look for common options in a chunk of text. Options that we don't handle
- # are passed back to our caller as |directive, param|
+ # are yielded to the caller.
def handle(text)
- text.gsub!(/^([ \t#]*):(\w+):\s*(.+)?\n/) do
+ text.gsub!(/^([ \t]*#?[ \t]*):(\w+):([ \t]*)(.+)?\n/) do
+ next $& if $3.empty? and $4 and $4[0, 1] == ':'
+
prefix = $1
directive = $2.downcase
- param = $3
+ param = $4
case directive
- when "include"
+ when 'include' then
filename = param.split[0]
- include_file(filename, prefix)
+ include_file filename, prefix
else
- yield(directive, param)
+ result = yield directive, param
+ result = "#{prefix}:#{directive}: #{param}\n" unless result
+ result
end
end
end
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(/---?/, '&#8212;'). #gsub(/--/, '&#8211;').
+ item.gsub(/---?/, '&#8212;'). #gsub(/--/, '&#8211;').
# convert ... to elipsis (and make sure .... becomes .<elipsis>)
gsub(/\.\.\.\./, '.&#8230;').gsub(/\.\.\./, '&#8230;').
# convert single closing quote
- gsub(%r{([^ \t\r\n\[\{\(])\'}, '\1&#8217;').
+ gsub(%r{([^ \t\r\n\[\{\(])\'}, '\1&#8217;'). # }
gsub(%r{\'(?=\W|s\b)}, '&#8217;').
# convert single opening quote
gsub(/'/, '&#8216;').
# convert double closing quote
- gsub(%r{([^ \t\r\n\[\{\(])\'(?=\W)}, '\1&#8221;').
+ gsub(%r{([^ \t\r\n\[\{\(])\'(?=\W)}, '\1&#8221;'). # }
# convert double opening quote
gsub(/'/, '&#8220;').
@@ -281,7 +328,6 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter
# convert and registered trademark
gsub(/\(r\)/, '&#174;')
-
end
def convert_special(special)
diff --git a/lib/rdoc/markup/to_html_crossref.rb b/lib/rdoc/markup/to_html_crossref.rb
index 32c922e70b..a6f29c5c2c 100644
--- a/lib/rdoc/markup/to_html_crossref.rb
+++ b/lib/rdoc/markup/to_html_crossref.rb
@@ -14,6 +14,7 @@ class RDoc::Markup::ToHtmlCrossref < RDoc::Markup::ToHtml
# correct relative paths for any hyperlinks that we find
def initialize(from_path, context, show_hash)
+ raise ArgumentError, 'from_path cannot be nil' if from_path.nil?
super()
# class names, variable names, or instance variables
@@ -47,28 +48,43 @@ class RDoc::Markup::ToHtmlCrossref < RDoc::Markup::ToHtml
def handle_special_CROSSREF(special)
name = special.text
+ return name if name =~ /\A[a-z]*\z/
+
return @seen[name] if @seen.include? name
- if name[0,1] == '#' then
+ 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
+ #
+ # Do not, however, use an if/elsif/else chain to do so. Instead, test
+ # each possible pattern until one matches. The reason for this is that a
+ # string like "YAML.txt" could be the txt() class method of class YAML (in
+ # which case it would match the first pattern, which splits the string
+ # into container and method components and looks up both) or a filename
+ # (in which case it would match the last pattern, which just checks
+ # whether the string as a whole is a known symbol).
+
+ 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
+ end
+
+ if !ref and
+ /([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
+ ref = @context.find_symbol lookup unless ref
+
out = if lookup =~ /^\\/ then
$'
elsif ref and ref.document_self then
diff --git a/lib/rdoc/markup/to_texinfo.rb b/lib/rdoc/markup/to_texinfo.rb
new file mode 100644
index 0000000000..533d3e34a0
--- /dev/null
+++ b/lib/rdoc/markup/to_texinfo.rb
@@ -0,0 +1,69 @@
+require 'rdoc/markup/formatter'
+require 'rdoc/markup/fragments'
+require 'rdoc/markup/inline'
+
+require 'rdoc/markup'
+require 'rdoc/markup/formatter'
+
+##
+# Convert SimpleMarkup to basic TexInfo format
+#
+# TODO: WTF is AttributeManager for?
+#
+class RDoc::Markup::ToTexInfo < RDoc::Markup::Formatter
+
+ def start_accepting
+ @text = []
+ end
+
+ def end_accepting
+ @text.join("\n")
+ end
+
+ def accept_paragraph(attributes, text)
+ @text << format(text)
+ end
+
+ def accept_verbatim(attributes, text)
+ @text << "@verb{|#{format(text)}|}"
+ end
+
+ def accept_heading(attributes, text)
+ heading = ['@majorheading', '@chapheading'][text.head_level - 1] || '@heading'
+ @text << "#{heading}{#{format(text)}}"
+ end
+
+ def accept_list_start(attributes, text)
+ @text << '@itemize @bullet'
+ end
+
+ def accept_list_end(attributes, text)
+ @text << '@end itemize'
+ end
+
+ def accept_list_item(attributes, text)
+ @text << "@item\n#{format(text)}"
+ end
+
+ def accept_blank_line(attributes, text)
+ @text << "\n"
+ end
+
+ def accept_rule(attributes, text)
+ @text << '-----'
+ end
+
+ def format(text)
+ text.txt.
+ gsub(/@/, "@@").
+ gsub(/\{/, "@{").
+ gsub(/\}/, "@}").
+ # gsub(/,/, "@,"). # technically only required in cross-refs
+ gsub(/\+([\w]+)\+/, "@code{\\1}").
+ gsub(/\<tt\>([^<]+)\<\/tt\>/, "@code{\\1}").
+ gsub(/\*([\w]+)\*/, "@strong{\\1}").
+ gsub(/\<b\>([^<]+)\<\/b\>/, "@strong{\\1}").
+ gsub(/_([\w]+)_/, "@emph{\\1}").
+ gsub(/\<em\>([^<]+)\<\/em\>/, "@emph{\\1}")
+ end
+end