pangrid 0.7.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 261bff344f0e6be4956c7da0bc73c4e540efb6dfe4800bfc5e0afbe603699747
4
- data.tar.gz: edb0a9b238e36d16bc1694fd03b9a45072e7b48776c79ffef86f29980d4b0869
3
+ metadata.gz: 26b71de762051d1191be0a5b3037988256421a98322dfaf9143a2cad43ed4b66
4
+ data.tar.gz: 33959c884e1763829893c2cdd327d423abfbee0cd8f71514d92c08636702bf4e
5
5
  SHA512:
6
- metadata.gz: f830c6e2ae1fbab22f0a1cf974de2a76cc4fcd2f073a07ed416ddea568dddb16e7ba48cf5eaa9adb5b1a94828a4a51cf0bd6b9b54066f2a56786cdc5f75fb50c
7
- data.tar.gz: 8c98d9c2e9c0dc626da8bf794fd9ee31d357a16f6fd564f7e1a8a620dd0926cfe8afcc74046741712f2539259b29c04ae658ad873b17bb9e37abf5e860552db4
6
+ metadata.gz: '094eb49c802d2afc2b39229a6eaca1f308a162a0ce709f53ade24e06c9a7f6f36aa58da2baed638c98f0050e731c549c2cf278f03af2b12d20c35f2e02102f7c'
7
+ data.tar.gz: 3e86ed2ffcf0a22210772dd39b33adb50e8de3b0515ef4e31320a41209bbf35249038e796f083e0e3874b7b1e7ee7ab101ca3a619026ddb70fb39b3a53efba76
data/README CHANGED
@@ -8,7 +8,7 @@ $ pangrid -f across-lite-binary -t text -i sample.puz -o sample.txt
8
8
  # Add clues from a text file, e.g. to a grid from qxw, which has no clues
9
9
  $ pangrid -f qxw -t across-lite-binary -i grid.qxw -c clues.txt -o puzzle.puz
10
10
 
11
- # List available plugins, and the features (such as bars) each supports
11
+ # List available plugins, and which features (such as bars) each supports
12
12
  $ pangrid -l
13
13
 
14
14
 
@@ -43,7 +43,8 @@ The clues replace any that were already in the input file.
43
43
 
44
44
  Features:
45
45
 
46
- Not every format can represent everything a crossword can contain. Currently
47
- the only such feature is bars between squares, which the qxw and exolve
48
- formats support. If the input uses a feature the output format does not
49
- support, pangrid prints a warning and converts the rest of the crossword.
46
+ Not every format can represent everything a crossword can contain, such as
47
+ bars, circled or coloured squares, or clue explanations and hints. If the
48
+ input uses a feature the output format does not support, pangrid prints a
49
+ warning and converts the rest of the crossword. Run pangrid -l to see which
50
+ plugins support which features.
@@ -0,0 +1,137 @@
1
+ module Pangrid
2
+
3
+ # A reference to another clue, e.g. the "5" in a linked clue "1, 5 Clue".
4
+ # direction is :across, :down, or nil for the same direction as the clue
5
+ # containing the reference.
6
+ ClueRef = Struct.new(:number, :direction) do
7
+ def to_s
8
+ number.to_s + {across: "a", down: "d"}.fetch(direction, "")
9
+ end
10
+ end
11
+
12
+ # A marked part of a clue's text: length characters starting at start. With
13
+ # no style, it marks the definition; exolve also allows a CSS class, e.g.
14
+ # "xlv-blue", to mark other parts such as indicators.
15
+ ClueSpan = Struct.new(:start, :length, :style) do
16
+ def range
17
+ start...(start + length)
18
+ end
19
+ end
20
+
21
+ # A clue. Simple formats just use the clue text, via to_s; richer formats
22
+ # (exolve, ipuz) can fill in the other fields.
23
+ #
24
+ # number: the clue number, if known
25
+ # label: a label shown instead of the number (e.g. "F" for "[F] Clue")
26
+ # text: the clue itself, without number or enumeration
27
+ # enumeration: e.g. "4,3" or "5-2"
28
+ # explanation: shown after the puzzle is solved (exolve "anno")
29
+ # answer: the answer, if it isn't just the letters in the grid
30
+ # hints: hints, revealed one at a time
31
+ # concise: a shorter version of the clue
32
+ # linked: ClueRefs to the other clues in a linked clue ("1, 5, 2d")
33
+ # parent: for the later parts of a linked clue, a ClueRef to the
34
+ # first part, which holds the clue text
35
+ # references: ClueRefs to other clues this clue mentions
36
+ # cells: [x, y] squares of the answer, for clues not placed by
37
+ # numbering (e.g. exolve nodir clues, ipuz zones)
38
+ # spans: ClueSpans marking parts of the text, usually the definition
39
+ # (exolve ~{...}~). They index into text, so text stays plain
40
+ # and formats without markup can use it as it is; change the
41
+ # spans along with the text.
42
+ # tags, type, image, highlight: ipuz metadata
43
+ class Clue
44
+ FIELDS = %i(number label text enumeration explanation answer hints concise
45
+ linked parent references cells spans tags type image highlight)
46
+
47
+ attr_accessor(*FIELDS)
48
+
49
+ # Fields that default to (and are never nil instead of) an empty list
50
+ LISTS = %i(hints linked references cells spans tags)
51
+
52
+ # Enumerations follow the rules exolve uses. The usual kind is numeric:
53
+ # numbers, commas, hyphens, apostrophes, periods and spaces, e.g. "4,3",
54
+ # "5-2", "2'1", "4.3". An enumeration can also count words or letters
55
+ # ("8, 2 words", "two words", "7, 2w", "6 letters"), contain a "?", or say
56
+ # that the answer is an abbreviation or acronym.
57
+ NUMERIC_ENUM = /\A\d[\d,\-\s.'’]*\z/
58
+ ENUM_WORDS = {"words" => 1, "letters" => 1, "abbreviation" => 3, "acronym" => 3}
59
+ # A parenthesised group, optionally followed by * (exolve: hide the enum)
60
+ PAREN = /\(([^()]*)\)\*?/
61
+
62
+ # :numeric, :other, or nil if s (the text between the parentheses) is not
63
+ # an enumeration
64
+ def self.enumeration_kind(s)
65
+ s = s.strip
66
+ return :numeric if s.match?(NUMERIC_ENUM)
67
+ words = s.downcase.scan(/[[:alpha:]]+/)
68
+ wordy = words.any? do |w|
69
+ ENUM_WORDS.any? {|full, min| w.length >= min && full.start_with?(w)}
70
+ end
71
+ :other if wordy || s.include?("?")
72
+ end
73
+
74
+ # The enumeration in a clue, as MatchData for the parenthesised group (with
75
+ # the enumeration in m[1]), or nil. If there are several candidates, the
76
+ # last numeric one is used, so that "Ends (word) here (4)" has the
77
+ # enumeration 4; only if there is no numeric one is the last other kind
78
+ # used.
79
+ def self.find_enumeration(s)
80
+ candidates = s.to_enum(:scan, PAREN).map { Regexp.last_match }
81
+ kinds = candidates.map {|m| enumeration_kind(m[1])}
82
+ numeric = candidates.zip(kinds).select {|_, k| k == :numeric}.map(&:first)
83
+ other = candidates.zip(kinds).select {|_, k| k}.map(&:first)
84
+ (numeric.empty? ? other : numeric).last
85
+ end
86
+
87
+ # Text ending in an enumeration -> [text, enumeration], or nil
88
+ def self.split_trailing_enumeration(s)
89
+ m = s.match(/#{PAREN}\s*\z/)
90
+ [m.pre_match.rstrip, m[1].strip] if m && enumeration_kind(m[1])
91
+ end
92
+
93
+ def initialize(text = nil, **args)
94
+ @text = text
95
+ LISTS.each {|f| instance_variable_set(:"@#{f}", [])}
96
+ args.each {|k, v| public_send(:"#{k}=", v)}
97
+ end
98
+
99
+ # Clue | String | nil -> Clue | nil
100
+ def self.coerce(c)
101
+ c.is_a?(Clue) || c.nil? ? c : new(c.to_s)
102
+ end
103
+
104
+ # Split a trailing enumeration off a clue: "Feline (3)" -> text "Feline",
105
+ # enumeration "3"
106
+ def self.parse(s, **args)
107
+ s = s.to_s.strip
108
+ text, enum = split_trailing_enumeration(s)
109
+ if enum
110
+ new(text, enumeration: enum, **args)
111
+ else
112
+ new(s, **args)
113
+ end
114
+ end
115
+
116
+ LISTS.each do |f|
117
+ define_method(:"#{f}=") {|v| instance_variable_set(:"@#{f}", v || [])}
118
+ end
119
+
120
+ # The clue as plain text, with its enumeration
121
+ def to_s
122
+ s = text.to_s
123
+ s = "See #{parent}" if s.empty? && parent
124
+ enumeration ? "#{s} (#{enumeration})".strip : s
125
+ end
126
+
127
+ def ==(other)
128
+ other.is_a?(Clue) && FIELDS.all? {|f| send(f) == other.send(f)}
129
+ end
130
+
131
+ def inspect
132
+ fields = FIELDS.map {|f| [f, send(f)]}.reject {|_, v| v.nil? || v == []}
133
+ "#<Clue #{fields.map {|f, v| "#{f}=#{v.inspect}"}.join(' ')}>"
134
+ end
135
+ end
136
+
137
+ end # module Pangrid
data/lib/pangrid/clues.rb CHANGED
@@ -50,7 +50,7 @@ class ClueList
50
50
  check("Line #{lineno}: clue does not start with a number: #{line}") { n }
51
51
  n = n.to_i
52
52
  check("Line #{lineno}: duplicate clue #{n}") { !section.key?(n) }
53
- section[n] = strip_clue_numbers([line], [n]).first
53
+ section[n] = Clue.parse(strip_clue_numbers([line], [n]).first, number: n)
54
54
  end
55
55
  check("No clues found") { !across.empty? || !down.empty? }
56
56
  self
@@ -110,20 +110,17 @@ class Plugin
110
110
  end
111
111
  end
112
112
 
113
+ # One row per feature, followed by the plugins that support it
113
114
  def self.list_features
114
- features = XWord::FEATURES.keys
115
115
  puts
116
116
  puts "-------------------------------------------------------"
117
- puts "Supported features (+ = yes, - = no):"
117
+ puts "Features, and the plugins that support them:"
118
118
  puts "-------------------------------------------------------"
119
- puts "".ljust(30) + features.join(" ")
120
- REGISTRY.keys.sort.each do |name|
121
- supported = REGISTRY[name].features
122
- marks = features.map {|f| (supported.include?(f) ? "+" : "-").ljust(f.length)}
123
- puts (" #{name}".ljust(30) + marks.join(" ")).rstrip
119
+ XWord::FEATURES.each do |f, desc|
120
+ plugins = REGISTRY.keys.sort.select {|name| REGISTRY[name].features.include?(f)}
121
+ puts " #{f.to_s.ljust(14)} #{desc}"
122
+ puts " #{' ' * 14} #{plugins.empty? ? '(none)' : plugins.join(' ')}"
124
123
  end
125
- puts
126
- features.each {|f| puts " #{f}: #{XWord::FEATURES[f]}"}
127
124
  end
128
125
 
129
126
  def self.get(name)
@@ -99,11 +99,13 @@ class AcrossLiteBinary < Plugin
99
99
  attr_accessor :xw, :cs
100
100
 
101
101
  DESCRIPTION = "AcrossLite binary format (.puz)"
102
+ FEATURES = [:circles]
102
103
 
103
104
  HEADER_FORMAT = "v A12 v V2 A4 v2 A12 c2 v3"
104
105
  HEADER_CHECKSUM_FORMAT = "c2 v3"
105
106
  EXT_HEADER_FORMAT = "A4 v2"
106
107
  EXTENSIONS = %w(LTIM GRBS RTBL GEXT)
108
+ GEXT_CIRCLE = 0x80
107
109
  FILE_MAGIC = "ACROSS&DOWN\0"
108
110
 
109
111
  def initialize
@@ -182,9 +184,10 @@ class AcrossLiteBinary < Plugin
182
184
  xw.scrambled_state ||= 0
183
185
  xw.version = "1.3"
184
186
  xw.notes ||= ""
185
- # rebus extensions are regenerated from the grid below
187
+ # rebus and circle extensions are regenerated from the grid below
188
+ gext = get_extension("GEXT")
186
189
  xw.extensions = (xw.extensions || []).reject {|e|
187
- %w(GRBS RTBL).include? e.section
190
+ %w(GRBS RTBL GEXT).include? e.section
188
191
  }
189
192
  xw.title ||= ""
190
193
  xw.author ||= ""
@@ -208,6 +211,16 @@ class AcrossLiteBinary < Plugin
208
211
  xw.extensions << e
209
212
  end
210
213
 
214
+ # GEXT: per-square flags; we keep the ones we don't use (e.g. squares the
215
+ # solver has revealed) and set the circle flag from the grid
216
+ size = xw.width * xw.height
217
+ flags = gext ? gext.grid.first(size).map {|b| b & ~GEXT_CIRCLE} : [0] * size
218
+ circles = xw.solution.flatten.map {|c| c.circle ? GEXT_CIRCLE : 0}
219
+ flags = flags.zip(circles).map {|f, c| f | c}
220
+ if flags.any? {|f| f != 0}
221
+ xw.extensions << Extension.new(section: "GEXT", grid: flags)
222
+ end
223
+
211
224
  # calculate checksums
212
225
  @cs = checksums
213
226
 
@@ -225,35 +238,24 @@ class AcrossLiteBinary < Plugin
225
238
  end
226
239
 
227
240
  private
228
- # sort incoming clues in xw.clues -> across and down
241
+ # Clues are stored in grid order, across before down for each square
242
+ # (which is numerical order for standard numbering)
243
+
244
+ # xw.clues -> across and down
229
245
  def unpack_clues
230
- across, down = xw.number
231
- clues = across.map {|x| [x, :a]} + down.map {|x| [x, :d]}
232
- clues.sort!
233
- xw.across_clues = []
234
- xw.down_clues = []
235
- clues.zip(xw.clues).each do |(n, dir), clue|
236
- if dir == :a
237
- xw.across_clues << clue
238
- else
239
- xw.down_clues << clue
240
- end
246
+ ac, dn = [], []
247
+ xw.lights.zip(xw.clues).each do |light, clue|
248
+ (light.direction == :across ? ac : dn) << clue
241
249
  end
250
+ xw.across_clues = ac
251
+ xw.down_clues = dn
242
252
  end
243
253
 
244
- # combine across and down clues -> xw.clues
254
+ # across and down clues -> xw.clues
245
255
  def pack_clues
246
- across, down = xw.number
247
- clues = across.map {|x| [x, :a]} + down.map {|x| [x, :d]}
248
- clues.sort!
249
256
  ac, dn = xw.across_clues.dup, xw.down_clues.dup
250
- xw.clues = []
251
- clues.each do |n, dir|
252
- if dir == :a
253
- xw.clues << ac.shift
254
- else
255
- xw.clues << dn.shift
256
- end
257
+ xw.clues = xw.lights.map do |light|
258
+ light.direction == :across ? ac.shift : dn.shift
257
259
  end
258
260
  check("Extra across clue") { ac.empty? }
259
261
  check("Extra down clue") { dn.empty? }
@@ -270,6 +272,12 @@ class AcrossLiteBinary < Plugin
270
272
  xw.time_elapsed = ltim.elapsed
271
273
  end
272
274
 
275
+ if (gext = get_extension("GEXT"))
276
+ gext.grid.first(xw.width * xw.height).each_with_index do |flags, i|
277
+ xw.solution[i / xw.width][i % xw.width].circle = true if flags & GEXT_CIRCLE != 0
278
+ end
279
+ end
280
+
273
281
  # we need both grbs and rtbl
274
282
  grbs, rtbl = get_extension("GRBS"), get_extension("RTBL")
275
283
  if grbs and rtbl
@@ -310,8 +318,9 @@ class AcrossLiteBinary < Plugin
310
318
  }.join
311
319
  end
312
320
 
321
+ # e.data includes the terminating \0
313
322
  def read_gext(e)
314
- e.grid = e.data.bytes
323
+ e.grid = e.data.bytes.first(e.len || e.data.length)
315
324
  end
316
325
 
317
326
  def write_gext(e)
@@ -442,8 +451,8 @@ class AcrossLiteText < Plugin
442
451
  ['SIZE', ["#{xw.height}x#{xw.width}"]],
443
452
  ['GRID', write_grid],
444
453
  ['REBUS', write_rebus],
445
- ['ACROSS', xw.across_clues],
446
- ['DOWN', xw.down_clues],
454
+ ['ACROSS', xw.across_clues.map(&:to_s)],
455
+ ['DOWN', xw.down_clues.map(&:to_s)],
447
456
  ['NOTEPAD', xw.notes.to_s.split("\n")]
448
457
  ]
449
458
  out = ["<ACROSS PUZZLE V2>"]