tapestry / lib / kamishibai.rb

# 
# camo wrote this code
# it is MIT licensed
# it is not edible
# proceed with caution
#

module KamishibaiUtils
  require 'iconv' unless String.method_defined?(:encode)

  def convert_to_utf8(str)
    # TODO change to auto detect encoding
    if String.method_defined?(:encode) 
      str.encode!('UTF-8', 'US-ASCII',{:invalid => :replace, :universal_newline => true})   
    else
      ic = Iconv.new('UTF-8', 'UTF-8//IGNORE')
      str = ic.iconv(str)
    end
  end
end

class Kamishibai
  include KamishibaiUtils

  attr_accessor :slideset

  # The Slip N Slide holds our wonky data.
  SlipnSlide = Struct.new(:title, :raw_attributes, :attributes) do
    def to_twee
      twee = ":: #{title}\n"
      twee << attributes['description'] + "\n\n\n"
      twee
    end
  end

  def initialize(gamefile_path)
    gamefile = File.open(gamefile_path, 'r')
    raise "Can't load gamefile" unless gamefile

    @slideset = []
    current_slide = nil
    gamefile.each do |line|
      line = convert_to_utf8 line 
      line.strip!

      if line =~ %r{^\[(.+)\]}
        @slideset << current_slide unless current_slide.nil? 
        current_slide = SlipnSlide.new(line.gsub(/\[|\]/, ''), '', [])
      else
        if line =~ %r{^\w}
          line = line.rjust(line.length + 1)
        end
        current_slide.raw_attributes += line if current_slide
      end

    end
    # welp I forgot to "clear the cache" so to speak
    @slideset << current_slide
    
    # dang I wanna use @slideset.collect!(&:parse_attributes) 'cause I <3 that syntax
    @slideset.collect! { |slide| Kamishibai.parse_attributes!(slide) }
  end

  def self.parse_attributes!(slide)
    attrs = slide.raw_attributes
    attrs = attrs.gsub('@', '||@').split('||')
    attrs.delete_if{|attr| attr.empty? }
    slide.attributes = Kamishibai.to_hash attrs
    slide
  end

  def self.to_hash ary
    hash = {}
    ary.collect do |str| 
      key, value = str.split(' = ')
      hash[key.tr('@', '')] = value
    end
    hash
  end

  def export
    @slideset.each { |slide| puts slide.to_twee }
  end
end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.