summaryrefslogtreecommitdiff
path: root/bin/csvtommd.rb
blob: 355b0f9e04b6e9f08509e8897ddf2f4f7298f2f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/ruby

input = STDIN.read

# find quoted cells and replace commas inside quotes with placeholder
input.gsub!(/"([^,].*?)"/m) { |quoted|
	quoted.gsub(/[\n\r]*/,'').gsub(/,/,'zXzX')
}
# replace remaining commas with table divider (pipe)
input.gsub!(/,/,"| ")
# remove quotes from quoted cells
input.gsub!(/(\| |^)"(.*?)"/,"\\1\\2")
# replace placeholders with commas
input.gsub!(/zXzX/,",")

input.each { |l|
	puts "| #{l.strip} |"
}