Retitle a tiddler - conceptually easy - HA!

134 views
Skip to first unread message

JWHoneycutt

unread,
Sep 19, 2017, 12:41:41 AM9/19/17
to TiddlyWiki

I realistically have a thousand tiddlers with titles like:

  • Foo Bar meeting 2016-9-12
  • Quarterly staff meeting 2017-04-22
  • 2017-07-21 Department Huddle

Of note: all dates start with the character string "201"

I am interested in renaming them with the date in the front of the title, so that I can "!sort[]" them in filters, easily giving me the most recent ones at the top of the list.

How do I accomplish this? My guess is:
  1. Filter for all tiddlers whose title ends in a date (must allow for optional 1 character months and days) (changing dates to add zeros was done by Stephan Hradek in this thread: https://groups.google.com/forum/#!topic/tiddlywiki/eTbODCaTuYk)
  2. Use the fieldmangler widget to modify the field named "title"
  3. First I need to capture the date as a variable
  4. Change the value of the field(title) by removing the date from the end (somehow using "suffix")
  5. Change the value of the field(title) by adding the same date to its beginning followed by a single space (somehow using "prefix" - maybe both functions can be done in one step?)
  6. Move onto the next tiddler in the filtered output of step #1
Is this line of thinking the easiest way to accomplish this (based on my early understanding of TW5)?

Mark S.

unread,
Sep 19, 2017, 1:03:42 AM9/19/17
to TiddlyWiki
In database theory, one of the central tenets is to use each field to do only one thing. TW already breaks this by using the Title field for a title and an ID. Now you want to also use it for a date field.

Why not extract the dates and put them in their own date field (e.g. actiondate). Then you can sort on that field. This has the additional benefit of not breaking any existing links.

Mark

TonyM

unread,
Sep 19, 2017, 1:06:26 AM9/19/17
to TiddlyWiki


Just a high level idea,

I agree with Mark

List all tiddlers with dates, which a check box to flag them suffix or Prefix or Other
Write a process to move the suffix or prefix into a field titledate
Manually fix the balance which is tagged other

You could stop there and sort the list based on the value of the field
titledate.

However you could list all tiddlers with a title-date next to there tiddler title and scan the list to ensure all are of the correct form
If should be fairly easy to then process all tagged suffix to remove the suffix and add the titledate back as the prefix title

PMario

unread,
Sep 19, 2017, 6:55:28 AM9/19/17
to TiddlyWiki
On Tuesday, September 19, 2017 at 6:41:41 AM UTC+2, JWHoneycutt wrote:

I realistically have a thousand tiddlers with titles like:

  • Foo Bar meeting 2016-9-12
  • Quarterly staff meeting 2017-04-22
  • 2017-07-21 Department Huddle
We don't have a regexp filter, that returns "matched groups", yet. ... there is a pending pull request but I'm not sure, if it will solve your particular problem.

I did create 2 short scripts, that you can copy paste into your developer console and execute there.

BUT - BACKUP - BACKUP - BACKUP first :)

The following script doesn't modify anything.
It just outputs a list of new tiddler names. ...

Check them carfully and if any one of them is wrong you shouldn't go to the second step.

first step - check the names


"use strict";
// read existing tiddlers
var titles = $tw.wiki.getTiddlers();

var newTitle = function (elem) {
   
var xx,
          match
= elem.match(/(.+)(\d{4}-\d?\d-\d?\d)(.*)/);
   
if (match != null) {
       
// matched text: match[0]
       
// match start: match.index
       
// capturing group n: match[n]

       
// swap matches
       
if (match[1] && match[2]) {
            xx
= match[2] + " " + match[1]
       
}

       
// If there is some text after the date append it
       
// format: <date> <match 1>--<match 3>
       
// just change the "--"   to your likings
       
if (match[3]) xx = xx + "--" + match[3];

       
return xx.trim(); // trim() removes trailing spaces
   
} else {
       
return elem;
   
}
}

// create new titles
var newTitles = titles.map(newTitle)

newTitles
.map(function(elem) {console.log(elem)})



Second step - Danger Zone
If the frist step went OK, you can use the second function to rewrite your titles.

WARNING - Use a BACKUP !!

"use strict";
// read existing tiddlers
var titles = $tw.wiki.getTiddlers();

var rewriteTitles = function (elem) {
   
var xx,
        match
= elem.match(/(.+)(\d{4}-\d?\d-\d?\d)(.*)/);
   
if (match != null) {
       
// matched text: match[0]
       
// match start: match.index
       
// capturing group n: match[n]

       
// swap matches
       
if (match[1] && match[2]) {
            xx
= match[2] + " " + match[1]
       
}

       
// If there is some text after the date append it
       
// format: <date> <match 1>--<match 3>
       
// just change the "--"   to your likings
       
if (match[3]) xx = xx + "--" + match[3];

       
var tid = $tw.wiki.getTiddler(elem)
       
        xx
.trim(); // trim() removes trailing spaces

       
// danger zone ...
       
if (tid) {
          $tw
.wiki.addTiddler(new $tw.Tiddler(tid, {title: xx}));
          $tw
.wiki.deleteTiddler(elem);

         
// create some feedback
          console
.log(elem, "-->", xx);
       
}

       
return xx;
   
} else {
       
return elem;
   
}
}

// Enter the Danger Zone!
// BACKUP - BACKUP - BACKUP before you execute this!

// You need to uncomment the next line to excute the function!!
// titles.map(rewriteTitles);



Depending on the number of tiddlers, this may take some time.

have fun!
mario



TonyM

unread,
Sep 19, 2017, 8:37:59 AM9/19/17
to TiddlyWiki
Mario,

You seem to be a wizard. Can you confirm your approach for me so I may learn from your example?

Loading tiddlywiki in your browser it is stored in active memory. I the developer console you are running a script to search then modify the tiddlywiki in memory. Then the user will save it (from memory) to disk and WALA it is now modified?

Is this another strong point for tiddlywikis single file quine nature or are there other cases that work the same?

Thanks
Tony

PMario

unread,
Sep 19, 2017, 11:44:46 AM9/19/17
to TiddlyWiki
On Tuesday, September 19, 2017 at 2:37:59 PM UTC+2, TonyM wrote:
You seem to be a wizard.

:)
 
Can you confirm your approach for me so I may learn from your example?

Loading tiddlywiki in your browser it is stored in active memory.


right.
 

I the developer console you are running a script to search then modify the tiddlywiki in memory.


right

Then the user will save it (from memory) to disk and WALA it is now modified?


right
 

Is this another strong point for tiddlywikis single file quine nature or are there other cases that work the same?


Kind of. ... Today almost any web page contains javascript. ... But TW contains js, that is actually useful (for the user;). ...
Way too many sites, today, just contain js to display adverts :/ and rarely improving the user experience.

... So many sites would work the same way but the dev console can only be used to debug the site by the site owners.

With enough knowledge of the core API, the console can be used to "hack/test/debug" TW itself, wich is useful .... I kink!

have fun!
mario






JWHoneycutt

unread,
Sep 19, 2017, 3:49:05 PM9/19/17
to TiddlyWiki
@Mark S

Thank you. You wrote:

In database theory, one of the central tenets is to use each field to do only one thing. TW already breaks this by using the Title field for a title and an ID. Now you want to also use it for a date field.

Agreed, except that BECAUSE the title is the ID, and each is unique, if I remove the date from the title I have 100 tiddlers all trying to share the title "Staff meeting"
 
Why not extract the dates and put them in their own date field (e.g. actiondate). Then you can sort on that field. This has the additional benefit of not breaking any existing links.

Also a good idea, except that my sorts look like this, and I am still working on figuring out "second order sorts"
<div class="tc-table-of-contents"> <<toc-selective-expandable 'Staff meeting' '!sort[title]'>> </div>
 
JWHoneycutt

JWHoneycutt

unread,
Sep 19, 2017, 3:54:07 PM9/19/17
to TiddlyWiki
@PMario

How exciting...

Thank you for the code - I take your warning to heart and created a backup TW5.html, renamed it, and am working on it in a separate "sandbox".

For some reason, all your comments start with "//" - but for my implementation of TW5, this just italicizes the text that follows, and therefore doesn't run.

It must be one of my loaded plugins?
Auto Generated Inline Image 1

JWHoneycutt

unread,
Sep 19, 2017, 4:06:53 PM9/19/17
to TiddlyWiki
@PMario

I will just remove the "//" and everything that follows to the end of the line (remove all comments and keep the programming)

I did create 2 short scripts, that you can copy paste into your developer console and execute there.
...
The following script doesn't modify anything.
It just outputs a list of new tiddler names. ... 

by copy paste into my developer console - you mean just add a new tiddler called "PMario step 1"  with the body containing your code (stripped of comments) and save it - and it is going to reveal me a list of tiddlers to check??? 

Developer console must be something else...

Joshua Fontany

unread,
Sep 19, 2017, 4:30:19 PM9/19/17
to tiddl...@googlegroups.com
The Developer Console in Chrome is usually accessed by pressing F12 while on the tab/page you want to inspect. "Console" will be one of the tabs (w/ debug output from TW). Paste PMario's code into the input bos at the bottom with the "> " symbol. Other browsers may have other methods to access their dev console.

BTW, that a neat trick. :)

Best,
Joshua Fontany

RichardWilliamSmith

unread,
Sep 19, 2017, 5:41:06 PM9/19/17
to TiddlyWiki
If you're using chrome, it's probably easier to paste the code as a 'snippet' into the sources panel of the dev tools and run it from there.

PMario

unread,
Sep 20, 2017, 6:54:27 AM9/20/17
to TiddlyWiki
Hi,
I did just push a short video, that describes, how to use this hack :)  https://youtu.be/5Eu4myq5LRA

As described in the video. FF displays a warning, that you should only paste code that you trust!

To activate pasting, you need to type:   allow pasting    and then hit [enter]

have fun!


Reply all
Reply to author
Forward
0 new messages