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

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
on évite
0x010D (discussion | contributions)
optimisation du code ; importation du bac à sable
Ligne 1 : Ligne 1 :
--Ce module implemente le modèle {{Documentation}}.
--Ce module implémente le modèle {{Documentation}}.


local p = {}
local p = {}


function p.corps(frame)
function p.corps(frame)
     local args = frame:getParent().args
     args = frame:getParent().args
     local page = mw.title.getCurrentTitle()
     local page = mw.title.getCurrentTitle()
    doc = p.docname(page)
     local corps = {}
     local corps = {}


Ligne 15 : Ligne 16 :


     --Génération de la documentation
     --Génération de la documentation
     table.insert(corps, frame:preprocess(p.entete(frame, args, page)))
     table.insert(corps, p.entete(page))
     table.insert(corps, p.contenu(frame, args, page))
     table.insert(corps, p.contenu(frame, page))
     table.insert(corps, frame:preprocess(p.notice(frame, args, page)))
     table.insert(corps, p.notice(page))


     --Code HTML brut
     --Code HTML brut
     if args.raw then
     if args.raw then
         return frame:preprocess('<nowiki>' .. table.concat(corps) .. '</nowiki>')
         return frame:preprocess('<nowiki>' .. table.concat(corps) .. '</nowiki>\n:' .. os.clock())
     end
     end


     return table.concat(corps)
     return table.concat(corps)
end
function p.ifexist(page)
    if not page then return false end
    if mw.title.new(page).exists then return true end
    return false
end
end


Ligne 48 : Ligne 42 :
end
end


function p.entete(frame, args, page)
function p.ifexist(page)
    if not page then return false end
    if mw.title.new(page).exists then return true end
    return false
end
 
function p.entete(page)
     local entete = {'<div style="clear:both; margin:1em 0 0 0; border:1px solid #aaa; background:'}
     local entete = {'<div style="clear:both; margin:1em 0 0 0; border:1px solid #aaa; background:'}
    local doc = p.docname(page)


     if not args.couleur then
     if not args.couleur then table.insert(entete, '#ecfcf4')
        table.insert(entete, '#ecfcf4')
     else table.insert(entete, args.couleur) end
     else
        table.insert(entete, args.couleur)
    end


     table.insert(entete, '; padding:1em 1em 0.8em;">')
     table.insert(entete, '; padding:1em 1em 0.8em;">')
Ligne 63 : Ligne 59 :
     table.insert(entete, '<span style="font-weight:bold; font-size:125%">&nbsp;')
     table.insert(entete, '<span style="font-weight:bold; font-size:125%">&nbsp;')


     if args.titre then
     if args.titre then table.insert(entete, args.titre)
        table.insert(entete, args.titre)
     else table.insert(entete, 'Documentation') end
     else
        table.insert(entete, 'Documentation')
    end
     table.insert(entete, '</span>')
     table.insert(entete, '</span>')


     if not args.contenu then
     if not args.contenu then
         table.insert(entete, '<span id="doc_editlinks" class="mw-editsection plainlinks">&#91;[')
         table.insert(entete, '<span class="mw-editsection plainlinks">&#91;[')
         table.insert(entete, '{{fullurl:')
         local arg = mw.title.new(args[1] or doc)


         if args[1] then
         if args[1] and p.ifexist(args[1]) or p.ifexist(doc) then
            if p.ifexist(args[1]) then
            table.insert(entete, arg:fullUrl('action=edit') .. ' modifier]')
                table.insert(entete, args[1])
            table.insert(entete, '&#93;&#32;&#91;[')
                table.insert(entete, '|action=edit modifier}}]&#93;&#32;&#91;{{Purger le cache|purger}}')
             table.insert(entete, page:fullUrl('action=purge') .. ' purger]')
             else
                table.insert(entete, args[1])
                table.insert(entete, '|action=edit&preload=Modèle:Documentation/Preload créer}}]')
            end
         else
         else
             if p.ifexist(doc) then
             table.insert(entete, arg:fullUrl({["action"]="edit", ["preload"]="Modèle:Documentation/Preload"}))
                table.insert(entete, doc)
             table.insert(entete, ' créer]')
                table.insert(entete, '|action=edit modifier}}]&#93;&#32;&#91;{{Purger le cache|purger}}')
             else
                table.insert(entete, doc)
                table.insert(entete, '|action=edit&preload=Modèle:Documentation/Preload créer}}]')
            end
         end
         end
         table.insert(entete, '&#93;</span>')
         table.insert(entete, '&#93;</span>')
Ligne 98 : Ligne 82 :
end
end


function p.contenu(frame, args, page)
function p.contenu(frame, page)
     local contenu = {}
     local contenu = {}
     local doc = p.docname(page)
     local arg = args[1] or doc
 
     if args.contenu then
     if args.contenu then
         table.insert(contenu, '\n')
         table.insert(contenu, '\n')
         table.insert(contenu, args.contenu)
         table.insert(contenu, args.contenu)
     else
     else
         table.insert(contenu, frame:preprocess('<nowiki />\n'))
         table.insert(contenu, frame:preprocess('<nowiki />'))
         --Évite toute récursion
         table.insert(contenu, '\n')
        if args[1]
        if args[1] and p.ifexist(args[1]) or p.ifexist(doc) then
        and frame:preprocess('{{FULLPAGENAMEE}}') ~= frame:preprocess('{{FULLPAGENAMEE:' .. args[1] .. '}}')
             table.insert(contenu, frame:preprocess('{{' .. arg .. '}}'))
        and p.ifexist(args[1]) then
            table.insert(contenu, frame:preprocess('{{' .. args[1] .. '}}'))
        elseif frame:preprocess('{{FULLPAGENAMEE}}') ~= frame:preprocess('{{FULLPAGENAMEE:' .. doc .. '}}')
        and p.ifexist(doc) then
             table.insert(contenu, frame:preprocess('{{' .. doc .. '}}'))
         end
         end
     end
     end


     table.insert(contenu, frame:preprocess('\n<nowiki /><div style="clear:both" />\n'))
    table.insert(contenu, '\n')
 
     table.insert(contenu, frame:preprocess('<nowiki />'))
    table.insert(contenu, '<div style="clear:both" />\n')
     return table.concat(contenu)
     return table.concat(contenu)
end
end


function p.notice(frame, args, page)
function p.notice(page)
     local notice = {}
     local notice = {}
     local doc = p.docname(page)
     local arg = mw.title.new(args[1] or doc)


     if args.contenu then
     if args.contenu then
Ligne 134 : Ligne 113 :
             table.insert(notice, args.notice)
             table.insert(notice, args.notice)
         else
         else
             table.insert(notice, 'La [[Aide:Comment documenter un modèle ?|documentation]] de ce [[Aide:Modèle|modèle]] ')
             table.insert(notice, 'La [[Aide:Comment documenter un modèle ?|documentation]] de ce ')
             table.insert(notice, 'est directement [[Aide:Inclusion|incluse]] dans le corps de ce dernier.<br />')
             table.insert(notice, '[[Aide:Modèle|modèle]] est directement [[Aide:Inclusion|incluse]] ')
             table.insert(notice, 'Si cette page est protégée, veuillez transférer le contenu de la documentation vers sa ')
            table.insert(notice, 'dans le corps de ce dernier.<br />Si cette page est protégée, ')
             table.insert(notice, '[{{fullurl:' .. doc)
             table.insert(notice, 'veuillez transférer le contenu de la documentation vers sa [')
             table.insert(notice, '|action=edit&preload=Modèle:Documentation/Preload sous-page dédiée}}].')
             table.insert(notice, arg:fullUrl({["action"]="edit", ["preload"]="Modèle:Documentation/Preload"}))
             table.insert(notice, ' sous-page dédiée].')
         end
         end
     else
     else
Ligne 145 : Ligne 125 :
             table.insert(notice, '<p style="margin-bottom:0; padding-left:1em; font-style:italic;">')
             table.insert(notice, '<p style="margin-bottom:0; padding-left:1em; font-style:italic;">')
             table.insert(notice, 'La [[Aide:Comment documenter un modèle ?|documentation]] de ce ')
             table.insert(notice, 'La [[Aide:Comment documenter un modèle ?|documentation]] de ce ')
             table.insert(notice, '[[Aide:Modèle|modèle]] est [[Aide:Inclusion|incluse]] depuis sa ')
             table.insert(notice, '[[Aide:Modèle|modèle]] est [[Aide:Inclusion|incluse]] depuis sa [[')
 
             table.insert(notice, tostring(arg) .. '|sous-page de documentation]]')
            if args[1] then
             table.insert(notice, '&nbsp;<span style="font-size:89%; font-style:normal;">([')
                table.insert(notice, '[[' .. args[1] .. '|sous-page de documentation]]')
             table.insert(notice, arg:fullUrl('action=edit'))
             else
             table.insert(notice, ' modifier]&nbsp;|&nbsp;[')
                table.insert(notice, '[[' .. doc .. '|sous-page de documentation]]')
            table.insert(notice, arg:fullUrl('action=history') .. ' historique])</span>.<br />')
            end
 
             table.insert(notice, '&nbsp;<span style="font-size:89%; font-style:normal;">(')
             table.insert(notice, '[{{fullurl:')
 
            if args[1] then
                table.insert(notice, args[1] .. '|action=edit}} modifier]')
                table.insert(notice, '&nbsp;|&nbsp;[{{fullurl:')
                table.insert(notice, args[1] .. '|action=history}} historique]')
             else
                table.insert(notice, doc .. '|action=edit}} modifier]')
                table.insert(notice, '&nbsp;|&nbsp;[{{fullurl:')
                table.insert(notice, doc .. '|action=history}} historique]')
            end
            table.insert(notice, ')</span>.<br />')
         else
         else
             table.insert(notice, '<div class="plainlinks">')
             table.insert(notice, '<div class="plainlinks">')
Ligne 172 : Ligne 137 :
         table.insert(notice, 'Les éditeurs peuvent travailler dans le ')
         table.insert(notice, 'Les éditeurs peuvent travailler dans le ')


         if args[1] and p.ifexist(frame:preprocess('{{#rel2abs:' .. args[1] .. '/../Bac à sable}}'))
         local bacasable = arg.subjectNsText .. ":" .. arg.baseText .. "/Bac à sable"
         or p.ifexist(frame:preprocess('{{#rel2abs:' .. doc .. '/../Bac à sable}}')) then
        local argbacasable = mw.title.new(bacasable)
             table.insert(notice, '[[{{#rel2abs:')
         if p.ifexist(bacasable) then
            if args[1] then
             table.insert(notice, '[[' .. bacasable .. '|bac à sable]]')
                table.insert(notice, args[1])
             table.insert(notice, '&nbsp;<span style="font-size:89%; font-style:normal;">([')
            else
             table.insert(notice, argbacasable:fullUrl('action=edit'))
                table.insert(notice, doc)
             table.insert(notice, ' modifier])</span>')
            end
            table.insert(notice, '/../Bac à sable}}|bac à sable]]')
             table.insert(notice, '&nbsp;<span style="font-size:89%; font-style:normal;">(')
             table.insert(notice, '[{{fullurl:{{#rel2abs:')
            if args[1] then
                table.insert(notice, args[1])
            else
                table.insert(notice, doc)
            end
             table.insert(notice, '/../Bac à sable}}|action=edit}} modifier])</span>')
         else
         else
             table.insert(notice, 'bac à sable&nbsp;<span style="font-size:89%; font-style:normal;">(')
             table.insert(notice, 'bacasable&nbsp;<span style="font-size:89%; font-style:normal;">([')
             table.insert(notice, '[{{fullurl:{{#rel2abs:')
             table.insert(notice, argbacasable:fullUrl({["action"]="edit", ["preload"]="Modèle:Documentation/Preload2"}))
            if args[1] then
             table.insert(notice, ' créer])</span>')
                table.insert(notice, args[1])
            else
                table.insert(notice, doc)
            end
            table.insert(notice, '/../Bac à sable}}|action=edit&preload=Modèle:Documentation/Preload2}} créer]')
             table.insert(notice, ')</span>')
         end
         end
         table.insert(notice, ' et la page de ')
         table.insert(notice, ' et la page de ')


         if args[1] and p.ifexist(frame:preprocess('{{#rel2abs:' .. args[1] .. '/../Test}}'))
         local test = arg.subjectNsText .. ":" .. arg.baseText .. "/Test"
         or p.ifexist(frame:preprocess('{{#rel2abs:' .. doc .. '/../Test}}')) then
        local argtest = mw.title.new(test)
             table.insert(notice, '[[{{#rel2abs:')
         if p.ifexist(test) then
            if args[1] then
             table.insert(notice, '[[' .. test .. '|test]]')
                table.insert(notice, args[1])
             table.insert(notice, '&nbsp;<span style="font-size:89%; font-style:normal;">([')
            else
             table.insert(notice, argtest:fullUrl('action=edit'))
                table.insert(notice, doc)
             table.insert(notice, ' modifier])</span>')
            end
            table.insert(notice, '/../Test}}|test]]')
             table.insert(notice, '&nbsp;<span style="font-size:89%; font-style:normal;">(')
             table.insert(notice, '[{{fullurl:{{#rel2abs:')
            if args[1] then
                table.insert(notice, args[1])
            else
                table.insert(notice, doc)
            end
             table.insert(notice, '/../Test}}|action=edit}} modifier])</span>')
         else
         else
             table.insert(notice, 'test&nbsp;<span style="font-size:89%; font-style:normal;">(')
             table.insert(notice, 'test&nbsp;<span style="font-size:89%; font-style:normal;">([')
             table.insert(notice, '[{{fullurl:{{#rel2abs:')
             table.insert(notice, argtest:fullUrl({["action"]="edit", ["preload"]="Modèle:Documentation/Preload3"}))
            if args[1] then
             table.insert(notice, ' créer])</span>')
                table.insert(notice, args[1])
            else
                table.insert(notice, doc)
            end
            table.insert(notice, '/../Test}}|action=edit&preload=Modèle:Documentation/Preload3}} créer]')
             table.insert(notice, ')</span>')
         end
         end
         table.insert(notice, '.<br />Veuillez placer les catégories dans la sous-page ')
         table.insert(notice, '.<br />Veuillez placer les catégories dans la sous-page ')
         table.insert(notice, '<span class="plainlinks">[')
         table.insert(notice, '<span class="plainlinks">[')
        table.insert(notice, '{{fullurl:')
 
         if args[1] and p.ifexist(args[1]) or p.ifexist(doc) then
         if args[1] and p.ifexist(args[1]) or p.ifexist(doc) then
             if args[1] then
             table.insert(notice, arg:fullUrl('action=edit'))
                table.insert(notice, args[1])
            else
                table.insert(notice, doc)
            end
            table.insert(notice, '|action=edit')
 
         else
         else
             if args[1] then
             table.insert(notice, arg:fullUrl({["action"]="edit", ["preload"]="Modèle:Documentation/Preload"}))
                table.insert(notice, args[1])
            else
                table.insert(notice, doc)
            end
            table.insert(notice, '|action=edit&preload=Modèle:Documentation/Preload')
         end
         end
         table.insert(notice, ' /Documentation}}].')
 
         table.insert(notice, ' /Documentation].')
     end
     end
     table.insert(notice, '</p></div></div>')
     table.insert(notice, '</p></div></div>')

Version du 28 juillet 2013 à 17:56

--Ce module implémente le modèle Modèle:Méta documentation de modèle.

local p = {}

function p.corps(frame)

   args = frame:getParent().args
   local page = mw.title.getCurrentTitle()
   doc = p.docname(page)
   local corps = {}
   --Bandeau pour les sous-pages /Bac à sable
   if page.subpageText == 'Bac à sable' then

table.insert(corps, '

')
       table.insert(corps, frame:preprocess('Modèle:Sous-page de bac à sable'))
   end
   --Génération de la documentation
   table.insert(corps, p.entete(page))
   table.insert(corps, p.contenu(frame, page))
   table.insert(corps, p.notice(page))
   --Code HTML brut
   if args.raw then
       return frame:preprocess('' .. table.concat(corps) .. '\n:' .. os.clock())
   end
   return table.concat(corps)

end

function p.docname(page)

   --On n'est pas dans une sous-page
   if not page.isSubpage then return page.subjectNsText .. ":" .. page.text .. "/Documentation" end
   --On est dans une sous-page
   if page.subpageText == 'Documentation'
   or page.subpageText == 'Bac à sable'
   or page.subpageText == 'Test' then
       return page.subjectNsText .. ":" .. page.baseText .. "/Documentation"
   else
       return page.subjectNsText .. ":" .. page.text .. "/Documentation"
   end

end

function p.ifexist(page)

   if not page then return false end
   if mw.title.new(page).exists then return true end
   return false

end

function p.entete(page)

local entete = {'
') table.insert(entete, '
')
   table.insert(entete, 'Documentation du modèle')
   table.insert(entete, ' ')
   if args.titre then table.insert(entete, args.titre)
   else table.insert(entete, 'Documentation') end
   table.insert(entete, '')
   if not args.contenu then
       table.insert(entete, '[[')
       local arg = mw.title.new(args[1] or doc)
       if args[1] and p.ifexist(args[1]) or p.ifexist(doc) then
           table.insert(entete, arg:fullUrl('action=edit') .. ' modifier]')
           table.insert(entete, '] [[')
           table.insert(entete, page:fullUrl('action=purge') .. ' purger]')
       else
           table.insert(entete, arg:fullUrl({["action"]="edit", ["preload"]="Modèle:Documentation/Preload"}))
           table.insert(entete, ' créer]')
       end
       table.insert(entete, ']')
   end
table.insert(entete, '
')
   return table.concat(entete)

end

function p.contenu(frame, page)

   local contenu = {}
   local arg = args[1] or doc
   if args.contenu then
       table.insert(contenu, '\n')
       table.insert(contenu, args.contenu)
   else
       table.insert(contenu, frame:preprocess(''))
       table.insert(contenu, '\n')
       if args[1] and p.ifexist(args[1]) or p.ifexist(doc) then
           table.insert(contenu, frame:preprocess('Modèle:' .. arg .. ''))
       end
   end
   table.insert(contenu, '\n')
   table.insert(contenu, frame:preprocess(''))
table.insert(contenu, '
\n')
   return table.concat(contenu)

end

function p.notice(page)

   local notice = {}
   local arg = mw.title.new(args[1] or doc)
   if args.contenu then
table.insert(notice, '