Make pretty formatted timestamps using BB codes for Xenforo2 forum posts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.0 KiB

#!/usr/bin/ruby
# BB_TIMESTAMPS.RB makes pretty formatted timestamps for Xenforo2 forum
# posts. Write the timestamps in any (raw) text editor, e.g.
#
# 01:23:45 Something funny happens here at 1h, 23min, 45s
# 01:56:02 Something else happens
#
# ...with a space separating the time from the comment, and newlines
# separating each timestamp. Save the timestamps, e.g. to timestamps.txt.
# Then run this script:
# ./bb_timestamps.rb timestamps.txt
# and the timestamps will be formatted nicely, using a colored, monospaced
# Courier New fonr for the times so that they all line up pretty.
# Optionally (but most usefully!) if the first line of timestamp.txt is
# the link to a Youtube video/stream, the output timestamps will be
# formatted with hyperlinks that can be clicked by a reader to jump to
# that moment in the stream, e.g.
# https://www.youtube.com/watch?v=XXXXXXXXXXX
# will be hyperlinked in the formatted timestamps as
# https://www.youtube.com/watch?v=XXXXXXXXXXX&time_continue=...
# with the continue time determined automatically from each timestamp.
#TODO: Works for Youtube, but perhaps this script could be used for e.g.
# Bitchute or others by changing just this one line?
TIMESTAMP_PARAMETER = "&time_continue="
# Use a light blue color for the timestamps if no hyperlink
TIMESTAMP_COLOR = "rgb(84, 172, 210)"
# Use monospaced Courier New for the time font
TIMESTAMP_FONT = "courier new"
# A class to represent a TIMESTAMP with a given time and comment.
# The BB code formatting for the time may be built up piece-by-piece
# through the augment_... methods.
#
# Example:
# # Create a new, vanilla timestamp and just print it
# stamp = Timestamp.new("01:23:45", "Something funny happens")
# puts stamp #=> 01:23:45 Something funny happens
#
# # Augment with a monospaced Courier New font
# stamp.augment_font("courier new")
# puts stamp #=> [FONT=courier new]01:23:45[/FONT] Something
# # funny happens
#
# # Further augment with a pure red color rgb(255,0,0)
# stamp.augment_color("rgb(255, 0, 0)")
# puts stamp #=> [COLOR=rgb(255, 0, 0)][FONT=courier new]01:23:45
# # [/FONT][/COLOR] Something funny happens
#
class Timestamp
# Create a new timestamp with the given TIME and COMMENT.
def initialize(time, comment)
@time = time
@comment = comment
# No augment tags yet
@front_tags = []
@end_tags = []
end
# Return the printable string representation for this timestamp,
# including all augment tags.
def to_s()
@front_tags.join("") + @time + @end_tags.reverse.join("") + " " + @comment
end
# Augment this timestamp with a BB code to COLOR the text.
def augment_color(color)
@front_tags << "[COLOR=#{color}]"
@end_tags << "[/COLOR]"
end
# Augment this timestamp with a BB code to format the time
# text in a given FONT.
def augment_font(font)
@front_tags << "[FONT=#{font}]"
@end_tags << "[/FONT]"
end
# Augment this timestamp with a BB code to hyperlink to the
# given URL.
def augment_url(url)
@front_tags << "[URL='#{url}']"
@end_tags << "[/URL]"
end
end
# Convert a given TIMESTAMP (e.g. "hrs:mins:secs" to the _h_m_s format
# used by Youtube's time_continue parameter.
#
# Usage:
# to_hms("01:23:45") #=> "1h23m45s"
# to_hms("12:34") #=> "0h12m34s"
# to_hms("56") #=> "0h0m56s"
def to_hms(timestamp)
hrsminssecs = timestamp.split(":").map {|str| str.to_i}
# Prepend 0s if the timestamp doesn't explicitly state the hrs or mins
while hrsminssecs.length < 3
hrsminssecs.unshift(0)
end
hrs, mins, secs = hrsminssecs
"#{hrs}h#{mins}m#{secs}s"
end
# Assume that the file full of timestamps is the first command line arg
if ARGV.length < 1
raise(ArgumentError, "No timestamps file specified in command line args")
else
timestamps_file = File.open(ARGV.join(""))
end
# Extract the first line, skip any leading whitespace
first_line = timestamps_file.gets.strip
while first_line.empty?
first_line = timestamps_file.gets.strip
end
# If the first non-whitespace line is a URL, use that for the video
# hyperlink. If not, color the timestamps so they stand out at least.
if first_line.start_with?("http")
url = first_line + TIMESTAMP_PARAMETER
elsif first_line.start_with?("www")
url = "https://" + first_line + TIMESTAMP_PARAMETER
else
url = nil
# In this case we read a timestamp line, so we process it here
time, comment = first_line.split(" ", 2)
stamp = Timestamp.new(time, comment)
stamp.augment_color(TIMESTAMP_COLOR)
stamp.augment_font(TIMESTAMP_FONT)
puts stamp.to_s + "\n"
end
# Process all of the timestamp lines (ignoring any blank lines)
timestamps_file.each do |line|
# Skip any preceding blank lines
line.strip!
if not line.empty?
time, comment = line.split(" ", 2)
stamp = Timestamp.new(time, comment)
if url.nil?
stamp.augment_color(TIMESTAMP_COLOR)
else
stamp.augment_url(url + to_hms(time))
end
stamp.augment_font(TIMESTAMP_FONT)
puts stamp.to_s + "\n"
end
end
# Done! Close the timestamp file.
timestamps_file.close()