Class: Markita::Preprocess

Inherits:
Object
  • Object
show all
Defined in:
lib/markita/preprocess.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Preprocess

Returns a new instance of Preprocess.



3
4
5
6
# File 'lib/markita/preprocess.rb', line 3

def initialize(file)
  @file = file.is_a?(String)? StringIO.new(file) : file
  @regx = @template = nil
end

Instance Method Details

#getsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/markita/preprocess.rb', line 8

def gets
  while (line=@file.gets)
    case line
    when @regx
      line = @template if @template
      $~.named_captures.each do |name, value|
        line = line.gsub("&#{name.downcase};", value)
        line = line.gsub("&#{name.upcase};", CGI.escape(value))
      end
      return line
    when %r{^! regx = /(.*)/$}
      @regx = Regexp.new $1
    when %r{^! template = "(.*)"$}
      @template = $1+"\n"
    else
      @regx &&= (@template=nil)
      return line
    end
  end
  nil
end