Creating a new linktag

13 views
Skip to first unread message

Kashgarinn

unread,
Jun 6, 2007, 5:03:50 AM6/6/07
to TiddlyWiki
Greetings.

I was wondering if I could ask you how to change the [[file.xls]] tag,
of course in the ease and comfort of a plugin so that I can import it
into a new wiki when I upgrade it in the future :D

There are two things I'd like it to do.. I'd like it to accept
general windows links to files, like [[C:\program files\user\my
documents\test.xls]] or links to network drives: [[o:\office\documents
\2007\test.xls]] or if the file has been uploaded to a file directory
on the server it could link to there like this: [[documents\test.xls]]

This would be so that people who are used to the windows links can
just copy/paste from an explorer window instead of having to use
[[file:///o:/silly/stupid/test.xls]]

Of course, being able to drag and drop the file from an explorer
window into the edit window, and it would automatically copy the whole
link location to the edit window where the pointer is would be just..
brilliant.. not sure if that's at all possible though.

I'll try to investigate it myself, but if you know of a simple way to
do this (in a plugin), that'd be great.

K.

FND

unread,
Jun 6, 2007, 8:41:17 AM6/6/07
to Tiddl...@googlegroups.com
This would be so that people who are used to the windows links can
just copy/paste from an explorer window instead of having to use
[[file:///o:/silly/stupid/test.xls]]
You could use a macro like this one:
//{{{
config.macros.fileLink = {};
config.macros.fileLink.handler = function(place, macroName, params) {
    var filePath = params[0].replace("\\", "/") // convert path separators to Unix format
    wikify("[[" + params[1] + "|file:///" + filePath + "]]", place);
};
//}}}

Paste that code into a new tiddler (e.g. "FileLinkMacro") and tag it with "systemConfig", then reload your TiddlyWiki.

Then you can use <<fileLink "c:\MyFile.txt" "My File">> to automatically generate a link like [[My File|file///c:/MyFile.txt]].
(That code is largely untested and could be expanded to automatically set a link title if none has been specified - nevertheless, it should work fine.)

HTH.


-- F.

Kashgarinn

unread,
Jun 7, 2007, 6:08:44 AM6/7/07
to TiddlyWiki
Thanks for your input FND, very much appreciated :D

I changed the macro name to FL instead of fileLink, the macro is too
hard to use though in my humble opinion.

I would rather want to change the [[]] to suit my needs, is that not
possible as a plugin?

[[file description|file link]] is just alot easier than :
<<FL "file link" "file decription">> because instead of 5 things
screwing up, you got 10 things screwing up, if you're confused about
what I mean, I'll "" all the things I mean:
[[file description|file link]] = 5 things:
1) "[["
2) "file description"
3) "|"
4) "file link"
5) "]]"

<<FL "file link" "file description">>
1) "<<"
2) "FL"
3) """
4) "file link"
5) """
6) """
7) "file description"
8) """
9) ">>"
10) the spaces between the commands

I will use the filelink macro though, as it does do what I want it to
do (and maybe it's just a matter of getting used to it), so again
thanks alot for your input :)

K.

FND

unread,
Jun 7, 2007, 6:39:44 AM6/7/07
to Tiddl...@googlegroups.com
> I changed the macro name to FL instead of fileLink, the macro is too
> hard to use though in my humble opinion.
> I would rather want to change the [[]] to suit my needs, is that not

I get your point - but I'm afraid there's not much I can do about that.

Nevertheless, the macro syntax is quite straightforward; you have the
<<macroDelimiters>> (corresponding to [[linkBrackets]]) and simply need
to add the macro name and parameters (enclosed in quotes if there's a
space in the parameter value):
<<name "parameter 1" "parameter 2">>
vs.
[[parameter 1|parameter 2]]
So it's not really much different from the PrettyLinks syntax, actually.

What you could do, though, is change the way the parameters are handled,
so you'd use something like
<<name parameter 1|parameter 2>>
You'd just have to untangle the parameter string accordingly.

Of course you could also try hijacking the prettyLink function and
adding some sort of pattern recognition - e.g. something like (in
pseudo-code):
if(URL like /^\w:\\/) { // paths starting with a drive, like "C:\"
URL = "file:///" + URL;)
}
In fact, if you pull that off and it works without any issues or risks,
JR *might* even be willing to include it in the core...


-- F.

Daniel Baird

unread,
Jun 7, 2007, 7:58:39 PM6/7/07
to Tiddl...@googlegroups.com
On 07/06/07, FND <Ace_...@gmx.net> wrote:
> [...]

> Of course you could also try hijacking the prettyLink function and
> adding some sort of pattern recognition - e.g. something like (in
> pseudo-code):
> if(URL like /^\w:\\/) { // paths starting with a drive, like "C:\"
> URL = "file:///" + URL;)
> }
> In fact, if you pull that off and it works without any issues or risks,
> JR *might* even be willing to include it in the core...

Nice idea. Get going on that, Kashgarinn :)

;D

--
Daniel Baird
http://tiddlyspot.com (free, effortless TiddlyWiki hosting)
http://danielbaird.com (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things That Suck)

FND

unread,
Jun 8, 2007, 2:39:58 AM6/8/07
to Tiddl...@googlegroups.com
> Nice idea. Get going on that, Kashgarinn :)

He might not have to:
In config.formatters, there's a section "name: 'prettyLink'".
Below the following lines:

// Pretty bracketted link
var link = lookaheadMatch[3];

I've added (in the actual source code, as I'm not sure how to
hijack/overwrite this):

// check for Windows file path (e.g. "C:\")
if(link.search(/^\w:\\/) != -1) {
link = "file:///" + link;
}

That should do it (though the RegEx is not heavily tested, so this
*might* be incomplete or even buggy).

Maybe I should move this to [twdev]...


-- F.

Kashgarinn

unread,
Jun 8, 2007, 6:40:37 AM6/8/07
to TiddlyWiki
You're talking about this right?:
----
name: "prettyLink",
match: "\\[\\[",
lookaheadRegExp: /\[\[(.*?)(?:\|(~)?(.*?))?\]\]/mg,
handler: function(w)
{
this.lookaheadRegExp.lastIndex = w.matchStart;
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
if(lookaheadMatch && lookaheadMatch.index == w.matchStart)
{
var e;
var text = lookaheadMatch[1];
if(lookaheadMatch[3])
{

// Pretty bracketted link
var link = lookaheadMatch[3];
e = (!lookaheadMatch[2] &&
config.formatterHelpers.isExternalLink(link))
? createExternalLink(w.output,link)
: createTiddlyLink(w.output,link,false,null,w.isStatic);
}
else
{
// Simple bracketted link
e = createTiddlyLink(w.output,text,false,null,w.isStatic);
}
createTiddlyText(e,text);
w.nextMatch = this.lookaheadRegExp.lastIndex;
}
}
-----

I added the code into my TW just below the var link =
lookaheadMatch[3]; , and it works.

a bit off-topic, I can only verify that it works in Internet explorer,
not firefox as I somehow was able to kill external links working in
firefox (mozilla as well.. somehow just a blank page pops up.. I'll
reinstall CCTW when it has been upgraded to the new version)

Thanks for that btw, not the solution I was looking for, but it works,
so I hope it'll get added to the core code.

K.

Xavier Vergés

unread,
Jun 8, 2007, 7:22:22 AM6/8/07
to TiddlyWiki
A possible and completely different approach that should be very easy
to code: how about a script placed in the "send to" folder that would
build the link and copy it to the clipboard? Now that I think of it...
I really need this asap!! :-)

On Jun 6, 11:03 am, Kashgarinn <steint...@gmail.com> wrote:

> Of course, being able to drag and drop the file from an explorer
> window into the edit window, and it would automatically copy the whole
> link location to the edit window where the pointer is would be just..
> brilliant.. not sure if that's at all possible though.
>

-Xavier

FND

unread,
Jun 8, 2007, 8:13:24 AM6/8/07
to Tiddl...@googlegroups.com
> You're talking about this right?:
> [snip]

> I added the code into my TW just below the var link =
> lookaheadMatch[3]; , and it works.

Yup, that should be it.
I've posted this issue on [twdev]; I'll get back to you if/when there's
news to report.

> not the solution I was looking for, but it works

It's not the solution you were looking for?
what did you have in mind then, short of the drag&drop thing (which
won't work with a simple text box)!?

> I can only verify that it works in Internet explorer, not firefox

Works fine for me in FF - not sure how you could have messed that up
though, sorry...


-- F.


FND

unread,
Jun 8, 2007, 9:16:14 AM6/8/07
to Tiddl...@googlegroups.com
> A possible and completely different approach that should be very easy
> to code: how about a script placed in the "send to" folder that would
> build the link and copy it to the clipboard?

http://www.autohotkey.com/forum/viewtopic.php?t=19907


-- F.

Jeremy Ruston

unread,
Jun 11, 2007, 4:51:41 AM6/11/07
to Tiddl...@googlegroups.com
Excellent stuff, thanks. I think autoconverting C:/-style links to
file:/// format is sensible. Let's leave it until after 2.2.2, though.
It would be very helpful if you could create a ticket and attach a
patch.

Cheers

Jeremy


--
Jeremy Ruston
mailto:jer...@osmosoft.com
http://www.tiddlywiki.com

FND

unread,
Jun 11, 2007, 5:34:29 AM6/11/07
to Tiddl...@googlegroups.com
> It would be very helpful if you could create a ticket and attach a
> patch.

Done: ticket #348 (http://trac.tiddlywiki.org/ticket/348)

That patch will also convert network paths ("\\MyServer\MyFile.txt").

Please note that the regular expressions used here are not heavily
tested, so this *might* be incomplete or even buggy.


-- F.

Jeremy Ruston

unread,
Jun 11, 2007, 11:29:13 AM6/11/07
to Tiddl...@googlegroups.com
That's great, thanks very much for your help.

Cheers

Jeremy

FND

unread,
Jun 11, 2007, 11:41:46 AM6/11/07
to Tiddl...@googlegroups.com
> That's great, thanks very much for your help.

This was an easy one - and I'm glad I could give something back...
(... which I'm working on in the "Suppress an empty tags panel?" thread,
though I'm unsure whether that's core-worthy.)


-- F.

Xavier Vergés

unread,
Aug 3, 2007, 7:31:06 PM8/3/07
to TiddlyWiki
Thanks --F.
Since I'm autohotkey impaired, I took the the same approach using
an .hta file. Details here:
http://xdexavier.blogspot.com/2007/08/sendto-clipboard-coolness-tiddlywiki.html

-Xavier

Reply all
Reply to author
Forward
0 new messages