Module:CRTModel: Difference between revisions

No edit summary
No edit summary
Line 208: Line 208:
)
)
-- Query for documents and build the list in Lua
-- 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=count\n' ..
' |?Has document type\n' ..
' |?Has document URL\n' ..
' |format=broadtable\n' ..
' |headers=hide\n' ..
' |mainlabel=-\n' ..
' |link=none\n' ..
'}}'
'}}'
local documentCount = frame:preprocess(documentsQueryWikitext)
local documentsResult = frame:preprocess(documentsQueryWikitext)
local hasDocuments = documentCount and tonumber(trim(documentCount)) and tonumber(trim(documentCount)) > 0
-- 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
-- Get the actual document data using #show for each document
-- Parse the table and rebuild as a bulleted list
-- First, get the list of document page names
local docPagesQueryWikitext =
'{{#ask:\n' ..
' [[Category:Documents]]\n' ..
' [[Has related CRT model::' .. fullPageName .. ']]\n' ..
' |limit=100\n' ..
'}}'
local docPagesResult = frame:preprocess(docPagesQueryWikitext)
-- Build links manually - use #show to get properties for each document
local docListHtml = '<ul style="margin:0; padding-left:1.5em;">'
local docListHtml = '<ul style="margin:0; padding-left:1.5em;">'
-- Parse document page names from the result
-- Extract rows from the table - look for patterns like <td>Type</td><td>URL</td>
for docPage in docPagesResult:gmatch('[^\n,]+') do
for docType, docUrl in documentsResult:gmatch('<td>([^<]*)</td>%s*<td>([^<]*)</td>') do
docPage = trim(docPage)
docType = trim(docType)
if docPage and docPage ~= '' then
docUrl = trim(docUrl)
-- Get document type and URL for this page
local docTypeWikitext = '{{#show:' .. docPage .. '|?Has document type}}'
if docUrl and docUrl ~= '' then
local docUrlWikitext = '{{#show:' .. docPage .. '|?Has document URL}}'
local linkText = (docType and docType ~= '') and docType or 'Document'
docListHtml = docListHtml .. '<li><a href="' .. docUrl .. '" class="external">' .. linkText .. '</a></li>'
local docType = trim(frame:preprocess(docTypeWikitext))
local docUrl = trim(frame:preprocess(docUrlWikitext))
if docUrl and docUrl ~= '' then
local linkText = docType or 'Document'
-- Use HTML anchor tag instead of wikitext syntax
docListHtml = docListHtml .. '<li><a href="' .. mw.text.encode(docUrl) .. '" class="external">' .. mw.text.encode(linkText) .. '</a></li>'
end
end
end
end
end
Line 253: Line 242:
docListHtml = docListHtml .. '</ul>'
docListHtml = docListHtml .. '</ul>'
-- Add the documents list and upload link (also as HTML anchor)
-- 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 as HTML anchor
-- 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)
MediaWiki Appliance - Powered by TurnKey Linux