Code Pattern needed - internal flag within list

96 views
Skip to first unread message

TW Tones

unread,
Jul 25, 2020, 9:34:11 PM7/25/20
to TiddlyWiki
Folks,

The following objective I believe exposes a limitation in tiddlywiki I would like to resolve; however I wish to find at least a work around for now.

I am just writing a macro to process lines in a tiddler
<$tiddler tiddler=<<tiddlername>> >
<$list filter="[all[current]get[text]splitregexp[\n]]" variable=line>

</$list>
</$tiddler>

However inside the list, I want to process each line, 
  • However I wish to detect a start and end condition and display only the lines including and between the start and end condition.
  • This start and end condition may occur more than once in the same tiddler.
  • A real world example may be extracting the content between an open and close html div or section tag.
To do this my first idea so far is 
  • setting a flag at the start of when the condition is met, 
  • displaying each line when this flag is on
  • and turning the flag off when the end condition is met.
However this flag would be a variable in the traditional (non tiddlywiki sense) and I do not believe we have one in, tiddlywiki that does not require a trigger to set a text reference to that value. This is a gap I have recognised for some time but had trouble communicating.

A second idea is to "call a sub-routine", while the condition is met, 
  • but to do this we need to have the ability to request the next line from the source tiddler
  • We can not do this to my knowledge because the flow must return to the next item in the list that calls the subroutine.

Can anyone share a solution to this issues please?

thanks in advance for considering this
TW Tones


Mark S.

unread,
Jul 25, 2020, 11:56:35 PM7/25/20
to TiddlyWiki

Hi Tony,

I think Mohammad's refnotes project may do something like this.

The algorithm, as I understand it is

  1. Look for the first marker in the string using splitbefore
  2. Use the result from (1) to remove the prefix from the original string
  3. Look for the 2nd marker in the string from (2) using splitbefore. The result is one of the strings you want. Output the string.
  4. Use the result from (3) to remove any prefix from the original string. That string then becomes the new "original" string, and is fed back to (1) recursively.

All of this could be done much more easily with PR 2963.

TW Tones

unread,
Jul 26, 2020, 2:33:56 AM7/26/20
to TiddlyWiki
Mark,

I see split before and split after may be away to operate on a string, I may need to treat the whole text field as a single string, and deal with multiple matches.

I will explore that but it would help if we could set values and flags within content. Set and vars have closing statements, and macros are defined once.

Regards
Tony

TW Tones

unread,
Jul 26, 2020, 2:35:45 AM7/26/20
to TiddlyWiki
Mark

Speaking of Mohammad (From Shiraz) I suspect international politics has made him absent a while now, again. I hope he is OK!

Regards
Tony

On Sunday, July 26, 2020 at 1:56:35 PM UTC+10, Mark S. wrote:

Mat

unread,
Jul 26, 2020, 4:31:38 AM7/26/20
to TiddlyWiki
A semi-answer:
First, make sure you only manipulate relevant tiddlers/tiddlertexts. Then to get only the lines containing your start marker, adding a regexp prefix condition. This shows the idea but may have syntactical errors: 
 
[all[current]search:text:regexp[^start]get[text]splitregexp[\n]regexp[^start]" variable=line

Thus you get a list of lines all prefixed wiht your start string. Then you cut off their tails in new listwidget:

[<line>splitbefore<endcondition>]

(not sure if splitbefore demands that the input has the endcondition to be passed through. This would not be desirable)

<:-)

dmigod forever

unread,
Jul 26, 2020, 5:33:17 AM7/26/20
to TiddlyWiki
I am couldn't exactly understand what is being asked but the way I understand, you want to select text that comes between two tags.

When I wanted to select that text that comes between <p> tags in a tiddler and discard the other text the filter I used was

[{Tiddler 1}split[<p>]splitbefore[</p>]removesuffix[</p>]!is[blank]]

Shouldn't this work for selecting between section tags as well?

Now you could add another split for spliting into separate lines if you want I think.

TW Tones

unread,
Jul 26, 2020, 10:04:21 AM7/26/20
to TiddlyWiki
Folks

I suspect the answer is close, and within what you have suggested so far but it is because we do not have access to real variables. I am experienced in procedural languages. I know how to do this in other languages almost with my eyes closed.

Just to explain this a little further an example is wrapping a section in the html section tag, ideally allowing me to extract one or more sections in a tiddler.

In this case I would like to keep the section open and close in my output so I may access other attributes on the section such as style, class or id. In a way I want to be able to transclude a whole section, or more from one tiddler in another tiddler

In a procedural language I would process a line at a time using splitregexp. When I find the start set a flag which will output the lines until the end conditions is true, when I clear the flag. Subsequent lines are ignored until the end or the start condition is triggered.

Its such simple logic.

If we could define a value then access and update it on the fly this would be easy. Unfortunatly in tiddlywiki set and vars are more like declarations. Even widgets tend to act on their contents and we do not have independent variables.

You can set a text reference to a value, retrieve and increment and save it however you must have a button to trigger it.

This needs forcing into tiddlywiki.

Regards
Tony

TW Tones

unread,
Jul 26, 2020, 10:39:36 PM7/26/20
to TiddlyWiki
Folks,

In fact I need not split into lines after extracting sections, just feed it into a wikify with html output.
  • In this case I want to keep the prefix and its content, so due to split I have to add the start one back.
  • I am not sure why the "!is[blank] is needed but it is.
  • by default this handles block sections, inline items like span's could be handled as well with mode=inline on the wikify?

Based on your comments I have come up with a working solution I plan to generalise further.

<$list filter="[{NewTiddler}split[<section]splitbefore[</section>]!is[blank]addprefix[<section ]]" variable=section>
   <$wikify name=display-section text="<
<section>>" output=html>
         <
<display-section>>
   </$wikify>
</$list>

Source tiddler "NewTiddler"
<section id="example">

;Bank line above
*WIkitext
*Wikt ''text''
</section>

<section id="example2">

;Bank line above
*WIkitext
*Wikt ''text''
</section>
If the sections are identical (unlike above with a change in ID) you only see it once. ie it is de-duplicated (A Good thing)

Now I have a solution to the original thread, thanks all. However the need to have a real variable feature, I think my example illustrated still exists, if not for this case.


Regards
Tony

TW Tones

unread,
Jul 27, 2020, 12:11:35 AM7/27/20
to tiddl...@googlegroups.com
Update

For anyone interested I have now generalise this to create a macro for extracting any hard coded html tags/sections from a tiddler. 

Uses
  • Allows you to divide tiddlers into sections using html, and access such sections from elsewhere
    • Not unlike transcluding a fragment of a tiddlywiki
Examples
  • Create an excerpt or abstract section in your tiddler and include only that in a separate list
  • Extract html tables from tiddlers to display and link to in an appendix
  • many more
I now realise it would only be a small step to extract content by HTML tag after rendering the content of a tiddler into html. 
This would allow any html tag which is the result of tiddlywikis render process also to be extracted and displayed, 

An example would be the use of tiddlywikis "|" table encoding method, since this is translated to html table tag during rendering you could extract table made this way OR HTML hard coded tables from any tiddler.

Further speculation
  • One could write tiddlers with wiki text and macros generating a required result, then access this from any other tiddler as finalised html or even more precisely extract value(s) from that result, ie we can pull content and reuse it without demanding additional wiki text and rendering. In a way this would be like screen scraping ones own site.
  • What if we could do this to the contents on an iframe or other content include method? Integration would be very easy.
Regards
TW Tones
Always wanting to extend the possibilities of Tiddlywiki

Adithya B M

unread,
Aug 15, 2020, 11:24:23 AM8/15/20
to TiddlyWiki
Hi Tony, 

I found this searching for "excerpt" on the group. is it possible to extract the contents of the First <p> occurance in a Tiddler using your macro?

That would be something I am interested in.

Thanks
Adithya

Reply all
Reply to author
Forward
0 new messages