soycamo avatar soycamo committed 50ac284

moved string conversion to KamishibaiUtils mixin

Comments (0)

Files changed (1)

 require 'pp'
+
+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
-  require 'iconv' unless String.method_defined?(:encode)
+  include KamishibaiUtils
+
   attr_accessor :slideset
 
   # The Slip N Slide holds our wonky data.
     @slideset = []
     current_slide = nil
     gamefile.each do |line|
-      # TODO change to auto detect encoding
-      if String.method_defined?(:encode) 
-        line.encode!('UTF-8', 'US-ASCII',{:invalid => :replace, :universal_newline => true})   
-      else
-        ic = Iconv.new('UTF-8', 'UTF-8//IGNORE')
-        line = ic.iconv(line)
-      end
+      line = convert_to_utf8 line 
       line.strip!
-      #puts line
+
       if line =~ %r{^\[(.+)\]}
         @slideset << current_slide unless current_slide.nil? 
         # use line.tr
-        current_slide = SlipnSlide.new(line[1..(line.length - 2)], '', [])
+        current_slide = SlipnSlide.new(line.gsub(/\[|\]/, ''), '', [])
       else
         if line =~ %r{^\w}
           line = line.rjust(line.length + 1)
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.