Module: RDoc::Text
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
Alias,
AnyMethod,
Attr,
ClassModule,
CodeObject,
Comment,
Constant,
Context,
Context::Section,
Extend,
Generator::JsonIndex,
Include,
Markup::Parser,
Markup::ToHtml,
Markup::ToHtmlCrossref,
Markup::ToHtmlSnippet,
MethodAttr,
Mixin,
NormalClass,
NormalModule,
Parser::C,
Require,
SingleClass,
Stats,
TomDoc,
TopLevel
| |
| Defined in: | lib/rdoc/text.rb |
Overview
Methods for manipulating comment text
Constant Summary
-
MARKUP_FORMAT =
# File 'lib/rdoc/text.rb', line 23
Maps markup formats to classes that can parse them. If the format is unknown, "rdoc" format is used.
{ 'markdown' => RDoc::Markdown, 'rdoc' => RDoc::Markup, 'rd' => RDoc::RD, 'tomdoc' => RDoc::TomDoc, } -
SPACE_SEPARATED_LETTER_CLASS =
# File 'lib/rdoc/text.rb', line 204
Character class to be separated by a space when concatenating lines.
/[\p{Nd}\p{Lc}\p{Pc}]|[!-~&&\W]/
Instance Attribute Summary
-
#language
rw
The language for this text.
Instance Method Summary
-
#flush_left(text)
Flush
textleft based on the shortest line. -
#markup(text)
Convert a string in markup format into HTML.
-
#normalize_comment(text)
Strips hashes, expands tabs then flushes
textto the left. -
#parse(text, format = 'rdoc')
Normalizes
textthen builds aMarkup::Documentfrom it. -
#snippet(text, limit = 100)
The first
limitcharacters oftextas HTML. -
#strip_hashes(text)
Strips leading # characters from
text -
#strip_newlines(text)
Strips leading and trailing \n characters from
text -
#strip_stars(text)
Strips /* */ style comments.
-
#wrap(txt, line_len = 76)
Wraps
txttoline_len
Instance Attribute Details
#language (rw)
The language for this text. This affects stripping comments markers.
# File 'lib/rdoc/text.rb', line 17
attr_accessor :language
Instance Method Details
#flush_left(text)
Flush text left based on the shortest line
#markup(text)
Convert a string in markup format into HTML.
Requires the including class to implement #formatter
#normalize_comment(text)
Strips hashes, expands tabs then flushes text to the left
# File 'lib/rdoc/text.rb', line 89
def normalize_comment(text) return text if text.empty? case language when :ruby text = strip_hashes text when :c text = strip_stars text end text = text text = flush_left text text = strip_newlines text text end
#parse(text, format = 'rdoc')
Normalizes text then builds a Markup::Document from it
# File 'lib/rdoc/text.rb', line 107
def parse(text, format = 'rdoc') return text if RDoc::Markup::Document === text return text.parse if RDoc::Comment === text text = normalize_comment text # TODO remove, should not be necessary return RDoc::Markup::Document.new if text =~ /\A\n*\z/ MARKUP_FORMAT[format].parse text end
#snippet(text, limit = 100)
The first limit characters of text as HTML
#strip_hashes(text)
Strips leading # characters from text
#strip_newlines(text)
Strips leading and trailing \n characters from text
# File 'lib/rdoc/text.rb', line 142
def strip_newlines(text) text.gsub(/\A\n*(.*?)\n*\z/m) do $1 end # block preserves String encoding end
#strip_stars(text)
Strips /* */ style comments
# File 'lib/rdoc/text.rb', line 149
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
#wrap(txt, line_len = 76)
Wraps txt to line_len
# File 'lib/rdoc/text.rb', line 171
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