Module:ABIF

From electowiki

This is the very beginning of a Lua implementation of ABIF.

(see also: Module:Electowidget)

Tests

The tests have been moved over to User:RobLa/ABIF/Tests2023September for now


-- From: User:RobLa
-- Comment: Copied from Module:Electowidget
local p = {}

function p.parse_prefs_line(frame)
    -- local prefs_table = {}
    
    local thisline = tostring(frame.args["line"])
    local qty, prefs = thisline:match('(%d+):%s*(.*)')

    if prefs == '' or prefs == nil then
        return { qty = qty, comment = nil, orderedlist = nil }
    end

    local rank = 1
    local prefs_table = {}

    for name, rating in prefs:gmatch('([^/]+)/([^ ]+)') do
        prefs_table[rank] = {
            name = name,
            rating = tonumber(rating),
        }
        rank = rank + 1
    end
    return {
        qty = tonumber(qty),
        prefs = prefs_table,
        comment = nil,
        orderedlist = true
    }
end

function p.prefs_line_json(frame)
	data = p.parse_prefs_line(frame)
	return mw.text.jsonEncode(data)
end


function p.get_data(frame)
	-- copied and adapted from 
	--   https://en.wikipedia.org/wiki/Module:Format_TemplateData
    -- local title, s = mw.title.new(tostring(datapage))
    local p1 = ""
    if frame.args[1] then
        p1 = frame.args[1]
	elseif frame.args["page"] then
	    p1 = frame.args["page"]
    else
        p1 = "FIXME"
	end

	local title = mw.title.new(p1)
	local s = title:getContent(title)
	if not s then
	    r = "[[" .. p1 .. "]]"
	end
	if s then
        local i, j = s:find( "<abif[^>]*>", 1 )
        if i then
            local k = s:find( "</abif>", j, true )
            if k then
                r = mw.text.trim( s:sub( j + 1,  k - 1 ) )
            end
        end
    end
    return r
end

return p