Class: PDD::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/pdd/source.rb

Overview

Source.

Instance Method Summary collapse

Constructor Details

#initialize(file, path) ⇒ Source

Ctor.

file

Absolute file name with source code

path

Path to show (without full file name)



31
32
33
34
# File 'lib/pdd/source.rb', line 31

def initialize(file, path)
  @file = file
  @path = path
end

Instance Method Details

#puzzlesObject

Fetch all puzzles.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pdd/source.rb', line 37

def puzzles
  PDD.log.info "Reading #{@path}..."
  puzzles = []
  lines = File.readlines(@file)
  lines.each_with_index do |line, idx|
    begin
      /[^\s]@todo/.match(line) do |_|
        raise Error, 'TODO must have a leading space to become a puzzle,
as this page explains: https://github.com/yegor256/pdd#how-to-format'
      end
      /@todo(?!\s+#)/.match(line) do |_|
        raise Error, "@todo found, but puzzle can't be parsed, \
most probably because TODO is not followed by a puzzle marker, as this page \
explains: https://github.com/yegor256/pdd#how-to-format"
      end
      %r{(.*(?:^|\s))@todo\s+#([\w\-\.:/]+)\s+(.+)}.match(line) do |match|
        puzzles << puzzle(lines.drop(idx + 1), match, idx)
      end
    rescue Error, ArgumentError => ex
      raise Error, "puzzle at line ##{idx + 1}; #{ex.message}"
    end
  end
  puzzles
end