FANDOM


local Ruby = {}
local getArgs = require('Dev:Arguments').getArgs 
local HF = require('Module:HF') 
 
function Ruby.ruby (frame)
    local args = getArgs(frame, {
    	trim = true,
    	removeBlanks = false --,
    	-- wrappers = {
    	-- 	'Template:Ruby'
    	-- }
    })
    return Ruby._ruby(args)
end
 
function Ruby._ruby (args)
    local rubytag = mw.html.create('ruby')
    local lang = args['lang'] or 'ja'
    local link = args['link']
    local items = {}
    rubytag:addClass('rubytext'):addClass('ruby-'..lang):attr('lang', lang)
    if #args == 1 then return args[1] end
    for i,v in args do
        if (i % 2 ~= 0) and (type(args[i + 1]) ~= string) then
            rubytag:wikitext(v)
        elseif (i % 2 ~= 0) then
            rubytag:tag('rb'):wikitext(v):done()
        else
            rubytag:tag('rp'):wikitext(' ('):done()
            rubytag:tag('rt'):wikitext(v):done()
            rubytag:tag('rp'):wikitext(') '):done()
        end
    end
    rubytag:allDone()
    return link and HF.Link(link, tostring(rubytag)) or tostring(rubytag)
    --[==[
    {{ #if: {{{2<includeonly>|</includeonly>}}}
  | <ruby class="rubytext ruby-{{{lang|ja}}}" lang="{{{lang|ja}}}"><rb>{{{1}}}</rb><rp></rp><rt>{{{2}}}</rt><rp></rp></ruby>
  | {{{1}}}[[Category:Pages with an unnecessary ((Ruby)) transclusion]]
}}{{ #if: {{{1<includeonly>|</includeonly>}}} || [[Category:Pages with an unnecessary ((Ruby)) transclusion]]
}}{{ #ifeq: {{{3}}} | ¬ || [[Category:((Ruby)) transclusions with too many parameters]] }}
    --]==]
end
 
function Ruby.split( frame )
    --
    -- implements {{Ruby/split}}
    --
 
    if frame == mw.getCurrentFrame() then
        args = frame:getParent().args
    else
        args = frame
    end
    if not args[1] then
        return
    end
 
    args[1] = mw.text.trim( args[1] )
 
    if not mw.ustring.find( args[1], '<ruby' ) then
        return args[1]
    end
 
    args[2] = args[2] and ( mw.text.trim( args[2] ) == 'base' and '' or mw.text.trim( args[2] ) ) or ''
 
    local first, parts, match
    first = mw.ustring.match( args[1], '^(.-)<ruby' ) or ''
    parts = mw.text.split( args[1], '<ruby' )
    match = args[2] == '' and '<rb[^>]*>(.-)</rb>' or '<rt[^>]*>(.-)</rt>'
 
    for i = 1, #parts do
        parts[i] = ( mw.ustring.match( parts[i], match ) or '' )
                .. ( mw.ustring.match( parts[i], '</ruby>(.*)$' ) or '' )
    end
 
    return first, table.concat( parts, '' )
end
 
return Ruby
*Disclosure: Some of the links above are affiliate links, meaning, at no additional cost to you, Fandom will earn a commission if you click through and make a purchase. Community content is available under CC-BY-SA unless otherwise noted.

Fandom may earn an affiliate commission on sales made from links on this page.

Stream the best stories.

Fandom may earn an affiliate commission on sales made from links on this page.

Get Disney+