Created
March 30, 2017 16:31
-
-
Save ryangreenberg/c0d971b78364b86958c8f24643acd720 to your computer and use it in GitHub Desktop.
tableize: Turns tab-separated lines into Atlassian or Markdown formatted tables, with header.
This file contains hidden or 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
#!/usr/bin/env ruby | |
# Turns tab-separated lines into Atlassian or Markdown formatted tables, with header. | |
# | |
# USAGE: copy some query results from SequelPro or some data out of a Google spredsheet, including header, then: | |
# > pbpaste | tableize | pbcopy | |
# paste in Jira. | |
# | |
# | |
# Atlassian Syntax: | |
# read from stdin, surround first line with double pipes and replace each tab with " || " | |
# read every other line, surround with pipe and replace each tab with " | " | |
# | |
# Markdown Syntax: | |
# Read from stdin, output pipe-separated headers | |
# Output separator | |
# | |
# Header | Header | Header | |
# -------|--------|------- | |
# Cell | Cell | Cell | |
# Cell | Cell | Cell | |
TAB = /\t/ | |
WHITESPACE = /\s+/ | |
SPLIT_ON = ARGV.include?("-w") ? WHITESPACE : TAB | |
FORMAT = ARGV.include?("-m") ? :markdown : :atlassian | |
def format_line(tsv, separator, split_on = /\t/) | |
str = tsv.chomp. | |
gsub(/\|/, "\\|"). | |
gsub(split_on," #{separator} ") | |
"#{separator} #{str} #{separator}" | |
end | |
def main(args) | |
if FORMAT == :markdown | |
STDIN.each_line.with_index do |line, idx| | |
puts format_line(line, separator, split_on=SPLIT_ON) | |
end | |
else | |
STDIN.each_line.with_index do |line, idx| | |
separator = idx == 0 ? "||" : "|" | |
puts format_line(line, separator, split_on=SPLIT_ON) | |
end | |
end | |
end | |
main(ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My most common usage of this command is
pbpaste | tableize | pbcopy
so I have a shortcut for that as well: