« 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
     local sparql = frame.args.sparql or ""
     local sparql = frame.args.sparql or ""
     local title  = frame.args.title  or ""
     local title  = frame.args.title  or ""
    -- Use frame:getParent() as fallback if parameters are passed directly to the template
    if sparql == "" and frame:getParent() then
        sparql = frame:getParent().args.sparql or ""
        title  = frame:getParent().args.title  or ""
    end


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


     local function htmlEncode(s)
     local function cleanAndEncode(s)
         return s:gsub('\194\160', ' ')           -- Normalize non-breaking spaces
         -- 1. Replace UTF-8 non-breaking spaces (\194\160) with standard spaces
                :gsub('&', '&')             -- Escape &
        s = s:gsub('\194\160', ' ')
                :gsub('"', '"')             -- Escape "
       
                :gsub('<', '&lt;')               -- Escape <
        -- 2. Collapse all newlines, tabs, and multiple spaces into single standard spaces
                 :gsub('>', '&gt;')               -- Escape >
        s = s:gsub('%s+', ' ')
                 :gsub('\r?\n', ' ')             -- Collapse multi-line queries onto one line
       
                 :gsub('%s+', ' ')               -- Clean up multiple consecutive spaces
        -- 3. Trim leading and trailing whitespace
        s = mw.text.trim(s)
 
        -- 4. Escape special HTML attribute characters
        return s:gsub('&', '&amp;')
                 :gsub('"', '&quot;')
                 :gsub('<', '&lt;')
                 :gsub('>', '&gt;')
     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>',
         htmlEncode(sparql),
         cleanSparql,
         htmlEncode(title)
         cleanTitle
     )
     )
end
end


return p
return p

Version du 21 septembre 2026 à 12:36

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

local p = {}

function p.render(frame)
    -- Fetch arguments, defaulting to empty string
    local sparql = frame.args.sparql or ""
    local title  = frame.args.title  or ""

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

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

    local function cleanAndEncode(s)
        -- 1. Replace UTF-8 non-breaking spaces (\194\160) with standard spaces
        s = s:gsub('\194\160', ' ')
        
        -- 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
        return s:gsub('&', '&amp;')
                :gsub('"', '&quot;')
                :gsub('<', '&lt;')
                :gsub('>', '&gt;')
    end

    local cleanSparql = cleanAndEncode(sparql)
    local cleanTitle  = cleanAndEncode(title)

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

return p