Iterative parsing in a wikirule module?

46 views
Skip to first unread message

David Szego

unread,
Mar 22, 2017, 2:36:43 PM3/22/17
to TiddlyWikiDev
Hi all, sorry to keep posting so many questions this week.

I'm trying to write a parser that iterates through an array, and runs a regex for each value.

Use case:
  • make an array of all tags
  • make a RegExp to find first instance of each tag in the array
  • parse to return a macro that wraps each tag in <<tag arrayValue>>
The code below ends up setting the regex to the last value in the array, and parsing based on it (made sense in hindsight):

exports.init = function(parser) {
 
this.parser = parser;
 
// For each tag, set a matchRegExp and parse
 
for (var i = 0; i < myArray.length; i++) {
   
this.matchRegExp = new RegExp(whatever...);
 
}
};

...

exports
.parse = function() {
 
// Move past the matched word
 
this.parser.pos = this.matchRegExp.lastIndex;
 
params.push(paramInfo);
 
return [{
   
...
 
}];  
};

So I expected maybe I could call this.parse() each time through, but this just dies:

exports.init = function(parser) {
 
this.parser = parser;
 
// For each tag, set a matchRegExp and parse
 
for (var i = 0; i < myArray.length; i++) {
   
this.matchRegExp = new RegExp(whatever...);
   
// Parse based on the RegExp tailored for this array value
   
this.parse();
 
}
};

...


I've looked through the built-in parsers, but can't grok it. I'd like to do it this way because it seems getting the RegEx right would be equally difficult!

Any thoughts?

Thanks,
David.

Jeremy Ruston

unread,
Mar 22, 2017, 2:46:52 PM3/22/17
to tiddly...@googlegroups.com
Hi David

Hi all, sorry to keep posting so many questions this week.

I may be behind on your questions, do feel free to bump them if so; you’re working on interesting stuff and I’d like to help.

I'm trying to write a parser that iterates through an array, and runs a regex for each value.

Use case:
  • make an array of all tags
  • make a RegExp to find first instance of each tag in the array
  • parse to return a macro that wraps each tag in <<tag arrayValue>>
OK, this approach breaks one of the assumptions of the parsing/rendering machinery: that the result of parsing a tiddler does not depend on the state of the tiddler store. This is important because parsing is a relatively expensive operation, and it enables us to cache the results of parsing a tiddler.

In your use case, the results of parsing a tiddler would change depending upon the currently defined tags, defeating the caching mechanism.

This issue has come up before in relation to automatically turning the text of references to tiddler titles to become links; I can’t find the reference right now, but the thread (either here or on GitHub) did include a number of suggestions for alternative approaches.

Best wishes

Jeremy

--
You received this message because you are subscribed to the Google Groups "TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywikide...@googlegroups.com.
To post to this group, send email to tiddly...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywikidev.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywikidev/da6e4662-c67d-4999-9059-3d8aa56ef85e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Szego

unread,
Mar 22, 2017, 3:41:45 PM3/22/17
to TiddlyWikiDev

...

This issue has come up before in relation to automatically turning the text of references to tiddler titles to become links;

That raises a good analogy:

Create a Tiddler, in the body type the sentence "I'm referencing AnotherTiddler"

You will get the text: "I'm referencing AnotherTiddler"


Create another Tiddler, called "AnotherTiddler"

The first one changes it's text to "I'm referencing AnotherTiddler" - no longer italic.
What happens is the first Tiddler refreshes and the classes change from tc-tiddlylink-missing to tc-tiddlylink-resolves.

Maybe it's not a parser / wikirule I'm looking for?

If I could harness that same mechanism:
- check for existence of Tiddlers of that name, change the class
then I could apply it here:
- check for the existence of Tags matching that work, wrap in macro

What is that mechanism, and is it feasible?

Cheers,
David.
Reply all
Reply to author
Forward
0 new messages