Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Readable wikitext: Difference between revisions

From Ophivana
touhou>Archimedes5000
m or not
(No difference)

Revision as of 00:12, 22 January 2024

Documentation for this module may be created at Module:Readable wikitext/doc

local p = {}
--lua pattern special chars escape function
function lua_escape(s)
   local chars = "[%^%$%(%)%%%.%[%]%*%+%-%?]"
   s = string.gsub(s, chars, "%%".."%0")
   return s
end
--main function
p.main = function(frame)
   local t = frame:getParent().args[1]
   local e = {}
   local tag = {}
   --replace escape tag "no" with strip markers and store values
   local i = 1
   for m, n in string.gmatch(t, "(<no.->(.-)</no>)") do
      t = string.gsub(t, lua_escape(m), "⌦"..i.."⌫")
      e[i] = lua_escape(n)
      i = i+1
   end
   --replace all opening/closing/self-closing tags with strip markers and store their insides
   i = 1
   for m in string.gmatch(t, "(<.->)") do
      t = string.gsub(t, lua_escape(m), "⌦TAG"..i.."⌫")
      tag[i] = lua_escape(m)
      i = i+1
   end
   --remove all whitespace
   t = string.gsub(t, "%s%s+", "")
   --remove newlines
   t = string.gsub(t, "\n", "")
   --replace back escape tag contents
   for m, n in string.gmatch(t, "(⌦(%d+)⌫)") do
      n = tonumber(n)
      t = string.gsub(t, lua_escape(m), e[n])
   end
   --replace back tags
   for m, n in string.gmatch(t, "(⌦TAG(%d+)⌫)") do
      n = tonumber(n)
      t = string.gsub(t, lua_escape(m), tag[n])
   end
   --return
   return t
end
return p