/Interface/AddOns/Prat-3.0/services/patterns.lua

https://gitlab.com/potray/wowInterface · Lua · 210 lines · 154 code · 19 blank · 37 comment · 0 complexity · f5cf8c35f18691eccf6c1769b3b45f62 MD5 · raw file

  1. ---------------------------------------------------------------------------------
  2. --
  3. -- Prat - A framework for World of Warcraft chat mods
  4. --
  5. -- Copyright (C) 2006-2011 Prat Development Team
  6. --
  7. -- This program is free software; you can redistribute it and/or
  8. -- modify it under the terms of the GNU General Public License
  9. -- as published by the Free Software Foundation; either version 2
  10. -- of the License, or (at your option) any later version.
  11. --
  12. -- This program is distributed in the hope that it will be useful,
  13. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. -- GNU General Public License for more details.
  16. --
  17. -- You should have received a copy of the GNU General Public License
  18. -- along with this program; if not, write to:
  19. --
  20. -- Free Software Foundation, Inc.,
  21. -- 51 Franklin Street, Fifth Floor,
  22. -- Boston, MA 02110-1301, USA.
  23. --
  24. --
  25. -------------------------------------------------------------------------------
  26. --[[ BEGIN STANDARD HEADER ]] --
  27. -- Imports
  28. local _G = _G
  29. local table = table
  30. local pairs, ipairs = pairs, ipairs
  31. local tinsert, tremove, tconcat = table.insert, table.remove, table.concat
  32. local wipe = wipe
  33. local type = type
  34. local setmetatable = setmetatable
  35. local rawset, rawget = rawset, rawget
  36. local tostring = tostring
  37. local LibStub = LibStub
  38. -- Isolate the environment
  39. setfenv(1, select(2, ...))
  40. --[[ END STANDARD HEADER ]] --
  41. local PatternRegistry = {}
  42. local debug = function(...) --[[_G.fprint(_G.ChatFrame1, ...)]] end
  43. -- Register a pattern with the pattern matching engine
  44. -- You can supply a priority 1 - 100. Default is 50
  45. -- 1 = highest, 100 = lowest.
  46. -- pattern = { pattern, matchfunc, priority, type}
  47. --
  48. -- Priorities arent used currently, they are to help with
  49. -- collisions later on if there are alot of patterns
  50. --
  51. do
  52. local PatternOwners = {}
  53. function RegisterPattern(pattern, who)
  54. tinsert(PatternRegistry, pattern)
  55. local idx = #PatternRegistry
  56. pattern.idx = idx
  57. debug("RegisterPattern", who, pattern)
  58. PatternOwners[#PatternRegistry] = who
  59. return idx
  60. end
  61. function UnregisterAllPatterns(who)
  62. debug([[DBG_PATTERN("UnregisterAllPatterns", who)]])
  63. local owner
  64. for k,owner in pairs(PatternOwners) do
  65. if owner == who then
  66. UnregisterPattern(k)
  67. end
  68. end
  69. end
  70. end
  71. function GetPattern(idx)
  72. return PatternRegistry[idx]
  73. end
  74. function UnregisterPattern(idx)
  75. tremove(PatternRegistry, idx)
  76. end
  77. do
  78. local tokennum = 1
  79. MatchTable = setmetatable({}, {
  80. __index = function(self, key)
  81. if type(rawget(self, key)) ~= "table" then
  82. rawset(self, key, {})
  83. end
  84. return rawget(self, key)
  85. end
  86. })
  87. function RegisterMatch(self, text, ptype)
  88. local token = "@##" .. tokennum .. "##@"
  89. debug("RegisterMatch", text, token)
  90. local mt = MatchTable[ptype or "FRAME"]
  91. mt[token] = text
  92. tokennum = tokennum + 1
  93. -- return text
  94. return token
  95. end
  96. local sortedRegistry = {}
  97. function MatchPatterns(text, ptype)
  98. ptype = ptype or "FRAME"
  99. tokennum = 1
  100. for i,v in ipairs(PatternRegistry) do
  101. sortedRegistry[i] = v
  102. end
  103. table.sort(sortedRegistry, function(a, b)
  104. local ap = a.priority or 50
  105. local bp = b.priority or 50
  106. return ap > bp
  107. end)
  108. debug("MatchPatterns -->", text, tokennum)
  109. -- Match and remove strings
  110. for _,v in ipairs(sortedRegistry) do
  111. if text and ptype == (v.type or "FRAME") then
  112. if type(v.pattern) == "string" and (v.pattern):len() > 0 then
  113. debug("MatchPatterns :", v.pattern)
  114. if v.deformat then
  115. text = v.matchfunc(text)
  116. else
  117. if v.matchfunc ~= nil then
  118. text = text:gsub(v.pattern, v.matchfunc)
  119. else
  120. debug("ERROR", v.pattern)
  121. end
  122. end
  123. end
  124. end
  125. end
  126. wipe(sortedRegistry)
  127. debug("MatchPatterns <--", text, tokennum)
  128. return text
  129. end
  130. function ReplaceMatches(text, ptype)
  131. --if true then return text end
  132. -- Substitute them (or something else) back in
  133. local mt = MatchTable[ptype or "FRAME"]
  134. debug("ReplaceMatches -->", text)
  135. local k
  136. for t=tokennum,1,-1 do
  137. k = "@##" .. tostring(t) .. "##@"
  138. if (mt[k]) then
  139. text = text:gsub(k, mt[k])
  140. else
  141. debug("ERROR", k)
  142. end
  143. mt[k] = nil
  144. end
  145. -- for k,v in pairs(mt) do
  146. -- text = text:gsub(k, v)
  147. -- mt[k] = nil
  148. -- end
  149. debug("ReplaceMatches <--", text)
  150. return text
  151. end
  152. end