Class: Deanswerify::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/deanswerify/parser.rb

Class Method Summary collapse

Class Method Details

.parse_files(list) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/deanswerify/parser.rb', line 3

def self.parse_files(list)
  list.each do |file_name|
    puts "Parsing #{file_name}..."

    text = File.read(file_name)
    text_new = Deanswerify::Parser.strip_answers(text)

    # Only write the file if the output is different from the input. This
    # prevents us from adding newlines to files that lack ending newlines but
    # are otherwise unchanged. Some might argue that's desirable behavior.
    #
    # No. Configure thine own editor.
    File.write(file_name, text_new) #unless text.eql?(text_new)
  end
end

.strip_answers(text) ⇒ Object

Parses a single file, removing all text between answer blocks

Parameters:

  • text (String)

    input from file



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/deanswerify/parser.rb', line 22

def self.strip_answers(text)
  regex = /[^\S\n]*\/\*\sSOLUTION\s\*\/.*?\/\*\sEND\sSOLUTION\s\*\/\s?/im
  replace = ""

  text = text.gsub(regex, replace)

  # Handle edge case where inline replacement eats whitespace up to brace.
  text = text.gsub(/{(\S)/, "{ \\1")
  text = text.gsub(/(\S)}/, "\\1 }")

  text
end