Zum Inhalt springen

Modul:Vorlage:All Coordinates

aus Wikipedia, der freien Enzyklopädie
Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus


-- Modul:Vorlage:All Coordinates
-- Temporär: Migration im ANR für Vorlage:SeitenKoordinaten (2023/2024)
-- 2025-03-14
local THIS = { }

THIS.sCat     = "Wikipedia:Vorlagenfehler/SeitenKoordinaten"
THIS.schrei   = "vermutlich an einer ungeeigneten Stelle eingebunden"
THIS.show     = "Hinweis Seiten-Koordinaten"
THIS.support  = "seitenkoordinaten-platzierung"
THIS.vorlagen = { All = { say  = "All Coordinates",
                          seek = "all[ _]+coordinates",
                          xcl  = { "|%s*pos%s*=%s*right",
                                   "|%s*pos%s*=%s*inline" }
                        }
                }
THIS.davor = { "*",
               "<references",
               "</references>",
               "</gallery>",
               "<small",
               "{{commonscat",
               "{{commons",
               "{{wikisource",
               "{{wikivoyage",
               "{{wiktionary",
               "{{wikiquote",
               "{{wikibooks",
               "{{wikiatlas",
               "{{wikinews",
               "{{wikispecies",
               "{{schwesterprojekte",
               "{{portal",
               "|}" }
THIS.danach = { geschweift = { "naviblock",
                               "navigationsleiste",
                               "folgeleiste",
                               "folgenleiste",
                               "personenleiste",
                               "artikelfolge",
                               "gesundheitshinweis",
                               "rechtshinweis",
                               "bildrechtshinweis",
                               "hinweis ",
                               "begriffsklärung",
                               "lesenswert",
                               "exzellent",
                               "informativ",
                               "kandidat",
                               "review",
                               "gesprochene version",
                               "gesprochener artikel",
                               "normdaten",
                               "sortierung",
                               "defaultsort",
                               "personendaten" },
                eckig      = { "%l%l%l?", "kategorie" } }



local fehler = function ( after, at1, at2 )
    local e = mw.html.create( "code" )
    local r = "All"
    local s = "[[Kategorie:%s/%s]]%s"
    local v = THIS.vorlagen.All
    local e2, say
    if after then
        r = string.format( "%s/%s", r, after )
    elseif at1 then
        r = string.format( "%s/section", r )
    elseif at2 then
        r = string.format( "%s/text", r )
    end
    say = string.format( "[[Vorlage:%s|%s]]", v.say, v.say )
    say = string.format( "&#123;{%s}&#125;", say )
    e:wikitext( say )
     :css( "white-space", "nowrap" )
    e2 = e
    e = mw.html.create( "div" )
               :css( "border",  "2px solid #FF0000" )
               :css( "margin",  "1em" )
               :css( "padding", "0.5em" )
               :node( e2 )
               :wikitext( " ",  THIS.schrei )
    e:addClass( "error" )
     :addClass( THIS.support )
    r = string.format( s,  THIS.sCat,  r,  tostring( e ) )
    return r
end -- fehler()



local first = function ( all, at, ask, always )
    local i = at
    local k, r
    while i do
        k = all:find( ask, i, always )
        if k == i then
            r = k
            break -- while i
        elseif i > 1 then
            i = i - 1
        else
            break -- while i
        end
    end -- while i
    return r
end -- first()



local flat = function ( all )
    local r = mw.text.trim( all )
    local i = r:find( "<!--", 1, true )
    local j, line, s, suffix
    while i do
        j = r:find( "-->",  i + 3,  true )
        if j then
            suffix = r:sub( j + 3 )
            if i == 1 then
                s = ""
            else
                s = r:sub( 1,  i - 1 )
                if s:match( "%S%s*\n%s*$" )  or
                   suffix:match( "^%s*\n" ) then
                    line = true
                end
                s = mw.text.trim( s )
                if line then
                    s = s .. "\n"
                    line = false
                end
            end
            r = s .. mw.text.trim( suffix )
            i = r:find( "<!--", i, true )
        else
            break -- while i
        end
    end -- while i
    return  mw.ustring.lower( r )
end -- flat()



local focus = function ( all, about )
    local i = 1
    local k, r1, r2, s
    while i do
        i, r2 = all:find( about.seek, i )
        if i  and  i > 2 then
            s = mw.text.trim( all:sub( 1, i - 1 ) )
            s = s:gsub( "[_ ]+", "" )
            if s:match( "{{$" )  or  s:match( "{{vorlage:$" ) then
                r2 = r2 + 1
                s  = all:sub( r2 )
                k = s:find( "}}" )
                if k then
                    if k > 1 then
                        s = s:sub( 1, k )
                        for j = 1, #about.xcl do
                            if s:match( about.xcl[ j ] ) then
                                k = false
                                break -- for j
                            end
                        end -- j
                    end
                    if k then
                        r1 = first( all,  i - 1,  "{{",  true )
                        if r1 then
                            r1 = r1 - 1
                            r2 = r2 + k + 2
                        else
                            r1 = false
                            r2 = false
                        end
                        break -- while i
                    else
                        i = r2 + 1
                    end
                else
                    break -- while i
                end
            else
                i = r2 + 1
            end
        else
            break -- while i
        end
    end -- while i
    return r1, r2
end -- focus()



local follows = function ( after )
    local s = mw.text.trim( after )
    local r = true
    if s == "" then
        r = false
    else
        local start = s:sub( 1, 2 )
        local l, w
        if start == "{{" then
            w = THIS.danach.geschweift
            l = true
        elseif start == "[[" then
            w = THIS.danach.eckig
        end
        if w then
            local i
            s = mw.text.trim( s:sub( 3 ) )
            for j = 1, #w do
                i = mw.ustring.find( s,  w[ j ],  1,  l )
                if i == 1 then
                    r = false
                    break -- for j
                end
            end -- j
        end
    end
    return r
end -- follows()



local fore = function ( all, at )
    local r = true
    if at > 2 then
        local s = mw.text.trim( all:sub( 1,  at - 1 ) )
        local i
        s = s:match( "\n([^\n]+)$" )
        if s then
            for j = 1, #THIS.davor do
                i = mw.ustring.find( s,  THIS.davor[ j ],  1,  true )
                if i == 1 then
                    r = false
                    break -- for j
                end
            end -- j
            if r  and
               mw.ustring.find( s,  "^[a-zßäöü]" ) then
                r = false
            end
        end
    end
    return r
end -- fore()



-- Export
local p = { }

p.f = function ( frame )
    local v       = THIS.vorlagen.All
    local max     = frame.args.max
    local section = frame.args.section
    local show    = frame.args.text
    local i, k, r, s
    if section == "" then
        section = false
    end
    if show == "" then
        show = false
    end
    THIS.title = mw.title.getCurrentTitle()
    s = THIS.title:getContent()
    if max then
        max = tonumber( max )
        if max  and  max > 100 then
            s = mw.ustring.sub( s,  - max )
        end
    end
    s = flat( s )
    i, k = focus( s, v )
    if i then
        if not max  and  i < 200 then
            r = fehler( "Anfang", section, show )
        elseif max  and  i == 0 then
        elseif follows( s:sub( k ) )  or
               fore( s, i ) then
            r = fehler( false, section, show )
        end
    end
    if not r then
        local params  = {}
        if section ~= "" then
            params.section = section
        end
        if show ~= "" then
            params.text = show
        end
        r = frame:expandTemplate{ title = THIS.show,
                                  args  = params }
    end
    return r or ""
end -- p.f

return p