class Deanswerify::Parser

Public Class Methods

parse_files(list) click to toggle source
# 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) click to toggle source

Parses a single file, removing all text between answer blocks

@param text [String] input from file

# 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