Module:Nihongo

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 05:17, 31 May 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

    -- 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"', 'style="font-family:Meiryo;" lang="ja"', 'style="font-style:italic; font-weight:normal;"')
    local type3Style = switchType(args.type3, 'style="font-style:italic; font-weight:normal;"', 'style="font-family:Meiryo;" lang="ja"', '')

    -- Initialize the result string
    local result = ''

    -- Identify the Kanji and Romaji based on type1 setting
    local kanji, romaji
    if args.type1 == 'J' then
        kanji = args[1]
        romaji = args[2]
    else
        kanji = args[2]
        romaji = args[3]
    end

    -- Add the English text if it exists and type1 is not J
    if args[1] and args[1] ~= '' and args.type1 ~= 'J' then
        result = result .. getSpan(args[1], type1Style)
    end

    -- Add a line break if specified
    if args.lineBreak and args.lineBreak ~= '' then
        result = result .. '<br />'
    end

    -- Add the Kanji if it exists
    if kanji and kanji ~= '' then
        if result ~= '' then
            result = result .. ' '
        end
        if not args.noParenthesis or args.noParenthesis == '' then
            result = result .. '('
        end

        -- If hover is true and both Kanji and Romaji are provided, create tooltip
        if args.hover and args.hover ~= '' and romaji and romaji ~= '' then
            local tooltipSpan = string.format('<span class="popup" data-content="%s" style="cursor: help;">%s</span>', romaji, kanji)
            result = result .. tooltipSpan
        else
            result = result .. getSpan(kanji, type2Style)
            if romaji and romaji ~= '' then
                result = result .. ', ' .. getSpan(romaji, type3Style)
            end
        end

        if not args.noParenthesis or args.noParenthesis == '' then
            result = result .. ')'
        end
    end

    -- Add the fourth argument (extra text) if it exists
    if args[4] and args[4] ~= '' then
        result = result .. ', ' .. args[4]
    end

    -- Add the fifth argument (extra text) if it exists
    if args[5] and args[5] ~= '' then
        result = result .. ' ' .. args[5]
    end

    -- Return the final result string
    return result
end

-- Return the module table
return p