Class: Pangrid::AcrossLiteText

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

Overview

Text format

Constant Summary

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.



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

def initialize
  @xw = XWord.new
end

Instance Attribute Details

#rebusObject

Returns the value of attribute rebus.



360
361
362
# File 'lib/pangrid/plugins/acrosslite.rb', line 360

def rebus
  @rebus
end

#xwObject

Returns the value of attribute xw.



360
361
362
# File 'lib/pangrid/plugins/acrosslite.rb', line 360

def xw
  @xw
end

Instance Method Details

#read(data) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/pangrid/plugins/acrosslite.rb', line 366

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



385
386
387
388
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
# File 'lib/pangrid/plugins/acrosslite.rb', line 385

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