Difference between revisions of "Module:Autolink"
Dinoguy1000 (talk | contribs) (fix frame stuff to also allow calling in other module invocations) |
Dinoguy1000 (talk | contribs) (show section links with a section sign in links without custom link text) |
||
Line 15: | Line 15: | ||
-- marker used for {{nolink}} support (doesn't have to be a zero-width non-joiner, that's just what was used in the template version) | -- marker used for {{nolink}} support (doesn't have to be a zero-width non-joiner, that's just what was used in the template version) | ||
local zwnj = '‌' | local zwnj = '‌' | ||
− | local linkarr, links, listmarkup, el, link, txt, formatl, formatr | + | local linkarr, links, listmarkup, el, link, raw, txt, formatl, formatr |
-- set default to stop errors | -- set default to stop errors | ||
Line 32: | Line 32: | ||
-- catch empty string at the start of lists | -- catch empty string at the start of lists | ||
− | if not el: | + | if not el:find( '^[*#;:]?$' ) then |
if listmarkup ~= '' then | if listmarkup ~= '' then | ||
listmarkup = ( el:match( '^([*#;:])' ) or '*' ) .. ' ' | listmarkup = ( el:match( '^([*#;:])' ) or '*' ) .. ' ' | ||
Line 38: | Line 38: | ||
end | end | ||
− | if el:find( zwnj ) or el:find( '%[%[' ) then | + | if el:find( zwnj ) or el:find( '%[%[.-|.-%]%]' ) then |
linkarr[i] = table.concat( { listmarkup, el }, '' ) | linkarr[i] = table.concat( { listmarkup, el }, '' ) | ||
else | else | ||
+ | raw = el:find( '%[%[' ) | ||
+ | el = el:match( '%[%[(.-)%]%]' ) or el | ||
link = el | link = el | ||
txt = args[2] or el | txt = args[2] or el | ||
Line 46: | Line 48: | ||
formatr = '' | formatr = '' | ||
− | link = link:gsub( '""', '' ):gsub( "'''?", '' ) | + | if raw ~= 1 then |
+ | link = link:gsub( '""', '' ):gsub( "'''?", '' ) | ||
− | + | -- check for formatting that can be moved out of the link entirely | |
− | + | if txt:find( '^""' ) and txt:find( '""$' ) then | |
− | + | formatl = '"' | |
− | formatr = '"' | + | formatr = '"' |
− | txt = txt:gsub( '""', '' ) | + | txt = txt:gsub( '""', '' ) |
+ | else | ||
+ | txt = txt:gsub( '""', '"' ) | ||
+ | end | ||
+ | if txt:find( "^'''" ) and txt:find( "'''$" ) then | ||
+ | formatl = formatl .. "'''" | ||
+ | formatr = "'''" .. formatr | ||
+ | txt = txt:gsub( "'''$", '' ):gsub( "^'''", '' ) | ||
+ | end | ||
+ | if txt:find( "^''" ) and txt:find( "''$" ) then | ||
+ | formatl = formatl .. "''" | ||
+ | formatr = "''" .. formatr | ||
+ | txt = txt:gsub( "''$", '' ):gsub( "^''", '' ) | ||
+ | end | ||
else | else | ||
− | txt = txt:gsub( '" | + | txt = txt:gsub( "'", ''' ) |
end | end | ||
− | if txt:find( | + | if txt:find( '#' ) then |
− | + | txt = txt:gsub( '#', ' § ') | |
− | |||
− | txt = txt:gsub( | ||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Revision as of 18:49, 9 January 2016
--
-- implements {{Autolink}} and {{Unlink}}
--
local p = {}
local text = mw.text
function p.link( frame )
if frame == mw.getCurrentFrame() then
args = frame:getParent().args
else
args = frame
end
-- marker used for {{nolink}} support (doesn't have to be a zero-width non-joiner, that's just what was used in the template version)
local zwnj = '‌'
local linkarr, links, listmarkup, el, link, raw, txt, formatl, formatr
-- set default to stop errors
links = args[1] and text.trim( args[1] ) or ''
-- check for nolink token at the front of the input, which prevents per-link/item processing
if links:find( zwnj ) ~= 1 then
linkarr = text.split( links, '\n' )
args[2] = #linkarr == 1 and args[2]
listmarkup = #linkarr == 1 and ''
for i = 1, #linkarr do
el = text.trim( linkarr[i] )
-- catch empty string at the start of lists
if not el:find( '^[*#;:]?$' ) then
if listmarkup ~= '' then
listmarkup = ( el:match( '^([*#;:])' ) or '*' ) .. ' '
el = el:gsub( '^[*#;:]%s*', '' )
end
if el:find( zwnj ) or el:find( '%[%[.-|.-%]%]' ) then
linkarr[i] = table.concat( { listmarkup, el }, '' )
else
raw = el:find( '%[%[' )
el = el:match( '%[%[(.-)%]%]' ) or el
link = el
txt = args[2] or el
formatl = ''
formatr = ''
if raw ~= 1 then
link = link:gsub( '""', '' ):gsub( "'''?", '' )
-- check for formatting that can be moved out of the link entirely
if txt:find( '^""' ) and txt:find( '""$' ) then
formatl = '"'
formatr = '"'
txt = txt:gsub( '""', '' )
else
txt = txt:gsub( '""', '"' )
end
if txt:find( "^'''" ) and txt:find( "'''$" ) then
formatl = formatl .. "'''"
formatr = "'''" .. formatr
txt = txt:gsub( "'''$", '' ):gsub( "^'''", '' )
end
if txt:find( "^''" ) and txt:find( "''$" ) then
formatl = formatl .. "''"
formatr = "''" .. formatr
txt = txt:gsub( "''$", '' ):gsub( "^''", '' )
end
else
txt = txt:gsub( "'", ''' )
end
if txt:find( '#' ) then
txt = txt:gsub( '#', ' § ')
end
if link == txt then
linkarr[i] = table.concat( { listmarkup, formatl, '[[', link, ']]', formatr }, '' )
else
linkarr[i] = table.concat( { listmarkup, formatl, '[[', link, '|', txt, ']]', formatr }, '' )
end
end
end
end
links = table.concat( linkarr, '\n' )
end
links = text.trim( links:gsub( zwnj, '' )
:gsub( '%[%[[Cc]ategory:', '[[:Category:' )
:gsub( '%[%[[Ff]ile:', '[[:File:' )
:gsub( '%[%[[Ii]mage:', '[[:File:' ) )
return links
end
function p.unlink( frame )
local args = frame:getParent().args
return args[1] and ( args[1]:match( '%[%[:?(.-)[|%]]' ) or text.trim( args[1] ) )
end
return p