markup_code()
public
Turns the method’s token stream into HTML.
Prepends line numbers if add_line_numbers is
true.
Show source
def markup_code
return '' unless @token_stream
src = ""
@token_stream.each do |t|
next unless t
style = case t
when RDoc::RubyToken::TkCONSTANT then 'ruby-constant'
when RDoc::RubyToken::TkKW then 'ruby-keyword'
when RDoc::RubyToken::TkIVAR then 'ruby-ivar'
when RDoc::RubyToken::TkOp then 'ruby-operator'
when RDoc::RubyToken::TkId then 'ruby-identifier'
when RDoc::RubyToken::TkNode then 'ruby-node'
when RDoc::RubyToken::TkCOMMENT then 'ruby-comment'
when RDoc::RubyToken::TkREGEXP then 'ruby-regexp'
when RDoc::RubyToken::TkSTRING then 'ruby-string'
when RDoc::RubyToken::TkVal then 'ruby-value'
end
text = CGI.escapeHTML t.text
if style then
src << "<span class=\"#{style}\">#{text}</span>"
else
src << text
end
end
indent = src.length
lines = src.lines.to_a
lines.shift if src =~ /\A.*#\ *File/
lines.each do |line|
if line =~ /^ *(?=\S)/
n = $&.length
indent = n if n < indent
break if n == 0
end
end
src.gsub!(/^#{' ' * indent}/, '') if indent > 0
add_line_numbers(src) if self.class.add_line_numbers
src
end