Módulo:Documentação1

Origem: Wikipédia, a enciclopédia livre.
--[=[ 2019-07-29
{{Usuário:Eduardo Augusto/Documentação}}
{{Documentação1}}
]=]



local Current = mw.title.getCurrentTitle()



local function fake( frame, access )
    local s = string.format( "[%s/%s %s]",
                             "https://pt.wikipedia.org/wiki",
                             mw.uri.encode( access, "WIKI" ),
                             "Documentação teste da WPpt" )
    local e = mw.html.create( "div" )
    e:css( "border",  "#A000A0 2px solid" )
     :css( "padding", "1em" )
     :wikitext( "[[Imagem:Redirectltr.png|#REDIRECT|link=]]" )
     :node( mw.html.create( "span" )
                   :addClass( "redirectText" )
                   :wikitext( s ) )
    return tostring( e )
end -- fake()



local function fatal( amend )
    local s = "Categoria:Wikipedia:Erros de predefinição/Predefinição:Documentação"
    if amend then
        s = string.format( "%s/%s", s, amend )
    end
    return string.format( "[[Categoria:%s]]", s )
end -- fatal()



local function findeSchalter( area )
    local i = area:find( "__", 1, true )
    local r
    if i then
        r = area:find( "__TOC__", i, true )   or
            area:find( "__NOTOC__", i, true )   or
            area:find( "__ÍNDICE__", i, true )   or
            area:find( "__SEM_ÍNDICE__", i, true )   or
            area:find( "__SEMÍNDICE__", i, true )
    end
    return r
end -- findeSchalter()



local function findeTemplateDataTOC( area )
    local i = area:find( "emplateData", 3, true )
    local r
    if i then
        if area:find( "TOC",  i + 11,  true ) then
            local k
            if i > 100 then
                i = i - 100
            else
                i = 1
            end
            i, k = area:find( "%{%{%s*[tT]emplateData%s*|", i )
            if i then
                r = area:find( "|%s*TOC%s*=%s*1%s*[|}]",  k - 1 )
            end
        end
    end
    return r
end -- findeTemplateDataTOC()



local function furnish( area, frame )
    local i = area:find( "==", 1, true )
    local r
    if i then
        local seek = "==[^\n]+==\n"
        local init, k, n
        if i == 1 then
            i, k = area:find( seek, 1 )
            if i == 1 then
                n = 1
            end
        end
        seek = "\n" .. seek
        if i and not n then
            i, k = area:find( seek,  i - 1 )
        end
        if i then
            n = 1
            init = i
            i, k = area:find( seek,  k - 1 )
            if i then
                n = 2
            end
        end
        if n == 1 then
            r = "__NOTOC__\n" .. area
        elseif n == 2 then
            local src    = "TOC nonum/styles.css"
            local toc    = mw.html.create( "div" )
                                  :newline()
                                  :addClass( "nonumtoc" )
                                  :wikitext( "__TOC__" )
                                  :newline()
            local styles = frame:extensionTag( "templatestyles",
                                               nil,
                                               { src = src } )
            toc = styles .. tostring( toc )
            if init == 1 then
                r = toc .. area
            else
                r = string.format( "%s%s%s",
                                   area:sub( 1,  init - 1 ),
                                   toc,
                                   area:sub( init ) )
            end
        end
    end
    return r or area
end -- furnish()



local function f( frame )
    local bottom  = { }
    local doc    = mw.title.makeTitle( Current.namespace,
                                        Current.text .. "/doc" )
    local sub     = Current.prefixedText .. "/doc"
    local r
    if doc.exists then
        r = frame:expandTemplate{ title = sub }
        if not findeSchalter( r ) then
            if not findeTemplateDataTOC( r ) then
                r = furnish( r, frame )
            end
        end
        bottom.lonely = "1"
    else
        local create = { action  = "edit",
                         preload = "Predefinição:Documentação1/preload-doc",
                         summary = "Recém-criado",
                         redlink = "1" }
        local path   = { sub,  mw.uri.buildQueryString( create ) }
        local setup  = frame:callParserFunction( "fullurl", path )
        local button = { Typ      = "progressive",
                         ["Grande"] = "1",
                         Link     = setup,
                         Text     = "Criar uma nova subpágina de documentação" }
        r = frame:expandTemplate{ title = "Button",
                                  args  = button }
        if mw.site.server:match( "%.beta%.wmflabs%.org$" ) then
            r = r .. fake( frame, sub )
        elseif Current.namespace == 10 then
            r = string.format( "%s[[Categoria:%s]]",
                               r,
                               "Modelo sem documentação" )
        end
    end
    r = r .. frame:expandTemplate{ title = "Predefinição:Documentação1/Rodapé",
                                   args  = bottom }
    return r
end -- f()



local p = {}

function p.hastemplate( frame )
    -- OBSOLETING
    local r = Current.text:gsub( "/doc$", "" )
                          :gsub( " ", "_" )
                          :gsub( "\"", """ )
    if Current.namespace ~= 10 then
        r = string.format( "%s:%s",
                           mw.site.namespaces[ Current.namespace ].name,
                           r )
    end
    return string.format( "hastemplate:%s+", r )
end -- p.hastemplate()



function p.transcludes()
    local s = Current.text:gsub( "/doc$", "" )
                          :gsub( " ", "_" )
                          :gsub( "\"", """ )
    local t = mw.title.new( "Suche", -1 )
    if Current.namespace ~= 10 then
        s = string.format( "%s:%s",
                           mw.site.namespaces[ Current.namespace ].name,
                           s )
    end
    s = string.format( "hastemplate:%s ", s )
    return t:fullUrl( { search = s } )
end -- p.transcludes()



function p.f( frame )
    local lucky, r = pcall( f, frame )
    if not lucky then
        local e = mw.html.create( "span" )
                         :attr( "class", "error" )
                         :wikitext( r )
        r = tostring( e ) .. fatal( "Intern" )
    end
    return r
end -- p.f()

return p