Class: Code2rubylearning::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/code2rubylearning/filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(current_file, options) ⇒ Filter

Returns a new instance of Filter.



3
4
5
6
7
8
9
# File 'lib/code2rubylearning/filter.rb', line 3

def initialize current_file, options
  @current_file = current_file
  @name = current_file.name
  @data = current_file.data
  @type = current_file.type
  @options = options
end

Instance Method Details

#applyObject



11
12
13
# File 'lib/code2rubylearning/filter.rb', line 11

def apply
  header(@name) << convert(@data) << footer
end

#convert(source) ⇒ Object



37
38
39
40
41
# File 'lib/code2rubylearning/filter.rb', line 37

def convert source
  source = source.gsub('<', '&lt;')
  source = source.gsub('>', '&gt;')
  source = source.gsub('Person', 'People')
end


33
34
35
# File 'lib/code2rubylearning/filter.rb', line 33

def footer
  "[/code]\n"
end

#header(file_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/code2rubylearning/filter.rb', line 15

def header file_name
  header = "[code #{ @type }#{@options[:linenumbers] ? ' linenumbers' : ''}]\n"
  if @options[:filenames]

    data_lines = @data.split("\n")

    if data_lines.first.include? "#!"
      bin_line = data_lines.first + "\n"
      header << bin_line
      @data = @data.gsub(bin_line, "")
    end

    header << "#filename: #{ file_name }\n"

  end
  header
end