prime.lua: skip colour sequences, add config
Colour sequence skipping is somewhat involved, we might want to
add a helper generator to the "degesch" Lua library, in the form of
{substring, is_formatting}.
formatter_parse_mirc() isn't useful, a pure Lua implementation
would be more appropriate (where do we put that?)
			
			
This commit is contained in:
		@@ -15,36 +15,54 @@
 | 
				
			|||||||
-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 | 
					-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 | 
				
			||||||
--
 | 
					--
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					local smallest, highlight = 0, "\x1f"
 | 
				
			||||||
 | 
					degesch.setup_config {
 | 
				
			||||||
 | 
						smallest = {
 | 
				
			||||||
 | 
							type = "integer",
 | 
				
			||||||
 | 
							default = "0",
 | 
				
			||||||
 | 
							comment = "smallest number to scan for primality",
 | 
				
			||||||
 | 
							on_change = function (v) smallest = math.max (v, 2) end
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
						highlight = {
 | 
				
			||||||
 | 
							type = "string",
 | 
				
			||||||
 | 
							default = "\"\\x1f\"",
 | 
				
			||||||
 | 
							comment = "the attribute to use for highlights",
 | 
				
			||||||
 | 
							on_change = function (v) highlight = v end
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- The prime test is actually very fast, so there is no DoS concern
 | 
					-- The prime test is actually very fast, so there is no DoS concern
 | 
				
			||||||
local is_prime = function (n)
 | 
					local do_intercolour = function (text)
 | 
				
			||||||
	if tonumber (n) < 2 then return false end
 | 
						return tostring (text:gsub ("%f[%w_]%d+", function (n)
 | 
				
			||||||
	for i = 2, n ^ (1 / 2) do if (n % i) == 0 then return false end end
 | 
							if tonumber (n) < smallest then return nil end
 | 
				
			||||||
	return true
 | 
							for i = 2, n ^ (1 / 2) do if (n % i) == 0 then return nil end end
 | 
				
			||||||
 | 
							return highlight .. n .. highlight
 | 
				
			||||||
 | 
						end))
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local do_interlink = function (run)
 | 
					local do_interlink = function (text)
 | 
				
			||||||
	-- Let's not cripple formatting, colours especially
 | 
						local rebuilt, last = {""}, 1
 | 
				
			||||||
	-- TODO: we can just employ the same approach as in do_text and skip over
 | 
						for start in text:gmatch ('()\x03') do
 | 
				
			||||||
	if run:find ('%c') then return run end
 | 
							table.insert (rebuilt, do_intercolour (text:sub (last, start - 1)))
 | 
				
			||||||
 | 
							local sub = text:sub (start + 1)
 | 
				
			||||||
	return run:gsub ("%f[%w_]%d+%f[^%w_]", function (number)
 | 
							last = start + (sub:match ('^%d%d?,%d%d?()') or sub:match ('^%d?%d?()'))
 | 
				
			||||||
		if is_prime (number) then return "\x1f" .. number .. "\x1f" end
 | 
							table.insert (rebuilt, text:sub (start, last - 1))
 | 
				
			||||||
		return number
 | 
						end
 | 
				
			||||||
	end)
 | 
						return table.concat (rebuilt) .. do_intercolour (text:sub (last))
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local do_text = function (text)
 | 
					local do_message = function (text)
 | 
				
			||||||
	local rebuilt, last = {""}, 1
 | 
						local rebuilt, last = {""}, 1
 | 
				
			||||||
	for run, link, endpos in text:gmatch ('(.-)(%f[%g]https?://%g+)()') do
 | 
						for run, link, endpos in text:gmatch ('(.-)(%f[%g]https?://%g+)()') do
 | 
				
			||||||
		last = endpos
 | 
							last = endpos
 | 
				
			||||||
		table.insert (rebuilt, do_interlink (run) .. link)
 | 
							table.insert (rebuilt, do_interlink (run) .. link)
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
	return table.concat (rebuilt, "") .. do_interlink (text:sub (last))
 | 
						return table.concat (rebuilt) .. do_interlink (text:sub (last))
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- XXX: sadly it won't typically highlight primes in our own messages,
 | 
					-- XXX: sadly it won't typically highlight primes in our own messages,
 | 
				
			||||||
-- unless IRCv3 echo-message is on
 | 
					-- unless IRCv3 echo-message is on
 | 
				
			||||||
degesch.hook_irc (function (hook, server, line)
 | 
					degesch.hook_irc (function (hook, server, line)
 | 
				
			||||||
	local start, message = line:match ("^(.- PRIVMSG .- :)(.*)$")
 | 
						local start, message = line:match ("^(.- PRIVMSG .- :)(.*)$")
 | 
				
			||||||
	return message and start .. do_text (message) or line
 | 
						return message and start .. do_message (message) or line
 | 
				
			||||||
end)
 | 
					end)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user