Class: Pangrid::AcrossLiteText

Inherits:
Plugin
  • Object
show all
Includes:
AcrossLiteUtils
Defined in:
lib/pangrid/plugins/acrosslite.rb

Overview

Text format

Constant Summary collapse

DESCRIPTION =
"AcrossLite text format"

Constants inherited from Plugin

Plugin::FAILED, Plugin::MISSING_DEPS, Plugin::REGISTRY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AcrossLiteUtils

#empty_fill, #pack_solution, #unpack_solution

Methods inherited from Plugin

class_to_name, get, inherited, list_all, load_all, load_plugin

Methods included from PluginUtils

#check

Constructor Details

#initializeAcrossLiteText

Returns a new instance of AcrossLiteText.



366
367
368
# File 'lib/pangrid/plugins/acrosslite.rb', line 366

def initialize
  @xw = XWord.new
end

Instance Attribute Details

#rebusObject

Returns the value of attribute rebus.



362
363
364
# File 'lib/pangrid/plugins/acrosslite.rb', line 362

def rebus
  @rebus
end

#xwObject

Returns the value of attribute xw.



362
363
364
# File 'lib/pangrid/plugins/acrosslite.rb', line 362

def xw
  @xw
end

Instance Method Details

#read(data) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/pangrid/plugins/acrosslite.rb', line 370

def read(data)
  s = data.each_line.map(&:strip)
  # first line must be <ACROSS PUZZLE> or <ACROSS PUZZLE V2>
  xw.version = { "<ACROSS PUZZLE>" => 1, "<ACROSS PUZZLE V2>" => 2 }[s.shift]
  check("Could not recognise Across Lite text file") { !xw.version.nil? }
  header, section = "START", []
  s.each do |line|
    if line =~ /^<(.*)>/
      process_section header, section
      header = $1
      section = []
    else
      section << line
    end
  end
  process_section header, section
  xw
end

#write(xw) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/pangrid/plugins/acrosslite.rb', line 389

def write(xw)
  @xw = xw

  # scan the grid for rebus squares and replace them with lookup keys
  xw.encode_rebus!

  # fill in dummy clues if none exist
  across, down = xw.number
  if xw.across_clues.empty?
    xw.across_clues = ["(no clue)"]*across.length
  end
  if xw.down_clues.empty?
    xw.down_clues = ["(no clue)"]*down.length
  end

  sections = [
    ['TITLE', [xw.title]],
    ['AUTHOR', [xw.author]],
    ['COPYRIGHT', [xw.copyright]],
    ['SIZE', ["#{xw.height}x#{xw.width}"]],
    ['GRID', write_grid],
    ['REBUS', write_rebus],
    ['ACROSS', xw.across_clues],
    ['DOWN', xw.down_clues],
    ['NOTEPAD', xw.notes.to_s.split("\n")]
  ]
  out = ["<ACROSS PUZZLE V2>"]
  sections.each do |h, s|
    next if s.nil? || s.empty?
    out << "<#{h}>"
    s.each {|l| out << " #{l}"}
  end
  out.join("\n") + "\n"
end