Skip to content

Instantly share code, notes, and snippets.

@jcsalterego
Created November 23, 2016 23:51
Show Gist options
  • Save jcsalterego/8dbcebe23f5434c58a8859eaeea9a728 to your computer and use it in GitHub Desktop.
Save jcsalterego/8dbcebe23f5434c58a8859eaeea9a728 to your computer and use it in GitHub Desktop.
Slack Paste
$ cat transcript | pbcopy
$ ./slackpaste.rb
alice: this is a test
alice: this is another line from alice
bob: this is bob chiming in
jerry: this is tomorrow!
jerry: another graet line from jerry
$ ./slackpaste.rb -x
$ pbpaste
alice: this is a test
alice: this is another line from alice
bob: this is bob chiming in
jerry: this is tomorrow!
jerry: another graet line from jerry
#!/usr/bin/env ruby
require 'fileutils'
FORMAT = "{{author}}: {{message}}"
TIMESTAMP_RE = /\[[0-9:A-Z ]+\]/
DAY_MARKER_RE = /(-){5}[^-]+(-){5}/
input = `pbpaste`
last_author = "???"
lines = input.
split(/\n/).
reject{|line| line =~ DAY_MARKER_RE}.
join("\n").
split(/\n\n/).
map(&:strip).map do |line|
if line =~ TIMESTAMP_RE
words = line.split(TIMESTAMP_RE).map(&:strip)
author = last_author
if words.size == 2 && !words[0].empty?
last_author = words[0]
message = words[1]
elsif words.size == 1
message = words[0]
else
message = words[1]
end
{:author=>last_author, :message=>message}
end
end
if lines.compact.empty?
$stderr.puts "Bailing!"
exit 1
end
paste = lines.map {|line|
output = FORMAT.dup
line.each do |k, v|
output = output.gsub(/{{#{k}}}/, v)
end
output
}.join("\n")
if ARGV[0] == "-x"
tmp = "/tmp/#{$$}.tmp"
fh = File.open(tmp, "w")
fh.puts(paste)
fh.close
`cat #{tmp} | pbcopy`
FileUtils.rm(tmp)
else
puts paste
end
alice [3:00 PM]
this is a test
[3:00]
this is another line from alice
bob [3:00 PM]
this is bob chiming in
----- Today November 23rd, 2016 -----
jerry [9:54 AM]
this is tomorrow!
[9:54]
another graet line from jerry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment