Modul:WLink: Unterschied zwischen den Versionen

5.046 Bytes hinzugefügt ,  25. November 2014
update
wp>PerfektesChaos
(updates)
wp>PerfektesChaos
(update)
Zeile 1: Zeile 1:
--[=[ 2014-10-28
--[=[ 2014-11-01
WLink
WLink
ansiPercent()
ansiPercent()
Zeile 23: Zeile 23:
isValidLink()
isValidLink()
isWikilink()
isWikilink()
wikilink()
]=]
]=]


Zeile 193: Zeile 194:
     return r1, r2;
     return r1, r2;
end -- extractWikilink()
end -- extractWikilink()
local prefix = function ( ask, ahead )
    -- Interprete prefix of language or project type
    -- Precondition:
    --    ask    -- string, with presumable prefix
    --    ahead  -- true, if first segment
    -- Postcondition:
    --    Returns  string,string or nil
    --                    first  string one of "lead", "lang", "project"
    --                    second string is formatted value
    --                      type is one of "lead", "lang", "project"
    --              nil if nothing found
    local r1, r2;
    local prefixes = { b          = true,
                      c          = "commons",
                      d          = true,
                      commons    = true,
                      m          = "meta",
                      mediawiki  = "mw",
                      mw          = true,
                      meta        = true,
                      n          = true,
                      q          = true,
                      s          = true,
                      simple      = false,
                      v          = true,
                      voy        = true,
                      w          = true,
                      wikibooks  = "b",
                      wikidata    = "d",
                      wikinews    = "n",
                      wikipedia  = "w",
                      wikiquote  = "q",
                      wikisource  = "s",
                      wikiversity = "v",
                      wikivoyage  = "voy",
                      wikt        = true,
                      wiktionary  = "wikt"
                    };
    local s = mw.text.trim( ask );
    if s == "" then
        if ahead then
            r1 = "lead";
            r2 = true;
        end
    else
        local p;
        s = s:lower();
        p = prefixes[ s ];
        if p == true then
            r1 = "project";
            r2 = s;
        elseif p then
            r1 = "project";
            r2 = p;
        elseif p == false then
            r1 = "lang";
            r2 = s;
        elseif s:match( "^%l%l%l?$" )
              and  mw.language.isSupportedLanguage( s ) then
            r1 = "lang";
            r2 = s;
        end
    end
    return r1, r2;
end -- prefix()




Zeile 278: Zeile 347:
             if not host then
             if not host then
                 url  = "http://" .. adjust;
                 url  = "http://" .. adjust;
                 host = URLutil.getHost( adjust );
                 host = URLutil.getHost( url );
             end
             end
             if host then
             if host then
Zeile 289: Zeile 358:
                     show = host;
                     show = host;
                 else
                 else
                    local i = path:find( "#" );
                    if i then
                        path = path:sub( 1,  i - 1 );
                    end
                     show = host .. path;
                     show = host .. path;
                 end
                 end
Zeile 497: Zeile 570:
     local r = attempt;
     local r = attempt;
     local i = 1;
     local i = 1;
     local j, k, n, lean, s, shift, suffix;
     local j, k, n, lean, s, shift, space, suffix;
     while ( true ) do
     while ( true ) do
         j = r:find( "[", i, true );
         j = r:find( "[", i, true );
Zeile 518: Zeile 591:
                 if lean then
                 if lean then
                     s, shift = extractWikilink( suffix );
                     s, shift = extractWikilink( suffix );
                     if not shift then
                    space = s:match( "^([^:]+):" );
                     if space then
                        space = mw.site.namespaces[ space ];
                        if space then
                            space = space.id;
                        end
                    end
                    if space == 6  or  space == 14 then
                        shift = "";
                    elseif not shift then
                         shift = s;
                         shift = s;
                     end
                     end
Zeile 792: Zeile 874:
     return ( m == 2 );
     return ( m == 2 );
end -- WLink.isWikilink()
end -- WLink.isWikilink()
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 tables with { value, type }
    --                      type is one of "lead",
    --                          "project", "lang", "ns", "space", "title"
    --              false if nothing found
    local s = contentWikilink( 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
        local j, k, o, v;
        r = { title = "" };
        if n > 4 then
            k = 4;
        else
            k = n - 1;
        end
        j = k;
        for i = 1, j do
            s = mw.text.trim( got[ i ] );
            if s ~= "" then
                o = mw.site.namespaces[ mw.text.trim( got[ i ] ) ];
                if o then
                    r.ns    = o.id;
                    r.space = o.name;
                    k = i + 1;
                    j = i - 1;
                    break; -- for i
                end
            end
        end -- for i
        for i = 1, j do
            o, v = prefix( got[ i ],  ( i == 1 ) );
            if o then
                if r[ o ] then
                    k = i;
                    break; -- for i
                else
                    r[ o ] = v;
                end
            else
                k = i;
                break; -- for i
            end
        end -- for i
        for i = k, n do
            r.title = r.title .. got[ i ];
            if i < n then
                r.title = r.title .. ":";
            end
        end -- for i
    end
    if r.lead and
      ( r.project or
        ( not r.lang  and  r.ns ~= 6  and  r.ns ~= 14 ) ) then
        r.lead = false;
    end
    if r.title == "" then
        r = false;
    end
    return r;
end -- wikilink()




Zeile 854: Zeile 1.017:
p.ansiPercent = function ( frame )
p.ansiPercent = function ( frame )
     return Template( frame, "ansiPercent" );
     return Template( frame, "ansiPercent" );
end
p.formatURL = function ( frame )
    return Template( frame, "formatURL" );
end
end
p.getArticleBase = function ( frame )
p.getArticleBase = function ( frame )
Zeile 866: Zeile 1.032:
p.getFile = function ( frame )
p.getFile = function ( frame )
     return Template( frame, "getFile" );
     return Template( frame, "getFile" );
end
p.formatURL = function ( frame )
    return Template( frame, "formatURL" );
end
end
p.getFragment = function ( frame )
p.getFragment = function ( frame )
Anonymer Benutzer