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)



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

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

Instance Method Details

#puzzlesObject

Fetch all puzzles.



38
39
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
66
# File 'lib/pdd/source.rb', line 38

def puzzles
  re = /(.*(?:^|\s))@todo\s+#([\w\-\.:\/]+)\s+(.+)/
  puzzles = []
  lines = File.readlines(@file)
  lines.each_with_index do |line, idx|
    re.match(line) do |match|
      prefix = match[1] + ' '
      total = 0
      body = (match[3] + ' ' + lines.drop(idx + 1)
        .take_while { |txt| txt.start_with?(prefix) }
        .map { |txt| txt[prefix.length, txt.length] }
        .map do |txt|
          total += 1
          txt
        end
        .join(' '))
        .gsub(/\s+/, ' ')
        .strip
      puzzles << Puzzle.new(
        marker(match[2]).merge(
          lines: "#{idx + 1}-#{idx + total + 1}",
          body: body,
          file: @path
        )
      )
    end
  end
  puzzles
end