Module:Outdent: Difference between revisions
Appearance
Content deleted Content added
m Protected "Module:Outdent": to match Template:Outdent ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite)) |
use borders instead of drawing boxes, code by User:BrandonXLF, discussed on talk page |
||
Line 3: | Line 3: | ||
function p.outdent (frame) |
function p.outdent (frame) |
||
local args = getArgs |
local args = getArgs(frame) |
||
local |
local width = 0 |
||
local Start = '┌' |
|||
local End = '┘' |
|||
local i = 0 |
|||
local line = '' |
|||
local outside = mw.html.create('span') |
|||
local indentTable = {'reverse','indent','in','r'} -- lsit of parameters that mean reverse/indent |
|||
for k,v in pairs(indentTable) do -- set start and end if reversed |
|||
if args[v] then |
|||
Start = '└' |
|||
End = '┐' |
|||
end |
|||
end |
|||
args['r'] = args['reverse'] or args['indent'] or args['r'] or args['in'] -- aliases for reverse |
|||
if not args[1] then args[1] = '' end -- un-nil args[1] |
if not args[1] then args[1] = '' end -- un-nil args[1] |
||
width = width + select(2, string.gsub(args[1],':','')) -- increase by 1 for every : |
|||
width = width + select(2, string.gsub(args[1],'*','')) -- increase by 1 for every * |
|||
count = count+1 -- increase count for every : |
|||
width = width + select(2, string.gsub(args[1],'#','')) * 2 -- increase by 2 for every # |
|||
end |
|||
for _ in string.gmatch(args[1],'*') do |
|||
count = count+1 -- increase count for every * |
|||
end |
|||
for _ in string.gmatch(args[1],'#') do |
|||
count = count+2 -- increase count by 2 for every # |
|||
end |
|||
if count == 0 then -- set count to args[1] if needed |
|||
count = tonumber(args[1]) |
|||
end |
|||
if not count then |
|||
count = 10 -- 10 default |
|||
end |
|||
if count > 40 then |
|||
count = 40 -- 40 max |
|||
end |
|||
count = count * 1.6 - 0.7 -- convert to width |
|||
local lineMath = math.ceil(count * 1.6) |
|||
while i < lineMath do -- create line |
|||
line = line..'─' |
|||
i = i+1 |
|||
end |
|||
if width == 0 then width = tonumber(args[1]) end -- set width to args[1] if needed |
|||
if count == - 0.7 then -- set to pipe | if count was 0 |
|||
line = '|' |
|||
Start = '' |
|||
End = '' |
|||
end |
|||
if not width then width = 10 end -- default width |
|||
outside -- create span tag |
|||
if width > 40 then width = 40 end -- max width |
|||
:addClass('outdent-template') |
|||
:css('display','block') |
|||
:css('margin-top','-0.5em') |
|||
:css('color','#AAA;') |
|||
:wikitext('<span style="display:inline-block; overflow:hidden;">'..Start..'</span>') -- start |
|||
:wikitext('<span style="display:inline-block; overflow:hidden; word-wrap:normal; width:'..count..'em;">'..line..'</span>') -- middle |
|||
:wikitext('<span style="display:inline-block; overflow:hidden;">'..End..'</span>') -- end |
|||
if line ~= '|' then -- if not pipe (|) |
|||
outside |
|||
:css('position','relative') |
|||
:css('left','-0.25em') |
|||
else |
|||
outside |
|||
:css('margin-top','0.25em') |
|||
end |
|||
width = width * 1.6 -- set width to proper width |
|||
if args['test_line_width'] then -- used to test line width before and after width reduction |
|||
return 'Before:'..'<br />'..line..'<br />'..'After:'..'<br />'..'<span style="display:inline-block;overflow:hidden;word-wrap:normal;width:'..count..'em;">'..line..'</span>'..'<br />'..'Final:'..'<br />'..tostring(outside) |
|||
end |
|||
local top = '<span style="display:block;width:' .. width .. 'em;height:0.5em;' .. (width == 0 and '' or 'border-bottom:1px solid #AAA;') .. 'border-' .. ((width == 0 or args['r']) and 'left' or 'right') ..':1px solid #AAA;"></span>' -- top half |
|||
if args[2] then |
|||
local bottom = '<span style="display:block;width:' .. width .. 'em;height:0.5em;border-' .. (args['r'] and 'right' or 'left') .. ':1px solid #AAA;"></span>' -- bottom half |
|||
local note = mw.html.create('span') |
|||
local note = args[2] and '<span>([[Wikipedia:Indentation#Outdenting|outdent]]) </span>' or '' -- note |
|||
return tostring(outside)..tostring(note) |
|||
else |
|||
return outside |
|||
end |
|||
return '<span class="outdent-template" style="position:relative;left:1px;">' .. top .. bottom .. note .. '</span>'; |
|||
end |
end |
||
Revision as of 11:31, 25 November 2019
Implements {{Outdent}}.
See also
local p = {}
local getArgs = require('Module:Arguments').getArgs
function p.outdent (frame)
local args = getArgs(frame)
local width = 0
args['r'] = args['reverse'] or args['indent'] or args['r'] or args['in'] -- aliases for reverse
if not args[1] then args[1] = '' end -- un-nil args[1]
width = width + select(2, string.gsub(args[1],':','')) -- increase by 1 for every :
width = width + select(2, string.gsub(args[1],'*','')) -- increase by 1 for every *
width = width + select(2, string.gsub(args[1],'#','')) * 2 -- increase by 2 for every #
if width == 0 then width = tonumber(args[1]) end -- set width to args[1] if needed
if not width then width = 10 end -- default width
if width > 40 then width = 40 end -- max width
width = width * 1.6 -- set width to proper width
local top = '<span style="display:block;width:' .. width .. 'em;height:0.5em;' .. (width == 0 and '' or 'border-bottom:1px solid #AAA;') .. 'border-' .. ((width == 0 or args['r']) and 'left' or 'right') ..':1px solid #AAA;"></span>' -- top half
local bottom = '<span style="display:block;width:' .. width .. 'em;height:0.5em;border-' .. (args['r'] and 'right' or 'left') .. ':1px solid #AAA;"></span>' -- bottom half
local note = args[2] and '<span>([[Wikipedia:Indentation#Outdenting|outdent]]) </span>' or '' -- note
return '<span class="outdent-template" style="position:relative;left:1px;">' .. top .. bottom .. note .. '</span>';
end
return p