Methods for manipulating comment text
ConstantsMaps markup formats to classes that can parse them. If the format is unknown, “rdoc” format is used.
Character class to be separated by a space when concatenating lines.
Maps an encoding to a Hash of characters properly transcoded for that encoding.
See also encode_fallback.
The language for this text. This affects stripping comments markers.
Public Class Methods Sourcedef self.encode_fallback(character, encoding, fallback) character.encode(encoding, :fallback => { character => fallback }, :undef => :replace, :replace => fallback) end
Transcodes character
to encoding
with a fallback
character.
def expand_tabs(text) expanded = [] text.each_line do |line| nil while line.gsub!(/(?:\G|\r)((?:.{8})*?)([^\t\r\n]{0,7})\t/) do r = "#{$1}#{$2}#{' ' * (8 - $2.size)}" r = RDoc::Encoding.change_encoding r, text.encoding r end expanded << line end expanded.join end
Expands tab characters in text
to eight spaces
def flush_left(text) indent = 9999 text.each_line do |line| line_indent = line =~ /\S/ || 9999 indent = line_indent if indent > line_indent end empty = '' empty = RDoc::Encoding.change_encoding empty, text.encoding text.gsub(/^ {0,#{indent}}/, empty) end
Flush text
left based on the shortest line
def markup(text) if @store.options locale = @store.options.locale else locale = nil end if locale i18n_text = RDoc::I18n::Text.new(text) text = i18n_text.translate(locale) end parse(text).accept formatter end
Convert a string in markup format into HTML.
Requires the including class to implement formatter
Sourcedef parse(text, format = 'rdoc') return text if RDoc::Markup::Document === text return text.parse if RDoc::Comment === text text = normalize_comment text return RDoc::Markup::Document.new if text =~ /\A\n*\z/ MARKUP_FORMAT[format].parse text end
Normalizes text
then builds a RDoc::Markup::Document
from it
def snippet(text, limit = 100) document = parse text RDoc::Markup::ToHtmlSnippet.new(options, limit).convert document end
The first limit
characters of text
as HTML
def strip_hashes(text) return text if text =~ /^(?>\s*)[^\#]/ empty = '' empty = RDoc::Encoding.change_encoding empty, text.encoding text.gsub(/^\s*(#+)/) { $1.tr '#', ' ' }.gsub(/^\s+$/, empty) end
Strips leading # characters from text
def strip_newlines(text) text.gsub(/\A\n*(.*?)\n*\z/m) do $1 end end
Strips leading and trailing n characters from text
def strip_stars(text) return text unless text =~ %r%/\*.*\*/%m encoding = text.encoding text = text.gsub %r%Document-method:\s+[\w:.#=!?|^&<>~+\-/*\%@`\[\]]+%, '' space = ' ' space = RDoc::Encoding.change_encoding space, encoding if encoding text.sub! %r%/\*+% do space * $&.length end text.sub! %r%\*+/% do space * $&.length end text.gsub! %r%^[ \t]*\*%m do space * $&.length end empty = '' empty = RDoc::Encoding.change_encoding empty, encoding if encoding text.gsub(/^\s+$/, empty) end
Strips /* */ style comments
Sourcedef to_html(text) html = (''.encode text.encoding).dup encoded = RDoc::Text::TO_HTML_CHARACTERS[text.encoding] s = StringScanner.new text insquotes = false indquotes = false after_word = nil until s.eos? do case when s.scan(/<(tt|code)>.*?<\/\1>/) then html << s.matched.gsub('\\\\', '\\') when s.scan(/<(tt|code)>.*?/) then warn "mismatched <#{s[1]}> tag" html << s.matched when s.scan(/<[^>]+\/?s*>/) then html << s.matched when s.scan(/\\(\S)/) then html << s[1] after_word = nil when s.scan(/\.\.\.(\.?)/) then html << s[1] << encoded[:ellipsis] after_word = nil when s.scan(/\(c\)/i) then html << encoded[:copyright] after_word = nil when s.scan(/\(r\)/i) 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 html << encoded[:open_dquote] after_word = nil when s.scan(/(?:'|'){2}/) then html << encoded[:close_dquote] after_word = nil when s.scan(/`/) then if insquotes or after_word html << '`' after_word = false else html << encoded[:open_squote] insquotes = true end when s.scan(/'|'/) then if insquotes html << encoded[:close_squote] insquotes = false elsif after_word html << encoded[:close_squote] else html << encoded[:open_squote] insquotes = true end after_word = nil else match = s.scan(/.+?(?=[<\\.("'`&-])/) if match then html << match after_word = match =~ /\w$/ else html << s.rest break end end end html end
Converts ampersand, dashes, ellipsis, quotes, copyright and registered trademark symbols in text
to properly encoded characters.
def wrap(txt, line_len = 76) res = [] sp = 0 ep = txt.length while sp < ep 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
Wraps txt
to line_len
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4