ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: <achim@jander.de>
To: "'mailing list for ConTeXt users'" <ntg-context@ntg.nl>
Subject: Re: Access current element in xmlsetsetup via xml.expressions
Date: Wed, 2 Oct 2019 19:48:28 +0200	[thread overview]
Message-ID: <003401d57949$992c79e0$cb856da0$@jander.de> (raw)
In-Reply-To: <f3c9abce-7033-bbed-bb9f-a2ecc1e965a5@xs4all.nl>

Dear Hans,
that is really great. Thank's a lot.  Ill check the next beta and try to wikifiy (maybe I need further help for that)

Achim

-----Ursprüngliche Nachricht-----
Von: ntg-context <ntg-context-bounces@ntg.nl> Im Auftrag von Hans Hagen
Gesendet: Mittwoch, 2. Oktober 2019 18:53
An: mailing list for ConTeXt users <ntg-context@ntg.nl>; mf <massifr@fastwebnet.it>
Betreff: Re: [NTG-context] Access current element in xmlsetsetup via xml.expressions

On 10/2/2019 12:32 PM, mf wrote:
> The answer is in lxml-lpt.lua, where built-in expressions are defined.
> You need a good knowledge of LPEG that i miss.
> 
> Some built-in expressions get the current element as first argument, 
> like count() or child() (lines 1300-1307 of lxml-lpt.lua):
> 
> expressions.child = function(e,pattern)
>      return applylpath(e,pattern) -- todo: cache end
> 
> expressions.count = function(e,pattern) -- what if pattern == empty or 
> nil
>      local collected = applylpath(e,pattern) -- todo: cache
>      return pattern and (collected and #collected) or 0 end
> 
> Some other expressions use a template that passes the "list", "ll", "l" 
> and "order" arguments you find cited in the XML manual §4.1 "Path 
> expressions - Expressions and filters".
> These are the lines 738-743 in lxml-lpt.lua:
> 
> local template_e = [[
>      local expr = xml.expressions
>      return function(list,ll,l,order)
>          return %s
>      end
> ]]
> 
> That template is used by the function that registers a new expression 
> (lines 807-812 in lxml-lpt.lua):
> 
> local function register_expression(expression)
>      local converted = lpegmatch(converter,expression)
>      local runner = load(format(template_e,converted))
>      runner = (runner and runner()) or function()
> errorrunner_e(expression,converted) end
>      return { kind = "expression", expression = expression, converted 
> = converted, evaluator = runner } end
> 
> Anyway i could not find a way to define an expression with a function 
> like this:
> 
> xml.expressions.myexpr( ... )
> 
> that gets access to those arguments.
> 
> The only arguments it gets are the ones you specify in your LPATH 
> expressions; AFAIK they are attributes values -- with the @attr syntax
> -- and strings.
Live is complex isn't it? The problem, is that we also support the normal path expressions which in retrospect maybe was a bad idea ... 
better be explicit. Strings without quotes are actually intercepted as element references (just like the @ is).

So, I added a few extra accessors:

\startbuffer[text]
<para>
     A <inline style="sup">sup 1</inline>
     B <inline style="sub">sub 1</inline>
     C <inline style="sup">sup 2</inline>
     D <inline style="sub">sub 2</inline>
     E <inline style="sup">sup 3</inline>
     F <inline style="sup">sup 4</inline>
     G <inline style="sup">sup 5</inline>
     H <inline style="sub">sub 5</inline>
     I <inline style="sup">sup 6</inline> </para> \stopbuffer

\startluacode
     function xml.expressions.MyCheck(list,position,what)
      -- print("list",    list)
      -- print("position",position)
      -- print("what",    what)
         local n = list[position+1]
         return n and n.at.style == what
     end
\stopluacode

\enabletrackers[lxml.*]

\startxmlsetups xml:textsetups

     \xmlsetsetup{#1}{para|inline}{xml:*}

     % with a helper:

%     \xmlsetsetup{#1}{inline[@style='sup' and 
MyCheck(list(),position(),"sub")]}{xml:supsub}
%     \xmlsetsetup{#1}{inline[@style='sup' and 
MyCheck(list(),position(),"sup")]}{xml:supsup}

     % rather verbose, we *need* to use ['at'] and not .at

%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 
list()[position()+1]['at']['style']=='sub')]}{xml:supsub}
%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 
list()[position()+1]['at']['style']=='sup')]}{xml:supsup}

     % also ok

%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 
attribute(list()[position()+1],'style')=='sub')]}{xml:supsub}
%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 
attribute(list()[position()+1],'style')=='sup')]}{xml:supsup}

     % direct checking

%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 
attribute(list()[position()+1],'style','sub'))]}{xml:supsub}
%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 
attribute(list()[position()+1],'style','sup'))]}{xml:supsup}

     % shorter

     \xmlsetsetup{#1}{inline[@style='sup' and (attribute(list()[position()+1],'style','sub'))]}{xml:supsub}
     \xmlsetsetup{#1}{inline[@style='sup' and (attribute(list()[position()+1],'style','sup'))]}{xml:supsup}
\stopxmlsetups

\xmlregistersetup{xml:textsetups}

\startxmlsetups xml:para
     \xmlflush{#1}\par
\stopxmlsetups

\startxmlsetups xml:supsub
     \color[red]{\xmlflush{#1}}
\stopxmlsetups

\startxmlsetups xml:supsup
     \color[blue]{\xmlflush{#1}}
\stopxmlsetups

\startxmlsetups xml:inline
     \color[green]{\xmlflush{#1}}
\stopxmlsetups

\starttext
     \xmlprocessbuffer{main}{text}{}
\stoptext

In the next beta. Now, who is going to wikify this ...

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
___________________________________________________________________________________

___________________________________________________________________________________
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-10-02 17:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-01 19:24 achim
2019-10-02  8:18 ` Taco Hoekwater
2019-10-02 10:32   ` mf
2019-10-02 16:52     ` Hans Hagen
2019-10-02 17:48       ` achim [this message]

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='003401d57949$992c79e0$cb856da0$@jander.de' \
    --to=achim@jander.de \
    --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).