Module:sla-cite

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local export = {}

local lang = require("Module:languages").getByCode("sla-pro")

local u = mw.ustring.char
local rfind = mw.ustring.find
local rsubn = mw.ustring.gsub
local rmatch = mw.ustring.match
local rsplit = mw.text.split
local ulower = mw.ustring.lower
local uupper = mw.ustring.upper
local usub = mw.ustring.sub
local ulen = mw.ustring.len
local ugmatch = mw.ustring.gmatch

-- Clone parent's args while also assigning nil to empty strings.
local function clone_args(frame)
	local args = {}
	for pname, param in pairs(frame:getParent().args) do
		args[pname] = param ~= "" and param or nil
	end
	return args
end

local function parse_page(pageval)
	local st, en = rfind(pageval, "^([0-9]+)([%-–—])([0-9]+)$")
	if st and en then
		return st, en
	else
		st = rfind(pageval, "^([0-9+])")
		if st then
			return st, nil
		else
			error("Unrecognized page value '" .. pageval .. "'")
		end
	end
end

local function compute_url_page_left_right(left)
	local right
	left = left + 0
	if (left % 2) == 0 then
		right = left + 1
	else
		right = left
		left = left - 1
	end
	left = left .. ""
	right = right .. ""
	if #left == 1 then
		left = "00" .. left
	elseif #left == 2 then
		left = "0" .. left
	end
	if #right == 1 then
		right = "00" .. right
	elseif #right == 2 then
		right = "0" .. right
	end
	return left, right
end

local function compute_url_vol(vol)
	vol = vol + 0
	vol = vol + ""
	if #vol == 1 then
		return "0" .. vol
	else
		return vol
	end
end

function export.essja_url(frame)
	local args = clone_args(frame)
	local page_left, page_right
	if args["page-left"] and args["page-right"] then
		page_left = args["page-left"]
		page_right = args["page-right"]
	else
		local st, en = parse_page(args["pages"] or args["page"])
		page_left, page_right = compute_url_page_left_right(st)
	end
	local vol = compute_url_vol(args["volume"] or args["vol"])
	return "http://essja.narod.ru/pg/" .. vol .. "/f" .. page_left .. "-" .. page_right .. ".htm"
end

return export