Skip to content

Instantly share code, notes, and snippets.

@neingeist
Forked from lypanov/elinkify.rb
Created November 3, 2023 22:08
Show Gist options
  • Select an option

  • Save neingeist/d22f157bc29a7bcc31020d1c1ffcc8be to your computer and use it in GitHub Desktop.

Select an option

Save neingeist/d22f157bc29a7bcc31020d1c1ffcc8be to your computer and use it in GitHub Desktop.

Revisions

  1. @lypanov lypanov created this gist Jan 9, 2020.
    43 changes: 43 additions & 0 deletions elinkify.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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