ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Wolfgang Schuster <wolfgang.schuster.lists@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>,
	"Thomas A. Schmitz" <thomas.schmitz@uni-bonn.de>
Subject: Re: lua lpeg + utf8
Date: Sun, 6 Jan 2019 19:20:26 +0100	[thread overview]
Message-ID: <15d5b962-f731-5c75-5152-440ee328b718@gmail.com> (raw)
In-Reply-To: <cc9c2957-dbb3-6c39-8741-0e675813b385@uni-bonn.de>

Thomas A. Schmitz schrieb am 06.01.19 um 18:53:
> Hi everybody,
> 
> best wishes for (the still new) 2019! My question is not strictly a 
> ConTeXt problem, but about the way luatex (and pure Lua) can handle utf8 
> in lpeg. Here is my Lua example:
> 
> mystring = "abcdeφὴὰabcde"
> 
> local replace_table = {
>    a = "y",
>    c = "z",
>    ὴ = "ή",
>    ὰ = "ά",
> }
> 
> function replace(s)
>      local patt = (lpeg.Cs(1)) / replace_table
>      local parser = lpeg.Cs((patt + 1)^0)
>      t = parser:match(s)
>      return t
> end
> 
> newstring = replace(mystring)
> 
> print(newstring)
> 
> This will successfully replace "a" and "c," but not "ὴ" or "ὰ" because 
> lpeg.Cs(1) sees only the first byte of these multibyte characters. Pure 
> Lua complains with an error message; luatex runs, but does not do the 
> replacement. What would be a good way to work around this limitation?

Below is a modified version of the example on page 103 of the ConTeXt 
Lua Documents (cld-mkiv.pdf) manual.

\starttext

\startluacode

print("abcdeφὴὰabcde")

local remap = utf.remapper { a = "y", c = "z", ὴ = "ή", ὰ = "ά" }

print(remap("abcdeφὴὰabcde"))

\stopluacode

\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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

  reply	other threads:[~2019-01-06 18:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-06 17:53 Thomas A. Schmitz
2019-01-06 18:20 ` Wolfgang Schuster [this message]
2019-01-06 19:14   ` Thomas A. Schmitz
2019-01-06 22:08     ` Hans Hagen
2019-01-06 22:40       ` Thomas A. Schmitz

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=15d5b962-f731-5c75-5152-440ee328b718@gmail.com \
    --to=wolfgang.schuster.lists@gmail.com \
    --cc=ntg-context@ntg.nl \
    --cc=thomas.schmitz@uni-bonn.de \
    /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).