Representing hyperlinks in Pollen
#27
Open
opened 5 years ago by basus
·
15 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 was wondering how people represent hyperlinks in Pollen. Currently I have the following tag functions for general links, and internal (same site) links:
So I can write links as
◊link["https://link.url"]{Link Text}
. It's a little suboptimal since I have to wrap the URL in quotes. I also don't handle alt text or such. Any other ideas for how to represent links?The only fancy thing is have an option for links that should open in a new tab:
Ideally I'd like to avoid quoting the url part, but I haven't bothered too much with it
Extending what @ergl gives but for alt text:
You could use this pattern for any properties you liked.
Personally speaking, when I started writing text in Pollen I thought it would be nice not to need quotation marks around the url of links because it somehow feels sub-optimal, but I got used to it very quickly. Now that I'm used to the syntax it would feel strange not to have them in there.
Beware — despite the name,
text
is a list of X-expression elements. If it contains more than one item, or something other than a string (say, another tagged X-expression) youralt
attribute will be invalid.Since your attribute value needs to be a single string, you’d want to extract the strings within
text
and concatenate them, perhaps like so:(apply string-append (findf*-txexpr (cons '@ text) string?))
In my current project, I specify all links reference-style, e.g.:
The implementation can be seen here. It’s probably more complicated than most people want, but hey, there are no quote marks around the urls. (Never thought about that before to be honest!) I just like not having inline URLs breaking up the prose.
Under this implementation, you can put the url tag for a given identifier anywhere in the document, even before it is referenced. If you create a link for an identifier that has no corresponding url, a "Missing reference: [link-id]" message will be substituted for the URL. Conversely, creating a url that is never referenced will produce no output and no warnings or errors.
@otherjoel separating it into
link
andurl
is very clever! But personally I kinda dislike using number because it's not descriptive and consequently make it easy to get it wrong. I would personally write◊link[google-web]{Google it}
and wheregoogle-web
is a Racket identifier. With this way, we get a check that our identifier is indeed defined for free! The disadvantage though is that the urls must be defined at the top. Perhaps there's a way to makeurl
a macro that lifts itself up to the top?me too! this is similar to the way most latex packages handle referencing. I would urge everyone to include something akin to the HTML "alt" attribute for accessibility (assuming your output target format can handle it properly)
a side-question: what kind of error message would we get here? I've been thinking about validating/throwing errors in tag functions lately, and I wonder if it's possible to include the location of the error in the source file (i.e., the location of the tag in the source file, not of the location of the tag function in
pollen.rkt
). is this location information available to the tag function? if not, can it be made available?I thought of that too 😎 Since I run the identifier through format, you can use any stringish value including a symbol:
Your idea is interesting too. For this project I really like the simplicity/flexibility tradeoff I’ve already made though :-)
This reminds me of something I was going to post here. I’ll start a separate discussion for it.
One of the designs I still do not totally understand is how the content is passed in variadically (and this is actually due to Scribble). This limits us to only have one content block. It would be cool if we can have multiple content blocks like LaTeX:
◊link{https://google.com}{Google it}
is equivalent to(link '("https://google.com") '("Google it"))
.I suggested this long ago, to no avail.
Using Racket symbols instead of URLs enables something I've wanted from CMS' for a while: defining commonly used links outside of individual posts. So instead of
'google
being defined in the same file, it could be defined in separatelinks.rkt
file somewhere and inserted by thelink
function.@basus Note that
'google
is a symbol (note the preceding apostrophe), that is, a literal value; like the string"google"
or the number 2, it is not definable, it just is what it is. Beautiful Racket has good explainers on the difference between symbols and identifiers (names for variables).But you’re on the right track. If you don’t mind specifying each reference before it is used, you can use identifiers in place of strings for URLs, with no changes to your existing tag functions:
Or if you want to define common links in a separate file as you were saying:
Now your Pollen sources can use the definition of
google
or anything else inlinks.rkt
without defining it first.Ahh ok, that's interesting. I am still a Racket newbie, so not entirely clear on the various different datatypes and their representations. This might be too much overhead for most uses, but we'll see.
Can you clarify what you mean by "overhead"? Computationally, it's practically nothing.
To make things even simpler, you could leave out the
links.rkt
file and justdefine
the links inpollen.rkt
. Anythingprovide
d there is automatically available in your Pollen sources.I meant overhead for authors/users. I don't know if there are links I repeat often enough to make this approach worthwhile. Still cool to be able to do it though.