Bearbeiten von „Modul:WLink

Zur Navigation springen Zur Suche springen
Warnung: Du bist nicht angemeldet. Deine IP-Adresse wird bei Bearbeitungen öffentlich sichtbar. Melde dich an oder erstelle ein Benutzerkonto, damit Bearbeitungen deinem Benutzernamen zugeordnet werden. Ein eigenes Benutzerkonto hat eine ganze Reihe von Vorteilen.

Die Bearbeitung kann rückgängig gemacht werden. Bitte prüfe den Vergleich unten, um sicherzustellen, dass du dies tun möchtest, und veröffentliche dann unten deine Änderungen, um die Bearbeitung rückgängig zu machen.

Aktuelle Version Dein Text
Zeile 1: Zeile 1:
local WLink = { suite  = "WLink",
local WLink = { suite  = "WLink",
                 serial = "2019-06-17",
                 serial = "2018-07-19",
                 item  = 19363224 };
                 item  = 19363224 };
--[=[
--[=[
Zeile 21: Zeile 21:
getTitle()
getTitle()
getWeblink()
getWeblink()
getWikilink()
isBracketedLink()
isBracketedLink()
isBracketedURL()
isBracketedURL()
Zeile 32: Zeile 31:
isTitledLink()
isTitledLink()
isValidLink()
isValidLink()
isValidLinktext()
isWikilink()
isWikilink()
pageLink()
pageTarget()
wikilink()
wikilink()
failsafe()
failsafe()
Zeile 44: Zeile 40:
-- local globals
-- local globals
local URLutil = false;
local URLutil = false;
local htmlInline = { b      = true,
                    big    = true,
                    code  = true,
                    em    = true,
                    i      = true,
                    kbd    = true,
                    s      = true,
                    samp  = true,
                    small  = true,
                    span  = true,
                    strong = true,
                    sub    = true,
                    sup    = true,
                    tt    = true,
                    var    = true };




Zeile 84: Zeile 62:
     return URLutil;
     return URLutil;
end -- utilURL()
end -- utilURL()
local cleanWikilink = function ( access )
    -- Refine wikilink spacing and decode
    -- Precondition:
    --    access  -- string, with presumable link
    -- Postcondition:
    --    Returns  string, with pretty target
    local r = access:gsub( "_",        " " )
                    :gsub( " ",  " " )
                    :gsub( " ", " " )
                    :gsub( " ",  " " )
                    :gsub( " ",  " " )
                    :gsub( "%s+",      " " );
    r = mw.text.decode( r );
    return r;
end -- cleanWikilink()




Zeile 232: Zeile 192:
             r1 = false;
             r1 = false;
         else
         else
             r1 = cleanWikilink( r1 );
             r1 = r1:gsub( "_",        " " )
                  :gsub( " ",  " " )
                  :gsub( " ", " " )
                  :gsub( " ",  " " )
                  :gsub( " ",  " " )
                  :gsub( "  +",      " " );
            r1 = mw.text.decode( r1 );
         end
         end
     end
     end
Zeile 240: Zeile 206:




local farming = function ( already )
local prefix = function ( ask, ahead )
    -- Retrieve wikifarm project information
    -- Precondition:
    --    already  -- table, with wikilink components
    -- Postcondition:
    --    Returns  table, with wikilink components extended
    local r = already;
    if not r.project then
        local codes  = { mediawiki  = "mw",
                        wikibooks  = "b",
                        wikidata    = "d",
                        wikinews    = "n",
                        wikipedia  = "w",
                        wikiquote  = "q",
                        wikisource  = "s",
                        wikiversity = "v",
                        wikivoyage  = "voy",
                        wiktionary  = "wikt" };
        local server = mw.site.server:gsub( "([/.])m%.",
                                            "%1" )
                                      :gsub( "%.beta%.wmflabs%.org$",
                                            ".org" );
        local site  = server:match( "[/.](%l+)%.org$" );
        r.project = codes[ site ];
        if r.project then
            if not r.lang  and
              r.project ~= "mw"  and  r.project ~= "d" then
                r.lang = server:match( "//(%l+)%." );
            end
        else
            site = server:match( "//(%l+)%.wikimedia%.org$" );
            if site == "commons"  or  site == "meta" then
                r.project = site;
            end
        end
    end
    if r.project  and  r.ns  and
      ( r.project == "commons"  or
        r.project == "d"        or
        r.project == "meta"    or
        r.project == "mw" ) then
        r.language = true;
    end
    return r;
end -- farming()
 
 
 
local prefix = function ( ask )
     -- Interprete prefix of language or project type
     -- Interprete prefix of language or project type
     -- Precondition:
     -- Precondition:
     --    ask    -- string, with presumable prefix
     --    ask    -- string, with presumable prefix
    --    ahead  -- true, if first segment
     -- Postcondition:
     -- Postcondition:
     --    Returns  string,string or nil
     --    Returns  string,string or nil
Zeile 326: Zeile 245:
                     };
                     };
     local s = mw.text.trim( ask );
     local s = mw.text.trim( ask );
     if s ~= "" then
     if s == "" then
        if ahead then
            r1 = "lead";
            r2 = true;
        end
    else
         local p;
         local p;
         s = s:lower();
         s = s:lower();
Zeile 405: Zeile 329:
                 end
                 end
             else
             else
                 s = string.format( "%%%02X", k );
                 s = string.format( "%%%2X", k );
             end
             end
             r = string.format( "%s%s%s",
             r = string.format( "%s%s%s",
Zeile 422: Zeile 346:
     -- Precondition:
     -- Precondition:
     --    adjust  -- string, with URL or domain/path or bracketed link
     --    adjust  -- string, with URL or domain/path or bracketed link
    --    assure  -- boolean, true for secure HTTP
     -- Postcondition:
     -- Postcondition:
     --    Returns  string, with bracketed link
     --    Returns  string, with bracketed link
Zeile 436: Zeile 359:
             host = URLutil.getHost( adjust );
             host = URLutil.getHost( adjust );
             if not host then
             if not host then
                 url = "://" .. adjust;
                 url = "http://" .. adjust;
                if assure then
                    url = "s" .. url;
                end
                url  = "http" .. url;
                 host = URLutil.getHost( url );
                 host = URLutil.getHost( url );
             end
             end
Zeile 529: Zeile 448:
     -- Postcondition:
     -- Postcondition:
     --    Returns  string, with suitable link title
     --    Returns  string, with suitable link title
     local s = mw.text.trim( attempt );
     return attempt:gsub( "\n", " " )
    return s:gsub( "\n", " " )
                  :gsub( "%[", "[" )
            :gsub( "%[", "[" )
                  :gsub( "%]", "]" )
            :gsub( "%]", "]" )
                  :gsub( "|",  "|" );
            :gsub( "|",  "|" );
end -- WLink.getEscapedTitle()
end -- WLink.getEscapedTitle()


Zeile 585: Zeile 503:
         r = find( "file" );
         r = find( "file" );
         if not r then
         if not r then
             local trsl = mw.site.namespaces[ 6 ];
             local trsl = mw.site.namespaces[6];
             r = find( trsl.name );
             r = find( trsl.name );
             if not r then
             if not r then
Zeile 744: Zeile 662:
                 if k > 1 then
                 if k > 1 then
                     n      = n - k;
                     n      = n - k;
                     i      = j + k + 1;
                     i      = j + k;
                     j      = i - 1;
                     j      = i - 1;
                     suffix = r:sub( j );
                     suffix = r:sub( j );
Zeile 792: Zeile 710:
             break; -- while true
             break; -- while true
         end
         end
     end   -- while true
     end -- while true
     return r;
     return r;
end -- WLink.getPlain()
end -- WLink.getPlain()
Zeile 989: Zeile 907:




function WLink.getWikilink( attempt, appear )
function WLink.isBracketedLink( attempt )
    -- Retrieve bracketed link from text
     -- Does attempt match a bracketed link?
    -- Precondition:
    --    attempt  -- string, with current target, or plain
    --    appear  -- string, with link title, or nil
    -- Postcondition:
    --    Returns  string, with first detected link target
    --              false if nothing found
    local r = WLink.pageTarget( attempt );
    if r then
        if appear then
            local show = WLink.getEscapedTitle( appear );
            if show ~= r  and  show ~= "" then
                r = string.format( "%s|%s", r, show );
            end
        end
        r = string.format( "[[%s]]", r );
    end
    return r;
end -- WLink.getWikilink()
 
 
 
function WLink.isBracketedLink( attempt )
     -- Does attempt match a bracketed link?
     -- Precondition:
     -- Precondition:
     --    attempt  -- string, with presumable link somewhere
     --    attempt  -- string, with presumable link somewhere
Zeile 1.203: Zeile 1.098:
     -- Postcondition:
     -- Postcondition:
     --    Returns  boolean
     --    Returns  boolean
     local u, r = WLink.getTarget( attempt );
     local s, r = WLink.getTarget( attempt );
     if r then
     if r then
         if r < 2 then
         r = true;
            if u:find( "''", 1, true ) then
                r = false;
            else
                r = true;
            end
        else
            r = true;
        end
     end
     end
     return r;
     return r;
Zeile 1.220: Zeile 1.107:




function WLink.isValidLinktext( attempt, allow )
function WLink.isWikilink( attempt )
     -- Is attempt a plain inline text?
     -- Does attempt match a wikilink?
     -- Precondition:
     -- Precondition:
     --    attempt  -- string, with presumable linktext
     --    attempt  -- string, with presumable link somewhere
    --    allow    -- boolean or nil, if multiline permitted
     -- Postcondition:
     -- Postcondition:
     --    Returns  boolean
     --    Returns  boolean
     local r = true;
     local s, m = WLink.getTarget( attempt );
    local i = attempt:find( "]", 1, true );
     return ( m == 2 );
    if i then
end -- WLink.isWikilink()
        r = false;
    else
        r = ( mw.text.unstrip( attempt ) == attempt );
     end
    if r  and  not allow  and  attempt:find( "\n", 1, true ) then
        r = false;
    end
    if r then
        i = attempt:find( "<", 1, true );
        if i then
            local s = mw.ustring.lower( attempt:sub( i ) );
            local sign, skip;
            while i and r do
                skip, sign = s:match( "^([^<]*< *)(%l[%l%d]*)[ /]*.*>" );
                if sign then
                    r = htmlInline[ sign ];
                    if r then
                        i = skip:len() + sign:len() + 1;
                        s = s:sub( i );
                    end
                else
                    i = false;
                end
            end    -- while i and r
        end
    end
    if r then
        local s = attempt .. " ";
        i = s:find( "ISBN ", 1, true );
        if i then
            r = not s:match( "ISBN %d[%-%d]+[%dxX]%W" );
        end
        if r then
            i = s:find( "PMID ", 1, true );
            if i then
                r = not s:match( "PMID %d+%W" );
            end
            if r then
                i = s:find( "RFC ", 1, true );
                if i then
                    r = not s:match( "RFC [1-6]%d?%d?%d?%W" );
                end
            end
        end
    end
    return r or false;
end -- WLink.isValidLinktext()






function WLink.isWikilink( attempt )
function WLink.wikilink( attempt )
    -- Does attempt match a wikilink?
     -- Retrieve wikilink components
    -- Precondition:
    --    attempt  -- string, with presumable link somewhere
    -- Postcondition:
    --    Returns  boolean
    local s, m = WLink.getTarget( attempt );
    return ( m == 2 );
end -- WLink.isWikilink()
 
 
 
function WLink.pageLink( attempt, appear, assure )
    -- Create safely standardized wikilink target of a page
    --    attempt  -- string, with presumable link
    --    appear  -- string or true or nil, with link title
    --    assure  -- string or nil, shield against wiki template syntax
    --                "URL" or "WIKI"
    -- Postcondition:
    --    Returns  string with link target
    local r = WLink.pageTarget( attempt, assure );
    if appear then
        local show;
        if type( appear ) == "string" then
            show = appear;
        else
            show = attempt;
        end
        r = string.format( "%s|%s", r, show );
    end
    return r;
end -- WLink.pageLink()
 
 
 
function WLink.pageTarget( attempt, assure )
     -- Create standardized wikilink target of a page
     -- Precondition:
     -- Precondition:
     --    attempt  -- string, with presumable link
     --    attempt  -- string, with presumable link
     --                        expected to be enclosed in "[[" "]]"
     --                        expected to be enclosed in "[[" "]]"
     --                        else wikilink
     --                        else wikilink
    --                table, of assignments with { type, value }
    --                        type is one of "lead",
    --                            "project", "lang",
    --                            "ns", "space", "title"
    --    assure  -- string or nil, shield against wiki template syntax
    --                "URL" or "WIKI"
     -- Postcondition:
     -- Postcondition:
     --    Returns  string with link target
     --    Returns  table or false
     local p = type( attempt );
    --              table of assignments with { type, value }
     local s = assure;
    --                      type is one of "lead",
     local r;
    --                          "project", "lang",
     if p == "string" then
    --                          "ns", "space", "title"
        p = WLink.wikilink( attempt );
    --              false if nothing found
     elseif p == "table" then
     local s = contentWikilink( attempt );
         p = attempt;
     local got, n, r;
    if not s then
        s = attempt;
     end
    i = s:find( "|", 1, true );
     if i then
        s = s:sub( 1, i - 1 );
    end
    got = mw.text.split( s, ":" );
    n  = table.maxn( got );
     if n == 1 then
         r = { title = mw.text.trim( s ) };
     else
     else
         p = false;
         local j, k, o, v;
    end
         r = { title = "" };
    if p then
         if n > 4 then
         local site  = p.project;
            k = 4;
         local slang = p.lang;
         else
         local lead;
             k = n - 1;
        if p.title:sub( 1, 1 ) == "#" then
             p.title = mw.title.getCurrentTitle().text + p.title;
         end
         end
         if p.ns then
         j = k;
             if not slang then
        for i = 1, j do
                 p = farming( p );
            s = mw.text.trim( got[ i ] );
            end
             if s ~= "" then
            if p.lang  and
                 o = mw.site.namespaces[ mw.text.trim( got[ i ] ) ];
              p.lang ~= mw.language.getContentLanguage():getCode() then
                if o then
                p.language = true;
                    r.ns    = o.id;
                    r.space = o.name;
                    k = i + 1;
                    j = i - 1;
                    break; -- for i
                end
             end
             end
            if p.language then
         end -- for i
                p.space = mw.site.namespaces[ p.ns ].canonicalName;
         for i = 1, j do
            end
             o, v = prefix( got[ i ]( i == 1 ) );
            lead = ( p.ns == 6  or  p.ns == 14 );
             if o then
         end
                if r[ o ] then
         if slang then
                    k = i;
             lead = true;
                    break; -- for i
        end
                else
        if s == "WIKI" then
                    r[ o ] = v;
            if not site  and
                end
              ( lead  or
             else
                ( not p.space  and
                k = i;
                  p.title and p.title:match( "^[*;]" ) ) ) then
                 break; -- for i
                p    = farming( p );
                site  = p.project;
                slang = p.lang;
            end
            s = false;
        end
        if site then
            r = site .. ":";
        elseif lead then
            r = ":";
        else
            r = "";
        end
        if slang then
            r = string.format( "%s%s:", r, slang );
        end
        if p.space then
            r = string.format( "%s%s:", r, p.space );
        end
        if p.title then
             r = r .. p.title;
        end
        if r == "" then
            r = false;
        end
    end
    if not r then
        p = { lang = mw.language.getContentLanguage():getCode() };
        if s == "WIKI" then
            r = WLink.pageTarget( p, s );
            s = false;
        else
            r = string.format( ":%s:", p.lang );
        end
    end
    if s == "URL"  and  r:match( "^[*#;:]" ) then
        r = mw.uri.encode( r:sub( 1, 1 ) )  ..  r:sub( 2 );
    end
    return r;
end -- WLink.pageTarget()
 
 
 
function WLink.wikilink( attempt )
    -- Retrieve wikilink components
    -- Precondition:
    --    attempt  -- string, with presumable link
    --                        expected to be enclosed in "[[" "]]"
    --                        else wikilink
    -- Postcondition:
    --    Returns  table or false
    --              table of assignments with { type, value }
    --                      type is one of "lead",
    --                          "project", "lang",
    --                          "ns", "space", "title"
    --              false if nothing found
    local s = contentWikilink( attempt or "" );
    local got, n, r;
    if not s then
        s = attempt;
    end
    if s:find( "%", 1, true ) then
        s = mw.uri.decode( s, "PATH" );
    end
    i = s:find( "|", 1, true );
    if i then
        s = s:sub( 1, i - 1 );
    end
    got = mw.text.split( s, ":" );
    n  = table.maxn( got );
    if n == 1 then
        r = { title = mw.text.trim( s ) };
    else
        local j, k, o, v;
        r = { title = "" };
        while ( got[ 1 ] == "" ) do
            r.lead = true;
            table.remove( got, 1 );
            n = n - 1;
        end   -- while  got[ 1 ] == ""
        if n > 4 then
             k = 4;
        elseif n > 1 then
            k = n - 1;
        else
            k = 1;
        end
        j = k;
        for i = 1, j do
            s = mw.text.trim( got[ i ] );
            if s ~= "" then
                o = mw.site.namespaces[ s ];
                 if o then
                    r.ns    = o.id;
                    r.space = o.name;
                    k = i + 1;
                    j = i - 1;
                    break; -- for i
                end
             end
             end
         end -- for i
         end -- for i
         for i = 1, j do
         for i = k, n do
             o, v = prefix( got[ i ] );
             r.title = r.title .. got[ i ];
             if o then
             if i < n then
                 if r[ o ] then
                 r.title = r.title .. ":";
                    k = i;
                    break; -- for i
                else
                    if i >= k then
                        k = i + 1;
                    end
                    r[ o ] = v;
                end
            else
                if i == 1  and  r.ns then
                    r.ns    = false;
                    r.space = false;
                end
                k = i;
                break; -- for i
             end
             end
         end -- for i
         end -- for i
        if k > 0 then
            for i = k, n do
                r.title = r.title .. got[ i ];
                if i < n then
                    r.title = r.title .. ":";
                end
            end -- for i
        end
     end
     end
    r.title = cleanWikilink( r.title );
     if r.lead and
     if r.lead and
       ( r.project  or
       ( r.project or  not r.title or
         ( not r.lang  and  r.ns ~= 6  and  r.ns ~= 14 ) ) then
         ( not r.lang  and  r.ns ~= 6  and  r.ns ~= 14 ) ) then
         r.lead = false;
         r.lead = false;
Zeile 1.527: Zeile 1.216:
                 local vsn = ent:formatPropertyValues( "P348" );
                 local vsn = ent:formatPropertyValues( "P348" );
                 if type( vsn ) == "table"  and
                 if type( vsn ) == "table"  and
                   type( vsn.value ) == "string" and
                   type( vsn.value) == "string" and
                   vsn.value ~= "" then
                   vsn.value ~= "" then
                     r = vsn.value;
                     r = vsn.value;
Zeile 1.558: Zeile 1.247:
     local s = false;
     local s = false;
     local r = false;
     local r = false;
     local safe, space;
     local space;
     for k, v in pairs( frame.args ) do
     for k, v in pairs( frame.args ) do
         if k == 1 then
         if k == 1 then
Zeile 1.566: Zeile 1.255:
                 s = mw.text.trim( v );
                 s = mw.text.trim( v );
             end
             end
         elseif ( k == 2   and
         elseif ( k == 2 and action == "getNamespaced" )   or
                ( action == "getNamespaced"  or
                  action == "getWikilink"  or
                  action == "pageLink" ) )    or
               ( k == "space"  and  action == "ansiPercent" ) then
               ( k == "space"  and  action == "ansiPercent" ) then
            v = mw.text.trim( v );
             if v ~= "" then
             if v ~= "" then
                 space = v;
                 space = v;
             end
             end
        elseif k == "safe"  and  action == "pageLink" then
            v = mw.text.trim( v );
            if v ~= "" then
                safe = v;
            end
        elseif k == "lines"  and  action == "isValidLinktext" then
            space = ( k == "1" );
         elseif k ~= "template" then
         elseif k ~= "template" then
             lucky = false;
             lucky = false;
Zeile 1.594: Zeile 1.272:
     if lucky then
     if lucky then
         if s or lone then
         if s or lone then
             lucky, r = pcall( WLink[ action ],  s,  space,  safe );
             lucky, r = pcall( WLink[ action ],  s,  space );
         else
         else
             r = "Parameter missing";
             r = "Parameter missing";
Zeile 1.677: Zeile 1.355:
p.getWeblink = function ( frame )
p.getWeblink = function ( frame )
     return Template( frame, "getWeblink" );
     return Template( frame, "getWeblink" );
end
p.getWikilink = function ( frame )
    return Template( frame, "getWikilink" );
end
end
p.isBracketedLink = function ( frame )
p.isBracketedLink = function ( frame )
Zeile 1.710: Zeile 1.385:
p.isValidLink = function ( frame )
p.isValidLink = function ( frame )
     return Template( frame, "isValidLink" );
     return Template( frame, "isValidLink" );
end
p.isValidLinktext = function ( frame )
    return Template( frame, "isValidLinktext" );
end
end
p.isWeblink = function ( frame )
p.isWeblink = function ( frame )
Zeile 1.719: Zeile 1.391:
p.isWikilink = function ( frame )
p.isWikilink = function ( frame )
     return Template( frame, "isWikilink" );
     return Template( frame, "isWikilink" );
end
p.pageLink = function ( frame )
    return Template( frame, "pageLink" );
end
end
p.failsafe = function ( frame )
p.failsafe = function ( frame )

Bitte beachte, dass alle Beiträge im Regiowiki von anderen Mitwirkenden bearbeitet, geändert oder gelöscht werden können. Reiche hier keine Texte ein, falls Du nicht willst, dass diese ohne Einschränkung geändert werden können.

Mit dem Speichern dieser Seite stimmst Du den Nutzungsbedingungen zu und versicherst, dass Du den Beitrag selbst verfasst hast bzw. dass er keine fremden Rechte verletzt, und willigst ein, ihn unter der Creative Commons Attribution/Share-Alike Lizenz 3.0 zu veröffentlichen. Übertrage ohne Genehmigung keine urheberrechtlich geschützten Inhalte!

Du stimmst einer Autorennennung mindestens durch URL oder Verweis auf den Artikel zu. Wenn Du nicht möchtest, dass Dein Text weiterbearbeitet und weiterverbreitet wird bzw. den Nutzungsbedingungen nicht zustimmst, dann speichere ihn nicht.

Abbrechen Bearbeitungshilfe (wird in einem neuen Fenster geöffnet)

[] [[]] | {{}} {{{}}} “” ‘’ «» ‹› „“ ‚‘ · × ° § ~~~~ <s></s> <small></small> <sup></sup> <sub></sub> <tt></tt> <code></code> <pre></pre> &nbsp; [[Kategorie:]] [[Datei:]] [[Media:]] {{DEFAULTSORT:}} {{NAMESPACE}} {{PAGENAME}} #REDIRECT[[]] <gallery></gallery> <!-- --> <nowiki></nowiki> <code><nowiki></nowiki></code> <includeonly></includeonly> <noinclude></noinclude> <onlyinclude></onlyinclude> <translate></translate> <tvar|></> <languages/>