Модуль:ColorAlphabet/Asadova

Материал из Поле цифровой дидактики

Для документации этого модуля может быть создана страница Модуль:ColorAlphabet/Asadova/doc

---- This module colors consonants blue and vowels red.
---- {{#invoke:ColorAlphabet|letter|whatever}} for colored letters
---- {{#invoke:ColorAlphabet|blank|whatever}} for colored non-breaking spaces

local getArgs = require('Module:Arguments').getArgs
local p = {}

-- Helper function to check if a character is a vowel (English vowels + case-insensitive)
local function isVowel(char)
    if not char then return false end
    local lower = mw.ustring.lower(char)
    return mw.ustring.match(lower, '[aeiouаеёиоуыэюя]') ~= nil  -- includes Russian vowels
end

-- Helper function to check if a character is a consonant (letters that are not vowels)
local function isConsonant(char)
    if not char then return false end
    -- Check if it's a letter in Russian or English alphabet
    local lower = mw.ustring.lower(char)
    return mw.ustring.match(lower, '[a-zа-яё]') ~= nil and not isVowel(char)
end

function p._main(text, nowiki, blank)
    local output = ""
    local position = 0
    
    -- Iterate through each character in the string
    local prowl = mw.ustring.gmatch(text, "(.)")
    repeat
        local letter = prowl()
        if not letter then break end
        position = position + 1
        
        local replacement = letter
        
        -- Apply color based on letter type
        if isVowel(letter) then
            -- Vowels: red
            replacement = '<span style="color:red;">' .. letter .. '</span>'
        elseif isConsonant(letter) then
            -- Consonants: blue
            replacement = '<span style="color:blue;">' .. letter .. '</span>'
        end
        
        -- If blank mode is enabled, replace letter with &nbsp; but keep the color
        if blank then
            if isVowel(letter) then
                replacement = '<span style="color:red;">&nbsp;</span>'
            elseif isConsonant(letter) then
                replacement = '<span style="color:blue;">&nbsp;</span>'
            else
                replacement = '&nbsp;'
            end
        end
        
        output = output .. replacement
    until false
    
    output = '<span style="display:inline-block;">' .. output .. '</span>'
    if nowiki then output = '<nowiki>' .. output .. '</nowiki>' end
    return output
end

-- Common code with all relevant args
function p.main(args, blank)
    local text = args[1] or ""
    local nowiki = args.nowiki or nil
    return p._main(text, nowiki, blank)
end

-- Entry for colorizing letters
function p.letter(frame)
    local args = getArgs(frame)
    return frame:preprocess(p.main(args, nil))
end

-- Entry for replacing letters with colored nonbreaking spaces
function p.blank(frame)
    local args = getArgs(frame)
    return frame:preprocess(p.main(args, true))
end

return p