Class: Pangrid::RedditBlank
- Includes:
- RedditWriter
- Defined in:
- lib/pangrid/plugins/reddit.rb
Constant Summary collapse
- DESCRIPTION =
"Reddit format (with blank squares)"
Constants inherited from Plugin
Plugin::FAILED, Plugin::MISSING_DEPS, Plugin::REGISTRY
Instance Method Summary collapse
- #grid(xw) ⇒ Object
- #is_grid_row(line) ⇒ Object
- #read(data) ⇒ Object
-
#strip_clues(nums, clues) ⇒ Object
make a best-attempt effort to strip off the clue number.
- #write(xw) ⇒ Object
Methods included from RedditWriter
#format_clues, #write_clues, #write_line, #write_table, #write_xw
Methods inherited from Plugin
class_to_name, get, inherited, list_all, load_all, load_plugin
Methods included from PluginUtils
Instance Method Details
#grid(xw) ⇒ Object
137 138 139 140 141 |
# File 'lib/pangrid/plugins/reddit.rb', line 137 def grid(xw) xw.to_array({:black => '*.*', :null => ' '}) do |c| c.number ? "^#{c.number}" : '' end end |
#is_grid_row(line) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/pangrid/plugins/reddit.rb', line 59 def is_grid_row(line) line = line.gsub(/\s/, '') ix = line.index('|') return false unless ix c = line[0...ix] c == "" || c == "*.*" || c =~ /^(\^\d+)?[[:alpha:]]*$/ # allow partially filled grids end |
#read(data) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/pangrid/plugins/reddit.rb', line 82 def read(data) xw = XWord.new lines = data.lines.map(&:chomp) # split input into grid and clues # # grid format is from the reddit table markup: # cell|cell|... # table header; first row for us # --|--|... # alignment markers; drop this # cell|cell|... # rest of xword # # we have to be careful about leading/trailing |s ix = lines.find_index {|row| row =~ /^[|\s-]+$/} check("Could not find grid") { not ix.nil? } width = lines[ix].gsub(/\s/, '').split('|').reject(&:empty?).length lines = [lines[(ix - 1)]] + lines[(ix + 1) .. -1] grid = lines.take_while {|i| is_grid_row(i)} clues = lines[grid.length .. -1] clues = lines.reject {|i| i.strip.empty?} # strip leading |s grid = grid.map {|line| line.sub(/^\|/, '')} grid = grid.map {|line| line.split("|", -1)[0..(width-1)]} grid = grid.map {|line| line.map(&:strip)} xw.width = grid[0].length xw.height = grid.length xw.solution = [] check("Grid is not rectangular") { grid.all? {|i| i.length == xw.width} } grid = grid.map do |row| xw.solution << row.map do |c| cell = Cell.new if c == '*.*' cell.solution = :black elsif c =~ /[[:alpha:]]/ cell.solution = c.gsub(/[^[:alpha:]]/, '') else cell.solution = :null end cell end end # Clues across, down = xw.number aix = clues.find_index {|row| row =~ /^\W*across/i} dix = clues.find_index {|row| row =~ /^\W*down/i} xw.across_clues = clues[(aix + 1) .. (aix + across.length)] xw.down_clues = clues[(dix + 1) .. (dix + down.length)] xw.across_clues = strip_clues(across, xw.across_clues) xw.down_clues = strip_clues(down, xw.down_clues) xw end |
#strip_clues(nums, clues) ⇒ Object
make a best-attempt effort to strip off the clue number
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/pangrid/plugins/reddit.rb', line 68 def strip_clues(nums, clues) nums.zip(clues).each_with_index.map do |(n, cl), i| b = cl.index(n.to_s) if b e = b + n.to_s.length if cl[0...b] =~ /\W*/ and cl[e + 1] =~ /\W/ cl[e+1 .. -1].sub(/^\W*\s/, '').strip else cl.strip end end end end |
#write(xw) ⇒ Object
55 56 57 |
# File 'lib/pangrid/plugins/reddit.rb', line 55 def write(xw) write_xw(xw) end |