You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note ("") -- Insert a true newline at the end of the string.
112
-
end -- of hyperlink
43
+
--[[
44
+
-- What it is --
45
+
A URL hyperlinker for MUSHclient.
46
+
47
+
-- Why it's needed --
48
+
We want to hyperlink all the URLs in a line, while preserving the original style and color of the line. However, due to how wildcard matching and triggers work, we can't make a trigger to match each URL on a line and hyperlink it.
49
+
What do we do?
50
+
51
+
-- How it works --
52
+
MUSHclient provides the plaintext of the whole line to scripts called from triggers, as the argument "line". MUSHclient also provides an array called "styles" to Lua scripts called from triggers. The styles array contains a structure (or "dictionary") for each styled segment of the whole line the trigger matched. The structure is: {textcolour, backcolour, style, text, length} where textcolour and backcolour are the foreground/background color, style is a set of OR-ed flags bold=1, underline=2, blink=4, text is the text in that segment, and length is the length of the segment. It's worth noting that the 'styles' array is contiguous, and the segments it contains span the whole line. Even segments with no special styling are included, and each style begins immediately after the previous one ends.
53
+
Using these two arguments, we can compose a line with all URLs hyperlinked, while preserving colors and other formatting.
54
+
55
+
Short breakdown:
56
+
The script takes a matched trigger line and breaks it up in two different ways: An array of segments based on styling is made, and an array of segments based on URLs is made. Using those two arrays, the segments of the line are pieced back together, placing the styling on the hyperlinks.
57
+
58
+
Long breakdown: (function names in parentheses)
59
+
1) Trigger on a line that contains at least one URL-like item. The 'styles' array is passed into the script, as well as the plaintext line.
60
+
61
+
2) Find all the URLs in the plaintext line, number them, and record their start/end points and text. We now have an array of (to-be) hyperlinks numbered 1-N, with start/end positions for each.
62
+
(findURLs)
63
+
64
+
3) Merge the start and end points of all the styles and hyperlinks into a sorted set. We now have a list of all points where either or both occurs:
65
+
* A style changes (even to/from 'no styling').
66
+
* A hyperlink begins or ends.
67
+
(getpoints)
68
+
69
+
4) Convert the start and end points into 'slices' of the text line. Disregarding the text itself, each of these slices is homogenous: the style is the same through the whole slice, and the slice is either not part of any hyperlink, or is part of only one hyperlink.
70
+
(getslices)
71
+
72
+
5) Iterate over the slices, recording style/hyperlink number/text for each slice
73
+
into an array named 'reformatted'.
74
+
(reformat)
75
+
76
+
6) Iterate over the 'reformatted' array. If the slice contains part of a hyperlink,
77
+
print it as such. Although the slice may contain only part of a hyperlink, the
78
+
whole URL was stored in the 'hyperlinks' array (in step 2), and can be looked
79
+
up by the slice's stored hyperlink number.
80
+
81
+
You now have a line with detected URLs changed into hyperlinks, and the original styling preserved.
82
+
--]]
83
+
84
+
-- CODE SECTION --
85
+
86
+
-- Returns an array {start, end, text}
87
+
function findURLs(text)
88
+
local URLs = {}
89
+
local start, position = 0, 0
90
+
-- "rex" is a table supplied by MUSHclient for PCRE functionality.
91
+
local re = rex.new("(?:https?://|mailto:)\\S*[\\w/=@#\\-\\?]")
92
+
re:gmatch(text,
93
+
function (link, _)
94
+
start, position = string.find(text, link, position, true)
0 commit comments