Skip to content

Instantly share code, notes, and snippets.

@neingeist
Forked from lypanov/elinkify.rb
Created November 3, 2023 22:08
Show Gist options
  • Save neingeist/d22f157bc29a7bcc31020d1c1ffcc8be to your computer and use it in GitHub Desktop.
Save neingeist/d22f157bc29a7bcc31020d1c1ffcc8be to your computer and use it in GitHub Desktop.
Post-process step using elinks -dump to create OSC-8 style hyperlinked terminal output
require "json"
require "pp"
state = false
references_raw, content = *(`elinks -dump /tmp/dump4`.lines.partition { |line| state = true if line == "References\n" ; state } )
content_began, delayed_link = false, nil
references = references_raw.inject({}) do |hsh, line|
if line =~ /Visible links$/
content_began = true
next hsh
end
if content_began && !delayed_link.nil?
line =~ /\s*(.*)$/
hsh[delayed_link] = $1
delayed_link = nil
elsif content_began
line =~ /\s*(\d+?)\. (([a-z]+:.*)|(.*))$/
if $3
hsh[$1.to_i] = $2
else
delayed_link = $1.to_i
end
end
hsh
end
content.each do |line|
loop do
if line =~ /^(.*?)\[(\d+)\](.*)$/
before, ref, after = $1, $2, $3
print before
url = references[ref.to_i]
spacer = url.nil? ? " " : "@"
text = spacer * (ref.length + 2)
print "\033[31;1m\e]8;;#{url}\e\\#{text}\e]8;;\e\\\033[0m"
line = after
else
print line.chomp
break
end
end
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment