local p =
local getArgs = require('Module:Arguments').getArgs
local function replace(s, p, r) return mw.ustring.gsub(s, p, r)end
local function collapseWhitespace(s) return replace(s, '%s*', )end
local function toLower(s) return mw.ustring.lower(s)end
local function collapseToLower(s) return toLower(collapseWhitespace(s))end
local function splitter(s, d, p) return mw.text.split(s, d, p or false)end
local function join(t, d) return table.concat(t, d or )end
local function firstUpper(p) p = toLower(p) if p ~= 'pawn' then if p ~= 'knight' then return mw.ustring.upper(splitter(p, )[1 ]) end return 'N' end return end
local function removeAlphas(s) if s and #s > 0 then local r = replace(s, '%D', ) if #r > 0 then return r end end return nilend
local function finder(hs, n) return mw.ustring.find(toLower(hs), n)end
local function buildDemo(configuration) local function eD return mw.html.create('div') end local function eP return mw.html.create('p') end local function eB return mw.html.create('b') end local function addSpan(n, s) local se = mw.html.create('span') se:wikitext(s) n:node(se) end local i = 0 local W = 'w"|' local B = 'b"|' local aN = '\n|' local tR = aN .. '-' local aS = '' local aP = '
' local aB = aS .. aP .. 'b">' local a = join local r = join local rW = rC .. W .. r local rB = r .. rC .. W local T = join local peice local board = eD local fallback = eD local chessDemo = eD local interface = eD local borderfix = eD chessDemo:attr('data-data', mw.text.jsonEncode(configuration.instructions)) chessDemo:attr('class', 'chessDemo') :addClass('cd-border') :css if configuration.notation.notes and configuration.notation.float then chessDemo:addClass('cd-notation-' .. configuration.notation.float) end if configuration.title then local n = eP n:attr('class', 'cd-title') :wikitext(configuration.title) chessDemo:node(n) end if configuration.info then local n = eP n:attr('class', 'cd-information') :wikitext(configuration.info) chessDemo:node(n) end interface:attr('class', 'cd-interface') if configuration.orientation then interface:addClass('cd-persp-b') end borderfix:attr('class', 'cd-border') :css('font-size', configuration.width.board .. 'px') board:attr('class', 'cd-board') :addClass('noprint') while i < #configuration.setup do i = i + 1 peice = eD peice:attr('class', configuration.setup[i ]) board:node(peice) end fallback:attr('class', 'cd-fallback') :wikitext('') board:node(fallback) borderfix:wikitext(T) :node(board) interface:node(borderfix) if configuration.instructions.controls then local controls = eD controls:attr('class', 'cd-controls') interface:node(controls) end chessDemo:node(interface) if configuration.notation.notes then local n local c local m local be local note local pe = eP local notes = local alternator = 1 local notewrap = eD local notation = eD local nnp = local columns = configuration.notation.columns local notecount = configuration.notation.numbering if configuration.blackfirst then alternator = 0 nnp = end notation:attr('class', 'cd-notation') pe:attr('class', 'cd-title') :wikitext('Notation') notation:node(pe) if configuration.notation.collapsible then notation:addClass('mw-collapsible') if configuration.notation.collapsed then notation:addClass('mw-collapsed') end notewrap:attr('class', 'mw-collapsible-content') end for index, value in ipairs(configuration.notation.notes) do n = value[1 ] c = value[2 ] m = index % 2 if not note then be = eB note = eP note:node(be) if columns then notes[#notes + 1 ] = note else notewrap:node(note) end if notecount then be:wikitext(tostring(notecount) .. nnp[m + 1 ]) end end if c then note:tag('br') addSpan(note, c) note = nil end if m0 then column = eD column:css('width', configuration.notation.width .. 'px') notewrap:node(column) created = created + 1 end column:node(value) end if created < columns then chessDemo:css('width', tostring(tonumber(configuration.width.master) - (tonumber(configuration.notation.width) + 20)) .. 'px') end elseif configuration.notation.float then notewrap:css('width', configuration.notation.width .. 'px') end notation:node(notewrap) chessDemo:node(notation) end chessDemo:wikitext('') return tostring(chessDemo)end
local function _getDemo(args) local pC = 'cd-piece-' local pF = 'cd-file-' local pR = 'cd-rank-' local pR1 = pR .. '1' local pR2 = pR .. '2' local pR7 = pR .. '7' local pR8 = pR .. '8' local pB = pC .. 'black' local pW = pC .. 'white' local configuration = configuration.instructions.setup = configuration.setup if args.initial then local i = removeAlphas(args.initial) if i then configuration.instructions.initial = tonumber(i) end else configuration.instructions.initial = 0 end if args.controls then if finder(args.controls, '^min') then configuration.instructions.controls = 'min' elseif finder(args.controls, '^med') then configuration.instructions.controls = 'med' else configuration.instructions.controls = false configuration.instructions.autostart = true end end if args.width then local width = removeAlphas(args.width) if width then width = tonumber(width) if width >= 200 then configuration.width.master = tostring(width + 4) configuration.width.board = tostring(width / 10) configuration.width.img = tostring((width / 10) * 8) end end end if not args.notation or (args.notation and args.notation ~= 'hidden') then local columns = tonumber(configuration.notation.columns or '1') configuration.notation.notes = configuration.notation.width = tostring((math.floor(tonumber(configuration.width.master) / columns) - 20) + math.floor(20 / columns)) if args.notation
'collapsed' then configuration.notation.collapsible = true if args.notation
3 then piece = pC .. firstUpper(bits[2 ]) .. ' ' coords = splitter(toLower(bits[3 ]), ) else piece = coords = splitter(toLower(bits[2 ]), ) end if configuration.blackfirst
firstmovefrom then configuration.blackfirst = color
local function _getNotation(args) local arg = local move = if args.castle then arg = args.castle move = move .. '0-0' if arg
'mate' then move = move .. '#' else move = move .. '+' end end move = move .. collapseWhitespace(args.punctuation or ) if args.gamescore then arg = toLower(args.gamescore) move = move .. ' ' if arg
'black' then move = move .. '0–1' else move = move .. '½–½' end end if args.comment then move = move .. '\n' .. args.comment end return moveend
function p.getNotation(frame) return _getNotation(getArgs(frame:getParent.args))end
function p.getDemo(frame) local args = getArgs(frame:getParent.args) if mw.isSubsting then local r = for key, value in pairs(args) do if not tonumber(key) then r[#r + 1 ] = key .. ' = ' .. value end end for index, value in ipairs(args) do r[#r + 1 ] = tostring(index) .. ' = ' .. value end return '' end return _getDemo(args)end
return p