Module:TemplateStarter: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 1: Line 1:
-- Module:TemplateStarter
-- Module:TemplateStarter
-- Generates empty template structures for new pages using ConfigRepository
-- 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 to improve performance
-- Cache for template lists
local templateListCache = nil
local templateListCache = nil
local templateListCacheTime = 0
local templateListCacheTime = 0
local CACHE_DURATION = 300 -- 5 minutes in seconds
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 - remove any potentially harmful characters
     -- 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 for better error handling
     -- 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
   
 
    -- Add outro boilerplate if available (kept for backward compatibility)
     -- Join with newlines
    if config.boilerplate and config.boilerplate.outro then
        lineCount = lineCount + 1
        lines[lineCount] = ""  -- Empty line for spacing
        lineCount = lineCount + 1
        lines[lineCount] = config.boilerplate.outro
    end
   
     -- Join with newlines (more efficient than multiple concatenations)
     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 JavaScript)
-- 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
      
      
     -- Simple approach without complex error handling
     -- 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 - if ConfigRepository fails, return empty list
 
    -- This ensures we only use data from the single source of truth
   
     -- Update cache
     -- Update cache
     templateListCache = templates
     templateListCache = templates
Line 192: Line 182:
end
end


-- Test function to list available templates (clean output for JavaScript consumption)
-- 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()