Module:Notation

-- Copyright ⓒ Adrien Burgun (Shad Amethyst) -- Licensed under the MIT licence

local base64 = require("Module:Base64") local getArgs = require("Dev:Arguments").getArgs local yesno = require("Dev:Yesno") local p = {}

function p.chessin5dlink(frame) return tostring(p._chessin5dlink(frame.args[1]:gsub('\\n', '\n'))) end

function p.notationbox(frame) local div = mw.html.create('div') div:addClass('notation-box') div:addClass('mw-highlight') div:wikitext(p._notationbox(frame.args[1]:gsub('\\n', '\n'))) return tostring(div) end

function p.notationframe(frame) local artificialFrame = {args = {}} for k, v in pairs(frame.args) do		if tonumber(k) == nil then artificialFrame[k] = v		end end local args = getArgs(artificialFrame)

if args.links == nil then args.links = true end if args.highlight == nil then args.highlight = true end if args.imageposition == nil then args.imageposition = 'top' end local divOuter = mw.html.create('div') divOuter:addClass('notation-outer') local divFrame = mw.html.create('div') divFrame:addClass('notation-frame') local divNotation = mw.html.create('div') divNotation:addClass('notation-box') divNotation:node(p._notationbox(frame.args[1]:gsub('\\n', '\n'))) if type(args.commentary) == 'string' and args.commentary ~= '' then local commentary = mw.html.create('div') commentary:addClass('notation-commentary') commentary:wikitext(args.commentary) divFrame:addClass('notation-shrink') divNotation:node(commentary) end divFrame:node(divNotation) if yesno(args.links, true) then local divLinks = mw.html.create('div') divLinks:addClass('notation-links') divLinks:wikitext(			tostring(p._chessin5dlink(frame.args[1]:gsub('\\n', '\n')))			.. ' | '			.. 'What is this?'		) divFrame:node(divLinks) end divOuter:node(divFrame) if type(args.image) == 'string' and args.image ~= '' then local image = mw.html.create('div') image:addClass('notation-image') local size = '1200px' if args.imageposition == 'left' or args.imageposition == 'right' then size = '600px' end image:wikitext('') if args.imageposition == 'left' then divOuter:addClass('notation-image-left') elseif args.imageposition == 'right' then divOuter:addClass('notation-image-right') elseif args.imageposition == 'bottom' then divOuter:addClass('notation-image-bottom') else divOuter:addClass('notation-image-top') end divOuter:node(image) end return tostring(divOuter) end

function p.notationspan(frame) local code = mw.html.create('code') code:addClass('notation-inline') code:wikitext(mw.text.trim(p._syntaxhighlight(frame.args[1]))) return tostring(code) end

function p._chessin5dlink(raw) local b64 = base64.enc(raw) mw.log(b64) local span = mw.html.create('span') span:addClass('chessin5d-link') span:wikitext('.. b64 .. ' See on chessin5d.net') return span end

function p._notationbox(raw, args) local formatted = raw if type(args) ~= 'table' or yesno(args.highlight, true) then formatted = p._syntaxhighlight(raw) end local pre = mw.html.create('pre') pre:wikitext(mw.text.trim(formatted)) return pre end

function p._syntaxhighlight(raw) local formatted = '' for line in mw.text.gsplit(raw, '\n') do		if line:sub(1, 1) == '[' then formatted = formatted .. ' '				.. line:gsub('^%[(%w+) "([^"]+)"%]', '[ %1 " %2 "]')					:gsub('^%[([a-zA-Z0-9%-+*/]+):([+%-]?%d+):([+%-]?%d+):([wb])%]', '[ %1 : %2 : %3 : %4  ]')				.. ' \n'		else			local last_space = false			for token in mw.text.gsplit(line, ' ') do				last_space = true				if token:match('%d%.%.?') ~= nil then					formatted = formatted .. ' ' .. token .. ' '				elseif token == '/' then					formatted = formatted .. ' / '				else					formatted = formatted						.. token							:gsub('>>?', ' %0 ')							:gsub('%(L?%dT%d%)', ' %0 ')							:gsub('[KNBRCSPUDW]', ' %0 ')							:gsub('([a-w])([0-9]+)', ' %1 %2 ')							:gsub('[%+~!?]', ' %0 ')						.. ' '				end			end			if last_space then				formatted = formatted:sub(1, -2)			end			formatted = formatted .. '\n'		end	end	return formatted end

return p