Difference between revisions of "Module:Autolink"
Dinoguy1000 (talk | contribs) (try this) |
Dinoguy1000 (talk | contribs) (*holds breath*) |
||
Line 11: | Line 11: | ||
-- zero-width non-joiner, used for {{nolink}} support | -- zero-width non-joiner, used for {{nolink}} support | ||
local zwnj = '‌' | local zwnj = '‌' | ||
− | local links, nolink, escape, el, link, text | + | local links, nolink, escape, el, link, text, formatl, formatr |
-- set default to stop errors | -- set default to stop errors | ||
Line 40: | Line 40: | ||
link = el | link = el | ||
text = args[2] or el | text = args[2] or el | ||
+ | formatl = {} | ||
+ | formatr = {} | ||
link = mw.ustring.gsub( link, '""', '' ) | link = mw.ustring.gsub( link, '""', '' ) | ||
link = mw.ustring.gsub( link, "'''?", '' ) | link = mw.ustring.gsub( link, "'''?", '' ) | ||
− | text = mw.ustring.gsub( text, '""', '"' ) | + | |
+ | if mw.ustring.find( text, '^""' ) and mw.ustring.find( text, '""$' ) then | ||
+ | formatl = table.insert( formatl, '"' ) | ||
+ | formatr = table.insert( formatr, '"' ) | ||
+ | text = mw.ustring.gsub( text, '""', '' ) | ||
+ | else | ||
+ | text = mw.ustring.gsub( text, '""', '"' ) | ||
+ | end | ||
+ | if mw.ustring.find( text, "^'''" ) and mw.ustring.find( text, "'''$" ) then | ||
+ | formatl = table.insert( formatl, "'''" ) | ||
+ | formatr = table.insert( formatr, 1, "'''" ) | ||
+ | text = mw.ustring.gsub( text, "^'''", '' ) | ||
+ | text = mw.ustring.gsub( text, "'''$", '' ) | ||
+ | end | ||
+ | if mw.ustring.find( text, "^''" ) and mw.ustring.find( text, "''$" ) then | ||
+ | formatl = table.insert( formatl, "''" ) | ||
+ | formatr = table.insert( formatr, 1, "''" ) | ||
+ | text = mw.ustring.gsub( text, "^''", '' ) | ||
+ | text = mw.ustring.gsub( text, "''$", '' ) | ||
+ | end | ||
if mw.ustring.find( link, '^[Cc]ategory:' ) or | if mw.ustring.find( link, '^[Cc]ategory:' ) or | ||
Line 55: | Line 76: | ||
if link == text then | if link == text then | ||
− | links[i] = table.concat( {'[[', escape, link, ']]'}, '' ) | + | links[i] = table.concat( {formatl, '[[', escape, link, ']]', formatr}, '' ) |
else | else | ||
− | links[i] = table.concat( {'[[', escape, link, '|', text, ']]'}, '' ) | + | links[i] = table.concat( {formatl, '[[', escape, link, '|', text, ']]', formatr}, '' ) |
end | end | ||
end | end |
Revision as of 13:33, 17 February 2015
--
-- implements {{Autolink}}
--
local p = {}
function p.link( frame )
local args = frame:getParent().args
-- for testing from console
-- local args = frame
-- zero-width non-joiner, used for {{nolink}} support
local zwnj = '‌'
local links, nolink, escape, el, link, text, formatl, formatr
-- set default to stop errors
args[1] = args[1] or ''
links = mw.text.split( args[1], '*' )
if mw.ustring.find( mw.text.trim( args[1] ), zwnj ) == 1 then
nolink = true
else
nolink = false
end
for i=1, #links do
el = mw.text.trim( links[i] )
-- catch empty string at the start of lists
if el ~= '' then
if mw.ustring.find( el, zwnj ) or nolink then
links[i] = el
elseif mw.ustring.find( el, '%[%[' ) then
el = mw.ustring.gsub( el, '%[%[[Cc]ategory:', '[[:Category:' )
el = mw.ustring.gsub( el, '%[%[[Ff]ile:', '[[:File:' )
el = mw.ustring.gsub( el, '%[%[[Ii]mage:', '[[:Image:' )
el = mw.ustring.gsub( el, '%[%[[Mm]edia:', '[[:Media:' )
links[i] = el
else
link = el
text = args[2] or el
formatl = {}
formatr = {}
link = mw.ustring.gsub( link, '""', '' )
link = mw.ustring.gsub( link, "'''?", '' )
if mw.ustring.find( text, '^""' ) and mw.ustring.find( text, '""$' ) then
formatl = table.insert( formatl, '"' )
formatr = table.insert( formatr, '"' )
text = mw.ustring.gsub( text, '""', '' )
else
text = mw.ustring.gsub( text, '""', '"' )
end
if mw.ustring.find( text, "^'''" ) and mw.ustring.find( text, "'''$" ) then
formatl = table.insert( formatl, "'''" )
formatr = table.insert( formatr, 1, "'''" )
text = mw.ustring.gsub( text, "^'''", '' )
text = mw.ustring.gsub( text, "'''$", '' )
end
if mw.ustring.find( text, "^''" ) and mw.ustring.find( text, "''$" ) then
formatl = table.insert( formatl, "''" )
formatr = table.insert( formatr, 1, "''" )
text = mw.ustring.gsub( text, "^''", '' )
text = mw.ustring.gsub( text, "''$", '' )
end
if mw.ustring.find( link, '^[Cc]ategory:' ) or
mw.ustring.find( link, '^[Ff]ile:' ) or
mw.ustring.find( link, '^[Ii]mage:' ) or
mw.ustring.find( link, '^[Mm]edia:' ) then
escape = ':'
else
escape = ''
end
if link == text then
links[i] = table.concat( {formatl, '[[', escape, link, ']]', formatr}, '' )
else
links[i] = table.concat( {formatl, '[[', escape, link, '|', text, ']]', formatr}, '' )
end
end
end
end
links = table.concat( links, '\n*' )
links = mw.ustring.gsub( links, zwnj, '' )
links = mw.text.trim( links )
return links
end
return p