On Saturday, July 4, 2020 at 11:40:32 PM UTC-7, Jake wrote:
>
> I wrote this and it actually works:
> \define video(url, w:"400px", h:"300px")
> <iframe width="$w$" height="$h$" src="""$url$""" frameborder="0" 
> allowfullscreen></iframe>
> \end
> but I don't want to manually change 
> https://www.youtube.com/watch?v=VIDEOID to 
> https://www.youtube.com/embed/VIDEOID.  I think I already know what 
> should be done: "removeprefix" of "https://www.youtube.com/watch?v="; and 
> "addprefix" of "https://www.youtube.com/embed/"; if the whole url is 
> inserted or just addprefix if that's only VIDEOID, "but"... I don't know 
> what the proper syntax should be ¯\_(ツ)_/¯ .  (as well as I don't quite 
> understand that "triple quotes" thingy).
>

First, the "triple quotes" thingy:

When substituting the $url$ macro parameter value into the iframe, there is 
a possibility, no matter how remote, that the $url$ value could contain 
double-quote characters (").  In that case, if the iframe parameter was 
just src="...", then the substitution would result in nested or unmatched 
quotes (i.e., src="http://stuff?arg="quoted stuff"&more=123&..."), which 
would break the syntax since nested quotes are not handled by HTML 
parameters.  To address this, TiddlyWiki extends the normal HTML parameter 
syntax to enable use of tripled double-quotes as outer delimiters so that 
the enclosed text can contain instances of double-quotes without breaking.  
Note that the same nested quotes issue can also be resolved by using 
single-quotes (i.e., src='...' - supported by standard HTML) or even 
doubled squarebrackets (i.e., src=[[...]] - another TiddlyWiki syntax 
extension).

Next, how to do the removeprefix/addprefix filter:

In addition to the special quote handling described above, TiddlyWiki 
extends the normal HTML parameter syntax to allow use of "inline filters" 
(the tripled curly braces stuff).
To achieve your desired results, you would write the iframe's src="..." 
parameter like this:
src={{{ 
[[$url$]removeprefix[https://www.youtube.com/watch?v=]addprefix[https://www.youtube.com/embed/]]
 
}}}

The first part of the filter simply starts with the provided url as literal 
text (i.e, enclosed in square brackets).  Then, the removeprefix[...] 
filter operator is applied to that text, followed by the addprefix[...] 
filter operator.  The end result is the desired URL text, which is then 
used as the value of the HTML src=... parameter.  Note that the entire 
filter syntax is contained in a matched set of outer square brackets, which 
is then contained in the tripled curly braces that indicate use of an 
inline filter.

Using the inline filter, your macro definition would look like this:
\define video(url, w:"400px", h:"300px")
<iframe width="$w$" height="$h$" src={{{ 
[[$url$]removeprefix[https://www.youtube.com/watch?v=]addprefix[https://www.youtube.com/embed/]]
 
}}} frameborder="0" allowfullscreen></iframe>
\end

One more note... to make the code a bit easier to understand, you could use 
a couple of variables to define the values of the prefixes and the modified 
src URL, like this:
\define video(url, w:"400px", h:"300px")
<$vars old="https://www.youtube.com/watch?v="; new=
"https://www.youtube.com/embed/";>
<$vars src={{{ [[$url$]removeprefix<old>addprefix<new>] }}}>
<iframe width="$w$" height="$h$" src=<<src>> frameborder="0" allowfullscreen
></iframe>
</$vars>
</$vars>
\end

As you can see, TiddlyWiki filter syntax can be a bit confusing at first 
but it's also VERY powerful and can help you achieve lots of magical stuff.

enjoy,
-e

-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/310b1d09-d847-472b-9865-565c7daafedco%40googlegroups.com.

Reply via email to