Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Unihan pronunciation

Permanently protected module
From Wikipedia, the free encyclopedia

local p = {}
local unicode_data = require("Module:Unicode data")

local passthrough = {
    [","] = true,
    ["-"] = true,
    [":"] = true
}

-- Helper: format a syllable, optionally putting tone as superscript
local function format_syllable(syllable, tone_mode)
    if tone_mode == "sup" and syllable then
        -- Move the last character (tone number) into <sup>
        local base = syllable:sub(1, -2)
        local tone = syllable:sub(-1)
        return base .. "<sup>" .. tone .. "</sup>"
    else
        return syllable
    end
end

--- @param frame table
function p.kCantonese(frame)
    local text = frame.args[1]
    local tone_mode = frame:getParent().args["tone"]
    local result = {}

    for char in mw.text.gsplit(text, "", true) do
        if char == " " then
            -- ignore whitespace
        elseif passthrough[char] then
            table.insert(result, char)
        else
            local code = mw.ustring.codepoint(char)
            local syllable = unicode_data.lookup_kCantonese(code)
            table.insert(result, format_syllable(syllable, tone_mode))
        end
    end

    return table.concat(result, " ")
end

return p