Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # Turns tab-separated lines into Atlassian or Markdown formatted tables, with header.
  4. #
  5. # USAGE: copy some query results from SequelPro or some data out of a Google spredsheet, including header, then:
  6. # > pbpaste | tableize | pbcopy
  7. # paste in Jira.
  8. #
  9. #
  10. # Atlassian Syntax:
  11. # read from stdin, surround first line with double pipes and replace each tab with " || "
  12. # read every other line, surround with pipe and replace each tab with " | "
  13. #
  14. # Markdown Syntax:
  15. # Read from stdin, output pipe-separated headers
  16. # Output separator
  17. #
  18. # Header | Header | Header
  19. # -------|--------|-------
  20. # Cell | Cell | Cell
  21. # Cell | Cell | Cell
  22.  
  23. TAB = /\t/
  24. WHITESPACE = /\s+/
  25.  
  26. SPLIT_ON = ARGV.include?("-w") ? WHITESPACE : TAB
  27. FORMAT = ARGV.include?("-m") ? :markdown : :atlassian
  28.  
  29. def format_line(tsv, separator, split_on = /\t/)
  30. str = tsv.chomp.
  31. gsub(/\|/, "\\|").
  32. gsub(split_on," #{separator} ")
  33. "#{separator} #{str} #{separator}"
  34. end
  35.  
  36. def main(args)
  37. if FORMAT == :markdown
  38. STDIN.each_line.with_index do |line, idx|
  39. puts format_line(line, separator, split_on=SPLIT_ON)
  40. end
  41. else
  42. STDIN.each_line.with_index do |line, idx|
  43. separator = idx == 0 ? "||" : "|"
  44. puts format_line(line, separator, split_on=SPLIT_ON)
  45. end
  46. end
  47. end
  48.  
  49. main(ARGV)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement