Yu-Gi-Oh! Wiki
Yu-Gi-Oh! Wiki
(concatenation has higher precedence than and/or, so this wouldn't've worked regardless)
Tag: sourceedit
No edit summary
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
+
local Ruby = {}
  +
local getArgs = require('Dev:Arguments').getArgs
  +
local HF = require('Module:HF')
   
function p.split( frame )
+
function Ruby.ruby (frame)
local args = frame:getParent().args
+
local args = getArgs(frame, {
  +
trim = true,
  +
removeBlanks = false
  +
})
  +
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']
  +
  +
rubytag:addClass('rubytext'):addClass('ruby-'..lang):attr('lang', lang)
  +
  +
for i,v in ipairs(args) do
  +
--if (i % 2 ~= 0) and (type(args[i + 1]) ~= string) then
  +
-- rubytag:wikitext(v)
  +
--else
  +
if (i % 2 ~= 0) then
  +
rubytag:tag('rb'):wikitext(v):done()
  +
elseif (i % 2 == 0) and
  +
((type(v) == nil) or
  +
mw.ustring.len( mw.text.trim( v ) ) == 0)
  +
then
  +
  +
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
 
if not args[1] then
 
return
 
return
 
end
 
end
  +
 
args[1] = mw.text.trim( args[1] )
 
args[1] = mw.text.trim( args[1] )
  +
 
if not mw.ustring.find( args[1], '<ruby' ) then
 
if not mw.ustring.find( args[1], '<ruby' ) then
 
return args[1]
 
return args[1]
 
end
 
end
  +
args[2] = args[2] or ''
 
args[2] = args[2] == 'base' and '' or args[2]
+
args[2] = args[2] and ( mw.text.trim( args[2] ) == 'base' and '' or mw.text.trim( args[2] ) ) or ''
  +
local parts, match
+
local first, parts, match
  +
first = mw.ustring.match( args[1], '^(.-)<ruby' ) or ''
 
parts = mw.text.split( args[1], '<ruby' )
 
parts = mw.text.split( args[1], '<ruby' )
match = mw.text.trim( args[2] ) ~= '' and '<rb>([^<]*)</rb>' or '<rt>([^<]*)</rt>'
+
match = args[2] == '' and '<rb[^>]*>(.-)</rb>' or '<rt[^>]*>(.-)</rt>'
   
for i=1, #parts do
+
for i = 1, #parts do
parts[i] = ( mw.ustring.match( parts[i], match ) or '' ) .. ( mw.ustring.match( parts[i], '</ruby>(.*)$' ) or '' )
+
parts[i] = ( mw.ustring.match( parts[i], match ) or '' )
  +
.. ( mw.ustring.match( parts[i], '</ruby>(.*)$' ) or '' )
 
end
 
end
  +
return table.concat( parts, '' )
+
return first, table.concat( parts, '' )
 
end
 
end
   
return p
+
return Ruby

Latest revision as of 21:55, 26 April 2019

Test Template Info-Icon - Version (2) Module documentation

The documentation for this module is missing. Click here to create it.

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
    })
    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']
    
    rubytag:addClass('rubytext'):addClass('ruby-'..lang):attr('lang', lang)
    
    for i,v in ipairs(args) do
        --if (i % 2 ~= 0) and (type(args[i + 1]) ~= string) then
        --    rubytag:wikitext(v)
        --else
        if (i % 2 ~= 0) then
            rubytag:tag('rb'):wikitext(v):done()
        elseif (i % 2 == 0) and 
            ((type(v) == nil) or
             mw.ustring.len( mw.text.trim( v ) ) == 0)
            then
            
        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