Module:Nihongo

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 16:31, 31 May 2024 by HudgynS (talk | contribs)
Jump to navigation Jump to search

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

local p = {}

-- Main function that generates the Nihongo text
function p.nihongo(frame)
    -- Retrieve the arguments passed to the template
    local args = frame:getParent().args

    -- Helper function to create a span element with the given content and style
    local function getSpan(content, style)
        if content == "" then return "" end
        return string.format('<span %s>%s</span>', style, content)
    end

    -- Helper function to determine the style based on the type argument
    local function switchType(type, defaultStyle, jaStyle, romajiStyle)
        if type == "J" then
            return jaStyle
        elseif type == "R" then
            return romajiStyle
        else
            return defaultStyle
        end
    end

    -- Determine the styles for each type
    local type1Style = switchType(args.type1, '', 'lang="ja" style="font-family:Meiryo; display:inline;"', 'style="font-style:italic; font-weight:normal;"')
    local type2Style = switchType(args.type2, 'style="font-family:Meiryo;" lang="ja" class="noexcerpt"', 'style="font-family:Meiryo;" lang="ja" class="noexcerpt"', 'style="font-style:italic; font-weight:normal;" class="noexcerpt"')
    local type3Style = switchType(args.type3, 'style="font-style:italic; font-weight:normal;" class="noexcerpt"', 'style="font-family:Meiryo;" lang="ja" class="noexcerpt"', 'class="noexcerpt"')

    -- Initialize the result string
    local result = '<span class="nihongo">'
    
    -- Initialize the variables
    local english, kanji, romaji, extra, extra2

    -- Assign values based on type1
    if args.type1 ~= 'J' then
        english = args[1]
        kanji = args[2]
        romaji = args[3]
        extra = args[4]
        extra2 = args[5]
    else
        kanji = args[1]
        romaji = args[2]
        extra = args[3]
        extra2 = args[4]
    end

    -- When no type or type not 'J', English comes first
    if args.type1 ~= 'J' and english and english ~= '' then
        result = result .. getSpan(english, type1Style)
    end

    -- Adjust Kanji and hover behavior
    if kanji and kanji ~= '' then
        if args.hover and args.hover ~= '' and romaji and romaji ~= '' then
            kanji = string.format('<span class="popup" data-content="%s"">%s</span>', romaji, kanji)
            romaji = nil  -- Prevent duplication in hover mode
        else
            kanji = getSpan(kanji, type2Style)
        end

        if args.type1 == 'J' then
            result = result .. kanji -- Display Kanji outside parentheses for type1=J
            if (romaji and romaji ~= '') or (extra and extra ~= '') then
                result = result .. '<span class="noexcerpt"> ('
                if romaji and romaji ~= '' then
                    result = result .. getSpan(romaji, type3Style)
                end
                if romaji and romaji ~= '' and extra and extra ~= '' then
                    result = result .. ', '
                end
                if extra and extra ~= '' then
                    result = result .. extra
                end
                result = result .. ')</span>'
            end
        else
            result = result .. '<span class="noexcerpt"> (' .. kanji
            if romaji and romaji ~= '' then
                result = result .. ', ' .. getSpan(romaji, type3Style)
            end
            if extra and extra ~= '' then
                result = result .. ', ' .. extra
            end
            result = result .. ')</span>'
        end
    end

    -- Append extra text outside parentheses if provided
    if extra2 and extra2 ~= '' then
        result = result .. ' ' .. extra2
    end

    -- Close the main span
    result = result .. '</span>'

    -- Handle line break
    if args.lineBreak and args.lineBreak ~= '' then
        result = result:gsub('</span> ', '</span><br />')
    end

	-- Handle noParenthesis
	if args.noParenthesis and args.noParenthesis ~= '' then
	    result = result:gsub('%(%s*', '')
	    result = result:gsub('%s*%)', '')
	end

    return result
end

return p