Multiple arguments to tag functions?
#37
Closed
opened 10 years ago by basus
·
2 comments
Loading…
Reference in New Issue
There is no content yet.
Delete Branch '%!s(<nil>)'
Deleting a branch is permanent. It CANNOT be undone. Continue?
I've been playing around with ways to implement hyperlinks using tag functions. I think the most generic way would be something like
◊a['href:"url"]{text}
, but I'd like to make some custom tags for things like internal links, links to particular domains, etc. Ideally I'd want to write something like◊internal[url]{text}
that clearly separates out the URL from the link text. Is there a simple way to go about doing this? I can't tell what the rules are for splitting the tag contents into the list that gets passed to the tag function.The doc entry on "The relationship of text mode and Racket mode" will be helpful. In short, the arguments in a text-mode command like this:
◊internal[url]{text}
Get converted into this Racket-mode command:
(internal url text)
So the tag function that corresponds to this would look like:
The wrinkle is that in text mode, the argument between curly braces — in this case,
{text}
— can actually resolve into multiple arguments (for instance, if it contains nested tags, or multiple lines of text), so what you actually get is this:(internal url text text2 text3 ...)
So while the tag function above will work with a single text argument, the better habit is to write tag functions with a rest argument, like so:
The rest argument is a list of all the arguments coming after the other named arguments. So in this case, whether
{text}
resolves to one text argument or a hundred, they will all be wrapped in a single list of arguments calledtext-args
.For more about this, see "Multiple input values & rest arguments" in the Pollen docs and "Declaring a rest argument" in the Racket guide.
BTW the shorthand
◊a['href:"url"]{text}
is mostly intended for when you're using a default tag function and just want a simple way of stuffing some attributes into the tag. If you're intending to make a custom tag function, it's not necessary (and may in fact be more cumbersome).Ahh... thanks. I just didn't read the text-mode/Racket-mode chapter closely enough and assumed that the first argument was always parsed as attributes.