Module:TemplateStarter: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 1: | Line 1: | ||
-- Module:TemplateStarter | -- Module:TemplateStarter | ||
-- Generates | -- Generates configurable template structures for new pages using ConfigRepository | ||
local p = {} | local p = {} | ||
| Line 8: | Line 8: | ||
local ErrorHandling = require('Module:ErrorHandling') | local ErrorHandling = require('Module:ErrorHandling') | ||
-- Cache for template lists | -- Cache for template lists | ||
local templateListCache = nil | local templateListCache = nil | ||
local templateListCacheTime = 0 | local templateListCacheTime = 0 | ||
local CACHE_DURATION = 300 -- 5 minutes | local CACHE_DURATION = 300 -- 5 minutes | ||
-- Generate empty template wikitext from template type | -- Generate empty template wikitext from template type | ||
| Line 20: | Line 20: | ||
end | end | ||
-- Sanitize input | -- Sanitize input: remove any potentially harmful characters | ||
templateType = mw.text.trim(tostring(templateType)) | templateType = mw.text.trim(tostring(templateType)) | ||
| Line 27: | Line 27: | ||
end | end | ||
-- Create error context | -- Create error context | ||
local errorContext = ErrorHandling.createContext("TemplateStarter") | local errorContext = ErrorHandling.createContext("TemplateStarter") | ||
| Line 95: | Line 95: | ||
lines[lineCount] = "}}" | lines[lineCount] = "}}" | ||
-- Add intro boilerplate after template if available | -- Add intro boilerplate after template (if available) | ||
if config.boilerplate and config.boilerplate.intro then | if config.boilerplate and config.boilerplate.intro then | ||
lineCount = lineCount + 1 | lineCount = lineCount + 1 | ||
| Line 104: | Line 104: | ||
lines[lineCount] = config.boilerplate.intro | lines[lineCount] = config.boilerplate.intro | ||
end | end | ||
-- Join with newlines | |||
-- Join with newlines | |||
return table.concat(lines, "\n") | return table.concat(lines, "\n") | ||
end | end | ||
| Line 146: | Line 138: | ||
end | end | ||
-- Generate a dynamic preload template (main function used by | -- Generate a dynamic preload template (main function used by JS) | ||
function p.preload(frame) | function p.preload(frame) | ||
local args = frame.args | local args = frame.args | ||
| Line 167: | Line 159: | ||
end | end | ||
-- | -- No complex error handling here | ||
local templates = {} | local templates = {} | ||
| Line 181: | Line 173: | ||
table.sort(templates) | table.sort(templates) | ||
end | end | ||
-- No fallback! If ConfigRepository fails, return empty list (single source of truth) | |||
-- No fallback | |||
-- Update cache | -- Update cache | ||
templateListCache = templates | templateListCache = templates | ||
| Line 192: | Line 182: | ||
end | end | ||
-- Test function to list available templates (clean output for | -- Test function to list available templates (clean output for JS consumption) | ||
function p.listTemplates(frame) | function p.listTemplates(frame) | ||
local templates = p.getAvailableTemplates() | local templates = p.getAvailableTemplates() | ||