Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1.  
  2. <!-- Triggers -->
  3.  
  4. <triggers>
  5. <trigger
  6. enabled="y"
  7. match="(?:https?://|mailto:|ftp:)\S*[\w/=@#\-\?]"
  8. omit_from_output="y"
  9. ignore_case="y"
  10. regexp="y"
  11. script="onURL"
  12. sequence="100"
  13. >
  14. </trigger>
  15.  
  16.  
  17. </triggers>
  18.  
  19. <!-- Script -->
  20. <script>
  21. <![CDATA[
  22.  
  23.  
  24. -- CODE SECTION --
  25.  
  26. -- Returns an array {start, end, text}
  27. function findURLs(text)
  28. local URLs = {}
  29. local start, position = 0, 0
  30. -- "rex" is a table supplied by MUSHclient for PCRE functionality.
  31. local re = rex.new("(?:https?://|mailto:|ftp:)\\S*[\\w/=@#\\-\\?]")
  32. re:gmatch(text,
  33. function (link, _)
  34. start, position = string.find(text, link, position, true)
  35. table.insert(URLs, {start=start, stop=position, text=link})
  36. end
  37. )
  38. return URLs
  39. end -- function findURL
  40.  
  41. -- Returns a table of points where formatting should change in the new string.
  42. function getpoints(styles, hyperlinks)
  43. local points = {}
  44. local unique_points = {}
  45. local pos = 1
  46. for _,v in pairs(styles) do
  47. table.insert(points, pos)
  48. table.insert(points, pos + v.length)
  49. pos = pos + v.length
  50. end
  51. -- The last value of points is now 1 past the end of the string.
  52.  
  53. for _,v in pairs(hyperlinks) do
  54. table.insert(points, v.start)
  55. table.insert(points, v.stop + 1)
  56. -- The hyperlink itself is at v.stop. v.stop+1 is where the change is.
  57. end
  58.  
  59. table.sort(points)
  60. return unique_array(points)
  61. end -- function getpoints
  62.  
  63. -- Returns an array with consecutive duplicate items removed.
  64. function unique_array(a)
  65. local uniq, current = {}, nil
  66. for _,v in pairs(a) do
  67. if v ~= current then
  68. table.insert(uniq, v)
  69. current = v
  70. end
  71. end
  72. return uniq
  73. end -- function unique_array
  74.  
  75. -- Given an array of numbers, return an array of pairs, to be used in string.sub().
  76. -- Each pair starts at the original array's key, and ends before the next key.
  77. -- Example: [1, 5, 9, 13] --> [{1,4},{5,8},{9,12}]
  78. function getslices(points)
  79. local newpoints = {}
  80. for i = 1, #points - 1, 1 do
  81. table.insert(newpoints, {start=points[i], stop=points[i+1] - 1})
  82. end
  83. return newpoints
  84. end -- function getslices
  85.  
  86. -- Returns an array of
  87. -- {startpos, endpos, textcolour, backcolour, style, hyperlink_number}
  88. function reformat(slices, styles, hyperlinks)
  89. local styles_i, hyperlinks_i = 1, 1
  90. local hyperlink_number = 0
  91. local reformatted = {}
  92. -- nextstyle is set at the character where the next style begins.
  93. local nextstyle = styles[1].length + 1
  94.  
  95. -- Add a fake hyperlink past the end of the string at the end of the array.
  96. -- This way, we don't have to keep checking (hyperlinks_i > #hyperlinks).
  97. table.insert(hyperlinks,{start=slices[#slices].stop + 1,stop=slices[#slices].stop + 1,text=""})
  98.  
  99. for _,v in pairs(slices) do
  100.  
  101. -- v.start is our 'current position'.
  102. -- Make sure we're using the correct style
  103. if v.start >= nextstyle then
  104. nextstyle = v.start + styles[styles_i + 1].length
  105. styles_i = styles_i + 1
  106. end
  107.  
  108. -- If we've passed the hyperlink marked by hyperlinks_i, increment it.
  109. if v.start > hyperlinks[hyperlinks_i].stop then
  110. hyperlinks_i = hyperlinks_i + 1
  111. end
  112.  
  113. -- The hyperlink_number is set to the hyperlink we're checking for if
  114. -- we're at or past its starting position. (In other words, inside it)
  115. if v.start >= hyperlinks[hyperlinks_i].start then
  116. hyperlink_number = hyperlinks_i
  117. else
  118. hyperlink_number = 0
  119. end
  120.  
  121. table.insert(reformatted,
  122. {startpoint = v.start
  123. ,endpoint = v.stop
  124. ,textcolour = styles[styles_i].textcolour
  125. ,backcolour = styles[styles_i].backcolour
  126. ,style = styles[styles_i].style
  127. ,hyperlink_number = hyperlink_number}
  128. )
  129. end
  130. return reformatted
  131. end -- function reformat
  132.  
  133. -- Line: Whole line that contains the trigger, in plaintext.
  134. -- Styles: [{textcolour, backcolour, text, length, style},...]
  135. function onURL (name, line, wildcards, styles, hyperlinks)
  136. local hyperlinks = findURLs(line)
  137. local reformatted = reformat(getslices(getpoints(styles,hyperlinks)), styles, hyperlinks)
  138. chat_world = "Links" -- Change this to change the world it sends to.
  139. local first_time = true
  140. local w = GetWorld (chat_world) -- get "chat" world
  141. -- if not found, try to open it
  142. if first_time and not w then
  143. local filename = GetInfo (67) .. chat_world .. ".mcl"
  144. Open (filename)
  145. w = GetWorld (chat_world) -- try again
  146. if not w then
  147. ColourNote ("white", "red", "Can't open chat world file: " .. filename)
  148. first_time = false -- don't repeatedly show failure message
  149. end -- can't find world
  150. end -- can't find world first time around
  151.  
  152. if w then -- if present
  153. for _,v in pairs(reformatted) do
  154. NoteStyle(v.style) -- Set style for the segment, regardless of type.
  155. if v.hyperlink_number ~= 0 then
  156.  
  157. w:Hyperlink(
  158. hyperlinks[v.hyperlink_number].text -- Hyperlink
  159. ,string.sub(line, v.startpoint, v.endpoint) -- Displayed text
  160. ,"Go to " .. hyperlinks[v.hyperlink_number].text -- Hover text
  161. ,RGBColourToName(v.textcolour) -- Foreground color
  162. ,RGBColourToName(v.backcolour) -- Background color
  163. ,1 -- Boolean: Open as a URL?
  164. )
  165.  
  166. end
  167.  
  168. end
  169. w:Note ("") -- Insert a newline in the links world
  170.  
  171. end -- function onURL
  172. end
  173. ]]>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement