Tiddlywiki5: Request: StringSlicerWidget

Created on 17 Jan 2017  路  12Comments  路  Source: Jermolene/TiddlyWiki5

I'd like a good way to slice out segments of text, probably equivalent to the javascript slice string method.

I have previously experimented with this in pure wikitext wich demanded various recursive loops and several nested filters because the current string operators are very limited. One problem is that while we can regexp to test the existance of a string segment, we cannot directly store it in a variable. I suspect the many filters and loops are costly and it was complicated to set up. Considering there is a js-native solution for this, I'm guessing it would not be overly difficult for someone in the know.

Most helpful comment

Hello @Jermolene! Is it possible/probable that @tobibeer's tobibeer/split will make it into core?

P.S. What about his tobibeer/join?

All 12 comments

Hi Mat, do you mind sharing one more more practical use cases / examples where this would have saved your day?

While not quite as performing or as pretty as:

{{{ [[foo bar baz]slice[5,3]] }}}

You can already use tobibeer/split like so:

{{{ [[foo bar baz]split:last at[5]split:first at[4]] }}}

Hm, my reference to native js slice might be misleading. Given the key role of texts, I'm thinking more about being able to slice out between arbitrary specified strings like words, html-tags, etc in texts. I attempted this in Cherrypicker, including the thoughts for un-implemented ideas I mention there. Another concept, not yet published, is TriEdit. I've attempted other stuff too that have not really come to fruition because of this. One major idea is to slice out e.g macros or style defs for reuse elsewhere.

Would your split operator be able to perform this? E.g in this text to slice out the strings between
Overall I think TW is a lacking in "string tools" considering TW is much about text.

Hi @twMat, I just released the new version 0.7.2 of tobibeer/split which now allows you to do:

{{{ [[foo bar baz]split:at[5,3]] }}}

Note: Unlike substr or slice, this literally starts with the 5th character in your string, not the 6th.

Hi @twMat, yes, tobibeer/split can do that for you, so long as you have defined opening and closing strings.

{{{ [[<foo>bar</foo>]split:last[<foo>]split:first[</foo>]] }}}

tobibeer/split - first trials look very promising. It seems even to handle very provocative characters in the text string, like various brackets and triple quotes, etc. This might just be what I'm hoping for! Thank you @tobibeer :-)

Side note ref. OP: An alternative to a native widget might be if tiddlywiki.com categorized community resources like Tobias' plugin, Not sure how categorization woudl be handled practically though, as the existing tag practice doesn't quite allow it.

@twMat, fyi, I'm planning to also implement these new suffixes for tobibeer/split:

  • split:before[<this>]

    • returns everything up until the first match of <this> using indexOf()

  • split:after[<this>]

    • returns everything after the first match of <this> using indexOf()

  • split:beforelast[<this>]

    • returns everything up until the last match of <this> using lastIndexOf()

  • split:afterlast[<this>]

    • returns everything after the last match of <this> using lastIndexOf()

...whereas <this> corresponds to the string you wish to match / at which you want to split.

Examples

{{{ [foo.bar.baz]split:before[.] }}}

foo

{{{ [foo.bar.baz]split:after[.] }}}

bar.baz

{{{ [foo.bar.baz]split:beforetlast[.] }}}

foo.bar

{{{ [foo.bar.baz]split:afterlast[.] }}}

baz

All of the above can already be done, however in a more complicated manner, at times requiring to also use tobibeer/join to tie split items back together.

@twMat, by the way, please be careful with terminology. At least, I don't think you're actually after a widget with your request, are you?

@tobibeer - with those additional suffixes, several ideas of mine will be a breeze to make! Wonderful easy to understand semantic commands. Wow!

@twMat, by the way, please be careful with terminology. At least, I don't think you're actually after a widget with your request, are you?

First off, true, I don't fully understand what characterizes a widget. The premise was that I think we need an effective tool for slicing out segments in text, given (again) how central texts are in TW. So, given the complexity of traversing through a text to find specific start/end points etc, my reasoning was that it should (still) be as primitive as possible. As far as I understand, this is what widgets are/for.

With tobibeer/join, I see how a filter operator makes much more sense from a practical point of view - especially for the example applications I mentioned previously.

Given your input, I'm closing this issue. (Yes, of course I fully understand that implementing those last suffixes are intentions, not promises. But I do look forward to it ;-) )

@twMat

Given your input, I'm closing this issue. (Yes, of course I fully understand that implementing those last suffixes are intentions, not promises. But I do look forward to it ;-) )

Go take a look ;-)

Haha!
ATM, no possibility to try it out but, in deed, you seem to have implemented it already!!! Warm thanks @tobibeer !

Hello @Jermolene! Is it possible/probable that @tobibeer's tobibeer/split will make it into core?

P.S. What about his tobibeer/join?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

twMat picture twMat  路  4Comments

Jermolene picture Jermolene  路  6Comments

twMat picture twMat  路  5Comments

KendrickLamarck picture KendrickLamarck  路  4Comments

diego898 picture diego898  路  5Comments