Module:Module introspection: Difference between revisions

Ganaram inukshuk (talk | contribs)
for modules that return a function, say there is no function name (to be changed later)
Ganaram inukshuk (talk | contribs)
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".
-- - Allow descriptions to be added to functions, as params called
--  desc_function_name. One description per function.


-- 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.strip_comments(code_unstripped)
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
for var, dep, direct_func in code:gmatch([[local%s+([%w_]+)%s*=%s*require%(%s*["']([^"']+)["']%s*%)%.?([%w_%.]*)]]) do
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.strip_comments(code) -- Blank-out comments
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