ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Checking for a Unicode prefix of a Unicode string
@ 2021-11-26  6:42 Joey McCollum via ntg-context
  2021-11-26  8:45 ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Joey McCollum via ntg-context @ 2021-11-26  6:42 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Joey McCollum


[-- Attachment #1.1: Type: text/plain, Size: 1780 bytes --]

I wasn't aware of a general-purpose "doifstartswith" macro in ConTeXt (the
\doifnextcharelse macro only works one character at a time, and the
\doifinstring macros may capture substrings that are not prefixes), and I'd
like to develop one for something I'm working on. I've been trying to do
this in Lua, as that seemed like the most natural approach. Normally,
something like this would work fine as a foundation:

```
  function isprefix(prefix, str)
    if string.sub(str, 1, string.len(prefix)) == prefix then
      return true
    end
    return false
  end
```

Unfortunately, if I want to check for prefixes that include two-byte
characters like § and ¶, then the positions and string lengths that I
specify no longer correspond to the actual byte offsets and lengths that
Lua uses. I'm aware of the utf8 plugin that was intended to address this
issue, but the following code also isn't working:

```
  function sbl.isprefix(prefix, str)
    -- lua is devious and measures string length in bytes, not chars,
    -- so we can't just use string.sub and string.len as we normally would.
    local i = utf8.offset(str, 1)
    local j = utf8.offset(str, utf8.len(prefix) + 1) - 1
    if string.sub(str, i, j) == prefix then
      return true
    end
    return false
  end
```

The only other detail that may be relevant is that I'm passing a macro as
the "str" input. But this should be expanded when the Lua code is
manipulating it, right?

I'm sure there's something obvious that I'm doing wrong, but I've been
trying to get this to work for at least a couple hours now, and I don't
know what else to try. If there is a simple fix to my Lua code or an
existing ConTeXt macro that will get the job done, I'd appreciate it!

Joey

[-- Attachment #1.2: Type: text/html, Size: 2093 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Checking for a Unicode prefix of a Unicode string
  2021-11-26  6:42 Checking for a Unicode prefix of a Unicode string Joey McCollum via ntg-context
@ 2021-11-26  8:45 ` Hans Hagen via ntg-context
  2021-11-26 16:57   ` Joey McCollum via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen via ntg-context @ 2021-11-26  8:45 UTC (permalink / raw)
  To: Joey McCollum via ntg-context; +Cc: Hans Hagen

On 11/26/2021 7:42 AM, Joey McCollum via ntg-context wrote:
> I wasn't aware of a general-purpose "doifstartswith" macro in ConTeXt 
> (the \doifnextcharelse macro only works one character at a time, and the 
> \doifinstring macros may capture substrings that are not prefixes), and 
> I'd like to develop one for something I'm working on. I've been trying 
> to do this in Lua, as that seemed like the most natural approach. 
> Normally, something like this would work fine as a foundation:
> 
> ```
>    function isprefix(prefix, str)
>      if string.sub(str, 1, string.len(prefix)) == prefix then
>        return true
>      end
>      return false
>    end
> ```

how about

if string.find(str,"^"..prefix) then

in:

\starttext

\def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find("#2","^".."#1"))}}

\DoIfPrefixElse{pre}{prefix}{YES}{NOP}
\DoIfPrefixElse{pre}{suffix}{YES}{NOP}

\stoptext

utf strings are just sequences of bytes so matching works

when you want to do more in lua you can decide for

\startluacode
interfaces.implement {
     name      = "DoIfPrefixElse",
     arguments = { "argument", "argument" },
     actions   = function(pre,str)
         commands.doifelse(string.find(str,"^"..pre))
     end
}
\stopluacode

\DoIfPrefixElse{pre}{prefix}{YES}{NOP}
\DoIfPrefixElse{pre}{suffix}{YES}{NOP}

but in any case: make sure that you don't clash with built in ...

if needed i can make a set of fast(er) ones but someone has to collect a 
list of 'handy helpers' first

Hans


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Checking for a Unicode prefix of a Unicode string
  2021-11-26  8:45 ` Hans Hagen via ntg-context
@ 2021-11-26 16:57   ` Joey McCollum via ntg-context
  2021-11-27 17:13     ` Joey McCollum via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Joey McCollum via ntg-context @ 2021-11-26 16:57 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Joey McCollum


[-- Attachment #1.1: Type: text/plain, Size: 4029 bytes --]

Thanks! Invoking string.find(str,"^"..pre) is certainly much easier. This
sort of thing would be a nice general-purpose helper function. Denis Maier
and I should have a "wish list" of desired helpers and features (mostly
related to bibliographies/publication support) ready for you (and Alan)
sometime soon.

But for now, it looks like I still need to work out errors that arise when
I pass a macro as the "str" input to be searched. I've defined a \loc macro
that accepts an assignment and outputs a formatted string, and later, I
want to check if this macro (after it has been fully expanded) starts with
a certain prefix. A minimal (non-)working example follows:

```

\starttexdefinition loc [#1]

\doifassignmentelse{#1} {

% if an assignment, then parse and format accordingly

\getparameters[loc][#1]

% Was a section number specified?

\doifdefined{locsec} {

§ \locsec\btxcomma

}

} {

% otherwise, just print the input as-is

#1

}

\stoptexdefinition


\def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find("#2","^".."#1"))}}


\def\currentbtxloctext{\loc[sec=31]}


\starttext

\DoIfPrefixElse{§}{\currentbtxloctext}{YES}{NOP}

\stoptext
```

Lua is throwing an "invalid escape sequence near '"\l'" error, which is
presumably because the macro \loc[sec=31] is not being expanded. How do I
fix this?

Thanks!

Joey

On Fri, Nov 26, 2021 at 3:46 AM Hans Hagen via ntg-context <
ntg-context@ntg.nl> wrote:

> On 11/26/2021 7:42 AM, Joey McCollum via ntg-context wrote:
> > I wasn't aware of a general-purpose "doifstartswith" macro in ConTeXt
> > (the \doifnextcharelse macro only works one character at a time, and the
> > \doifinstring macros may capture substrings that are not prefixes), and
> > I'd like to develop one for something I'm working on. I've been trying
> > to do this in Lua, as that seemed like the most natural approach.
> > Normally, something like this would work fine as a foundation:
> >
> > ```
> >    function isprefix(prefix, str)
> >      if string.sub(str, 1, string.len(prefix)) == prefix then
> >        return true
> >      end
> >      return false
> >    end
> > ```
>
> how about
>
> if string.find(str,"^"..prefix) then
>
> in:
>
> \starttext
>
>
> \def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find("#2","^".."#1"))}}
>
> \DoIfPrefixElse{pre}{prefix}{YES}{NOP}
> \DoIfPrefixElse{pre}{suffix}{YES}{NOP}
>
> \stoptext
>
> utf strings are just sequences of bytes so matching works
>
> when you want to do more in lua you can decide for
>
> \startluacode
> interfaces.implement {
>      name      = "DoIfPrefixElse",
>      arguments = { "argument", "argument" },
>      actions   = function(pre,str)
>          commands.doifelse(string.find(str,"^"..pre))
>      end
> }
> \stopluacode
>
> \DoIfPrefixElse{pre}{prefix}{YES}{NOP}
> \DoIfPrefixElse{pre}{suffix}{YES}{NOP}
>
> but in any case: make sure that you don't clash with built in ...
>
> if needed i can make a set of fast(er) ones but someone has to collect a
> list of 'handy helpers' first
>
> Hans
>
>
> -----------------------------------------------------------------
>                                            Hans Hagen | PRAGMA ADE
>                Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
>         tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
> -----------------------------------------------------------------
>
> ___________________________________________________________________________________
> 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
>
> ___________________________________________________________________________________
>

[-- Attachment #1.2: Type: text/html, Size: 6745 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Checking for a Unicode prefix of a Unicode string
  2021-11-26 16:57   ` Joey McCollum via ntg-context
@ 2021-11-27 17:13     ` Joey McCollum via ntg-context
  2021-11-27 18:44       ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Joey McCollum via ntg-context @ 2021-11-27 17:13 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Joey McCollum


[-- Attachment #1.1: Type: text/plain, Size: 6219 bytes --]

All right, I think I've solved the expansion problem I described before: to
ensure that the first input is expanded when it is passed to Lua, I have to
pass it as [==[#1]==], not "#1". But the updated MWE below still does not
seem to work, as the \DoIfPrefixElse macro is printing "NOP" instead of
"YES":

```

\starttexdefinition loc [#1]

\doifassignmentelse{#1} {

% if an assignment, then parse and format accordingly

\getparameters[loc][#1]

% Was a section number specified?

\doifdefined{locsec} {

§ \locsec

}

} {

% otherwise, just print the input as-is

#1

}

\stoptexdefinition


\def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find([==[#2]==],"^"..[==[#1]==]))}}


\def\currentbtxloctext{\loc[sec=31]}


\starttext

\currentbtxloctext\blank

\DoIfPrefixElse{§}{\currentbtxloctext}{YES}{NOP}

\stoptext
```

Indeed, if I add a simple \doifelse equality check, it looks like the value
I expect is not the same as what the macro produces, even though they look
identical:

```

\starttexdefinition loc [#1]
    \doifassignmentelse{#1} {
        % if an assignment, then parse and format accordingly
        \getparameters[loc][#1]
        % Was a section number specified?
        \doifdefined{locsec} {
            § \locsec
        }
    } {
        % otherwise, just print the input as-is
        #1
    }
\stoptexdefinition

\def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find([==[#2]==],"^"..[==[#1]==]))}}

\def\currentbtxloctext{\loc[sec=31]}

\starttext
    § 31\blank%the raw text we expect
    \currentbtxloctext\blank%the text as produced by the macro
    \doifelse{\currentbtxloctext}{§ 31}{YES}{NOP}\blank% should output YES,
but doesn't
    \DoIfPrefixElse{§}{\currentbtxloctext}{YES}{NOP}\blank% should output
YES, but doesn't
\stoptext
```

What am I missing here?

Joey

On Fri, Nov 26, 2021 at 11:57 AM Joey McCollum <jmccollum20140511@gmail.com>
wrote:

> Thanks! Invoking string.find(str,"^"..pre) is certainly much easier. This
> sort of thing would be a nice general-purpose helper function. Denis Maier
> and I should have a "wish list" of desired helpers and features (mostly
> related to bibliographies/publication support) ready for you (and Alan)
> sometime soon.
>
> But for now, it looks like I still need to work out errors that arise when
> I pass a macro as the "str" input to be searched. I've defined a \loc macro
> that accepts an assignment and outputs a formatted string, and later, I
> want to check if this macro (after it has been fully expanded) starts with
> a certain prefix. A minimal (non-)working example follows:
>
> ```
>
> \starttexdefinition loc [#1]
>
> \doifassignmentelse{#1} {
>
> % if an assignment, then parse and format accordingly
>
> \getparameters[loc][#1]
>
> % Was a section number specified?
>
> \doifdefined{locsec} {
>
> § \locsec\btxcomma
>
> }
>
> } {
>
> % otherwise, just print the input as-is
>
> #1
>
> }
>
> \stoptexdefinition
>
>
>
> \def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find("#2","^".."#1"))}}
>
>
> \def\currentbtxloctext{\loc[sec=31]}
>
>
> \starttext
>
> \DoIfPrefixElse{§}{\currentbtxloctext}{YES}{NOP}
>
> \stoptext
> ```
>
> Lua is throwing an "invalid escape sequence near '"\l'" error, which is
> presumably because the macro \loc[sec=31] is not being expanded. How do I
> fix this?
>
> Thanks!
>
> Joey
>
> On Fri, Nov 26, 2021 at 3:46 AM Hans Hagen via ntg-context <
> ntg-context@ntg.nl> wrote:
>
>> On 11/26/2021 7:42 AM, Joey McCollum via ntg-context wrote:
>> > I wasn't aware of a general-purpose "doifstartswith" macro in ConTeXt
>> > (the \doifnextcharelse macro only works one character at a time, and
>> the
>> > \doifinstring macros may capture substrings that are not prefixes), and
>> > I'd like to develop one for something I'm working on. I've been trying
>> > to do this in Lua, as that seemed like the most natural approach.
>> > Normally, something like this would work fine as a foundation:
>> >
>> > ```
>> >    function isprefix(prefix, str)
>> >      if string.sub(str, 1, string.len(prefix)) == prefix then
>> >        return true
>> >      end
>> >      return false
>> >    end
>> > ```
>>
>> how about
>>
>> if string.find(str,"^"..prefix) then
>>
>> in:
>>
>> \starttext
>>
>>
>> \def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find("#2","^".."#1"))}}
>>
>> \DoIfPrefixElse{pre}{prefix}{YES}{NOP}
>> \DoIfPrefixElse{pre}{suffix}{YES}{NOP}
>>
>> \stoptext
>>
>> utf strings are just sequences of bytes so matching works
>>
>> when you want to do more in lua you can decide for
>>
>> \startluacode
>> interfaces.implement {
>>      name      = "DoIfPrefixElse",
>>      arguments = { "argument", "argument" },
>>      actions   = function(pre,str)
>>          commands.doifelse(string.find(str,"^"..pre))
>>      end
>> }
>> \stopluacode
>>
>> \DoIfPrefixElse{pre}{prefix}{YES}{NOP}
>> \DoIfPrefixElse{pre}{suffix}{YES}{NOP}
>>
>> but in any case: make sure that you don't clash with built in ...
>>
>> if needed i can make a set of fast(er) ones but someone has to collect a
>> list of 'handy helpers' first
>>
>> Hans
>>
>>
>> -----------------------------------------------------------------
>>                                            Hans Hagen | PRAGMA ADE
>>                Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
>>         tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
>> -----------------------------------------------------------------
>>
>> ___________________________________________________________________________________
>> 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
>>
>> ___________________________________________________________________________________
>>
>

[-- Attachment #1.2: Type: text/html, Size: 10478 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Checking for a Unicode prefix of a Unicode string
  2021-11-27 17:13     ` Joey McCollum via ntg-context
@ 2021-11-27 18:44       ` Hans Hagen via ntg-context
  2021-11-28  1:39         ` Joey McCollum via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen via ntg-context @ 2021-11-27 18:44 UTC (permalink / raw)
  To: Joey McCollum, mailing list for ConTeXt users; +Cc: Hans Hagen

On 11/27/2021 6:13 PM, Joey McCollum wrote:
> All right, I think I've solved the expansion problem I described before: 
> to ensure that the first input is expanded when it is passed to Lua, I 
> have to pass it as [==[#1]==], not "#1". But the updated MWE below still 
> does not seem to work, as the \DoIfPrefixElse macro is printing "NOP" 
> instead of "YES":
> 
> ```
> 
> \starttexdefinition loc [#1]
> 
> \doifassignmentelse{#1} {
> 
> % if an assignment, then parse and format accordingly
> 
> \getparameters[loc][#1]
> 
> % Was a section number specified?
> 
> \doifdefined{locsec} {
> 
> § \locsec
> 
> }
> 
> } {
> 
> % otherwise, just print the input as-is
> 
> #1
> 
> }
> 
> \stoptexdefinition
> 
> 
> \def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find([==[#2]==],"^"..[==[#1]==]))}}
> 
> 
> \def\currentbtxloctext{\loc[sec=31]}
> 
> 
> \starttext
> 
> \currentbtxloctext\blank
> 
> \DoIfPrefixElse{§}{\currentbtxloctext}{YES}{NOP}
> 
> \stoptext
> 
> ```
> 
> Indeed, if I add a simple \doifelse equality check, it looks like the 
> value I expect is not the same as what the macro produces, even though 
> they look identical:
> 
> ```
> 
> \starttexdefinition loc [#1]
>      \doifassignmentelse{#1} {
>          % if an assignment, then parse and format accordingly
>          \getparameters[loc][#1]
>          % Was a section number specified?
>          \doifdefined{locsec} {
>              § \locsec
>          }
>      } {
>          % otherwise, just print the input as-is
>          #1
>      }
> \stoptexdefinition
> 
> \def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find([==[#2]==],"^"..[==[#1]==]))}}
> 
> \def\currentbtxloctext{\loc[sec=31]}
> 
> \starttext
>      § 31\blank%the raw text we expect
>      \currentbtxloctext\blank%the text as produced by the macro
>      \doifelse{\currentbtxloctext}{§ 31}{YES}{NOP}\blank% should output 
> YES, but doesn't
>      \DoIfPrefixElse{§}{\currentbtxloctext}{YES}{NOP}\blank% should 
> output YES, but doesn't
> \stoptext
> 
> ```
> 
> What am I missing here?
Expansion hell ... and i fear that you draw yourself into more and more 
trouble with this approach (which is why you don't find that kind of 
hackery in the core unless we're real desperate) so maybe try to explain 
what the real problem is that needs to be solved. Parsing tex is seldom 
a solution (at least not in context).

You can add:

     \edef\Whatever{\currentbtxloctext}\meaning\Whatever
     \doifelse{\currentbtxloctext}{§ 31}{YES}{NOP}\blank

and see what comes back. Now, as always in tex, there's of course a 
solution because after all it's a programming language too (and at some 
point these solutions start looking so complex that one enters guru state)

\starttexdefinition loc [#1]
     \beginlocalcontrol
     \doifassignmentelse{#1} {
         \getparameters[loc][#1]
         \doifdefinedelse{locsec} {
         \endlocalcontrol
             § \locsec
         } {
             \endlocalcontrol
         }
     } {
         \endlocalcontrol
         #1
     }
\stoptexdefinition

a curious mix between a fully expanded result, using protected macros 
and hiding what tex does but hard to explain

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Checking for a Unicode prefix of a Unicode string
  2021-11-27 18:44       ` Hans Hagen via ntg-context
@ 2021-11-28  1:39         ` Joey McCollum via ntg-context
  2021-11-28 10:39           ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Joey McCollum via ntg-context @ 2021-11-28  1:39 UTC (permalink / raw)
  To: Hans Hagen; +Cc: Joey McCollum, mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 7348 bytes --]

I was afraid that might be the problem. I've described some of the intended
purpose of this code near the end of the "Checking for a macro in a string
without expanding it" thread on the mailing list, but I will get into more
detail here. I'm working on a ConTeXt implementation of citation style
language (CSL) locators, particularly for the SBL citation rendering
(although the code could easily be adapted for use in general or with other
bibliographic renderings). This would allow for a consistent,
language-sensitive syntax that could be used within citations or in any
part of a text and could accommodate changes in bibliographic style without
requiring the user to reformat all their page number citations. In SBL
style, for instance, volume, part, and page numbers are not prefixed by
abbreviations like "vol.", "pt." and "p.", but in other styles, they might
be. The locator syntax

```
\loc[vol=33,pt=1,p=86]
```

would be defined in the SBL rendering file to output

```
33.1:86
```

while in some other style, it might be defined to output

```
vol. 33, pt. 1, p. 86
```

To accommodate the use of these macros within citations, I've added
"loctext" (locator text) and "altloctext" (alternate locator text)
arguments to the set of parameters used by the \cite macro, because in some
bibliographic categories, different locators can be specified for different
parts of a citation (e.g., one for a passage in an ancient text and another
for the book reproducing it), and therefore cannot just be specified in the
"righttext" parameter. So now we can do something like

```
\cite[lefttext={See},altloctext={1.3},loctext={8:223},righttext={for
further details}][clementinehomilies]
```

to get

See *The Clementine Homilies* 1.3 (*ANF *8:223) for further details.

or

```
\cite[lefttext={See},loctext={\loc[p=8]},righttext={; but there are also
contradictory statements, e.g. \loc[p=12].}][Doe:Title]
```

to get

See Doe, *Title*, 8; but there are also contradictory statements, e.g. 12.


The main problem is determining when to include a comma before printing the
"loctext" and when not to include one. In the first example above, we don't
want a comma between *ANF* and 8:223 (SBL style does not add a comma
between a multivolume set abbreviation and a volume number), but in the
second example, we do want one between *Title *and 8. Similarly, we want to
remove the comma if the first locator in the loctext is a section marker
(§) or paragraph marker (¶). This is why I want to check the beginning of
the \currentloctext macro for the presence of a volume number (a number
followed by a colon), a section mark, or a paragraph mark.

While storing the locator parameters as btx parameters would allow for
another solution to this problem, it would complicate defining the \loc
macro in a way that would be suitable outside of citations or for multiple
uses in the same citation (notice how a second \loc invocation occurs in
the righttext of the second example above). Parsing the output of \loc
seemed like the simplest solution, but as you've pointed out, it has its
own difficulties. I'm inclined to try one of the TeX-based solutions you
describe above to avoid complicating other things, but if my description of
the intended use gives you any better ideas, please suggest them!

Thanks again!

Joey

On Sat, Nov 27, 2021 at 1:44 PM Hans Hagen <j.hagen@xs4all.nl> wrote:

> On 11/27/2021 6:13 PM, Joey McCollum wrote:
> > All right, I think I've solved the expansion problem I described before:
> > to ensure that the first input is expanded when it is passed to Lua, I
> > have to pass it as [==[#1]==], not "#1". But the updated MWE below still
> > does not seem to work, as the \DoIfPrefixElse macro is printing "NOP"
> > instead of "YES":
> >
> > ```
> >
> > \starttexdefinition loc [#1]
> >
> > \doifassignmentelse{#1} {
> >
> > % if an assignment, then parse and format accordingly
> >
> > \getparameters[loc][#1]
> >
> > % Was a section number specified?
> >
> > \doifdefined{locsec} {
> >
> > § \locsec
> >
> > }
> >
> > } {
> >
> > % otherwise, just print the input as-is
> >
> > #1
> >
> > }
> >
> > \stoptexdefinition
> >
> >
> >
> \def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find([==[#2]==],"^"..[==[#1]==]))}}
> >
> >
> > \def\currentbtxloctext{\loc[sec=31]}
> >
> >
> > \starttext
> >
> > \currentbtxloctext\blank
> >
> > \DoIfPrefixElse{§}{\currentbtxloctext}{YES}{NOP}
> >
> > \stoptext
> >
> > ```
> >
> > Indeed, if I add a simple \doifelse equality check, it looks like the
> > value I expect is not the same as what the macro produces, even though
> > they look identical:
> >
> > ```
> >
> > \starttexdefinition loc [#1]
> >      \doifassignmentelse{#1} {
> >          % if an assignment, then parse and format accordingly
> >          \getparameters[loc][#1]
> >          % Was a section number specified?
> >          \doifdefined{locsec} {
> >              § \locsec
> >          }
> >      } {
> >          % otherwise, just print the input as-is
> >          #1
> >      }
> > \stoptexdefinition
> >
> >
> \def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find([==[#2]==],"^"..[==[#1]==]))}}
> >
> > \def\currentbtxloctext{\loc[sec=31]}
> >
> > \starttext
> >      § 31\blank%the raw text we expect
> >      \currentbtxloctext\blank%the text as produced by the macro
> >      \doifelse{\currentbtxloctext}{§ 31}{YES}{NOP}\blank% should output
> > YES, but doesn't
> >      \DoIfPrefixElse{§}{\currentbtxloctext}{YES}{NOP}\blank% should
> > output YES, but doesn't
> > \stoptext
> >
> > ```
> >
> > What am I missing here?
> Expansion hell ... and i fear that you draw yourself into more and more
> trouble with this approach (which is why you don't find that kind of
> hackery in the core unless we're real desperate) so maybe try to explain
> what the real problem is that needs to be solved. Parsing tex is seldom
> a solution (at least not in context).
>
> You can add:
>
>      \edef\Whatever{\currentbtxloctext}\meaning\Whatever
>      \doifelse{\currentbtxloctext}{§ 31}{YES}{NOP}\blank
>
> and see what comes back. Now, as always in tex, there's of course a
> solution because after all it's a programming language too (and at some
> point these solutions start looking so complex that one enters guru state)
>
> \starttexdefinition loc [#1]
>      \beginlocalcontrol
>      \doifassignmentelse{#1} {
>          \getparameters[loc][#1]
>          \doifdefinedelse{locsec} {
>          \endlocalcontrol
>              § \locsec
>          } {
>              \endlocalcontrol
>          }
>      } {
>          \endlocalcontrol
>          #1
>      }
> \stoptexdefinition
>
> a curious mix between a fully expanded result, using protected macros
> and hiding what tex does but hard to explain
>
> Hans
>
> -----------------------------------------------------------------
>                                            Hans Hagen | PRAGMA ADE
>                Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
>         tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
> -----------------------------------------------------------------
>

[-- Attachment #1.2: Type: text/html, Size: 9147 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Checking for a Unicode prefix of a Unicode string
  2021-11-28  1:39         ` Joey McCollum via ntg-context
@ 2021-11-28 10:39           ` Hans Hagen via ntg-context
  2021-11-29 17:11             ` Joey McCollum via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen via ntg-context @ 2021-11-28 10:39 UTC (permalink / raw)
  To: Joey McCollum; +Cc: Hans Hagen, mailing list for ConTeXt users

On 11/28/2021 2:39 AM, Joey McCollum wrote:
> I was afraid that might be the problem. I've described some of the 
> intended purpose of this code near the end of the "Checking for a macro  > ...

Maybe play with:

\starttext

\def\WhateverA{\removeunwantedspaces\removepunctuation\space ¶ 1.23}
\def\WhateverB{¶ 1.23}

foo bar, \WhateverA\ test
foo bar, \WhateverB\ test

\stoptext

so, wrap ¶ into a macro

\def\MyParSymbol{\removeunwantedspaces\removepunctuation\space ¶}

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Checking for a Unicode prefix of a Unicode string
  2021-11-28 10:39           ` Hans Hagen via ntg-context
@ 2021-11-29 17:11             ` Joey McCollum via ntg-context
  2021-11-29 20:13               ` Joey McCollum via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Joey McCollum via ntg-context @ 2021-11-29 17:11 UTC (permalink / raw)
  To: Hans Hagen; +Cc: Joey McCollum, mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2087 bytes --]

Okay. So using this example as a template, I'd like to do the following:

   - define a general-purpose \loc macro that can be invoked in the main
   text (i.e., outside of citations) in the usual way; this one would
   correspond to \WhateverB above and would not remove punctuation preceding
   the macro.
   - separately define a \loctextloc macro that accepts the same input as
   \loc, but conditionally removes preceding punctuation based on variables
   set outside of this macro during the citation process (e.g.,
   \btxsblshorthandbeforetextloc); this would correspond to \WhateverA above.
   - in the macro for typesetting the loctext btx parameter, redefine \loc
   to do what \loctextloc does, and then invoke \currentbtxloctext so that the
   \loc invocations it contains will be expanded like \loctextloc, as follows:

\begingroup
\def\loc\loctextloc
\currentbtxloctext

\btxcomma

\endgroup

Is this a ConTeXt-friendly way to do this? I'm attempted this now, but I
have some errors to debug, and I'd like to know if this is even a feasible
direction before I continue.

Joey

On Sun, Nov 28, 2021 at 5:39 AM Hans Hagen <j.hagen@xs4all.nl> wrote:

> On 11/28/2021 2:39 AM, Joey McCollum wrote:
> > I was afraid that might be the problem. I've described some of the
> > intended purpose of this code near the end of the "Checking for a macro
> > ...
>
> Maybe play with:
>
> \starttext
>
> \def\WhateverA{\removeunwantedspaces\removepunctuation\space ¶ 1.23}
> \def\WhateverB{¶ 1.23}
>
> foo bar, \WhateverA\ test
> foo bar, \WhateverB\ test
>
> \stoptext
>
> so, wrap ¶ into a macro
>
> \def\MyParSymbol{\removeunwantedspaces\removepunctuation\space ¶}
>
> Hans
>
> -----------------------------------------------------------------
>                                            Hans Hagen | PRAGMA ADE
>                Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
>         tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
> -----------------------------------------------------------------
>

[-- Attachment #1.2: Type: text/html, Size: 2921 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Checking for a Unicode prefix of a Unicode string
  2021-11-29 17:11             ` Joey McCollum via ntg-context
@ 2021-11-29 20:13               ` Joey McCollum via ntg-context
  0 siblings, 0 replies; 9+ messages in thread
From: Joey McCollum via ntg-context @ 2021-11-29 20:13 UTC (permalink / raw)
  To: Hans Hagen; +Cc: Joey McCollum, mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2486 bytes --]

After some debugging, the example code I offered above seems to work, with
minor adjustments:

\begingroup
\let\loc\loctextloc
\currentbtxloctext
\btxcomma

\endgroup

Thanks again for all the help!

Joey

On Mon, Nov 29, 2021 at 12:11 PM Joey McCollum <jmccollum20140511@gmail.com>
wrote:

> Okay. So using this example as a template, I'd like to do the following:
>
>    - define a general-purpose \loc macro that can be invoked in the main
>    text (i.e., outside of citations) in the usual way; this one would
>    correspond to \WhateverB above and would not remove punctuation preceding
>    the macro.
>    - separately define a \loctextloc macro that accepts the same input as
>    \loc, but conditionally removes preceding punctuation based on variables
>    set outside of this macro during the citation process (e.g.,
>    \btxsblshorthandbeforetextloc); this would correspond to \WhateverA above.
>    - in the macro for typesetting the loctext btx parameter, redefine
>    \loc to do what \loctextloc does, and then invoke \currentbtxloctext so
>    that the \loc invocations it contains will be expanded like \loctextloc, as
>    follows:
>
> \begingroup
> \def\loc\loctextloc
> \currentbtxloctext
>
> \btxcomma
>
> \endgroup
>
> Is this a ConTeXt-friendly way to do this? I'm attempted this now, but I
> have some errors to debug, and I'd like to know if this is even a feasible
> direction before I continue.
>
> Joey
>
> On Sun, Nov 28, 2021 at 5:39 AM Hans Hagen <j.hagen@xs4all.nl> wrote:
>
>> On 11/28/2021 2:39 AM, Joey McCollum wrote:
>> > I was afraid that might be the problem. I've described some of the
>> > intended purpose of this code near the end of the "Checking for a
>> macro  > ...
>>
>> Maybe play with:
>>
>> \starttext
>>
>> \def\WhateverA{\removeunwantedspaces\removepunctuation\space ¶ 1.23}
>> \def\WhateverB{¶ 1.23}
>>
>> foo bar, \WhateverA\ test
>> foo bar, \WhateverB\ test
>>
>> \stoptext
>>
>> so, wrap ¶ into a macro
>>
>> \def\MyParSymbol{\removeunwantedspaces\removepunctuation\space ¶}
>>
>> Hans
>>
>> -----------------------------------------------------------------
>>                                            Hans Hagen | PRAGMA ADE
>>                Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
>>         tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
>> -----------------------------------------------------------------
>>
>

[-- Attachment #1.2: Type: text/html, Size: 3788 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-11-29 20:13 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-26  6:42 Checking for a Unicode prefix of a Unicode string Joey McCollum via ntg-context
2021-11-26  8:45 ` Hans Hagen via ntg-context
2021-11-26 16:57   ` Joey McCollum via ntg-context
2021-11-27 17:13     ` Joey McCollum via ntg-context
2021-11-27 18:44       ` Hans Hagen via ntg-context
2021-11-28  1:39         ` Joey McCollum via ntg-context
2021-11-28 10:39           ` Hans Hagen via ntg-context
2021-11-29 17:11             ` Joey McCollum via ntg-context
2021-11-29 20:13               ` Joey McCollum via ntg-context

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).