#!/usr/local/bin/ruby
# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.
# QR-code html version
# requires QRcode class library for ruby version 0.50beta6 by Y.Swetake

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/wabisabi-generator"

class QRCodeHtml
  def initialize(path)
    @qrcode = QRCode.new(path)
  end

  def generate_htree(d)
    generate_htree_from_data(@qrcode.make_qrcode(d))
  end

  def generate_htree_from_data(d)
    ar = []
    ar[?0] = [:td]
    ar[?1] = [:th]

    table = d.map {|line|
      tr = []
      line.chomp.each_byte {|b|
	tr << ar[b]
      }
      [:tr, tr]
    }

    [:table, {:class=>"qrcode"}, table]
  end

  def generate_html(d) # not use
    generate_html_from_data(@qrcode.make_qrcode(d))
  end

  def generate_html_from_data(d)
    ar = []
    ar[?0] = "<td/>"
    ar[?1] = "<th/>"
    ["<table class=\"qrcode\">",
      d.map {|line|
	tr = "<tr>"
	line.chomp.each_byte {|b|
	  tr << ar[b]
	}
	tr << "</tr>"
	tr
      }.join(""),
      "</table>"].join("")
  end
end
