Module:PrinterInstructionsLookup: Difference between revisions
Created page with "local p = {} function p.getInstructions(frame) local args = frame:getParent().args local model = args['Printer Model'] or '' local access = args['Access'] or '' local output = {} -- Normalize for case-insensitive matching local modelLower = mw.ustring.lower(model) -- Check if model contains each brand local hasCanon = mw.ustring.find(modelLower, 'canon', 1, true) ~= nil local hasHP = mw.ustring.find(modelLower, 'hp',..." |
No edit summary |
||
| Line 5: | Line 5: | ||
local model = args['Printer Model'] or '' | local model = args['Printer Model'] or '' | ||
local access = args['Access'] or '' | local access = args['Access'] or '' | ||
local printRelease = args['Print Release Station'] or '' | |||
local output = {} | local output = {} | ||
| Line 26: | Line 27: | ||
local hasEmployees = accessSet['Employees'] == true | local hasEmployees = accessSet['Employees'] == true | ||
local hasStudents = accessSet['Students'] == true | local hasStudents = accessSet['Students'] == true | ||
-- Check Print Release Station (handles Yes/yes/true/1/on) | |||
local prLower = mw.ustring.lower(mw.ustring.gsub(printRelease, '^%s*(.-)%s*$', '%1')) | |||
local hasPrintRelease = (prLower == 'yes' or prLower == 'true' | |||
or prLower == '1' or prLower == 'on') | |||
-- Dymo: include regardless of access | -- Dymo: include regardless of access | ||
| Line 50: | Line 56: | ||
if hasHP and hasStudents then | if hasHP and hasStudents then | ||
table.insert(output, '{{Printing Instructions for Students - HP}}') | table.insert(output, '{{Printing Instructions for Students - HP}}') | ||
end | |||
-- Print Release Station instructions | |||
if hasPrintRelease then | |||
table.insert(output, '{{Print Release Station Instructions}}') | |||
end | end | ||
Latest revision as of 14:40, 14 May 2026
This module handles the conditional inclusion of printing instruction templates on printer pages, based on the printer's model, access permissions, and print release station status. It is invoked by Template:Printer.
Usage
This module is not intended to be called directly. It is invoked from within the Printer template using:
{{#invoke:PrinterInstructionsLookup|getInstructions}}
The module reads its inputs from the parent template's parameters, so it must be invoked from a template that has the relevant parameters defined.
Parameters Read
The module reads the following parameters from the parent template:
| Parameter | Type | Description |
|---|---|---|
Printer Model |
String | The full model name of the printer (e.g., "Canon imageRUNNER 2630", "HP LaserJet 4555", "Dymo LabelWriter 450"). Matched case-insensitively against known brand names. |
Access |
Comma-separated list | Who is permitted to use the printer. Recognized values: Employees, Students. Multiple values may be supplied, separated by commas.
|
Print Release Station |
Boolean | Whether the printer has a print release station. Truthy values: Yes, yes, true, 1, on (case-insensitive). Any other value is treated as false.
|
Logic
The module evaluates each of the following conditions independently and appends the matching template to the output. A single printer may trigger multiple inclusions.
| Condition | Template Included |
|---|---|
| Printer Model contains "Dymo" | {{Printing Instructions for Dymo LabelWriter}}
|
| Printer Model contains "Canon" AND Access includes "Employees" | {{Printing Instructions for Employees - Canon}}
|
| Printer Model contains "HP" AND Access includes "Employees" | {{Printing Instructions for Employees - HP}}
|
| Printer Model contains "Canon" AND Access includes "Students" | {{Printing Instructions for Students - Canon}}
|
| Printer Model contains "HP" AND Access includes "Students" | {{Printing Instructions for Students - HP}}
|
| Print Release Station is truthy | {{Print Release Station Instructions}}
|
Brand matching is case-insensitive and uses substring matching, so "canon imagerunner" and "Canon imageRUNNER" both match the Canon condition.
The Dymo condition does not depend on the Access value — Dymo instructions are included for any printer with "Dymo" in the model, regardless of who can use it.
Output
The module returns wikitext containing the matched template inclusions, joined by newlines. The output is preprocessed via frame:preprocess() so that template calls are expanded rather than appearing as literal text.
If no conditions match, the module returns an empty string.
Functions
getInstructions(frame)
The single entry point of the module. Reads parameters from the parent frame, evaluates all conditions, and returns the assembled wikitext.
- Parameters
frame— The Scribunto frame object. The module callsframe:getParent().argsto access the invoking template's parameters.
- Returns
- A string of wikitext containing zero or more template inclusions.
Adding a New Brand
To add support for an additional printer brand (for example, Brother):
- Create the relevant instruction template(s), such as
Template:Printing Instructions for Employees - Brother. - Edit Module:PrinterInstructionsLookup and add a detection line near the existing brand checks:
local hasBrother = mw.ustring.find(modelLower, 'brother', 1, true) ~= nil
- Add the corresponding conditional blocks alongside the Canon and HP ones:
if hasBrother and hasEmployees then table.insert(output, '{{Printing Instructions for Employees - Brother}}') end
No changes to the Printer template are required.
Adding a New Access Type
To add a new access type (for example, "Guests"):
- Update the Printer template's
Accessparameter to accept the new value (in the<templatedata>block and any forms). - In the module, add a lookup line near the existing ones:
local hasGuests = accessSet['Guests'] == true
- Add the conditional blocks for the brand/access combinations you want to support.
- If a new category is needed, add it to the
#arraymap/#switchblock in the Printer template.
Debugging
To inspect the values the module is receiving, temporarily add a debug return at the end of getInstructions, before the final return frame:preprocess(result):
return 'Model: "' .. model .. '" | Access: "' .. access
.. '" | PrintRelease: "' .. printRelease .. '"'
.. ' | Canon: ' .. tostring(hasCanon)
.. ' | HP: ' .. tostring(hasHP)
.. ' | Dymo: ' .. tostring(hasDymo)
.. ' | Employees: ' .. tostring(hasEmployees)
.. ' | Students: ' .. tostring(hasStudents)
.. ' | PR: ' .. tostring(hasPrintRelease)
This will replace the normal output with a diagnostic string showing what the module parsed from the parameters. Remember to remove the debug line once finished.
Dependencies
- Extension:Scribunto — required to run Lua modules.
- Template:Printer — the template that invokes this module.
- The instruction templates referenced above. Missing templates will produce red links in the rendered output.
See also
- Template:Printer — the template that invokes this module
- Category:FacStaff Printer — printers accessible to employees
- Category:Student Printer — printers accessible to students
local p = {}
function p.getInstructions(frame)
local args = frame:getParent().args
local model = args['Printer Model'] or ''
local access = args['Access'] or ''
local printRelease = args['Print Release Station'] or ''
local output = {}
-- Normalize for case-insensitive matching
local modelLower = mw.ustring.lower(model)
-- Check if model contains each brand
local hasCanon = mw.ustring.find(modelLower, 'canon', 1, true) ~= nil
local hasHP = mw.ustring.find(modelLower, 'hp', 1, true) ~= nil
local hasDymo = mw.ustring.find(modelLower, 'dymo', 1, true) ~= nil
-- Parse the Access list into a set for easy lookup
local accessSet = {}
for value in mw.ustring.gmatch(access, '([^,]+)') do
-- Trim whitespace
local trimmed = mw.ustring.gsub(value, '^%s*(.-)%s*$', '%1')
accessSet[trimmed] = true
end
local hasEmployees = accessSet['Employees'] == true
local hasStudents = accessSet['Students'] == true
-- Check Print Release Station (handles Yes/yes/true/1/on)
local prLower = mw.ustring.lower(mw.ustring.gsub(printRelease, '^%s*(.-)%s*$', '%1'))
local hasPrintRelease = (prLower == 'yes' or prLower == 'true'
or prLower == '1' or prLower == 'on')
-- Dymo: include regardless of access
if hasDymo then
table.insert(output, '{{Printing Instructions for Dymo LabelWriter}}')
end
-- Canon + Employees
if hasCanon and hasEmployees then
table.insert(output, '{{Printing Instructions for Employees - Canon}}')
end
-- HP + Employees
if hasHP and hasEmployees then
table.insert(output, '{{Printing Instructions for Employees - HP}}')
end
-- Canon + Students
if hasCanon and hasStudents then
table.insert(output, '{{Printing Instructions for Students - Canon}}')
end
-- HP + Students
if hasHP and hasStudents then
table.insert(output, '{{Printing Instructions for Students - HP}}')
end
-- Print Release Station instructions
if hasPrintRelease then
table.insert(output, '{{Print Release Station Instructions}}')
end
-- Join and preprocess so the template calls actually expand
local result = table.concat(output, '\n')
return frame:preprocess(result)
end
return p