Parse a tiddler's content an create a file from it.

33 views
Skip to first unread message

Nicolas Syssoieff

unread,
Mar 3, 2008, 9:49:04 AM3/3/08
to tiddlywiki
Hi guys,

I'd like to achieve something specific : turn tiddler's content into files.
What I have today is an implementation of a simple address book (compared to Twab), where every entry is a tiddler, and each tiddler looks like :
<<formTiddler MyTemplate>>
<data>{"generalname":"My Friend","phone":"+3311111111","email":"myfr...@host.com","company":"Some company"}</data>

Could anyone point me to the right plugins or script parts that would allow me, in the most intelligent way, to extract from each of these tiddlers, the data part, and then to create actual files with this content (the ultimate goal being to generate .vcf files from that, so that then I could feed it to my phone, for example) ?

Assumptions are :
- data tiddlers are all tagged "addressBook" or anything elese, so it won't be a trouble to find them within the TW
- the data part of a tiddler does not always contain those fields, it contains only the ones for which there is actual data.

Thanks for your ideas,
Nicolas.

Gam

unread,
Mar 3, 2008, 11:19:35 AM3/3/08
to TiddlyWiki

Wish I knew more about data, but I don't. Mean time you may find
something at:

http://del.icio.us/TiddlyWikiPlugin?setcount=100

There are plugins in there maybe the PublishMacro could open doors in
the direction you wish?


Also a great repository of plugins and scripts and macros is at:

http://www.tiddlytools.com/

There are other locals too for plugins, but I really don't know what
you would need.

wolfgang

unread,
Mar 3, 2008, 2:21:22 PM3/3/08
to TiddlyWiki
Hi Nic,

I did something similar once by exporting the store area with
TiddlersExportPlugin bookmarklet.
Then opening this file in a good external editor, with multi-line
search and replace changed all the data fields into the tiddler slices
I wanted them to be - and imported the whole lot again.

This is fast said - but takes a lot of patience to do. :-(

Now the real fast part, :-)

to have all these tiddlers exported as separate text files: use
RenameTag bookmarklet to rename the tag of all tiddlers tagged
'addressBook' into 'Archive'. With the ArchivePlugin installed
beforehand, this will save all these tiddlers content into separate
files in a subfolder 'archive' and with the file extension .txt. Then
you would only have to batch rename the file extension of those text
files into .vcf.

http://www.tiddlytools.com/#InstantBookmarklets
http://jackparke.googlepages.com/jtw.html#ArchivePlugin

hope that helps a tiny bid further,

W.

Nicolas Syssoieff

unread,
Mar 3, 2008, 2:37:54 PM3/3/08
to Tiddl...@googlegroups.com
Hey Wolf,

This seems very boring to patiently implement :D, but very promising...
I'll give it a shot tomorrox and let you know.

Thanks for that.
Nicolas.

Eric Shulman

unread,
Mar 3, 2008, 11:56:44 PM3/3/08
to TiddlyWiki
The following inline script (*) will extract the data from each
tiddler tagged with "addressBook", and create a .VCF-formatted file in
the current directory, using the tiddler title as the filename (with
a .vcf extension, of course)
-----------------------------------------
<script label="create vCards">
var tag="addressBook";
var startMarker='<data>{"';
var endMarker='"}</data>';
var pre="begin:vcard\n";
var post="\nend:vcard\n";
var ext=".vcf";

var path=getLocalPath(document.location.href);
var slashpos=path.lastIndexOf("/");
if (slashpos==-1) slashpos=path.lastIndexOf("\\");
if (slashpos!=-1) path=path.substr(0,slashpos+1);

var tids=store.getTaggedTiddlers(tag);
for (var t=0; t<tids.length; t++) {
var start=tids[t].text.indexOf(startMarker);
if (start==-1) continue;
start+=startMarker.length;
var end=tids[t].text.indexOf(endMarker,start);
txt=tids[t].text.substring(start,end);
txt=txt.replace(/","/g,"\n").replace(/":"/g,":");
var fn=path+tids[t].title+ext;
var link="file:///"+fn.replace(/\\/g,'/');
if (saveFile(fn,pre+txt+post))
displayMessage("created file: "+fn,link);
}
</script>
-----------------------------------------

(*) requires http://www.TiddlyTools.com/#InlineJavascriptPlugin

enjoy,
-e
Eric Shulman
TiddlyTools / ELS Design Studios

Nicolas Syssoieff

unread,
Mar 4, 2008, 5:04:29 AM3/4/08
to Tiddl...@googlegroups.com
Hi Eric,

This script is awesome, it does it all in a handful of lines.... I can't believe my eyes :)
With a little tweaking (such as changing all fields into the corresponding vcf fields), that will be just it !

I'll try to modify all this myself, and I'll come back is something blocks me.

Thanks a lot !
Nicolas.

Eric Shulman

unread,
Mar 4, 2008, 5:44:11 AM3/4/08
to TiddlyWiki
> With a little tweaking (such as changing all fields into the corresponding
> vcf fields), that will be just it !
> I'll try to modify all this myself, and I'll come back is something blocks
> me.

Part of the compact-ness of the current script is that it doesn't have
to split up the <data>...</data> block to alter any field names or
values... it only has to convert the "," and ":" sequences that
separate them. Rather than having the script map the form field names
to standard vCard names, I suggest simply changing the existing field
names in your current <form> and <data> definitions to use the
standard vCard field names directly.

enjoy,
-e

Nicolas Syssoieff

unread,
Mar 4, 2008, 5:53:23 AM3/4/08
to Tiddl...@googlegroups.com
Hi Eric,

I thought about what you suggest, but in fact, this cannot be implemented directly, for some fields are non-standard, for example the address or note vcard format is :
NOTE;ENCODING=QUOTED-PRINTABLE:any_note_data

Clearly it is not possible (or at least complicated) to name one of my form's field this way, so I in fact inserted multiple replace strings, following your syntax to the letter, like this :
               txt=txt.replace(/","/g,"\r\n").replace(/":"/g,":");
               txt=txt.replace(/generalname/,"N");
               txt=txt.replace(/company/,"ORG");
               txt=txt.replace(/address/,"NOTE;ENCODING=QUOTED-PRINTABLE");
etc etc...

It works like a charm, and is easy to update if I switch to another phone using a different vcf encoding scheme, especially since I only have maybe 15 to 20 fields.

What do you think ?

Nicolas.

Eric Shulman

unread,
Mar 4, 2008, 6:06:32 AM3/4/08
to TiddlyWiki
> txt=txt.replace(/","/g,"\r\n").replace(/":"/g,":");
> txt=txt.replace(/generalname/,"N");
> txt=txt.replace(/company/,"ORG");
> txt=txt.replace(/address/,"NOTE;ENCODING=QUOTED-PRINTABLE");
> What do you think ?

As long as none of the field names occurs in the data itself, this
will work. However, if for example, a given 'generalname' field has a
value of "My company", then the result for that field would be
N:My ORG
rather than the intended
N:My company

-e

Nicolas Syssoieff

unread,
Mar 4, 2008, 7:05:35 AM3/4/08
to Tiddl...@googlegroups.com
Hi Eric,

Yeah, you're right about this, but as the TW remains in English, and the code also, but the data is in French, there should be no such occurence and everything should be fine

Thanks for your insight on the subject.

Nicolas
Reply all
Reply to author
Forward
0 new messages