#!/usr/bin/ruby # encoding: UTF-8 require 'set' REGEX_HASHTAGS = /(?<=^| )#([\p{L}\p{N}]+)/ HASHTAG_URLS = { mastodon_social_profile: 'https://mastodon.social/@FiXato/tagged/%{keyword}', google_search: 'https://www.google.com/search?q=%%23%{keyword}&oq=%%23%{keyword}&ie=UTF-8' } referenced_hashtags = Set.new() parsed_lines = [] ARGF.each_line do |line| line.gsub!(REGEX_HASHTAGS) do |s| hashtag = $1 # Exclude hex-encoded colours from hashtags. # FIXME: I guess this could lead to for instance #deadbeef not being tagged, but it's the best I could come up with right now. if hashtag.match?(/[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$/) || line.match?(/#{s}\s*[{><%]/) next s else referenced_hashtags << hashtag next "#{HASHTAG_URLS[:mastodon_social_profile]}[#%{keyword}]" % {keyword: hashtag} end end parsed_lines << line end end_of_header = parsed_lines.find_index{|line|line.strip == ''} parsed_lines[end_of_header] = ":referenced_hashtags: #{referenced_hashtags.to_a.compact.sort.join(', ')}\n" puts parsed_lines.join("")