Module:CRTModel: Difference between revisions
No edit summary |
No edit summary |
||
| Line 208: | Line 208: | ||
) | ) | ||
-- Query for documents | -- Query for documents using broadtable format which is easier to parse | ||
local documentsQueryWikitext = | local documentsQueryWikitext = | ||
'{{#ask:\n' .. | '{{#ask:\n' .. | ||
' [[Category:Documents]]\n' .. | ' [[Category:Documents]]\n' .. | ||
' [[Has related CRT model::' .. fullPageName .. ']]\n' .. | ' [[Has related CRT model::' .. fullPageName .. ']]\n' .. | ||
' |format= | ' |?Has document type\n' .. | ||
' |?Has document URL\n' .. | |||
' |format=broadtable\n' .. | |||
' |headers=hide\n' .. | |||
' |mainlabel=-\n' .. | |||
' |link=none\n' .. | |||
'}}' | '}}' | ||
local | local documentsResult = frame:preprocess(documentsQueryWikitext) | ||
local hasDocuments = | |||
-- Check if we have any documents (the result will be a table if there are docs) | |||
local hasDocuments = documentsResult and trim(documentsResult) and documentsResult:find('<table') | |||
if hasDocuments then | if hasDocuments then | ||
-- | -- Parse the table and rebuild as a bulleted list | ||
local docListHtml = '<ul style="margin:0; padding-left:1.5em;">' | local docListHtml = '<ul style="margin:0; padding-left:1.5em;">' | ||
-- | -- Extract rows from the table - look for patterns like <td>Type</td><td>URL</td> | ||
for | for docType, docUrl in documentsResult:gmatch('<td>([^<]*)</td>%s*<td>([^<]*)</td>') do | ||
docType = trim(docType) | |||
docUrl = trim(docUrl) | |||
if docUrl and docUrl ~= '' then | |||
local linkText = (docType and docType ~= '') and docType or 'Document' | |||
docListHtml = docListHtml .. '<li><a href="' .. docUrl .. '" class="external">' .. linkText .. '</a></li>' | |||
end | end | ||
end | end | ||
| Line 253: | Line 242: | ||
docListHtml = docListHtml .. '</ul>' | docListHtml = docListHtml .. '</ul>' | ||
-- Add the documents list and upload link | -- Add the documents list and upload link | ||
local combinedHtml = docListHtml .. '<div style="margin-top:0.5em; font-style:italic;"><a href="' .. tostring(uploadUrl) .. '" class="external">Upload a document</a></div>' | local combinedHtml = docListHtml .. '<div style="margin-top:0.5em; font-style:italic;"><a href="' .. tostring(uploadUrl) .. '" class="external">Upload a document</a></div>' | ||
addFullRow(tbl, combinedHtml, 'padding:0.2em 0.4em;', true) | addFullRow(tbl, combinedHtml, 'padding:0.2em 0.4em;', true) | ||
else | else | ||
-- Just show the upload link | -- Just show the upload link | ||
local uploadLinkHtml = '<div style="font-style:italic;"><a href="' .. tostring(uploadUrl) .. '" class="external">Upload a document</a></div>' | local uploadLinkHtml = '<div style="font-style:italic;"><a href="' .. tostring(uploadUrl) .. '" class="external">Upload a document</a></div>' | ||
addFullRow(tbl, uploadLinkHtml, 'padding:0.2em 0.4em;', true) | addFullRow(tbl, uploadLinkHtml, 'padding:0.2em 0.4em;', true) | ||