Project

General

Profile

Bug #7608 » QRCoder.rb

wonderingtomato (Tom Pave), 12/23/2012 01:40 PM

 
#!/usr/bin/env ruby
# encoding: UTF-8

require 'rubygems'
require 'rqrcode' # from: http://whomwah.github.com/rqrcode/
require 'erb' # from std-lib

# takes any word from the stdin
@raw_qr = RQRCode::QRCode.new(gets)

# this is the line that doesn't work:
erb_file = ERB.new(HTML_TEMPLATE)
# this works:
# erb_file = ERB.new(HTML_TEMPLATE, nil, "-")

@pretty_qr = erb_file.result

puts @pretty_qr



BEGIN {

# creates a rhtml template file
# after creation, it gsubs it to remove leading whitespace.
# almost completely copied from: http://whomwah.github.com/rqrcode/
HTML_TEMPLATE = <<-HERE.gsub(/^\s+/,"")
<!DOCTYPE html>
<html>
<head>
<title>QR Code</title>
<style type="text/css">
table {
border-width: 0;
border-style: none;
border-color: #0000ff;
border-collapse: collapse;
}
td {
border-width: 0;
border-style: none;
border-color: #0000ff;
border-collapse: collapse;
padding: 0;
margin: 0;
width: 10px;
height: 10px;
}
td.black { background-color: #000; }
td.white { background-color: #fff; }
</style>
</head>
<body>
<table>
<% @raw_qr.modules.each_index do |x| -%>
<tr>
<% @raw_qr.modules.each_index do |y| -%>
<% if @raw_qr.dark?(x,y) -%>
<td class="black"/>
<% else -%>
<td class="white"/>
<% end -%>
<% end -%>
</tr>
<% end -%>
</table>
</body>
</html>
HERE

}
    (1-1/1)