Setting HTML attributes with a macro?

190 views
Skip to first unread message

RichardWilliamSmith

unread,
Oct 29, 2015, 5:09:21 AM10/29/15
to TiddlyWiki
Tiddlywiki is really twisting my brain again. I'm trying to make a one-click image exporter. At first I tried to do it by decoding the base64 tiddlers myself, using a javascript macro - I seemed to get something that was almost, but not quite, the right encoding... as I was reading for possible fixes, I hit on the idea of just using an HTML <a> element with the download attribute set to the title of the tiddler and the href set with the datauri macro.

This didn't work. As far as I can tell the crux of the problem is that content inside html tags is not wikified, so for example;

\define MyMacro(text)
this is hurting my $text$
<$set name="test" value="yo">
<<test>>
</$set>
\end
<a href=<<MyMacro "Brain">>><<MyMacro "Brain">></a>

The content of the a tag is set correctly ("this is hurting my brain yo") but the target of the link isn't wikified in the same way.

I think this is because the macro itself only performs variable substitution and relies on being returned into a context where wikification occurs. So, I can do this;

\define MyMacro(text)
this is hurting my $text$ $(test)$
\end
<$set name="test" value="yo">
<a href=<<MyMacro "Brain">>><<MyMacro "Brain">></a>
</$set>

And it works fine.

But when I try to "assign" the output of the datauri macro to a variable, so that I can use it like this, the result is ultimately never wikified

\define thisDataUri()
$(thisDataUri)$
\end
\define MyMacro(text)
<a href=<<thisDataUri>>>yo</a>
\end
\define MyOtherMacro()
{{!!title}}
\end
<$set name="thisDataUri" value=<<datauri [[Introduction Video Thumbnail.jpg]]>>>
<$set name="test" value="yo">
<<MyMacro>>
<<thisDataUri>>


</$set>
</$set> 
I have tried lots of different ways to solve this problem and now I'm spinning in circles a bit. It seemed that I should be able to use the macrocall widget to get around it but this can't be used as a parameter to another widget or macro, can it?

I'm left feeling like there is some core concept I'm still not understanding - can anybody offer any advice?

Regards,
Richard 

Eric Shulman

unread,
Oct 29, 2015, 7:13:36 AM10/29/15
to TiddlyWiki
On Thursday, October 29, 2015 at 2:09:21 AM UTC-7, RichardWilliamSmith wrote:
Tiddlywiki is really twisting my brain again. I'm trying to make a one-click image exporter. At first I tried to do it by decoding the base64 tiddlers myself, using a javascript macro - I seemed to get something that was almost, but not quite, the right encoding... as I was reading for possible fixes, I hit on the idea of just using an HTML <a> element with the download attribute set to the title of the tiddler and the href set with the datauri macro.

This didn't work.... As far as I can tell the crux of the problem is that content inside html tags is not wikified, so for example...

Try this.... it works (tested on TiddlyWiki.com):

\define makedatalink()
<a href=<<makedatauri type:"$(type)$" text:"$(text)$">> download="$(tid)$">$(label)$</a>
\end

\define datalink(label,tid)
<$vars label="$label$" tid="$tid$" type={{$tid$!!type}} text={{$tid$!!text}}>
<<makedatalink>>
</
$vars>
\end

<<datalink "download image" "Introduction Video Thumbnail.jpg">>

enjoy,
-e
Eric Shulman
ELS Design Studios
TiddlyTools - "Small Tools for Big Ideas!"
InsideTiddlyWiki: The Missing Manuals

YOUR DONATIONS ARE VERY IMPORTANT!
HELP ME TO HELP YOU - MAKE A CONTRIBUTION TO MY "TIP JAR"...

Professional TiddlyWiki Consulting Services...
Analysis, Design, and Custom Solutions:

RichardWilliamSmith

unread,
Oct 29, 2015, 7:50:11 AM10/29/15
to TiddlyWiki
Hi Eric,

A thousand thank yous. Your solution is very neat. Gratifyingly, I came back so soon to say that I found another way to do it also, by writing a javascript macro to piece the html string together (I assume this works because the whole thing is passed back into a context where wikitext parsing is active?). Mine looked like this...

/*\
title: $:/core/modules/macros/makedownloadlink.js
type: application/javascript
module-type: macro
Macro to make a data URI download link.
<<makedownloadlink text:"Text to be used for the download uri(href)" title:"title for download attribute" type:"text/vnd.tiddlywiki">>
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Information about this macro
*/
exports.name = "makedownloadlink";
exports.params = [
{name: "text"},
{name: "title"},
{name: "type"}
];
/*
Run the macro
*/
exports.run = function(text,title, type) {
var theUri = $tw.utils.makeDataUri(text,type);
var theString = "<a href=\"" + theUri +"\" download=\"" + title + "\" >{{$:/core/images/export-button}}</a>";
return theString;
};
})();
Your solution is better and was what I was trying to achieve at one point - thank you again, I will examine it more closely in the morning to figure out what I was doing wrong.

Regards,
Richard
Reply all
Reply to author
Forward
0 new messages