Discussion:
[tw] [TW5] Macro Question: Is there an easier way to achieve this?
Add Reply
Stephan Hradek
2017-10-11 18:34:57 UTC
Reply
Permalink
Raw Message
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+***@googlegroups.com.
To post to this group, send email to ***@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.
c***@outlook.com
2017-10-11 20:16:35 UTC
Reply
Permalink
Raw Message
Post by Stephan Hradek
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
--
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+***@googlegroups.com.
To post to this group, send email to ***@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/9561e82c-a3b5-41c1-817c-9fe82169aa9e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
PMario
2017-10-12 16:16:23 UTC
Reply
Permalink
Raw Message
Hi Stephan,

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

-m
--
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+***@googlegroups.com.
To post to this group, send email to ***@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/e9bfde6c-86c0-46db-bc6d-0094b51df4a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...