[NTG-context] Access current element in xmlsetsetup via xml.expressions

Hans Hagen j.hagen at xs4all.nl
Wed Oct 2 18:52:54 CEST 2019


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


More information about the ntg-context mailing list