Module:Module introspection: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
Ganaram inukshuk (talk | contribs)
No edit summary
Line 67: Line 67:
return table.concat(lines, "\n")
return table.concat(lines, "\n")
end
end
-- Helper function
-- Blanks strings while preserving line numbers
function p.strip_strings(code_unstripped)
-- Handle long bracketed strings [[...]]
code_unstripped = code_unstripped:gsub("%[%[(.-)%]%]", function(s)
-- Replace all non-newline characters with spaces
local blanked = s:gsub("[^\n]", " ")
return blanked .. "]]"
end)
-- Handle double-quoted strings not inside require()
code_unstripped = code_unstripped:gsub('(%f[%w]require%s*%(%s*)"(.-)"', function(prefix, s)
return prefix .. '"' .. s .. '"'
end)
code_unstripped = code_unstripped:gsub('"[^"\n]*"', function(s)
return string.rep(" ", #s)
end)
-- Handle single-quoted strings not inside require()
code_unstripped = code_unstripped:gsub("(%f[%w]require%s*%(%s*)'(.-)'", function(prefix, s)
return prefix .. "'" .. s .. "'"
end)
code_unstripped = code_unstripped:gsub("'[^'\n]*'", function(s)
return string.rep(" ", #s)
end)
return code_unstripped
end


-- Helper function
-- Helper function
Line 263: Line 233:
local code = title:getContent()
local code = title:getContent()
code = p.strip_comments(code) -- Blank-out comments
code = p.strip_comments(code) -- Blank-out comments
code = p.strip_strings(code) -- Blank out strings
-- Get dependencies and their functions used, then build a table
-- Get dependencies and their functions used, then build a table