Module:SMWUtils: Difference between revisions
Fix column index calculation - account for subject column in broadtable format (via update-page on MediaWiki MCP Server) |
Add mainlabel support to include subject/page name column in output (via update-page on MediaWiki MCP Server) |
||
| Line 112: | Line 112: | ||
-- printouts: Comma-separated properties to display | -- printouts: Comma-separated properties to display | ||
-- headers: Comma-separated header names | -- headers: Comma-separated header names | ||
-- mainlabel: Optional header for the subject/page name column | |||
-- format: table, ul, ol (default: table) | -- format: table, ul, ol (default: table) | ||
-- intro: Custom intro text | -- intro: Custom intro text | ||
| Line 122: | Line 123: | ||
local headers = trim(args.headers) or '' | local headers = trim(args.headers) or '' | ||
local intro = trim(args.intro) | local intro = trim(args.intro) | ||
local mainlabel = trim(args.mainlabel) | |||
if not minProperty then | if not minProperty then | ||
| Line 184: | Line 186: | ||
if format == 'ul' then | if format == 'ul' then | ||
return p._formatList(filtered, printoutProps, headerList, 'ul', introText) | return p._formatList(filtered, printoutProps, headerList, 'ul', introText, mainlabel) | ||
elseif format == 'ol' then | elseif format == 'ol' then | ||
return p._formatList(filtered, printoutProps, headerList, 'ol', introText) | return p._formatList(filtered, printoutProps, headerList, 'ol', introText, mainlabel) | ||
else | else | ||
return p._formatTable(filtered, printoutProps, headerList, introText) | return p._formatTable(filtered, printoutProps, headerList, introText, mainlabel) | ||
end | end | ||
end | end | ||
| Line 201: | Line 203: | ||
local headers = trim(args.headers) or '' | local headers = trim(args.headers) or '' | ||
local intro = trim(args.intro) | local intro = trim(args.intro) | ||
local mainlabel = trim(args.mainlabel) | |||
if not maxProperty then | if not maxProperty then | ||
| Line 261: | Line 264: | ||
if format == 'ul' then | if format == 'ul' then | ||
return p._formatList(filtered, printoutProps, headerList, 'ul', introText) | return p._formatList(filtered, printoutProps, headerList, 'ul', introText, mainlabel) | ||
elseif format == 'ol' then | elseif format == 'ol' then | ||
return p._formatList(filtered, printoutProps, headerList, 'ol', introText) | return p._formatList(filtered, printoutProps, headerList, 'ol', introText, mainlabel) | ||
else | else | ||
return p._formatTable(filtered, printoutProps, headerList, introText) | return p._formatTable(filtered, printoutProps, headerList, introText, mainlabel) | ||
end | end | ||
end | end | ||
| Line 271: | Line 274: | ||
-- Format results as a wikitable | -- Format results as a wikitable | ||
-- Note: row[1] is subject, row[2..n+1] are printouts, row[n+2] is min/max value | -- Note: row[1] is subject, row[2..n+1] are printouts, row[n+2] is min/max value | ||
function p._formatTable(rows, printouts, headers, introText) | function p._formatTable(rows, printouts, headers, introText, mainlabel) | ||
local output = {} | local output = {} | ||
| Line 279: | Line 282: | ||
-- Header row | -- Header row | ||
table.insert(output, '|-\n! #') | table.insert(output, '|-\n! #') | ||
if mainlabel then | |||
table.insert(output, ' !! ' .. mainlabel) | |||
end | |||
for i, prop in ipairs(printouts) do | for i, prop in ipairs(printouts) do | ||
local headerText = headers[i] or prop:gsub('.*%.', ''):gsub('Has ', '') | local headerText = headers[i] or prop:gsub('.*%.', ''):gsub('Has ', '') | ||
| Line 288: | Line 294: | ||
for i, row in ipairs(rows) do | for i, row in ipairs(rows) do | ||
local tr = '|-\n| ' .. tostring(i) | local tr = '|-\n| ' .. tostring(i) | ||
if mainlabel then | |||
tr = tr .. ' || ' .. (row[1] or '') | |||
end | |||
for j = 1, #printouts do | for j = 1, #printouts do | ||
-- Printout j is in column j+1 (offset by subject column) | -- Printout j is in column j+1 (offset by subject column) | ||
| Line 301: | Line 310: | ||
-- Format results as a list | -- Format results as a list | ||
function p._formatList(rows, printouts, headers, listType, introText) | function p._formatList(rows, printouts, headers, listType, introText, mainlabel) | ||
local output = {} | local output = {} | ||
local bullet = listType == 'ol' and '#' or '*' | local bullet = listType == 'ol' and '#' or '*' | ||
| Line 309: | Line 318: | ||
for _, row in ipairs(rows) do | for _, row in ipairs(rows) do | ||
local parts = {} | local parts = {} | ||
if mainlabel then | |||
local subject = row[1] | |||
if subject and subject ~= '' then | |||
table.insert(parts, subject) | |||
end | |||
end | |||
for j = 1, #printouts do | for j = 1, #printouts do | ||
-- Printout j is in column j+1 (offset by subject column) | -- Printout j is in column j+1 (offset by subject column) | ||