Skip to content

Instantly share code, notes, and snippets.

@no6v
Created March 20, 2013 01:13
Show Gist options
  • Save no6v/5201558 to your computer and use it in GitHub Desktop.
Save no6v/5201558 to your computer and use it in GitHub Desktop.
# encoding: UTF-8
str = <<'EOT'
連続する、句読点。
どう扱えば、、いいのか。。。
EOT
punc = "、。,."
h = "ー「」→↑←↓=…"
v = "|¬∟↓→↑←॥:"
hv = (h + v)
vh = (v + h)
lines = []
str.tr(hv, vh).each_line{|line|
lines << line.chomp.split(/([#{punc}]+)/o)
}
status = []
lines.each{|line| status << line.each_slice(2).with_object(["", ""]){|(main, puncta), (punctuation, mains)|
punctuation << " " * main.size.pred + puncta
mains << main + " " * puncta.size.pred
}}
status.flatten!.each{|line| line.chomp!(" ")}
max = status.map(&:size).max
status = status.map{|line|
line.chars.fill(" ", line.size...max)
}.transpose.map{|line| line.reverse.join}.join("\n")
status.gsub!(/ +$/, "")
puts status
# >> ど 連
# >> う 続
# >> 扱 す
# >> え る、
# >> ば、句
# >>  、読
# >> い 点。
# >> い
# >> の
# >> か。
# >>  。
# >>  。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment