Difference between revisions of "Module:Autolink"
Dinoguy1000 (talk | contribs) (add unlink function from sandbox) |
Dinoguy1000 (talk | contribs) (allow for ordered lists to be autolinked too) |
||
Line 11: | Line 11: | ||
-- marker used for {{nolink}} support (doesn't have to be a zero-width non-joiner, that's just what I used in the template version) | -- marker used for {{nolink}} support (doesn't have to be a zero-width non-joiner, that's just what I used in the template version) | ||
local zwnj = '‌' | local zwnj = '‌' | ||
− | local links, nolink, escape, el, link, text, formatl, formatr | + | local links, nolink, listmarkup, escape, el, link, text, formatl, formatr |
-- set default to stop errors | -- set default to stop errors | ||
Line 17: | Line 17: | ||
args[1] = mw.text.trim( args[1] ) | args[1] = mw.text.trim( args[1] ) | ||
− | links = mw.text.split( args[1], ' | + | links = mw.text.split( args[1], '\n' ) |
args[2] = #links == 1 and args[2] | args[2] = #links == 1 and args[2] | ||
Line 28: | Line 28: | ||
-- catch empty string at the start of lists | -- catch empty string at the start of lists | ||
if el ~= '' then | if el ~= '' then | ||
+ | if #links > 1 then | ||
+ | listmarkup = mw.ustring.match( el, '^([*#])' ) or '*' | ||
+ | el = mw.ustring.gsub( el, '^[*#]', '' ) | ||
+ | else | ||
+ | listmarkup = '' | ||
+ | end | ||
+ | |||
if mw.ustring.find( el, zwnj ) or mw.ustring.find( el, '%[%[' ) or nolink then | if mw.ustring.find( el, zwnj ) or mw.ustring.find( el, '%[%[' ) or nolink then | ||
el = mw.ustring.gsub( el, '%[%[[Cc]ategory:', '[[:Category:' ) | el = mw.ustring.gsub( el, '%[%[[Cc]ategory:', '[[:Category:' ) | ||
Line 72: | Line 79: | ||
if link == text then | if link == text then | ||
− | links[i] = table.concat( { formatl, '[[', escape, link, ']]', formatr }, '' ) | + | links[i] = table.concat( { listmarkup, formatl, '[[', escape, link, ']]', formatr }, '' ) |
else | else | ||
− | links[i] = table.concat( { formatl, '[[', escape, link, '|', text, ']]', formatr }, '' ) | + | links[i] = table.concat( { listmarkup, formatl, '[[', escape, link, '|', text, ']]', formatr }, '' ) |
end | end | ||
end | end | ||
Line 80: | Line 87: | ||
end | end | ||
− | links = table.concat( links, '\n | + | links = table.concat( links, '\n' ) |
links = mw.ustring.gsub( links, zwnj, '' ) | links = mw.ustring.gsub( links, zwnj, '' ) | ||
links = mw.text.trim( links ) | links = mw.text.trim( links ) |
Revision as of 06:50, 24 February 2015
--
-- implements {{Autolink}} and {{Unlink}}
--
local p = {}
function p.link( frame )
local args = frame:getParent().args
-- for testing from console
-- local args = frame
-- marker used for {{nolink}} support (doesn't have to be a zero-width non-joiner, that's just what I used in the template version)
local zwnj = '‌'
local links, nolink, listmarkup, escape, el, link, text, formatl, formatr
-- set default to stop errors
args[1] = args[1] or ''
args[1] = mw.text.trim( args[1] )
links = mw.text.split( args[1], '\n' )
args[2] = #links == 1 and args[2]
nolink = mw.ustring.find( args[1], zwnj ) == 1
for i=1, #links do
el = mw.text.trim( links[i] )
-- catch empty string at the start of lists
if el ~= '' then
if #links > 1 then
listmarkup = mw.ustring.match( el, '^([*#])' ) or '*'
el = mw.ustring.gsub( el, '^[*#]', '' )
else
listmarkup = ''
end
if mw.ustring.find( el, zwnj ) or mw.ustring.find( el, '%[%[' ) or nolink 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:' )
links[i] = el
else
link = el
text = args[2] or el
formatl = ''
formatr = ''
link = mw.ustring.gsub( link, '""', '' )
link = mw.ustring.gsub( link, "'''?", '' )
-- check for formatting that can be moved out of the link entirely
if mw.ustring.find( text, '^""' ) and mw.ustring.find( text, '""$' ) then
formatl = '"'
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 = formatl .. "'''"
formatr = "'''" .. formatr
text = mw.ustring.gsub( text, "^'''", '' )
text = mw.ustring.gsub( text, "'''$", '' )
end
if mw.ustring.find( text, "^''" ) and mw.ustring.find( text, "''$" ) then
formatl = formatl .. "''"
formatr = "''" .. formatr
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:' ) then
escape = ':'
else
escape = ''
end
if link == text then
links[i] = table.concat( { listmarkup, formatl, '[[', escape, link, ']]', formatr }, '' )
else
links[i] = table.concat( { listmarkup, 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
function p.unlink( frame )
local args = frame:getParent().args
args[1] = args[1] or ''
return mw.ustring.match( args[1], '%[%[:?(.-)[|%]]' ) or mw.text.trim( args[1] )
end
return p