Module:Nihongo
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('"', """)
return str
end
-- Determine the styles for each type
local type1Style = switchType(args.type1 or 'E', '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 or 'J', '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 type3Style = switchType(args.type3 or 'R', 'class="nihongo-english"', 'lang="ja" style="font-family:Meiryo; display:inline;" class="nihongo-japanese"', 'style="font-style:italic; font-weight:normal;" class="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
kanji = args[1]
romaji = args[2]
extra = args[3]
extra2 = args[4]
elseif args.type1 == 'R' then
romaji = args[1]
kanji = args[2]
extra = args[3]
extra2 = args[4]
else
english = args[1]
kanji = args[2]
romaji = args[3]
extra = args[4]
extra2 = args[5]
end
-- When no type or type not 'J', English comes first
if args.type1 ~= 'J' and args.type1 ~= 'R' 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, type1Style)
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 or args.noParenthesis == '' then
result = result .. '<span class="nihongo-bracket"> (</span>'
else
result = result .. ' '
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 or args.noParenthesis == ''then
result = result .. '<span class="nihongo-bracket">)</span>'
end
result = result .. '</span>'
end
elseif args.type1 == 'R' then
romaji = getSpan(romaji, type1Style)
result = result .. romaji
if (kanji and kanji ~= '') or (extra and extra ~= '') then
result = result .. '<span class="noexcerpt">'
if not args.noParenthesis or args.noParenthesis == ''then
result = result .. '<span class="nihongo-bracket"> (</span>'
else
result = result .. ' '
end
result = result .. getSpan(kanji, type2Style)
if extra and extra ~= '' then
result = result .. ', ' .. extra
end
if not args.noParenthesis or 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 or args.noParenthesis == ''then
result = result .. '<span class="nihongo-bracket"> (</span>'
else
result = result .. ' '
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 or args.noParenthesis == ''then
result = result .. '<span class="nihongo-bracket">)</span>'
end
result = result .. '</span>'
end
elseif romaji and romaji ~= '' then
romaji = getSpan(romaji, type3Style)
result = result .. romaji
if (extra and extra ~= '') then
result = result .. '<span class="noexcerpt"> '
if not args.noParenthesis or args.noParenthesis == ''then
result = result .. '<span class="nihongo-bracket"> (</span>'
else
result = result .. ' '
end
if extra and extra ~= '' then
result = result .. extra
end
if not args.noParenthesis or 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