Module:Module introspection: Difference between revisions
for modules that return a function, say there is no function name (to be changed later) |
mNo edit summary |
||
| Line 10: | Line 10: | ||
-- - Identify invokable functions, which are functions that accept a single | -- - Identify invokable functions, which are functions that accept a single | ||
-- param called "frame". | -- param called "frame". | ||
-- Inspects a module for its functions, its dependencies, and the functions used | -- Inspects a module for its functions, its dependencies, and the functions used | ||
| Line 22: | Line 20: | ||
-- quotes). | -- quotes). | ||
-- Helper function | -- Helper function: preprocess lua code | ||
-- Blanks comments but preserves line numbers | -- Blanks comments but preserves line numbers | ||
function p. | function p.preprocess_code(code_unstripped) | ||
if not code_unstripped then return "" end | if not code_unstripped then return "" end | ||
| Line 79: | Line 77: | ||
-- Only consider dependencies that have "Module:" in its name | -- Only consider dependencies that have "Module:" in its name | ||
local raw_deps = {} -- Dependencies used; unsorted and to be processed in step 2 | local raw_deps = {} -- Dependencies used; unsorted and to be processed in step 2 | ||
local pattern = [[local%s+([%w_]+)%s*=%s*require%(%s*["']([^"']+)["']%s*%)%.?([%w_%.]*)]] | |||
for var, dep, direct_func in code:gmatch(pattern) do | |||
if dep:match("^Module:") then | if dep:match("^Module:") then | ||
if direct_func == "" then direct_func = nil end | if direct_func == "" then direct_func = nil end | ||
| Line 317: | Line 316: | ||
local module_name = args["Module Name" ] | local module_name = args["Module Name" ] | ||
local main_function = args["Main Function"] | local main_function = args["Main Function"] | ||
local descriptions = args["Descriptions"] | local descriptions = args["Descriptions" ] | ||
-- Preprocess module and blank-out comments | -- Preprocess module and blank-out comments | ||
local title = mw.title.new('Module:' .. module_name) | local title = mw.title.new('Module:' .. module_name) | ||
local code = title:getContent() | local code = title:getContent() | ||
code = p. | code = p.preprocess_code(code) -- Blank-out comments | ||
-- Get dependencies and their functions used, then build a table | -- Get dependencies and their functions used, then build a table | ||