Last active
July 31, 2017 14:29
-
-
Save dcorrigan/84e98baadc8570e9401bdacdb4787004 to your computer and use it in GitHub Desktop.
quick script for wedding list CSV to IDTT for layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
require 'erb' | |
class Address | |
def initialize(row) | |
@row = row | |
end | |
def name | |
text(@row[0]) | |
end | |
def house | |
text(@row[1]) | |
end | |
def apartment | |
@row[2] ? text(@row[2]) : nil | |
end | |
def city | |
text(@row[3] + ', ' + @row[4] + ' ' + @row[5]) | |
end | |
def text(str) | |
return str if str.ascii_only? | |
str.split(/([^[[:ascii:]]])/).map do |chunk| | |
if chunk.ascii_only? | |
chunk | |
else | |
codepoint = chunk.codepoints[0] | |
"<0x#{sprintf('%04x', codepoint).upcase}>" | |
end | |
end.join | |
end | |
end | |
class Addr2IDTT | |
IDTT_HEAD = <<-IDTT | |
<ASCII-MAC> | |
<DefineParaStyle:name=> | |
<DefineParaStyle:house=> | |
<DefineParaStyle:apt=> | |
<DefineParaStyle:city=> | |
<DefineParaStyle:break=> | |
IDTT | |
IDTT_ADDRESS = ERB.new <<-IDTT | |
<ParaStyle:name><%= address.name %> | |
<ParaStyle:house><%= address.house %><% if address.apartment %> | |
<ParaStyle:apt><%= address.apartment %><% end %> | |
<ParaStyle:city><%= address.city %> | |
<ParaStyle:break>@ | |
IDTT | |
def initialize(txt) | |
@txt = txt | |
@idtt = IDTT_HEAD.dup | |
end | |
def run | |
CSV.parse(@txt) do |row| | |
address = Address.new(row) | |
@idtt << IDTT_ADDRESS.result(binding) | |
end | |
@idtt | |
end | |
end | |
a2i = Addr2IDTT.new(File.read(ARGV[0])) | |
File.open(ARGV[1], 'w') do |f| | |
f.write a2i.run | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment