From WikiChip
Module:comptable
Revision as of 09:24, 10 May 2023 by QuietRub (talk | contribs) (Testing ask with ad hoc template.)

Documentation for this module may be created at Module:comptable/doc

local p = {}

function p.main(frame)
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
    
    local r = ''
    
    for i = 2, 30 do
    	local val = origArgs[i]
		if not val then break end
		if string.find(val, ":") then
			r = r .. string.gsub(val, "(%d+):(.+)", '<th colspan="%1">%2</th>')
		else
			r = r .. '<th>' .. val .. '</th>'
		end
	end

	return '<tr class="comptable-header"><th>&nbsp;</th>' .. r .. '</tr>'
end

function p.lsep(frame)
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
    
    local r = ''
    
    for i = 2, 30 do
    	local val = origArgs[i]
		if not val then break end
		if string.find(val, ":") then
			r = r .. string.gsub(val, "(%d+):(.+)", '<th colspan="%1" style="text-align: left;">%2</th>')
		else
			r = r .. '<th style="text-align: left;">' .. val .. '</th>'
		end
	end

	return '<tr class="comptable-header"><th>&nbsp;</th>' .. r .. '</tr>'
end

function p.cols(frame)
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
    
    local r = ''
    
    for i = 2, 30 do
    	local val = origArgs[i]
		if not val then break end
		if string.match(val, '^%%.+') then
			r = r .. '<th data-sort-type="number">' .. string.sub(val, 2) .. '</th>'
		else
			r = r .. '<th>' .. val .. '</th>'
		end
	end

	return '<tr class="comptable-header"><th class="unsortable">Model</th>' .. r .. '</tr>'
end

---------------------------------------------

function p.main2(frame)
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
    
    local r = ''
    
    for i = 2, 30 do
    	local val = origArgs[i]
		if not val then break end
		if string.find(val, ":") then
			r = r .. string.gsub(val, "(%d+):(.+)", '<th colspan="%1">%2</th>')
		else
			r = r .. '<th>' .. val .. '</th>'
		end
	end

	return '<tr class="comptable2-header"><th>&nbsp;</th>' .. r .. '</tr>'
end

function p.lsep2(frame)
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
    
    local r = ''
    
    for i = 2, 30 do
    	local val = origArgs[i]
		if not val then break end
		if string.find(val, ":") then
			r = r .. string.gsub(val, "(%d+):(.+)", '<th colspan="%1" style="text-align: left;">%2</th>')
		else
			r = r .. '<th style="text-align: left;">' .. val .. '</th>'
		end
	end

	return '<tr class="comptable2-header"><th>&nbsp;</th>' .. r .. '</tr>'
end

function p.cols2(frame)
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
    
    local r = ''
    
    for i = 2, 30 do
    	local val = origArgs[i]
		if not val then break end
		if string.match(val, '^%%.+') then
			r = r .. '<th data-sort-type="number">' .. string.sub(val, 2) .. '</th>'
		else
			r = r .. '<th>' .. val .. '</th>'
		end
	end

	return '<tr class="comptable2-header"><th class="unsortable">Model</th>' .. r .. '</tr>'
end

function p.askt(frame)
    local origArgs = frame.args
    if not (origArgs[1] or origArgs.condition) and frame:getParent() then
        -- Use args of template containing the #invoke.
        origArgs = frame:getParent().args
    end

    local function undo_nowiki(s)
        s = mw.text.unstripNoWiki(s)
        --[[ Unfortunate because we can't tell if nowiki escaped the
            characters or a user did, and we need them for query
            conditions and to output HTML tags. But &#60; and &#62;
            works. ]]
        s = s:gsub('&lt;', '<'):gsub('&gt;', '>')
        -- https://www.mediawiki.org/wiki/Writing_systems/Syntax
        return s:gsub('%-&#123;', '-{'):gsub('&#125;%-', '}-')
    end

    local pound, eqsign, vbar = 35, 61, 124

    local function check_querycond(s)
        local cond, i, j, k = false, 1
        j, k = s:find('^%s*')
        if j then i = k + 1 end
        while i <= #s and s:byte(i) ~= vbar do
            j = nil
            if cond then
                j, k = s:find('^OR%s*', i)
            end
            if j then
                i = k + 1
                cond = false
            else
                j, k = s:find('^%[%[[^%]]+%]%]%s*', i)
                if j then
                    i = k + 1
                    cond = true
                else
                    error('Missing/invalid condition', 2)
                end
            end
        end
        if not cond then
            error('Missing condition', 2)
        end
        if i > #s then return s, nil end
        return s:sub(1, i - 1), s:sub(i + 1, -1)
    end

    local function check_number(n, default, arg_name)
        if not n then return default end
        n = tonumber(n)
        if not n or n < 0 then
            error('Invalid ' .. arg_name, 2)
        end
        return math.floor(n)
    end

    local special_props = { '#querycondition', '#querylimit',
        '#resultoffset', '#rowcount', '#rownumber', '#userparam' }

    -- May be nil.
    local userparam = origArgs.userparam

    local function transform_template(querycond, s)
        local query = { querycond } -- mw.smw.ask() parameter

        local prop_index = {}
        local n_props = 0
        for i, v in ipairs(special_props) do
            prop_index[v] = i
        end
        local function append_prop(prop)
            local index = prop_index[prop]
            if not index then
                n_props = n_props + 1
                index = #special_props + n_props
                prop_index[prop] = index
                if querycond then
                    table.insert(query, string.format('?%s=%d', prop, n_props))
                end
            end
            return index
        end

        append_prop('') -- page name i.e. SMW ask mainlabel

        local i, depth = 1, 0

        local function parse()
            if depth > 4 then error('Too many defaults', 2) end
            depth = depth + 1

            local templ = {}

            while i <= #s do
                local stop = #s + 1
                if depth > 1 then
                    local j = s:find('}}}', i)
                    if j then stop = j end
                end

                local j, k, prop = s:find('{{{%s*([%w#][^#=|}]*)', i)
                if j == nil or j >= stop then
                    if i < stop then
                        local prefix = s:sub(i, stop - 1)
                        table.insert(templ, { prefix=prefix })
                    end
                    i = stop
                    break
                end
                local prefix = s:sub(i, j - 1)
                i = k + 1 -- skip {{{ and prop name
                prop = prop:match('(.-)%s*$') -- remove trailing whitespace
                local format = nil
                if prop:byte(1) == pound then
                    if not prop_index[prop] then
                        error('Unknown special property "' .. prop .. '"', 2)
                    elseif prop == '#userparam' and not userparam then
                        error('No value for "#userparam" specified', 2)
                    end
                elseif not querycond then
                    error('Unexpected property "' .. prop ..
                          '" in intro/outro template', 2)
                else
                    -- {{{property#format}}}
                    j, k, format = s:find('^(#[^#=|}]+)', i)
                    if j then i = k + 1 end
                end

                local default = {}
                local c = s:byte(i)
                if c == eqsign then
                    error('Property labels not supported', 2)
                elseif c == pound then
                    error('Invalid format for property "' .. prop .. '"', 2)
                elseif c == vbar then
                    i = i + 1
                    -- {{{property|default}}} recursion
                    default = parse()
                end

                if not s:find('^}}}', i) then
                    error('Undelimited property "' .. prop .. '"', 2)
                end
                i = i + 3

                if prop == 'page' then
                    prop = '' -- SMW ask mainlabel: '?' or '?#-'
                    if format and format ~= '#-' then
                        error('Special property "page" supports only format #-', 2)
                    end
                end
                if format == '#tick' then -- not supported in SMW < 3.0
                    format = function(value)
                        if type(value) == 'boolean' then
                            return value and '✔' or '✘'
                        end
                        return tostring(value) or ''
                    end
                else
                    if format then prop = prop .. format end
                    format = function(value)
                        return tostring(value) or ''
                    end
                end

                table.insert(templ, { prefix=prefix, index=append_prop(prop),
                                      default=default, format=format })
            end

            depth = depth - 1

            return templ
        end

        local templ = parse()
        return templ, query
    end

    local valuesep = origArgs.valuesep
    if not valuesep then valuesep = ', ' end

    local function concat_values(format, values)
        if values == nil then return '' end
        if type(values) ~= 'table' then return format(values) end
        local output = {}
        for i, value in ipairs(values) do
            value = format(value)
            if value ~= '' then table.insert(output, value) end
        end
        return table.concat(output, valuesep)
    end

    local function substitute(output, templ, values)
        for i, f in ipairs(templ) do
            table.insert(output, f.prefix)
            if f.index then
                local text = concat_values(f.format, values[f.index])
                if text == '' then
                    substitute(output, f.default, values)
                else
                    table.insert(output, text)
                end
            end
        end
    end

    local function test()
        local function test(pat, a, e1, e2)
            local success, r1, r2 = pcall(check_querycond, a)
            if not success then
                if not pat then
                    error('Function failed unexpectedly: ' .. r1, 2)
                elseif not r1:find(pat) then
                    error(string.format('Unexpected error msg\n✘ %s\n✔ %s\n',
                                        r1, pat), 2)
                end
            elseif pat then
                error('Function succeeded unexpectedly', 2)
            elseif r1 ~= e1 or r2 ~= e2 then
                error(string.format('Unexpected result\n✘ %s,%s\n✔ %s,%s\n',
                                    r1, r2, e1, e2), 2)
            end
        end

        test('Missing cond.*', ' ')
        test('.*inv.*', 'x')
        test('.*inv.*', '[x]')
        test('.*inv.*', '[[x')
        test('.*inv.*', '[[]]')
        test('.*inv.*', 'OR[[x]]')
        test('Missing cond.*', '[[x]]OR')
        local s = '  [[x]]  '
        test(nil, s, s, nil)
        local s = '[[x::foo]][[x::~*bar*]]  [[x<<x]]'
        test(nil, s, s, nil)
        local s = ' \n [[x||x]]  OR \n  [[x>>x]]OR[[x]]\n'
        test(nil, s, s, nil)
        test(nil, '[[x||x]] \n ||[[x]]', '[[x||x]] \n ', '|[[x]]')
        test(nil, '[[x]]| \nx | ', '[[x]]', ' \nx | ')

        local function test(pat, a, exp_cond, exp_outp)
            local success, r1, r2 = pcall(transform_template, cond, a)
            if not success then
                if not pat then
                    error('Function failed unexpectedly: ' .. r1, 2)
                elseif not r1:find(pat) then
                    error(string.format('Unexpected error msg\n✘ %s\n✔ %s\n',
                                        r1, pat), 2)
                end
                return
            elseif pat then
                error('Function succeeded unexpectedly', 2)
            end
            local function escape_concat(s, sep)
                if not s then s = '(nil)' end
                if type(s) == 'table' then s = table.concat(s, sep) end
                -- do return s end
                s = s:gsub('[<%[{]', function(c)
                    return string.format('&#%d;', c:byte()) end)
                return s
            end
            if cond then
                r2 = escape_concat(r2, '|')
                exp_cond = escape_concat(exp_cond)
                if r2 ~= exp_cond then
                    error(string.format('Unexpected query\n✘ %s\n✔ %s\n',
                                        r2, exp_cond), 2)
                end
            end
            local output = {}
            local values = { '[[x]]', 'B', 'C', 'D', 'E', userparam or '',
                'G', 'H', 42, '', true, false, nil }
            substitute(output, r1, values)
            output = escape_concat(output)
            exp_outp = escape_concat(exp_outp)
            if output ~= exp_outp then
                error(string.format('Unexpected output\n✘ %s\n✔ %s\n',
                                    output, exp_outp), 2)
            end
        end

        local function test1()
            local s = ' |<br />\nfoo||{# {{\n '
            test(nil, s, '[[x]]|?=1', s)
            test(nil, 'a{{{=b}}}c', '[[x]]|?=1', 'a{{{=b}}}c')
            test('Unknown.*', '{{{#}}}')
            test('Unknown.*', 'a{{{ #-}}}c')
            test('Unknown.*', '{{{#foo}}}')
            test('Unknown.*', '{{{# rowcount}}}')
            test('Invalid format.*', '{{{#rowcount#hex}}}')
            test('.*label.*', '{{{#rowcount=x|foo}}}')
            test(nil, '{{{#rowcount|foo=x}}}', '[[x]]|?=1', 'D')
            test(nil, 'a{{{#querycondition}}}c', '[[x]]|?=1', 'a[[x]]c')
            test(nil, 'a{{{ #querylimit}}}b', '[[x]]|?=1', 'aBb')
            test(nil, 'a{{{#resultoffset }}}b', '[[x]]|?=1', 'aCb')
            test(nil, 'a{{{#rowcount|default}}}b', '[[x]]|?=1', 'aDb')
            test(nil, 'a{{{#rownumber}}}b', '[[x]]|?=1', 'aEb')
            userparam = nil
            test('No value.*', 'a{{{#userparam}}}b')
            userparam = 'X'
            test(nil, 'a{{{#userparam}}}b', '[[x]]|?=1', 'aXb')
        end

        cond = nil
        test('Unexp.*', 'x{{{x')
        test('Unexp.*', '{{{a}}}}')
        test('Unexp.*', 'a{{{ b # GHz[-] }}}\n{{{d}}}e')
        test('Unexp.*', 'a{{{b=c|foo}}}d')
        test('Unexp.*', 'a{{{b#GHz=c|foo}}}d')
        test('Unexp.*', 'a{{{page}}}c')
        test('Unexp.*', 'a{{{page#-}}}c')
        test1()

        cond = '[[x]]'
        test('Undelim.*', 'x{{{x')
        test(nil, 'a{{{}}}c', '[[x]]|?=1', 'a{{{}}}c')
        test(nil, 'a{{{b}}}c', '[[x]]|?=1|?b=2', 'aHc')
        test(nil, 'a{{{ b }}}', '[[x]]|?=1|?b=2', 'aH')
        test(nil, 'a{{{{ b}}}c', '[[x]]|?=1|?b=2', 'a{Hc')
        test(nil, 'a{{{{{{{{ b}}}c', '[[x]]|?=1|?b=2', 'a{{{{{Hc')
        test(nil, '{{{b}}}}c', '[[x]]|?=1|?b=2', 'H}c')
        test(nil, '{{{b}}}}}}}}c', '[[x]]|?=1|?b=2', 'H}}}}}c')
        test(nil, 'a{{{ b # GHz[-] }}}\n{{{d}}}e',
             '[[x]]|?=1|?b# GHz[-] =2|?d=3', 'aH\n42e')
        test(nil, '{{{a}}}{{{b#tick}}}c{{{d#tick}}}e{{{f#tick}}}' ..
             '{{{g#tick}}}h{{{i#tick}}}j{{{k#tick|x}}}l',
             '[[x]]|?=1|?a=2|?b=3|?d=4|?f=5|?g=6|?i=7|?k=8', 'H42ce✔hjxl')
        test('.*label.*', 'a{{{b=c|foo}}}d')
        test('.*label.*', 'a{{{b#GHz=c|foo}}}d')
        test(nil, 'a{{{=b}}}c', '[[x]]|?=1', 'a{{{=b}}}c')
        test(nil, 'a{{{page}}}c', '[[x]]|?=1', 'aGc')
        test(nil, 'a{{{page#-}}}c', '[[x]]|?=1|?#-=2', 'aHc')
        test('page.*format', '{{{page#GHz}}}')
        test('page.*format', '{{{page# -}}}')
        test1()
        test(nil, '{{{a}}}{{{b}}}{{{c}}}{{{d}}}',
             '[[x]]|?=1|?a=2|?b=3|?c=4|?d=5', 'H42true')
        test(nil, '{{{a|}}}{{{b|}}}{{{c|}}}{{{d|}}}',
             '[[x]]|?=1|?a=2|?b=3|?c=4|?d=5', 'H42true')
        test(nil, '{{{a|1}}}{{{b|2}}}{{{c|3}}}{{{d|4}}}',
             '[[x]]|?=1|?a=2|?b=3|?c=4|?d=5', 'H423true')
        test(nil, '{{{a#1}}}{{{b#2}}}{{{c#3|d#3}}}{{{e|f#4}}}',
             '[[x]]|?=1|?a#1=2|?b#2=3|?c#3=4|?e=5', 'H42d#3true')
        test(nil, '{{{a}}}{{{b}}}{{{c| \n3 }}}',
             '[[x]]|?=1|?a=2|?b=3|?c=4', 'H42 \n3 ')
        test(nil, '{{{a}}}{{{b}}}{{{c|d{{{e}}}{{{f}}}{{{g}}}#f}}}',
             '[[x]]|?=1|?a=2|?b=3|?e=4|?f=5|?g=6|?c=7', 'H42dtruefalse#f')
        test(nil, '{{{a}}}{{{b}}}{{{c|d{{{b}}}e}}}',
             '[[x]]|?=1|?a=2|?b=3|?c=4', 'H42d42e')
        test(nil, '{{{a}}}{{{b}}}{{{c|{{{d}}}{{{e}}}{{{f}}}{{{g|{{{a|x}}}}}}}}}',
             '[[x]]|?=1|?a=2|?b=3|?d=4|?e=5|?f=6|?g=7|?c=8', 'H42truefalseH')
        test(nil, '{{{a|{{{b|{{{c|{{{d|x}}}}}}}}}}}}',
             '[[x]]|?=1|?d=2|?c=3|?b=4|?a=5', 'true')
        test('Too many.*', '{{{a|{{{b|{{{c|{{{d|{{{e|x}}}}}}}}}}}}}}}')
        test(nil, '{{{a}}}{{{b}}}{{{c|{{ifeq:{{{#rowcount}}}|1|2|{{{a}}}}}}}}',
             '[[x]]|?=1|?a=2|?b=3|?c=4', 'H42{{ifeq:D|1|2|H}}')
        test('Unknown.*', '{{{a}}}{{{b}}}{{{c|{{{#duckcount}}}}}}')
        test(nil, '{{{a}}}{{{b}}}{{{c|{{{}}}',
             '[[x]]|?=1|?a=2|?b=3|?c=4', 'H42{{{')
        test(nil, '{{{a}}}{{{b}}}{{{c|}}}}}}',
             '[[x]]|?=1|?a=2|?b=3|?c=4', 'H42}}}')

        mw.log('Test passed.')
        return 'Test passed.'
    end

    if not mw.smw then
        error('Semantic Mediawiki is not available', 1)
    end

    local querycond = origArgs.condition
    local template, template2 = origArgs.template
    if querycond then
        querycond, template2 = check_querycond(querycond)
    else
        querycond = origArgs[1]
        if not querycond then error('Missing condition', 1) end
        if querycond == 'test' then return test() end
        querycond, template2 = check_querycond(undo_nowiki(querycond))
        if not template then
            template = origArgs[2]
            if not template then
                template = template2
                template2 = nil
            end
        end
    end
    if template2 then error('Invalid condition', 1) end
    if not template then error('Missing template', 1) end

    local querylimit = check_number(origArgs.limit, 500, 'limit')
    local resultoffset = check_number(origArgs.offset, 0, 'offset')

    local itempl, otempl
    if origArgs.introtemplate then
        itempl = transform_template(nil, undo_nowiki(origArgs.introtemplate))
    end
    if origArgs.outrotemplate then
        otempl = transform_template(nil, undo_nowiki(origArgs.outrotemplate))
    end
    local rtempl, query = transform_template(querycond, undo_nowiki(template))

    query.limit = querylimit
    query.offset = resultoffset

    local sort = origArgs.sort
    if sort and sort ~= '' then
        sort = string.gsub(',' .. sort .. ',', '%s*,[%s,]*', ',')
        -- Blank = mainlabel, this is also the default.
        sort = sort:gsub(',page,', ',,'):match('^,(.-),$')
        if sort ~= '' then query.sort = sort end
    end

    local order = origArgs.order
    if order and order ~= '' then
        query.order = order
    end

    local results = mw.smw.ask(query)
    if type(results) ~= 'table' or #results < 1 then
        local default = origArgs.default
        if not default then default = '' end
        return default
    end

    local output = {}
    local values_0 = { querycond, querylimit, resultoffset,
        #results, 0, userparam or '' } -- rownumber = 0

    if itempl then substitute(output, itempl, values_0) end

    for i, row in ipairs(results) do
        assert(i >= 1) -- rownumber = 1 to #results incl
        local values_i = { querycond, querylimit, resultoffset,
            #results, i, userparam or '' }
        if type(row) ~= 'table' then row = { row } end
        for j, col in pairs(row) do
            values_i[#special_props + j] = col
        end
        substitute(output, rtempl, values_i)
    end

    if otempl then substitute(output, otempl, values_0) end

    return frame:preprocess(table.concat(output))
end

return p