Modul:WLink: Unterschied zwischen den Versionen

2.566 Bytes hinzugefügt ,  2. Oktober 2014
+
wp>PerfektesChaos
(update)
wp>PerfektesChaos
(+)
Zeile 1: Zeile 1:
--[=[ 2014-05-03
--[=[ 2014-09-28
WLink
WLink
]=]
]=]
Zeile 142: Zeile 142:
     -- Postcondition:
     -- Postcondition:
     --    Returns  string, string
     --    Returns  string, string
     --                  first with target and title
     --                  first with target
     --                  second result false if not piped
     --                  second result title, or false if not piped
     --              false if nothing found
     --              false if nothing found
     local r1 = false;
     local r1 = false;
Zeile 159: Zeile 159:
         if r1 == "" then
         if r1 == "" then
             r1 = false;
             r1 = false;
        else
            r1 = r1:gsub( "_",        " " )
                  :gsub( " ",  " " )
                  :gsub( " ", " " )
                  :gsub( " ",  " " )
                  :gsub( " ",  " " )
                  :gsub( "  +",      " " );
            r1 = mw.text.decode( r1 );
         end
         end
     end
     end
     return r1, r2;
     return r1, r2;
end -- extractWikilink()
end -- extractWikilink()
function target( attempt )
    -- Retrieve first target (wikilink or URL), or entire string
    -- Precondition:
    --    attempt  -- string, with presumable link somewhere
    -- Postcondition:
    --    Returns  string, number
    --                  string, with detected link target, or entire
    --                  number, with number of brackets, if found, or 2
    --              false if nothing found
    local r1, r2 = WLink.getTarget( attempt );
    if not r1 then
        r1 = mw.text.trim( attempt );
        r2 = 2;
    end
    return r1, r2;
end -- target()




Zeile 208: Zeile 235:
     --    attempt  -- string, with wikilink or page title
     --    attempt  -- string, with wikilink or page title
     -- Postcondition:
     -- Postcondition:
     --    Returns  string, with identified segment
     --    Returns  string, with identified segment, or all
     local r = false;
     local r = false;
    local s, m = target( attempt );
    if m == 2 then
        local sub = s:match( "/([^/]+)$" );
        if sub then
            r = sub;
        else
            r = s;
        end
    end
     return r;
     return r;
end -- WLink.getBaseTitle()
end -- WLink.getBaseTitle()
Zeile 224: Zeile 260:
     --              false if no extension found
     --              false if no extension found
     local r = false;
     local r = false;
     local s, m = WLink.getTargetPage( attempt );
     local s, m = target( attempt );
    if not s then
        s = attempt;
        m = 2;
    end
     if m == 2 then
     if m == 2 then
         s = s:match( "%.(%a+)$" );
         s = s:match( "%.(%a+)$" );
Zeile 234: Zeile 266:
             r = s:lower();
             r = s:lower();
         end
         end
     elseif s:upper():match( "[%./](PDF)%A?" ) then
     elseif s:upper():match( "[%./](PDF)%W?" ) then
         r = "pdf";
         r = "pdf";
     end
     end
Zeile 251: Zeile 283:
     --              false if no file found
     --              false if no file found
     local r = false;
     local r = false;
    local s, m = target( attempt );
    if m == 2 then
        local slow    = ":" .. s:lower();
        local find = function ( a )
                        local seek = string.format( ":%s:().+%%.%%a+$",
                                                    a:lower() );
                        local join = slow:find( seek );
                        local ret;
                        if join then
                            ret = s:sub( join + #a + 1 );
                        end
                        return ret;
                    end;
        r = find( "file" );
        if not r then
            local trsl = mw.site.namespaces[6];
            r = find( trsl.name );
            if not r then
              trsl = trsl.aliases;
              for k, v in pairs( trsl ) do
                  r = find( v );
                  if r then
                      break; -- for k, v
                  end
              end -- for k, v
          end
        end
    end
     return r;
     return r;
end -- WLink.getFile()
end -- WLink.getFile()
Zeile 257: Zeile 317:


function WLink.getFragment( attempt )
function WLink.getFragment( attempt )
     -- Retrieve first target page (page name or URL of page)
     -- Retrieve fragment
     -- Precondition:
     -- Precondition:
     --    attempt  -- string, with presumable link somewhere
     --    attempt  -- string, with presumable fragment
     -- Postcondition:
     -- Postcondition:
     --    Returns  string, with first detected linked page
     --    Returns  string, with detected fragment
     --              false if nothing found
     --              false if no address found
     local r = false;
     local r = false;
     local s = WLink.getTarget( attempt );
     local s, m = target( attempt );
     if r then
     if s then
         local i = s:find( "#", 1, true );
         local i = s:find( "#", 1, true );
         if i then
         if i then
             r = mw.text.trim( r:sub( i + 1 ) );
             if i > 1 then
                s = s:sub( i - 1 );
                i = 2;
            end
            if s:find( "&#", 1, true ) then
                s = mw.text.decode( s );
                i = s:find( "#", 1, true );
                if not i then
                  s = "";
                  i = 0;
                end
            end
            s = s:sub( i + 1 );
            r = mw.text.trim( s );
             if r == "" then
             if r == "" then
                 r = false;
                 r = false;
            elseif m == 2 then
                r = r:gsub( "%.(%x%x)", "%%%1" )
                    :gsub( "_", " " );
                r = mw.uri.decode( r, "PATH" );
             end
             end
         end
         end
Zeile 287: Zeile 364:
     --              false if no project language found
     --              false if no project language found
     local r = false;
     local r = false;
    local s, m = target( attempt );
     return r;
     return r;
end -- WLink.getLanguage()
end -- WLink.getLanguage()
Zeile 300: Zeile 378:
     --              false if no namespace found
     --              false if no namespace found
     local r = false;
     local r = false;
    local s, m = target( attempt );
     return r;
     return r;
end -- WLink.getNamespace()
end -- WLink.getNamespace()
Zeile 643: Zeile 722:
         r = string.format( "<span class=\"error\">%s</span>", r );
         r = string.format( "<span class=\"error\">%s</span>", r );
     end
     end
     return r
     return r;
end -- Template()
end -- Template()


Anonymer Benutzer