Class: RDoc::AnyMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/sdoc/generator.rb

Constant Summary collapse

TITLE_AFTER =
%w(def class module)

Instance Method Summary collapse

Instance Method Details

#sdoc_markup_codeObject

Turns the method’s token stream into HTML.

Prepends line numbers if add_line_numbers is true.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sdoc/generator.rb', line 37

def sdoc_markup_code
  return '' unless @token_stream

  src = ""
  starting_title = false

  @token_stream.each do |t|
    next unless t

    style = case t
            when RDoc::RubyToken::TkFLOAT    then 'ruby-number'
            when RDoc::RubyToken::TkINTEGER  then 'ruby-number'
            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

    if RDoc::RubyToken::TkId === t && starting_title
      starting_title = false
      style = 'ruby-keyword ruby-title'
    end

    if RDoc::RubyToken::TkKW === t && TITLE_AFTER.include?(t.text)
      starting_title = true
    end

    text = CGI.escapeHTML t.text

    if style then
      src << "<span class=\"#{style}\">#{text}</span>"
    else
      src << text
    end
  end

  # dedent the source
  indent = src.length
  lines = src.lines.to_a
  lines.shift if src =~ /\A.*#\ *File/i # remove '# File' comment
  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