module Pangrid::ExolveReader
Public Instance Methods
parse_grid(lines)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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