Re: [NTG-context] How to check if buffer or block has content
That's great, I wikified a Note for testing if a buffer exists or has content (using the \getbuffer[…] approach for the moment). If I was asked, I'd opt for \doifelseemptybuffer (doifemptyelse?) since one could think of similar commands that test for empty "objects". Is there an option for testing blocks? (I actually use blocks quite frequently …) I just realized they are probably handled very differently during the typesetting. Basically I would just need to test if during the first run any of \beginBLOCK … \endBLOCK was invoked (but if one gets a test for some content in-between that's even better). As an alternative solution, is there a way to add content to buffers? Unfortunately, I can't use \getbuffer[first,second], but \startbuffer[first][add=yes] … \stopbuffer would be nice. Thanks! Benjamin
On Aug 2, 2021, at 17:17, ntg-context-request@ntg.nl wrote:
\starttext
\startbuffer[test] test \stopbuffer
\doifelsebuffer{test} {YES} {NOP}
\permanent\protected\def\doifelsebuffercontent#1% {\doifelse{\luaexpr{(string.strip(buffers.getcontent("#1")) ~= "" and "yes")}}{yes}}
\startbuffer[test] \stopbuffer
\doifelsebuffercontent{test} {YES} {NOP}
\startbuffer[test]
\stopbuffer
\doifelsebuffercontent{test} {YES} {NOP}
\stoptext
On 8/2/2021 8:54 PM, Benjamin Buchmuller via ntg-context wrote:
That's great, I wikified a Note for testing if a buffer exists or has content (using the \getbuffer[…] approach for the moment).
If I was asked, I'd opt for \doifelseemptybuffer (doifemptyelse?) since one could think of similar commands that test for empty "objects".
Is there an option for testing blocks? (I actually use blocks quite frequently …)
I just realized they are probably handled very differently during the typesetting. Basically I would just need to test if during the first run any of \beginBLOCK … \endBLOCK was invoked (but if one gets a test for some content in-between that's even better)
indeed, multiple axis ... so then we need three arguments instead of one for the test \defineblock[testa] \defineblock[testb] \starttext \begintesta oeps \endtesta \doifelseblocksempty {testa}{}{} {YES} {NOP} % {name}{tag}{criterium} \doifelseblocksempty {testb}{}{} {YES} {NOP} % {name}{tag}{criterium} \stoptext but you have to wikify it 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 -----------------------------------------------------------------
On 8/2/2021 8:54 PM, Benjamin Buchmuller via ntg-context wrote:
That's great, I wikified a Note for testing if a buffer exists or has content (using the \getbuffer[…] approach for the moment).
If I was asked, I'd opt for \doifelseemptybuffer (doifemptyelse?) since one could think of similar commands that test for empty "objects".
Is there an option for testing blocks? (I actually use blocks quite frequently …)
I just realized they are probably handled very differently during the typesetting. Basically I would just need to test if during the first run any of \beginBLOCK … \endBLOCK was invoked (but if one gets a test for some content in-between that's even better).
As an alternative solution, is there a way to add content to buffers? Unfortunately, I can't use \getbuffer[first,second], but \startbuffer[first][add=yes] … \stopbuffer would be nice. just for fun I made this:
\starttext \startbuffer[test] test \stopbuffer \startbuffer[oeps] oeps \stopbuffer \typebuffer[test] \getbuffer[test] \startbuffer[test = * + test] prepend \stopbuffer \typebuffer[test] \getbuffer[test] \startbuffer[test = test + *] append \stopbuffer \typebuffer[test] \getbuffer[test] \startbuffer[test = * +++ test] before \stopbuffer \typebuffer[test] \getbuffer[test] \startbuffer[test = test +++ *] after \stopbuffer \typebuffer[test] \getbuffer[test] \startbuffer[test = * + oeps + test + oeps + * ] DONE \stopbuffer \typebuffer[test] \getbuffer[test] \stoptext which results in test test prependtest prependtest prependtestappend prependtestappend before prependtestappend before prependtestappend before prependtestappend after before prependtestappend after DONEoepsbefore prependtestappend afteroepsDONE DONEoepsbefore prependtestappend afteroepsDONE so basically one can combine buffers any way + : nothing in between ++ : space in between +++ : empty line in between ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
Wow, that's powerful! I'll add the wiki entries later tonight. Thanks a lot!
On Aug 3, 2021, at 17:02, Hans Hagen
wrote: On 8/2/2021 8:54 PM, Benjamin Buchmuller via ntg-context wrote:
That's great, I wikified a Note for testing if a buffer exists or has content (using the \getbuffer[…] approach for the moment). If I was asked, I'd opt for \doifelseemptybuffer (doifemptyelse?) since one could think of similar commands that test for empty "objects". Is there an option for testing blocks? (I actually use blocks quite frequently …) I just realized they are probably handled very differently during the typesetting. Basically I would just need to test if during the first run any of \beginBLOCK … \endBLOCK was invoked (but if one gets a test for some content in-between that's even better). As an alternative solution, is there a way to add content to buffers? Unfortunately, I can't use \getbuffer[first,second], but \startbuffer[first][add=yes] … \stopbuffer would be nice. just for fun I made this:
\starttext
\startbuffer[test] test \stopbuffer
\startbuffer[oeps] oeps \stopbuffer
\typebuffer[test] \getbuffer[test]
\startbuffer[test = * + test] prepend \stopbuffer
\typebuffer[test] \getbuffer[test]
\startbuffer[test = test + *] append \stopbuffer
\typebuffer[test] \getbuffer[test]
\startbuffer[test = * +++ test] before \stopbuffer
\typebuffer[test] \getbuffer[test]
\startbuffer[test = test +++ *] after \stopbuffer
\typebuffer[test] \getbuffer[test]
\startbuffer[test = * + oeps + test + oeps + * ] DONE \stopbuffer
\typebuffer[test] \getbuffer[test]
\stoptext
which results in
test test prependtest prependtest prependtestappend prependtestappend before prependtestappend before prependtestappend before prependtestappend after before prependtestappend after DONEoepsbefore prependtestappend afteroepsDONE DONEoepsbefore prependtestappend afteroepsDONE
so basically one can combine buffers any way
+ : nothing in between ++ : space in between +++ : empty line in between
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (2)
-
Benjamin Buchmuller
-
Hans Hagen