Module:T-LibraryInterview: Difference between revisions
Appearance
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 7: | Line 7: | ||
* integration with other ICANNWiki modules. | * integration with other ICANNWiki modules. | ||
* | * | ||
]] | ]] | ||
| Line 39: | Line 15: | ||
local ErrorHandling = require('Module:ErrorHandling') | local ErrorHandling = require('Module:ErrorHandling') | ||
local ConfigRepository = require('Module:ConfigRepository') | local ConfigRepository = require('Module:ConfigRepository') | ||
-- ========== Module-level caches ========== | |||
-- Cache for wiki link extraction (key: input string, value: extracted name) | |||
local extractCache = {} | |||
-- Cache for current page ID (single value cache) | |||
local currentPageIdCache = nil | |||
-- ========== Helper Functions ========== | -- ========== Helper Functions ========== | ||
-- Extract page name from wiki link [[Name]] or [[Name|Text]] | -- Extract page name from wiki link [[Name]] or [[Name|Text]] | ||
-- Optimized with anchored patterns and caching | |||
local function extractFromWikiLink(value) | local function extractFromWikiLink(value) | ||
local name = value:match("%[%[([^%|%]]+)%]%]") | -- Early return for nil or empty values | ||
if not value or value == "" then | |||
return value | |||
end | |||
-- Check cache first | |||
if extractCache[value] then | |||
return extractCache[value] | |||
end | |||
-- Optimized pattern matching with anchored patterns where possible | |||
-- First try exact [[Name]] pattern | |||
local name = value:match("^%[%[([^%|%]]+)%]%]$") | |||
-- If not found, try [[Name|Text]] pattern | |||
if not name then | |||
name = value:match("%[%[([^%|%]]+)%|.-%]%]") | |||
end | |||
-- If still not found, try unanchored [[Name]] pattern as fallback | |||
if not name then | |||
name = value:match("%[%[([^%|%]]+)%]%]") | |||
end | |||
-- Store result in cache | |||
local result = name or value | |||
extractCache[value] = result | |||
return result | |||
end | end | ||
-- Get current page ID | -- Get current page ID with caching | ||
local function getCurrentPageId() | local function getCurrentPageId() | ||
-- Return cached value if available | |||
if currentPageIdCache ~= nil then | |||
return currentPageIdCache | |||
end | |||
-- Get page ID and cache it | |||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
currentPageIdCache = title and title.id | |||
return currentPageIdCache | |||
end | end | ||
-- Create error context for the module | |||
local errorContext = ErrorHandling.createContext("T-LibraryInterview") | |||
-- ========== Template Registration ========== | -- ========== Template Registration ========== | ||
| Line 96: | Line 118: | ||
-- ========== Preprocessors ========== | -- ========== Preprocessors ========== | ||
-- Process a person field to extract wiki links | |||
-- Modularized function to handle both Interviewer and Interviewee fields | |||
local function processPersonField(args, fieldName) | |||
-- Early return if field is empty | |||
if not args[fieldName] or args[fieldName] == "" then | |||
return | |||
end | |||
-- Store the original value for display | |||
local originalKey = "_original" .. fieldName | |||
args[originalKey] = args[fieldName] | |||
-- Extract the name from wiki link for semantic processing using protected call | |||
args[fieldName] = ErrorHandling.protect( | |||
errorContext, | |||
"extractFromWikiLink_" .. fieldName, | |||
extractFromWikiLink, | |||
args[fieldName], -- fallback to original value on error | |||
args[fieldName] | |||
) | |||
end | |||
-- Add preprocessor for wiki link extraction | -- Add preprocessor for wiki link extraction | ||
Blueprint.addPreprocessor(template, function(template, args) | Blueprint.addPreprocessor(template, function(template, args) | ||
-- Process | -- Process person fields with modularized function | ||
processPersonField(args, "Interviewee") | |||
processPersonField(args, "Interviewer") | |||
return args | return args | ||
| Line 122: | Line 154: | ||
-- Main render function that delegates to the template's render method | -- Main render function that delegates to the template's render method | ||
function p.render(frame) | function p.render(frame) | ||
return template.render(frame) | return ErrorHandling.protect( | ||
errorContext, | |||
"render", | |||
function() | |||
return template.render(frame) | |||
end, | |||
"<!-- Error rendering LibraryInterview template -->", | |||
frame | |||
) | |||
end | end | ||
return p | return p | ||
Revision as of 11:49, 21 April 2025
Documentation for this module may be created at Module:T-LibraryInterview/doc
--[[
* T-LibraryInterview.lua
* Module for rendering the Library Interview template using the Blueprint framework
*
* This module implements the Library Interview template functionality using the
* standardized Blueprint architecture, providing improved maintainability and
* integration with other ICANNWiki modules.
*
]]
local p = {}
-- ========== Required modules ==========
local Blueprint = require('Module:LuaTemplateBlueprint')
local ErrorHandling = require('Module:ErrorHandling')
local ConfigRepository = require('Module:ConfigRepository')
-- ========== Module-level caches ==========
-- Cache for wiki link extraction (key: input string, value: extracted name)
local extractCache = {}
-- Cache for current page ID (single value cache)
local currentPageIdCache = nil
-- ========== Helper Functions ==========
-- Extract page name from wiki link [[Name]] or [[Name|Text]]
-- Optimized with anchored patterns and caching
local function extractFromWikiLink(value)
-- Early return for nil or empty values
if not value or value == "" then
return value
end
-- Check cache first
if extractCache[value] then
return extractCache[value]
end
-- Optimized pattern matching with anchored patterns where possible
-- First try exact [[Name]] pattern
local name = value:match("^%[%[([^%|%]]+)%]%]$")
-- If not found, try [[Name|Text]] pattern
if not name then
name = value:match("%[%[([^%|%]]+)%|.-%]%]")
end
-- If still not found, try unanchored [[Name]] pattern as fallback
if not name then
name = value:match("%[%[([^%|%]]+)%]%]")
end
-- Store result in cache
local result = name or value
extractCache[value] = result
return result
end
-- Get current page ID with caching
local function getCurrentPageId()
-- Return cached value if available
if currentPageIdCache ~= nil then
return currentPageIdCache
end
-- Get page ID and cache it
local title = mw.title.getCurrentTitle()
currentPageIdCache = title and title.id
return currentPageIdCache
end
-- Create error context for the module
local errorContext = ErrorHandling.createContext("T-LibraryInterview")
-- ========== Template Registration ==========
-- Register the template with the Blueprint
local template = Blueprint.registerTemplate('LibraryInterview')
-- Initialize the standard configuration
Blueprint.initializeConfig(template)
-- Set the table class to "library-box"
template.config.constants = template.config.constants or {}
template.config.constants.tableClass = "library-box"
-- ========== Custom Field Processors ==========
-- Add custom field processors
template.config.processors = {
-- ID processor - use current page ID if not provided
ID = function(value, args, template)
if not value or value == "" then
return tostring(getCurrentPageId() or "")
end
return value
end,
-- Date processor - uses date format from central configuration
Date = function(value, args, template)
local NormalizationDate = require('Module:NormalizationDate')
return NormalizationDate.formatDate(value)
end,
-- Interviewer processor - use original value with wiki links for display
Interviewer = function(value, args, template)
return args._originalInterviewer or value
end,
-- Interviewee processor - use original value with wiki links for display
Interviewee = function(value, args, template)
return args._originalInterviewee or value
end
}
-- ========== Preprocessors ==========
-- Process a person field to extract wiki links
-- Modularized function to handle both Interviewer and Interviewee fields
local function processPersonField(args, fieldName)
-- Early return if field is empty
if not args[fieldName] or args[fieldName] == "" then
return
end
-- Store the original value for display
local originalKey = "_original" .. fieldName
args[originalKey] = args[fieldName]
-- Extract the name from wiki link for semantic processing using protected call
args[fieldName] = ErrorHandling.protect(
errorContext,
"extractFromWikiLink_" .. fieldName,
extractFromWikiLink,
args[fieldName], -- fallback to original value on error
args[fieldName]
)
end
-- Add preprocessor for wiki link extraction
Blueprint.addPreprocessor(template, function(template, args)
-- Process person fields with modularized function
processPersonField(args, "Interviewee")
processPersonField(args, "Interviewer")
return args
end)
-- ========== Main Render Function ==========
-- Main render function that delegates to the template's render method
function p.render(frame)
return ErrorHandling.protect(
errorContext,
"render",
function()
return template.render(frame)
end,
"<!-- Error rendering LibraryInterview template -->",
frame
)
end
return p