« Module:WikidataTable » : différence entre les versions

De Wikiromandie.org
Aller à la navigation Aller à la recherche
Aucun résumé des modifications
Aucun résumé des modifications
 
Ligne 2 : Ligne 2 :


function p.render(frame)
function p.render(frame)
     -- Fetch arguments, defaulting to empty string
     -- Fetch arguments from frame or parent template frame
     local sparql = frame.args.sparql or ""
     local getArgs = function(key)
    local title  = frame.args.title  or ""
        return (frame.args and frame.args[key])
            or (frame:getParent() and frame:getParent().args and frame:getParent().args[key])
            or ""
    end


     -- Use frame:getParent() as fallback if parameters are passed directly to the template
     local sparql = getArgs("sparql")
    if sparql == "" and frame:getParent() then
    local title  = getArgs("title")
        sparql = frame:getParent().args.sparql or ""
        title  = frame:getParent().args.title or ""
    end


     if sparql == "" then
     if sparql == "" then
Ligne 16 : Ligne 16 :
     end
     end


     local function cleanAndEncode(s)
     -- Clean query string
        -- 1. Replace UTF-8 non-breaking spaces (\194\160) with standard spaces
    sparql = sparql:gsub('<nowiki>', ''):gsub('</nowiki>', '')
        s = s:gsub('\194\160', ' ')
                  :gsub('\194\160', ' ') -- Remove non-breaking spaces
       
                  :gsub('%s+', ' ')     -- Collapse spaces and newlines
        -- 2. Collapse all newlines, tabs, and multiple spaces into single standard spaces
        s = s:gsub('%s+', ' ')
       
        -- 3. Trim leading and trailing whitespace
        s = mw.text.trim(s)


        -- 4. Escape special HTML attribute characters
    local function htmlEncode(s)
         return s:gsub('&', '&amp;')
         return s:gsub('&', '&amp;')
                 :gsub('"', '&quot;')
                 :gsub('"', '&quot;')
Ligne 33 : Ligne 28 :
     end
     end


    local cleanSparql = cleanAndEncode(sparql)
    local cleanTitle  = cleanAndEncode(title)
    -- Return compact HTML output without outer whitespace or trailing newlines
     return string.format(
     return string.format(
         '<div class="wikidata-sparql-table" data-sparql="%s" data-title="%s"></div>',
         '<div class="wikidata-sparql-table" data-sparql="%s" data-title="%s"></div>',
         cleanSparql,
         htmlEncode(sparql),
         cleanTitle
         htmlEncode(title)
     )
     )
end
end


return p
return p

Dernière version du 21 septembre 2026 à 12:41

La documentation pour ce module peut être créée à Module:WikidataTable/doc

local p = {}

function p.render(frame)
    -- Fetch arguments from frame or parent template frame
    local getArgs = function(key)
        return (frame.args and frame.args[key]) 
            or (frame:getParent() and frame:getParent().args and frame:getParent().args[key]) 
            or ""
    end

    local sparql = getArgs("sparql")
    local title  = getArgs("title")

    if sparql == "" then
        return "⚠️ No SPARQL query provided."
    end

    -- Clean query string
    sparql = sparql:gsub('<nowiki>', ''):gsub('</nowiki>', '')
                   :gsub('\194\160', ' ') -- Remove non-breaking spaces
                   :gsub('%s+', ' ')     -- Collapse spaces and newlines

    local function htmlEncode(s)
        return s:gsub('&', '&amp;')
                :gsub('"', '&quot;')
                :gsub('<', '&lt;')
                :gsub('>', '&gt;')
    end

    return string.format(
        '<div class="wikidata-sparql-table" data-sparql="%s" data-title="%s"></div>',
        htmlEncode(sparql),
        htmlEncode(title)
    )
end

return p