ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Wolfgang Schuster <schuster.wolfgang@googlemail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: pinyin in context
Date: Sun, 15 Mar 2009 18:20:34 +0100	[thread overview]
Message-ID: <0F0A9C5F-CC0E-4B0F-AC00-B9189287F00D@gmail.com> (raw)
In-Reply-To: <20090315031958.GA8541@localhost.localdomain>


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
___________________________________________________________________________________

  parent reply	other threads:[~2009-03-15 17:20 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-11  4:44 Bill Long
2009-03-11  5:08 ` Yue Wang
2009-03-12  4:48   ` Bill Long
2009-03-11 15:26 ` Arthur Reutenauer
2009-03-12  6:27   ` Bill Long
2009-03-15  3:23 ` Bill Long
2009-03-15 16:17   ` Hans Hagen
2009-03-15 17:20   ` Wolfgang Schuster [this message]
2009-03-16 16:17     ` Bill Long
2009-03-16 18:29       ` Arthur Reutenauer
2009-03-16 20:23       ` Wolfgang Schuster
2009-03-16 21:17         ` Arthur Reutenauer
2009-03-17 21:22         ` Bill Long
2009-03-17 21:47           ` Wolfgang Schuster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0F0A9C5F-CC0E-4B0F-AC00-B9189287F00D@gmail.com \
    --to=schuster.wolfgang@googlemail.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).