Substring of tiddler name

735 views
Skip to first unread message

stevesuny

unread,
Mar 17, 2017, 5:13:00 PM3/17/17
to TiddlyWiki
 Hi,

this topic seems to have come up before, but without a clear answer.

How do I define a substring with  the first x characters of a tiddler name?

If I have $tiddler$ named "01 01 Long name of tiddler" (those are spaces) I'd like to be extract "01 01" to <<short-name>> so I can write <$link to="$tiddler$><<short-name>></$link> to display "01 01" as a link.

Thanks!

(It seems I could play with prefix, but I kind of messed up with my tiddler names by having two spaces, so I can't make a prefix of the first five characters...)

//steve.


PMario

unread,
Mar 17, 2017, 5:29:55 PM3/17/17
to TiddlyWiki
Hi Steve,

Is your number prefix unique? So there is only one tiddler that matches? It would be possible to create a regexp filter - list - link - macro

-m

stevesuny

unread,
Mar 17, 2017, 5:37:17 PM3/17/17
to TiddlyWiki
Yes, number string is unique.

I've been working with the regexp, but can't quite get it to work...

This is what I've got:

<$set name="digit-pattern" value="[0-9]{2} [0-9]{2}">        ||> this is the unique pattern that begins each tiddler
<$list filter="[regexp:title<digit-pattern>]">                         ||> this (currently) gets all matching tiddlers
1 <<currentTiddler>><br>
<$set name="shortName" value=[{!!title}splitbefore[W]]>
2 <<shortName>><br>          ||> i was hoping that <<shortName>> would be the first five chars
</$list>

stevesuny

unread,
Mar 17, 2017, 5:38:32 PM3/17/17
to TiddlyWiki
oops, splitBefore[W] was a placeholder; it would be splitbefore[regexp[A-Z]]

PMario

unread,
Mar 17, 2017, 5:42:09 PM3/17/17
to TiddlyWiki
global macro like this:

\define myLink(prefix)
<$set name="digit-pattern" value="^$prefix$">
<$list filter="[regexp:title<digit-pattern>]" emptyMessage="no match"><$link to=<<currentTiddler>> >$prefix$</$link></$list>
</$set>
\end



usage:

<<myLink "01 01">>

The only problem is, if there are several matches. ... It doesn't look good then.

have fun!
mario

stevesuny

unread,
Mar 17, 2017, 7:00:47 PM3/17/17
to TiddlyWiki
hmm.....

Mario, thanks!  This works .... sort of ....

But I don't know the prefix - I have a list of 50+ tiddlers with unique digit patterns as part of the tiddler names.  I need to extract the prefix.

Is there a substring function in TW that can operate on a variable? Like, =substring(string,5) that returns the first 5 chars?


//steve.

Steven Schneider

unread,
Mar 17, 2017, 10:09:35 PM3/17/17
to TiddlyWiki
It seems there isn't such a function in TW core. But, is it possible (for someone who knows how to do it) to write a substring js macro.

The javascript is this:

var str = "Hello world!";
var res = str.substring(x, y);

so <$macrocall $name="substring" string="Hello,World" x="1" y="5"/> would return "Hello".

...which is well beyond my skills (my first attempt generated java script error, and forced me to jump through some hoops to delete the tiddler, as it was hidden behind the red screen of death...

Mat

unread,
Mar 18, 2017, 6:26:54 AM3/18/17
to TiddlyWiki
Steve, be sure to look around at Grand TWizard Tobias Beers stuff from time to time. I'm pretty sure he has created filter operators that can do what you ask for.

<:-)

The Bo

unread,
Mar 20, 2017, 6:13:46 AM3/20/17
to tiddl...@googlegroups.com
Hey,

you could try it with a "substring" macro like this:
/*\
title: $:/substring_from_to
type: application/javascript
module-type: macro

A macro to get certain characters from a string.

\*/

(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Information about this macro
*/


exports
.name = "substr";
exports
.params = [
                             
{name: "from",default:"0"},
                             
{name: "to", default:"0"}
];
/*
Run the macro
*/

exports
.run = function(from, to) {

var tiddlertitle = this.getVariable("currentTiddler");
     
if (to !== "0") {
     
return tiddlertitle.substring(from,to);
     
} else {
     
return tiddlertitle.substring(from);
   
}

};
})();
tag: $:/tags/Macro
type: application/javascript
module-type: macro


Usage: (even with a Filter)
<$list filter="[!is[system]!is[shadow]tag[yourTag]]">

<$link to=<
<currentTiddler>>><<substr "0" "5">></$link>


Regards,
The Bo

PMario

unread,
Mar 20, 2017, 7:53:31 AM3/20/17
to TiddlyWiki
Hi Steve,

Substring can be solved with a macro as "The Bo" did. ...

If we needed more complicated patterns, something that has been discussed at: https://groups.google.com/forum/#!topic/tiddlywikidev/qSNdKIZWNrc ... would be needed.

-m

Steven Schneider

unread,
Mar 21, 2017, 11:14:14 PM3/21/17
to TiddlyWiki
Hello The Bo: Thanks for the code. It looks like is should work...

But, in tiddlywiki.com, I

1 - Created a new tiddler with these fields:

title: $:/substring_from_to
type: application/javascript
module-type: macro
tag: $:/tags/Macro
text: copied all lines provided

2 - Created a new tiddler with no tags, no fields other than text:

Usage: (even with a Filter)<br>
<$list filter="[!is[system]!is[shadow]tag[HelloThere]]">
1 <<currentTiddler>><br>
2 <$link to=<<currentTiddler>>> <<substr "0" "5">> </$link><br>
</$list>


For my line 1, I got tiddler names. For my line 2, I had nothing returned. :(

Any help, much appreciated. This looks very promising, and exactly what I had hoped to have.

//steve.

PMario

unread,
Mar 22, 2017, 3:54:22 AM3/22/17
to TiddlyWiki
Hi Steve

You need to use fileds as described in the code header

title: $:/substring_from_to
type: application/javascript
module-type: macro


save and reload, to activate the macro.

-m

James

unread,
Apr 18, 2017, 10:55:09 PM4/18/17
to TiddlyWiki
Is that possible to restrict the number of character to be listed from the content instead of title?
Reply all
Reply to author
Forward
0 new messages