New and improved Hyperlink_URL (Not an April Fools joke)

Posted by Sketch-The-Fox on Sat 01 Apr 2006 05:32 PM — 5 posts, 24,305 views.

#0
Worked on this for a while. It's about time I gave something back to the community. This version of Hyperlink_URL preserves colors, and can parse more than one hyperlink per line. Would it be worth putting up in the plugins page? Regardless of that, here it is.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Saturday, 1 April 2006, 12:29 PM -->
<!-- MuClient version 3.73 -->
<muclient>
<plugin name="Hyperlink_URL" author="Sketch" id="520bc4f29806f7af0017985f" language="Lua" purpose="Makes URLs on a line into hyperlinks." date_written="2006-04-01" requires="3.72" version="1.0">
<description trim="y">
<![CDATA[
Detects text starting with HTTP:xxx and makes that part into a hyperlink.
Limits: HTTP:// and the following character must be the same color
]]>
</description>

</plugin>

<!--  Triggers  -->

<triggers>
  <trigger
    enabled="y"
    match="(.*)((https?|mailto)://(?:[\w\d\.\?\/\%#@!&quot;&amp;_]+[/\w\d#]))(.*)$"
    omit_from_output="y"
    ignore_case="y"
    regexp="y"
    script="OnHyperlink"
    sequence="100"
  >
  </trigger>
</triggers>

<!--  Script  -->
<script>
<![CDATA[
function OnHyperlink (name, line, wildcards, styles)

  hyperlinks = {}
  newstyle = {}
  i = 1
  hyperlinkcount = 0
  doingURL = 0
  while i <= table.getn(styles) do -- Doesn't use pairs() because of problems with field-injection.
    if doingURL == 0 then
     -- **** Not a URL **** --
      cut = string.find(styles[i].text, "([hHtTpPsSmMaAiIlLoO]+://[%S]*[%w#/])")
      if cut == nil then -- If there's nothing to cut, copy the whole line
        table.insert(newstyle, {textcolour = styles[i].textcolour
                               ,backcolour = styles[i].backcolour
                               ,style = styles[i].style
                               ,text = styles[i].text})
      else
        table.insert(newstyle, {textcolour = styles[i].textcolour
                               ,backcolour = styles[i].backcolour
                               ,style = styles[i].style
                               ,text = string.sub(styles[i].text, 1, cut - 1)})
        table.insert(styles, i + 1, {textcolour = styles[i].textcolour
                                ,backcolour = styles[i].backcolour
                                ,style = styles[i].style
                                ,text = string.sub(styles[i].text, cut)})
        doingURL = 1
        hyperlinkcount = hyperlinkcount + 1
      end -- if
    else -- **** IS a URL **** --
      -- Search for a URL. If the string is completely a URL...
      -- Jump to the next table field. And keep doing such.
      cut, length, temp = string.find(styles[i].text, "^([%S]*[%w#/])")
      if cut ~= nil then
        if hyperlinks[hyperlinkcount] ~= nil then
          hyperlinks[hyperlinkcount] = hyperlinks[hyperlinkcount] .. temp
        else
          hyperlinks[hyperlinkcount] = temp
        end -- if
        table.insert(newstyle, {textcolour = styles[i].textcolour
                               ,backcolour = styles[i].backcolour
                               ,style = styles[i].style
                               ,text = string.sub(styles[i].text, 1, length)
                               ,hypernumber = hyperlinkcount})
        styles[i].text = string.sub(styles[i].text, length + 1)
        if styles[i].text ~= "" then
          i = i - 1    -- The first hyperlink was cut, so scan the same field for more.
          doingURL = 0
        else
          doingURL = 1
        end
      else 
        doingURL = 0
        i = i - 1
      end -- if (cut)
    end -- if (doingURL)
    i = i + 1
  end -- while

  for x, y in pairs (newstyle) do -- x is the style number, y is the style-data table.
   NoteStyle (y.style)
   if y.hypernumber ~= nil then
     Hyperlink(hyperlinks[y.hypernumber], y.text, "Go to " .. hyperlinks[y.hypernumber]
               , RGBColourToName(y.textcolour), RGBColourToName(y.backcolour), 1)
   else
     ColourTell (RGBColourToName(y.textcolour), RGBColourToName(y.backcolour), y.text)
   end
  end -- while
  Note ("") -- Insert a true newline at the end of the string.
end -- of hyperlink
]]>
</script>
</muclient>
Greece #1
Hello.
This script rocks.
That is all.
Australia Forum Administrator #2
Very nice. Also supports multiple hyperlinks on the same line. I have added it to the plugins page.
Australia Forum Administrator #3
BTW - you know Lua has booleans? So rather than:


doingURL = 1


and then


if doingURL == 1 then 


You can write:


doingURL = true


and then


if doingURL then 


Just looks a bit neater. :)
Canada #4
Sorry to raise this topic from the dead, but I'm trying to figure out how to change the color of the URL that is output but can't seem to get anything to work.

I thought that if I changed RGBColourToName(y.textcolour) to RGBColourToName(blue) that it would work, but I'm wrong.

Any help?