Module:CRTModel: Difference between revisions

No edit summary
No edit summary
Line 197: Line 197:
addSectionRow(tbl, 'Documents')
addSectionRow(tbl, 'Documents')
-- Build the upload link
-- Build the upload link
local target = pageName .. " - document"
local target = pageName .. " - document"
local uploadUrl = mw.uri.fullUrl(
local uploadUrl = mw.uri.fullUrl(
Line 208: Line 208:
)
)
-- Query for documents using broadtable format which is easier to parse
-- Query for documents - use plainlist format which gives us clean text
local documentsQueryWikitext =
local documentsListWikitext =
'{{#ask:\n' ..
'{{#ask:\n' ..
' [[Category:Documents]]\n' ..
' [[Category:Documents]]\n' ..
Line 215: Line 215:
' |?Has document type\n' ..
' |?Has document type\n' ..
' |?Has document URL\n' ..
' |?Has document URL\n' ..
' |format=broadtable\n' ..
' |format=plainlist\n' ..
' |headers=hide\n' ..
' |limit=100\n' ..
' |mainlabel=-\n' ..
' |link=none\n' ..
'}}'
'}}'
local documentsResult = frame:preprocess(documentsQueryWikitext)
local documentsList = frame:preprocess(documentsListWikitext)
-- Check if we have any documents (the result will be a table if there are docs)
-- Check if we have documents
local hasDocuments = documentsResult and trim(documentsResult) and documentsResult:find('<table')
local hasDocuments = documentsList and trim(documentsList) and trim(documentsList) ~= ''
if hasDocuments then
if hasDocuments then
-- Parse the table and rebuild as a bulleted list
-- Build list by extracting document data using a simpler query approach
local docListHtml = '<ul style="margin:0; padding-left:1.5em;">'
-- Query each property separately to avoid parsing issues
local docListWikitext = '<ul style="margin:0; padding-left:1.5em;">'
-- Extract rows from the table - look for patterns like <td>Type</td><td>URL</td>
-- Get list of document pages
for docType, docUrl in documentsResult:gmatch('<td>([^<]*)</td>%s*<td>([^<]*)</td>') do
local docPagesWikitext =
docType = trim(docType)
'{{#ask:\n' ..
docUrl = trim(docUrl)
' [[Category:Documents]]\n' ..
' [[Has related CRT model::' .. fullPageName .. ']]\n' ..
if docUrl and docUrl ~= '' then
' |limit=100\n' ..
local linkText = (docType and docType ~= '') and docType or 'Document'
' |link=none\n' ..
docListHtml = docListHtml .. '<li><a href="' .. docUrl .. '" class="external">' .. linkText .. '</a></li>'
'}}'
local docPages = frame:preprocess(docPagesWikitext)
-- Process each document page
if docPages and trim(docPages) then
-- Split by comma or newline
for docPage in docPages:gmatch('[^,\n]+') do
docPage = trim(docPage)
if docPage and docPage ~= '' then
-- Get type and URL using #show
local typeQuery = '{{#show:' .. docPage .. '|?Has document type}}'
local urlQuery = '{{#show:' .. docPage .. '|?Has document URL}}'
local docType = frame:preprocess(typeQuery)
local docUrl = frame:preprocess(urlQuery)
docType = trim(docType)
docUrl = trim(docUrl)
if docUrl and docUrl ~= '' then
local displayText = (docType and docType ~= '') and docType or 'Document'
docListWikitext = docListWikitext .. '<li><a href="' .. docUrl .. '" class="external">' .. displayText .. '</a></li>'
end
end
end
end
end
end
docListHtml = docListHtml .. '</ul>'
docListWikitext = docListWikitext .. '</ul>'
-- Create upload link
local uploadLinkWikitext = '<div style="margin-top:0.5em; font-style:italic;"><a href="' .. tostring(uploadUrl) .. '" class="external">Upload a document</a></div>'
-- Add the documents list and upload link
-- Combine and add to table - use wikitext so it gets rendered properly
local combinedHtml = docListHtml .. '<div style="margin-top:0.5em; font-style:italic;"><a href="' .. tostring(uploadUrl) .. '" class="external">Upload a document</a></div>'
local combinedWikitext = docListWikitext .. uploadLinkWikitext
addFullRow(tbl, combinedHtml, 'padding:0.2em 0.4em;', true)
addFullRow(tbl, combinedWikitext, 'padding:0.2em 0.4em;', false)
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 uploadLinkWikitext = '<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, uploadLinkWikitext, 'padding:0.2em 0.4em;', false)
end
end


MediaWiki Appliance - Powered by TurnKey Linux