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">>




-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to tiddlywiki+unsubscr...@googlegroups.com.
To post to this group, send email to tiddlywiki@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/135ec056-6dd5-4c2d-9454-ade3b020c324%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to