2017-11-27 05:45:24 -05:00
|
|
|
|
# frozen_string_literal: true
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# For RDoc::Text#to_html
|
|
|
|
|
|
|
|
|
|
require 'strscan'
|
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
|
##
|
|
|
|
|
# Methods for manipulating comment text
|
|
|
|
|
|
|
|
|
|
module RDoc::Text
|
|
|
|
|
|
2019-01-21 14:46:46 -05:00
|
|
|
|
attr_accessor :language
|
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
|
##
|
|
|
|
|
# Maps markup formats to classes that can parse them. If the format is
|
|
|
|
|
# unknown, "rdoc" format is used.
|
|
|
|
|
|
|
|
|
|
MARKUP_FORMAT = {
|
|
|
|
|
'markdown' => RDoc::Markdown,
|
|
|
|
|
'rdoc' => RDoc::Markup,
|
|
|
|
|
'rd' => RDoc::RD,
|
|
|
|
|
'tomdoc' => RDoc::TomDoc,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MARKUP_FORMAT.default = RDoc::Markup
|
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
|
##
|
|
|
|
|
# Maps an encoding to a Hash of characters properly transcoded for that
|
|
|
|
|
# encoding.
|
|
|
|
|
#
|
|
|
|
|
# See also encode_fallback.
|
|
|
|
|
|
|
|
|
|
TO_HTML_CHARACTERS = Hash.new do |h, encoding|
|
|
|
|
|
h[encoding] = {
|
|
|
|
|
:close_dquote => encode_fallback('”', encoding, '"'),
|
|
|
|
|
:close_squote => encode_fallback('’', encoding, '\''),
|
|
|
|
|
:copyright => encode_fallback('©', encoding, '(c)'),
|
|
|
|
|
:ellipsis => encode_fallback('…', encoding, '...'),
|
|
|
|
|
:em_dash => encode_fallback('—', encoding, '---'),
|
|
|
|
|
:en_dash => encode_fallback('–', encoding, '--'),
|
|
|
|
|
:open_dquote => encode_fallback('“', encoding, '"'),
|
|
|
|
|
:open_squote => encode_fallback('‘', encoding, '\''),
|
|
|
|
|
:trademark => encode_fallback('®', encoding, '(r)'),
|
|
|
|
|
}
|
2016-09-07 18:23:38 -04:00
|
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Transcodes +character+ to +encoding+ with a +fallback+ character.
|
|
|
|
|
|
|
|
|
|
def self.encode_fallback character, encoding, fallback
|
|
|
|
|
character.encode(encoding, :fallback => { character => fallback },
|
|
|
|
|
:undef => :replace, :replace => fallback)
|
|
|
|
|
end
|
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
|
##
|
|
|
|
|
# Expands tab characters in +text+ to eight spaces
|
|
|
|
|
|
|
|
|
|
def expand_tabs text
|
|
|
|
|
expanded = []
|
|
|
|
|
|
|
|
|
|
text.each_line do |line|
|
2015-01-12 04:01:28 -05:00
|
|
|
|
nil while line.gsub!(/(?:\G|\r)((?:.{8})*?)([^\t\r\n]{0,7})\t/) do
|
2011-02-07 02:07:12 -05:00
|
|
|
|
r = "#{$1}#{$2}#{' ' * (8 - $2.size)}"
|
2017-11-27 05:45:24 -05:00
|
|
|
|
r = RDoc::Encoding.change_encoding r, text.encoding
|
2011-02-07 02:07:12 -05:00
|
|
|
|
r
|
2015-01-12 04:01:28 -05:00
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
|
|
expanded << line
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
expanded.join
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Flush +text+ left based on the shortest line
|
|
|
|
|
|
|
|
|
|
def flush_left text
|
2011-05-13 20:39:16 -04:00
|
|
|
|
indent = 9999
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
|
|
text.each_line do |line|
|
2011-05-13 20:39:16 -04:00
|
|
|
|
line_indent = line =~ /\S/ || 9999
|
|
|
|
|
indent = line_indent if indent > line_indent
|
2010-04-01 03:45:16 -04:00
|
|
|
|
end
|
|
|
|
|
|
2011-02-07 02:07:12 -05:00
|
|
|
|
empty = ''
|
2017-11-27 05:45:24 -05:00
|
|
|
|
empty = RDoc::Encoding.change_encoding empty, text.encoding
|
2011-02-07 02:07:12 -05:00
|
|
|
|
|
2011-05-13 20:39:16 -04:00
|
|
|
|
text.gsub(/^ {0,#{indent}}/, empty)
|
2010-04-01 03:45:16 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
|
# Convert a string in markup format into HTML.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
#
|
|
|
|
|
# Requires the including class to implement #formatter
|
|
|
|
|
|
|
|
|
|
def markup text
|
2014-09-04 21:41:25 -04:00
|
|
|
|
if @store.rdoc.options
|
|
|
|
|
locale = @store.rdoc.options.locale
|
|
|
|
|
else
|
|
|
|
|
locale = nil
|
|
|
|
|
end
|
|
|
|
|
if locale
|
|
|
|
|
i18n_text = RDoc::I18n::Text.new(text)
|
|
|
|
|
text = i18n_text.translate(locale)
|
|
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
|
parse(text).accept formatter
|
2010-04-01 03:45:16 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Strips hashes, expands tabs then flushes +text+ to the left
|
|
|
|
|
|
|
|
|
|
def normalize_comment text
|
|
|
|
|
return text if text.empty?
|
|
|
|
|
|
2019-01-21 14:46:46 -05:00
|
|
|
|
case language
|
|
|
|
|
when :ruby
|
|
|
|
|
text = strip_hashes text
|
|
|
|
|
when :c
|
|
|
|
|
text = strip_stars text
|
|
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
|
text = expand_tabs text
|
|
|
|
|
text = flush_left text
|
2011-02-01 19:32:30 -05:00
|
|
|
|
text = strip_newlines text
|
|
|
|
|
text
|
2010-04-01 03:45:16 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Normalizes +text+ then builds a RDoc::Markup::Document from it
|
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
|
def parse text, format = 'rdoc'
|
2010-04-01 03:45:16 -04:00
|
|
|
|
return text if RDoc::Markup::Document === text
|
2012-11-26 23:28:14 -05:00
|
|
|
|
return text.parse if RDoc::Comment === text
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
|
text = normalize_comment text # TODO remove, should not be necessary
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
|
|
return RDoc::Markup::Document.new if text =~ /\A\n*\z/
|
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
|
MARKUP_FORMAT[format].parse text
|
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
|
##
|
|
|
|
|
# The first +limit+ characters of +text+ as HTML
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
|
def snippet text, limit = 100
|
|
|
|
|
document = parse text
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
|
RDoc::Markup::ToHtmlSnippet.new(options, limit).convert document
|
2010-04-01 03:45:16 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Strips leading # characters from +text+
|
|
|
|
|
|
|
|
|
|
def strip_hashes text
|
|
|
|
|
return text if text =~ /^(?>\s*)[^\#]/
|
2011-02-01 19:32:30 -05:00
|
|
|
|
|
|
|
|
|
empty = ''
|
2017-11-27 05:45:24 -05:00
|
|
|
|
empty = RDoc::Encoding.change_encoding empty, text.encoding
|
2011-02-01 19:32:30 -05:00
|
|
|
|
|
|
|
|
|
text.gsub(/^\s*(#+)/) { $1.tr '#', ' ' }.gsub(/^\s+$/, empty)
|
2010-04-01 03:45:16 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Strips leading and trailing \n characters from +text+
|
|
|
|
|
|
|
|
|
|
def strip_newlines text
|
2011-02-01 19:32:30 -05:00
|
|
|
|
text.gsub(/\A\n*(.*?)\n*\z/m) do $1 end # block preserves String encoding
|
2010-04-01 03:45:16 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Strips /* */ style comments
|
|
|
|
|
|
|
|
|
|
def strip_stars text
|
2012-11-26 23:28:14 -05:00
|
|
|
|
return text unless text =~ %r%/\*.*\*/%m
|
|
|
|
|
|
2016-09-07 18:23:38 -04:00
|
|
|
|
encoding = text.encoding
|
2011-02-07 02:07:12 -05:00
|
|
|
|
|
2018-03-27 23:18:00 -04:00
|
|
|
|
text = text.gsub %r%Document-method:\s+[\w:.#=!?|^&<>~+\-/*\%@`\[\]]+%, ''
|
2011-02-07 02:07:12 -05:00
|
|
|
|
|
|
|
|
|
space = ' '
|
2017-11-27 05:45:24 -05:00
|
|
|
|
space = RDoc::Encoding.change_encoding space, encoding if encoding
|
2011-02-07 02:07:12 -05:00
|
|
|
|
|
|
|
|
|
text.sub! %r%/\*+% do space * $&.length end
|
|
|
|
|
text.sub! %r%\*+/% do space * $&.length end
|
|
|
|
|
text.gsub! %r%^[ \t]*\*%m do space * $&.length end
|
|
|
|
|
|
|
|
|
|
empty = ''
|
2017-11-27 05:45:24 -05:00
|
|
|
|
empty = RDoc::Encoding.change_encoding empty, encoding if encoding
|
2011-02-07 02:07:12 -05:00
|
|
|
|
text.gsub(/^\s+$/, empty)
|
2010-12-19 22:22:49 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Converts ampersand, dashes, ellipsis, quotes, copyright and registered
|
|
|
|
|
# trademark symbols in +text+ to properly encoded characters.
|
|
|
|
|
|
|
|
|
|
def to_html text
|
2017-11-27 05:45:24 -05:00
|
|
|
|
html = (''.encode text.encoding).dup
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
2016-09-07 18:23:38 -04:00
|
|
|
|
encoded = RDoc::Text::TO_HTML_CHARACTERS[text.encoding]
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
|
|
s = StringScanner.new text
|
|
|
|
|
insquotes = false
|
|
|
|
|
indquotes = false
|
|
|
|
|
after_word = nil
|
|
|
|
|
|
|
|
|
|
until s.eos? do
|
|
|
|
|
case
|
2012-11-26 23:28:14 -05:00
|
|
|
|
when s.scan(/<(tt|code)>.*?<\/\1>/) then # skip contents of tt
|
2010-12-19 22:22:49 -05:00
|
|
|
|
html << s.matched.gsub('\\\\', '\\')
|
2012-11-26 23:28:14 -05:00
|
|
|
|
when s.scan(/<(tt|code)>.*?/) then
|
|
|
|
|
warn "mismatched <#{s[1]}> tag" # TODO signal file/line
|
2010-12-19 22:22:49 -05:00
|
|
|
|
html << s.matched
|
|
|
|
|
when s.scan(/<[^>]+\/?s*>/) then # skip HTML tags
|
|
|
|
|
html << s.matched
|
|
|
|
|
when s.scan(/\\(\S)/) then # unhandled suppressed crossref
|
|
|
|
|
html << s[1]
|
|
|
|
|
after_word = nil
|
|
|
|
|
when s.scan(/\.\.\.(\.?)/) then
|
|
|
|
|
html << s[1] << encoded[:ellipsis]
|
|
|
|
|
after_word = nil
|
|
|
|
|
when s.scan(/\(c\)/) then
|
|
|
|
|
html << encoded[:copyright]
|
|
|
|
|
after_word = nil
|
|
|
|
|
when s.scan(/\(r\)/) then
|
|
|
|
|
html << encoded[:trademark]
|
|
|
|
|
after_word = nil
|
|
|
|
|
when s.scan(/---/) then
|
|
|
|
|
html << encoded[:em_dash]
|
|
|
|
|
after_word = nil
|
|
|
|
|
when s.scan(/--/) then
|
|
|
|
|
html << encoded[:en_dash]
|
|
|
|
|
after_word = nil
|
|
|
|
|
when s.scan(/"|"/) then
|
|
|
|
|
html << encoded[indquotes ? :close_dquote : :open_dquote]
|
|
|
|
|
indquotes = !indquotes
|
|
|
|
|
after_word = nil
|
|
|
|
|
when s.scan(/``/) then # backtick double quote
|
|
|
|
|
html << encoded[:open_dquote]
|
|
|
|
|
after_word = nil
|
|
|
|
|
when s.scan(/''/) then # tick double quote
|
|
|
|
|
html << encoded[:close_dquote]
|
|
|
|
|
after_word = nil
|
|
|
|
|
when s.scan(/'/) then # single quote
|
|
|
|
|
if insquotes
|
|
|
|
|
html << encoded[:close_squote]
|
|
|
|
|
insquotes = false
|
|
|
|
|
elsif after_word
|
|
|
|
|
# Mary's dog, my parents' house: do not start paired quotes
|
|
|
|
|
html << encoded[:close_squote]
|
|
|
|
|
else
|
|
|
|
|
html << encoded[:open_squote]
|
|
|
|
|
insquotes = true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
after_word = nil
|
|
|
|
|
else # advance to the next potentially significant character
|
|
|
|
|
match = s.scan(/.+?(?=[<\\.("'`&-])/) #"
|
|
|
|
|
|
|
|
|
|
if match then
|
|
|
|
|
html << match
|
|
|
|
|
after_word = match =~ /\w$/
|
|
|
|
|
else
|
|
|
|
|
html << s.rest
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
html
|
2010-04-01 03:45:16 -04:00
|
|
|
|
end
|
|
|
|
|
|
2011-02-05 01:20:57 -05:00
|
|
|
|
##
|
|
|
|
|
# Wraps +txt+ to +line_len+
|
|
|
|
|
|
|
|
|
|
def wrap(txt, line_len = 76)
|
|
|
|
|
res = []
|
|
|
|
|
sp = 0
|
|
|
|
|
ep = txt.length
|
|
|
|
|
|
|
|
|
|
while sp < ep
|
|
|
|
|
# scan back for a space
|
|
|
|
|
p = sp + line_len - 1
|
|
|
|
|
if p >= ep
|
|
|
|
|
p = ep
|
|
|
|
|
else
|
|
|
|
|
while p > sp and txt[p] != ?\s
|
|
|
|
|
p -= 1
|
|
|
|
|
end
|
|
|
|
|
if p <= sp
|
|
|
|
|
p = sp + line_len
|
|
|
|
|
while p < ep and txt[p] != ?\s
|
|
|
|
|
p += 1
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
res << txt[sp...p] << "\n"
|
|
|
|
|
sp = p
|
|
|
|
|
sp += 1 while sp < ep and txt[sp] == ?\s
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
res.join.strip
|
|
|
|
|
end
|
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
|
end
|