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
- can find something easier
- 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">>