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)



35
36
37
38
# File 'lib/pdd/source.rb', line 35

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

Instance Method Details

#match_markers(line) ⇒ Object



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

def match_markers(line)
  if line.downcase.include? 'todo'
    /[^\s]\x40todo/.match(line) do |_|
      raise Error, get_no_leading_space_error("\x40todo")
    end
    /\x40todo(?!\s+#)/.match(line) do |_|
      raise Error, get_no_puzzle_marker_error("\x40todo")
    end
    /\x40todo\s+#\s/.match(line) do |_|
      raise Error, get_space_after_hash_error("\x40todo")
    end
    /[^\s]TODO:?/.match(line) do |_|
      raise Error, get_no_leading_space_error('TODO')
    end
    /TODO(?=[:\s])(?!:?\s+#)/.match(line) do |_|
      raise Error, get_no_puzzle_marker_error('TODO')
    end
    /TODO:?\s+#\s/.match(line) do |_|
      raise Error, get_space_after_hash_error('TODO')
    end
    a = [%r{(.*(?:^|\s))(?:\x40todo|TODO:|TODO)\s+#([\w\-.:/]+)\s+(.+)}.match(line)]
    a.compact
  else
    []
  end
end

#puzzlesObject

Fetch all puzzles.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pdd/source.rb', line 68

def puzzles
  PDD.log.info "Reading #{@path} ..."
  puzzles = []
  lines = File.readlines(@file, encoding: 'UTF-8')
  lines.each_with_index do |line, idx|
    begin
      match_markers(line).each do |m|
        puzzles << puzzle(lines.drop(idx + 1), m, idx)
      end
    rescue Error, ArgumentError => e
      message = "#{e.class} at #{@path}:#{idx + 1}: #{e.message}"
      raise Error, message unless PDD.opts && PDD.opts['skip-errors']
    end
  end
  puzzles
end