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-boun...@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 <mass...@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 ___________________________________________________________________________________