Module:Diff/sandbox: Difference between revisions

Content deleted Content added
poke
update from commons
Line 203:
-- Wiki diff style, currently just for a line
-----------------------------------------------------------------------------
local function wikiDiff(old, new, separator, opts)
opts = opts or {}
local tokens = diff(old, new, separator)
local root = mw.html.create('')
root:wikitext(mw.getCurrentFrame():extensionTag('templatestyles', '', {src = 'Module:Diff/styles.css'}))
 
local token, status
 
-- Override default border-width for browsers that support them.
local plusMinusStyle = 'width: 2%; padding: 0.25em; font-weight: bold;' ..
-- Needed for RTL support; forbidden in TemplateStyles.
'font-size: 1.25em; text-align: end;'
local tdDivStyletdSharedStyle = 'word-wrapwebkit-border-end-width: break-word1px; direction-webkit-border-start-width: ltr4px; ' ..
'-webkitmoz-border-end-width: 1px; -webkitmoz-border-start-width: 4px; ' ..
 
local tdSharedStyle = 'width: 48%; border-style: solid; border-radius: 0.33em; ' ..
local is_different = false
'padding: 0.33em 0.5em; font-size: 88%; white-space: pre-wrap; border-width: 1px 1px 1px 4px; ' ..
for _, token_record in ipairs(tokens) do
'-webkit-border-end-width: 1px; -webkit-border-start-width: 4px; ' ..
if token_record[2] ~= SAME then
'-moz-border-end-width: 1px; -moz-border-start-width: 4px;' -- these overrides default border-width for browsers supports them, needed for RTL UI on commons
is_different = true
local insDelSharedStyle = 'padding: 0.25em 0; font-weight: bold; text-decoration: initial;'
break
end
end
 
local tbl = root:tag('table'):attr('lang', ''):addClass('diff')
if (opts.oldTitle or opts.newTitle) then
local tr = tbl:tag('tr')
tr:wikitexttag('th')
:attr('scope', 'col')
:attr('colspan', '2')
:wikitext(opts.oldTitle)
tr:tag('th')
:attr('scope', 'col')
:attr('colspan', '2')
:wikitext(opts.newTitle)
end
 
local tr = root:tag('table'):addClass('diff'):css('width', '100%')tbl:tag('tr')
 
tr:tag('td')
:addClass('diff-marker')
:wikitext(is_different and '−' or ' ')
:cssText(plusMinusStyle)
:wikitext('−')
 
local deleted = tr
:tag('td')
:cssText('border-color: #ffe49c; ' .. tdSharedStyle)
:addClass(is_different and 'diff-deletedline' or 'diff-context')
:tag('div')
:cssText(tdDivStyle)
 
for i, token_record in ipairs(tokens) do
Line 240 ⟶ 255:
deleted
:tag('del')
:cssText('background: #feeec8; ' .. insDelSharedStyle)
:addClass('diffchange')
:addClass('diffchange-inline')
Line 250 ⟶ 264:
 
tr:tag('td')
:cssTextaddClass(plusMinusStyle'diff-marker')
:wikitext(is_different and '+' or ' ')
 
local inserted = tr
:tag('td')
:cssText('border-color: #a3d3ff; ' .. tdSharedStyle)
:addClass(is_different and 'diff-addedline' or 'diff-context')
:tag('div')
:cssText(tdDivStyle)
 
for i, token_record in ipairs(tokens) do
Line 266 ⟶ 279:
inserted
:tag('ins')
:cssText('background: #d8ecff; ' .. insDelSharedStyle)
:addClass('diffchange')
:addClass('diffchange-inline')
Line 276 ⟶ 288:
 
return tostring(root)
end
 
local function tidyVal(val)
if ((type(val) == 'string') and (val == '')) then
return nil
end
return val
end
 
local function main(frame)
local args = frame.args
return wikiDiff(mw.text.unstrip(mw.text.decode(frame.args[1])), mw.text.decode(mw.text.unstrip(frame.args[2])), frame.args[3] or '[%s%.:-]+')
local pargs = (frame:getParent() or {}).args or {}
return wikiDiff(
mw.text.unstrip(mw.text.decode(args[1])),
mw.text.unstrip(mw.text.decode(args[2])),
frame.args[3] or '[%s%.:-]+',
{
oldTitle = tidyVal(args['1title']) or tidyVal(pargs['1title']),
newTitle = tidyVal(args['2title']) or tidyVal(pargs['2title']),
}
)
end