לדלג לתוכן

יחידה:פרמטר שם בשפת המקור

מתוך חב"דפדיה, אנציקלופדיה חב"דית חופשית

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:פרמטר שם בשפת המקור/תיעוד

local p = {}
local PropertyLink = require('Module:PropertyLink')
local Page = mw.title.getCurrentTitle()
local MusicalWorks = require('Module:עיצוב יצירות מוזיקליות')
local Brackets = require('Module:סוגריים')
local SpecialCharDecoder = require('Module:קידוד תווים מיוחדים')

local function getDefaultTitle()
    return SpecialCharDecoder.decode_html_entities_internal(Brackets.remove_parents_internal(Page.text))
end

local function extractWikidataValues(value)
    local results = {}
    
    -- Iterate through all matches of <span> elements
    for lang, text in value:gmatch('<span lang="(.-)" [^>]*>(.-)</span>') do
        table.insert(results, {
            text = text,
            lang = lang
        })
    end

    -- If there are no matches, just return the original value as "unknown"
    if #results == 0 then
        table.insert(results, {
            text = value,
            lang = "unknown"
        })
    end
    
    return results
end


local function getBestNonHebrewTitle(propertyId)
    local options = {
        ['allowMulti'] = true,
    }
    
    local values = PropertyLink.getPropertyByOptions(propertyId, nil, options)
    if values then
        for _, value in ipairs(extractWikidataValues(values)) do
            if value.lang ~= 'he' and value.lang ~= 'hbo' then
                return value
            end
        end
    end
    
    return nil
end

function p.main(frame)
    local args = frame:getParent().args
    local param1 = args[1] or ''
    local param2 = args[2] or ''
    local param3 = args[3] or ''
    local isSingle = args['סינגל'] or 'לא'
    local isAlbum = 'לא'
    local isBold = 'לא'
	
	if param1 == "-" then return end
	
    -- Get the default title if param1 is not specified
    local title = (param1 and param1 ~= '') and param1 or getDefaultTitle()
    
    -- Format the title using MusicalWorks (עיצוב יצירות מוזיקליות)
    local formattedTitle = MusicalWorks.main({
        getParent = function()
            return {
                args = {
                    ['שם'] = title,
                    ['סינגל'] = isSingle,
                    ['אלבום'] = isAlbum,
                    ['הדגשה'] = isBold
                }
            }
        end
    })

    -- Add param3 if it exists
    if param3 and param3 ~= '' then
        formattedTitle = formattedTitle .. ' ' .. param3
    end
    
    -- Handle param2 logic with a newline
    if param2 == '' then
        -- If param2 is an empty string, fetch from Wikidata
        local altTitle = getBestNonHebrewTitle('P1705') or getBestNonHebrewTitle('P1476') or getBestNonHebrewTitle('P1559')

        if altTitle and altTitle.text and altTitle.text ~= title then
            formattedTitle = formattedTitle .. '<br />' .. MusicalWorks.main({
		        getParent = function()
		            return {
		                args = {
		                    ['שם'] = altTitle.text,
		                    ['סינגל'] = isSingle,
		                    ['אלבום'] = isAlbum,
		                    ['הדגשה'] = isBold
		                }
		            }
		        end
            })
	    end
    elseif param2 ~= '-' and param2 ~= title then
        -- If param2 is not empty and not '-', display it directly
        formattedTitle = formattedTitle .. '<br />' .. MusicalWorks.main({
	        getParent = function()
	            return {
	                args = {
	                    ['שם'] = param2,
	                    ['סינגל'] = isSingle,
	                    ['אלבום'] = isAlbum,
	                    ['הדגשה'] = isBold
	                }
	            }
	        end
	    })
    end

    return formattedTitle
end

return p