[TW5] Macro Question: Is there an easier way to achieve this?

86 views
Skip to first unread message

Stephan Hradek

unread,
Oct 11, 2017, 2:34:57 PM10/11/17
to TiddlyWiki
Hi!

I am in need for something that gives me, given a path, just the filename part of it.

Of course I could use a JavaScript Macro, but I do not like the overhead.

The TW5 standard list operators do not give me much useful for splitting a string, Just the "splitbefore".

But after some testing I came up with this solution (explained at the bottom) which is a recursive macro, and I'm asking you whether you

  1. can find something easier
  2. also think we need a few more operators

\define basename(path)
 
<$list filter="[[$path$]] +[regexp[/]]">
   
<$list filter="[[$path$]] +[splitbefore[/]]" variable="dir">
     
<$list filter="[[$path$]] +[removeprefix<dir>]">
       
<$macrocall $name="basename" path=<<currentTiddler>>/>
      </
$list>
   
</$list>
  </
$list>
 
<$list filter="[[$path$]] +[!regexp[/]]">
   
<$text text=<<currentTiddler>>/>
  </
$list>
\end

Examples:

a
/b/c: <<basename "a/b/c">>

abc
: <<basename "abc">>

How does it work?

\define basename(path)
 
# Check whether we have a path seperator. Only execute the indented part if we have.
 
<$list filter="[[$path$]] +[regexp[/]]">
   
# split before the first path seperator. In the example this will give us "a/" for the first round.
   
# store this in <<dir>>
   
<$list filter="[[$path$]] +[splitbefore[/]]" variable="dir">
     
# Now take the full path and remove the <<dir>> we just found
     
<$list filter="[[$path$]] +[removeprefix<dir>]">
       
# recursively call the basename for the rest "b/c" in the first round in our example
       
<$macrocall $name="basename" path=<<currentTiddler>>/>
      </
$list>
   
</$list>
  </
$list>
 
# Check whether we have NO path seperator. Just print when we have none
 
<$list filter="[[$path$]] +[!regexp[/]]">
   
<$text text=<<currentTiddler>>/>
  </
$list>
\end

a
/b/c: <<basename "a/b/c">>

abc
: <<basename "abc">>




codacoder...@outlook.com

unread,
Oct 11, 2017, 4:16:36 PM10/11/17
to TiddlyWiki


On Wednesday, October 11, 2017 at 1:34:57 PM UTC-5, Stephan Hradek wrote:

 > also think we need a few more operators

THIS.

But that's some pretty nifty filtering, Stephan.  I'd have been holding my breath running a recursive filter ;)

I certainly think there is need for some basic string manip macros -- they seem to follow on naturally from the enlist operator, IMO.

Coda

PMario

unread,
Oct 12, 2017, 12:16:23 PM10/12/17
to TiddlyWiki
Hi Stephan,

You could test this PR and have a closer look. https://github.com/Jermolene/TiddlyWiki5/pull/2963
and give feedback.

-m



Reply all
Reply to author
Forward
0 new messages