Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Link: Difference between revisions

From Neodyland Wiki
m Protected Module:Link: Widely used template ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite))
 
(5 intermediate revisions by the same user not shown)
Line 13: Line 13:
     callback: Function that will be called on each wikilink with linkInfo as first parameter
     callback: Function that will be called on each wikilink with linkInfo as first parameter
       linkInfo is a table consisting of the following properties:
       linkInfo is a table consisting of the following properties:
       .isRedir (boolean)
       .redir (boolean)
       .isRed (boolean)
       .red (boolean)
       .text (String)
       .text (String)
       .link (String or nil)
       .link (String or nil)
Line 20: Line 20:
        
        
Returns:  
Returns:  
     A table (array-like) of all links
     The a table (array-like) of the results of the supplied callback or,
    if no callback function was supplied, a table of linkInfo tables
   
   
Example:
Example:
Line 30: Line 31:


function p.forEachLink(wikitext, callback)
function p.forEachLink(wikitext, callback)
mw.log("Do not use! Will be disabled with [[:gerrit:110983]]")
     local msg    = mw.message.newRawMessage( wikitext )
     local msg    = mw.message.newRawMessage( wikitext )
     local parsed  = msg:parse()
     local parsed  = msg:parse()
Line 40: Line 43:
     for link in matches do
     for link in matches do
         local linkInfo = {
         local linkInfo = {
             isRedir = not not link:find('class="mw-redirect"', 1, true),
             redir  = not not link:find('class="mw-redirect"', 1, true),
             isRed  = not not link:find('class="new"', 1, true),
             red    = not not link:find('class="new"', 1, true),
             text    = link:match('>(.-)</a>'),
             text    = link:match('>(.-)</a>'),
             link    = link:match('href="(.-)"'),
             link    = link:match('href="(.-)"'),
             title  = link:match('title="(.-)"')
             title  = link:match('title="(.-)"')
         }
         }
         if text and cbCompat then
         if linkInfo.text then
             callback( linkInfo )
            if cbCompat then
                table.insert( allLinkInfos, callback( linkInfo ) )
             else
                table.insert( allLinkInfos, linkInfo )
            end
         end
         end
        table.insert( allLinkInfos, linkInfo )
     end
     end
      
      

Latest revision as of 15:37, 10 February 2014

Module documentation[ create · purge ]
This documentation is transcluded from Module:Link/doc.
local p = {}

--[=[
 forEachLink

 Parses wikilinks and returns information about them
 
Usage:
 require( 'Module:Link' ).forEachLink("wikitext", function(linkInfo) end)
 
Parameters:
    wikitext: String of wikitextformatted links. E.g. "[[Main Page]][[Main page]][[does not exist]]"
    callback: Function that will be called on each wikilink with linkInfo as first parameter
      linkInfo is a table consisting of the following properties:
      .redir  (boolean)
      .red  (boolean)
      .text (String)
      .link (String or nil)
      .title (String or nil)
      
Returns: 
    The a table (array-like) of the results of the supplied callback or, 
    if no callback function was supplied, a table of linkInfo tables
 
Example:
 require( 'Module:Link' ).forEachLink("[[Main Page]][[Main page]][[does not exist]]", function(li)
    mw.log(li.text .. " points to " .. li.link .. " and has title " .. title)
 end)
 
]=]

function p.forEachLink(wikitext, callback)
	mw.log("Do not use! Will be disabled with [[:gerrit:110983]]")
	
    local msg     = mw.message.newRawMessage( wikitext )
    local parsed  = msg:parse()
    local matches = parsed:gmatch( '<a.-</a>' )
    local cbType  = type(callback)
    local cbCompat= cbType == 'function' or ( cbType == 'table' and getmetatable(callback).__call )
    
    local allLinkInfos = {}
    
    for link in matches do
        local linkInfo = {
            redir   = not not link:find('class="mw-redirect"', 1, true),
            red     = not not link:find('class="new"', 1, true),
            text    = link:match('>(.-)</a>'),
            link    = link:match('href="(.-)"'),
            title   = link:match('title="(.-)"')
        }
        if linkInfo.text then
            if cbCompat then
                table.insert( allLinkInfos, callback( linkInfo ) )
            else
                table.insert( allLinkInfos, linkInfo )
            end
        end
    end
    
    return allLinkInfos
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.