Class: RstFilter::Rewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/rstfilter/rewriter.rb

Instance Method Summary collapse

Constructor Details

#initialize(opt) ⇒ Rewriter

Returns a new instance of Rewriter.



149
150
151
# File 'lib/rstfilter/rewriter.rb', line 149

def initialize opt
  @opt = opt
end

Instance Method Details

#rewrite(src, filename) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rstfilter/rewriter.rb', line 153

def rewrite src, filename
  # check syntax
  prev_v, $VERBOSE = $VERBOSE, false
  ast = RubyVM::AbstractSyntaxTree.parse(src)
  $VERBOSE = prev_v
  last_lineno = ast.last_lineno

  # rewrite
  src           = src.lines[0..last_lineno].join # remove __END__ and later
  ast, comments = Parser::CurrentRuby.parse_with_comments(src)
  buffer        = Parser::Source::Buffer.new('(example)')
  buffer.source = src
  rewriter      = RecordAll.new @opt
  mod_src       = rewriter.rewrite(buffer, ast)

  if @opt.verbose
    pp ast
    puts "     #{(0...80).map{|i| i%10}.join}"
    puts mod_src.lines.map.with_index{|line, i| '%4d:%s' % [i+1, line] }
  end

  return src, mod_src, comments
end