Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement