Module: Pangrid::ExolveReader

Included in:
ExolveFilled
Defined in:
lib/pangrid/plugins/exolve.rb

Instance Method Summary collapse

Instance Method Details

#parse_grid(lines) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/pangrid/plugins/exolve.rb', line 97

def parse_grid(lines)
  grid = lines.map(&:strip).map {|x| x.split(//)}
  grid.map do |col|
    col.map do |c|
      Cell.new(:solution => parse_grid_char(c))
    end
  end
end

#parse_grid_char(char) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/pangrid/plugins/exolve.rb', line 89

def parse_grid_char(char)
  case char
  when '0'; :null
  when '.'; :black
  else; char
  end
end

#read(data) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pangrid/plugins/exolve.rb', line 45

def read(data)
  s = data.each_line.map(&:rstrip)
  first = s.index('exolve-begin')
  check("exolve-begin missing") { first }
  last = s.index('exolve-end')
  check("exolve-end missing") { last }
  lines = s[(first + 1)...last]
  xw = XWord.new
  s = sections(lines)
  s.each do |_, field, data|
    if %w(title setter copyright prelude).include? field
      xw[field] = data
    elsif %w(height width).include? field
      xw[field] = data.to_i
    elsif %(across down).include? field
      xw["#{field}_clues"] = data
    elsif field == "grid"
      xw.solution = parse_grid(data)
    end
  end
  xw
end

#sections(lines) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pangrid/plugins/exolve.rb', line 68

def sections(lines)
  headers = lines.each.with_index.map do |l, i|
    m = l.match(/^(\s+)exolve-(\w+):(.*)$/)
    if m
      _, indent, field, data = m.to_a
      [i, field, data.strip]
    else
      nil
    end
  end
  headers.compact!
  headers.push([lines.length, "", ""])
  headers.each_cons(2) do |i, j|
    if i[2].empty?
      i[2] = lines[(i[0] + 1)...j[0]].map(&:strip)
    end
  end
  headers.pop
  headers
end