I test your code but it seems it does not handle all cases at all:
print(quote"]=]==")
[==[
]=]==]==]
Oops! Indeed... My bad.
This error can be fixed by appending a closing bracket to the input string before processing:
function quote (s)
-- find maximum length of sequences of equal signs
local n = -1
for w in string.gmatch(s.."]", "]([]=]*)]") do
for e in string.gmatch(w, "=*") do
n = math.max(n, #e)
end
end
-- The rest of the code is exactly the same as in the book