Am 15.03.2009 um 04:23 schrieb Bill Long:

Dear Hans,

Can you help check if you can implement a new feature of "pinyin"
input in context?


Is a lua based method a option for you?

Here is a primitive converter, not perfect and could fail but it gives
you a first impression, the missing 'ǎ' in the output is a font problem.

\startluacode

function commands.convertpinyin(text,number)
    local replaced
    local number = tonumber(number)
    if string.find(text,"a") or string.find(text,"e") then
        if number==1 then
            text = string.gsub(text,"a","ā")
            text = string.gsub(text,"e","ē")
        elseif number==2 then
            text = string.gsub(text,"a","á")
            text = string.gsub(text,"e","é")
        elseif number==3 then
            text = string.gsub(text,"a","ǎ")
            text = string.gsub(text,"e","ě")
        elseif number==4 then
            text = string.gsub(text,"a","à")
            text = string.gsub(text,"e","è")
        end
        replaced = false
    else
        replaced = true
    end
    while replaced do
        if number==1 then
            text = string.gsub(text,"i","ī")
            text = string.gsub(text,"o","ō")
if string.find(text,"o") then else text = string.gsub(text,"u","ū") end
            text = string.gsub(text,"v","ǖ")
        elseif number==2 then
            text = string.gsub(text,"i","í")
            text = string.gsub(text,"o","ó")
if string.find(text,"o") then else text = string.gsub(text,"u","ú") end
            text = string.gsub(text,"v","ǘ")
        elseif number==3 then
            text = string.gsub(text,"o","ǒ")
            text = string.gsub(text,"i","ǐ")
if string.find(text,"o") then else text = string.gsub(text,"u","ǔ") end
            text = string.gsub(text,"u","ǔ")
            text = string.gsub(text,"v","ǚ")
        elseif number==4 then
            text = string.gsub(text,"o","ò")
            text = string.gsub(text,"i","ì")
if string.find(text,"o") then else text = string.gsub(text,"u","ù") end
            text = string.gsub(text,"v","ǜ")
        end
        replaced = false
    end
    tex.sprint(tex.ctxcatcodes,text)
end

local letter = lpeg.R("az")
local number = lpeg.R("09")

local pinyin = lpeg.C(letter^1) * lpeg.C(number^0) / commands.convertpinyin

local parser = (pinyin)^0

function commands.pinyin(str)
    parser:match(str)
end

\stopluacode

\def\pinyin#1{\ctxlua{commands.pinyin([[#1]])}}

\starttext

\pinyin{ni2hao3}
\pinyin{huai4}

\stoptext

Wolfgang

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : https://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to