Module:Nihongo

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 22:05, 1 July 2024 by Vish (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
    
    local function escapeQuotes(str)
	    if not str then return str end
	    str = str:gsub('"', "&quot;")
	    return str
	end

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

    -- Initialize the result string
    local result = '<span class="nihongo popup-wrapper">'
    
    -- 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 nihongo-japanese" data-content="%s">%s</span>', escapeQuotes(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 not args.noParenthesis then
                	result = result .. '<span class="nihongo-bracket"> (</span>'
                end
                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
                if not args.noParenthesis then
                	result = result .. '<span class="nihongo-bracket">)</span>'
                end
                result = result .. '</span>'
            end
        else
        	result = result .. '<span class="noexcerpt"> '
        	if not args.noParenthesis then
            	result = result .. '<span class="nihongo-bracket"> (</span>'
        	end
        	result = result .. kanji
            if romaji and romaji ~= '' then
                result = result .. ', ' .. getSpan(romaji, type3Style)
            end
            if extra and extra ~= '' then
                result = result .. ', ' .. extra
            end
            if not args.noParenthesis then
            	result = result .. '<span class="nihongo-bracket">)</span>'
            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 class="noexcerpt">)', '%1<br />%2')
    end

    return result
end

return p