Access current element in xmlsetsetup via xml.expressions
Hello, I'm trying to build an xmlsetsetup with an own lua function. XML in MKIV says on pg. 33: You can also use normal Lua functions as long as you make sure that you pass the right arguments. There are a few predefined variables available inside such functions. list table the list of matches l number the current index in the list of matches ll element the current element that matched order number the position of the root of the path But I can't figure out how to get access to list, ll etc. My MWE (only for testing the access, is always true): \startbuffer[text] <para>Dies ist <inline style="sup">ein</inline> xxx<inline style="sub">zwei</inline> noch ein <inline style="sup">sup</inline> Test<inline style="sub">sub</inline></para> \stopbuffer %\enabletrackers[xml.parse,xml.path] \startluacode function xml.expressions.nextnodeatt(e) inspect(e) inspect(ll) return('sub') end \stopluacode \startxmlsetups xml:textsetups \xmlsetsetup{#1}{*}{+} \xmlsetsetup{#1}{para}{xml:*} \xmlsetsetup{#1}{inline[@style='sup'][xml.expressions.nextnodeatt(ll)=='sub' ]}{xml:sub} \stopxmlsetups \xmlregistersetup{xml:textsetups} \startxmlsetups xml:para \xmlflush{#1}\par \stopxmlsetups \startxmlsetups xml:sub \color[red]{\xmlflush{#1}} \stopxmlsetups \starttext \xmlprocessbuffer{main}{text}{} \stoptext Thanks in advance, Achim
On 1 Oct 2019, at 21:24, achim@jander.de wrote:
Hello, I’m trying to build an xmlsetsetup with an own lua function. XML in MKIV says on pg. 33: You can also use normal Lua functions as long as you make sure that you pass the right arguments. There are a few predefined variables available inside such functions. list table the list of matches l number the current index in the list of matches ll element the current element that matched order number the position of the root of the path
But I can’t figure out how to get access to list, ll etc.
Neither can I :( Taco
My MWE (only for testing the access, is always true):
\startbuffer[text] <para>Dies ist <inline style="sup">ein</inline> xxx<inline style="sub">zwei</inline> noch ein <inline style="sup">sup</inline> Test<inline style="sub">sub</inline></para> \stopbuffer %\enabletrackers[xml.parse,xml.path]
\startluacode function xml.expressions.nextnodeatt(e) inspect(e) inspect(ll) return('sub') end \stopluacode
\startxmlsetups xml:textsetups \xmlsetsetup{#1}{*}{+} \xmlsetsetup{#1}{para}{xml:*} \xmlsetsetup{#1}{inline[@style='sup'][xml.expressions.nextnodeatt(ll)=='sub']}{xml:sub} \stopxmlsetups
\xmlregistersetup{xml:textsetups}
\startxmlsetups xml:para \xmlflush{#1}\par \stopxmlsetups
\startxmlsetups xml:sub \color[red]{\xmlflush{#1}} \stopxmlsetups
\starttext \xmlprocessbuffer{main}{text}{} \stoptext
Thanks in advance, Achim ___________________________________________________________________________________ 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 ___________________________________________________________________________________
Taco Hoekwater Elvenkind BV
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. Best wishes, Massi
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 -----------------------------------------------------------------
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
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 ___________________________________________________________________________________
participants (4)
-
achim@jander.de
-
Hans Hagen
-
mf
-
Taco Hoekwater