پودمان:PoetryNew

از ویکی تراث

توضیحات این پودمان می‌تواند در پودمان:PoetryNew/توضیحات قرار گیرد.

local aliases = {
    width = {'عرض'},
    title = {'عنوان'},
    shaer = {'شاعر'},
    bahar = {'قالب'},
    nass = {1, 'متن'},
	shaterbyshater = {'پلکانی'},
	fasil = {'فاصله','separator','جداکننده'},
	free = {'نو', 'شعر نو'},
}
local string = mw.ustring

local function argument_wrapper(arg)
	local nilargs = {}
	return setmetatable({},
	{
		__index = function ( tbl, k )
			local v = rawget(tbl,k)
			if v then
				return v
			elseif nilargs[k] then
				return nil
			end
			local list = (type(k)=="number") and k or aliases[k];
			for _,arglist in ipairs(arg) do
				if type( list ) == 'table' then
					for _, alias_key in ipairs( list ) do
						if arglist[alias_key] then
							v = arglist[alias_key]
							break;
						end
					end
				elseif list ~= nil then
					v = arglist[list]
				end

				if v then
					break;
				end
			end
			if v == nil then
				nilargs[k] = true
			else
				rawset( tbl, k, v )
			end
			return v
		end,
	});
end

local function validString(s)
	if(type(s) == 'string')	then
		s = mw.text.trim(s) 
		if( s ~= '' ) then return s end
	end
	return nil
end

local function poetry(frame)
	local A = argument_wrapper({frame:getParent().args, frame.args,mw.loadData("Module:Art pref/data")["اشعار"] or {}})
	
	local mashtoor = false
	local header = ''
	local free = A.free and (A.free ~= "0")
	local fasil = A.fasil and string.gsub(A.fasil, '[%%%^%$%(%)%[%]%.%*%+%-%?]', '%%%1') or "\\\\"
	local fasil_s,fasil_e = string.find(mw.text.trim(A.nass),fasil)

	local sdrdiv = '<div class="poetry-sdr">'
	if not fasil_s and not A[2] then
		mashtoor=true
		sdrdiv = '<div>'
	end
	local nass= ""
	local ret = '<div class="poetry-wrapper' .. (mashtoor and ' mashtoor' or '') .. (free and ' free' or '') ..
	(A.shaterbyshater and ' poetry-shaterbyshater' or '') .. 
	'" style="width:' .. (A.width or 'fit-content') .. ';">'
	
    if A.title then
        header = header .. "<center>'''" .. A.title .. "'''</center>"
    end
    if A.shaer then
        header = header .. "<center>" .. A.shaer .. "</center>"
    end
    if A.bahar then
        header = header .. "<center>''[از " .. A.bahar .. "]''</center>"
    end

	-- سبک سازگار با سبک های قالب قدیمی
	if A[2] then
    	local shater_num =1
    	local aa,bb
    	aa = validString(A[shater_num])
    	bb = validString(A[shater_num+1])
    	while aa  ~= nil or bb ~= nil do
    	    if aa~= nil and bb ~= nil  then
    			ret=ret .. '<div class="poetry-sdr">' ..aa .. "</div>"
    			ret=ret .. '<div class="poetry-ajz">' ..bb .. "</div>\n"
			else
    			ret=ret .. '<div class="poetry-single_bayt">' ..(aa or bb) .. "</div>\n"
			end
    		shater_num=shater_num+2
    		aa = validString(A[shater_num])
    		bb = validString(A[shater_num+1])
		end
	else
	-- سبک ابیات
		nass = mw.text.trim(A.nass)
		if fasil_s == 1 then
	        ret = ret .. '<div class="poetry-single_bayt">'
	        nass = string.sub(nass,fasil_e+1)
	    else
	        ret = ret .. sdrdiv
	    end
	
	    nass=string.gsub(nass,"\n"..fasil,'</div><div class="poetry-single_bayt">')
	    nass=string.gsub(nass,"\n([^|])",'</div>' .. sdrdiv ..'%1')
	    nass=string.gsub(nass, fasil,'</div><div class="poetry-ajz">')
	end
    return header .. ret .. nass .. "</div></div>"
end

return {poetry=poetry}