Skip to main content

Module href

Module href 

Source
Expand description

This module defines the href feature (Href), which provides three commands that insert a link to an address outside the document:

writtenlinks toshows
\href{https://example.com}{Click here}https://example.comClick here
\url{https://example.com/}https://example.com/example.com
\email{someone@example.com}mailto:someone@example.comsomeone@example.com

The first argument of each command is the address, as the author wrote it. The target of a link is the address to which the link points: for \email, the address preceded by mailto:, and for \href and \url, the address itself (HrefCommand::prefix). A link written with \href shows its second argument. A link written with \url or \email shows the display of its target, that is, the target without a leading http://, https:// or mailto:, and without trailing /, # and ? characters (display).

The three commands can be written only in text mode. The feature has no configuration. The language guide describes the commands for authors (hyperlinks).

§The address

The address argument is read as raw text up to the matching } (ArgStdKind::Verbatim). Every character before this brace is part of the address, including %, #, \, ~ and nested braces. For instance, \url{https://example.com/a%20b#top} links to https://example.com/a%20b#top.

In the template of a user definition, a placeholder in an address is replaced by the corresponding argument of the call, and a literal # is written ##, as everywhere in a template. For instance, if the template of \doi is \href{https://doi.org/#1}{#1}, the call \doi{10.1000/xyz} links to https://doi.org/10.1000/xyz. The argument of the call must be plain text. Otherwise, the error AddressNotCharacters is reported and no link is made.

The feature does not validate or normalize addresses. A backend can refuse an address when it writes a link. For instance, the HTML backend refuses an address whose scheme is not allowed by the render option HtmlAllowedUrlSchemes. The HTML backend then writes the link without an href attribute, and reports the error flm.html.refused-address.

§The element and the lowering rules

The three commands share one element type, LINK_ELEMENT (flm.href.link). Each command has its own element value, whose data is an HrefCommand.

The feature registers two lowering rules for this element type:

  • The default rule RULE (LinkRule) is used on every backend except latex-recomposed. It lowers a link to a flm.pres.link node of kind LINK_KIND (url), which points to the target of the link. The node contains the text argument of \href, or the display of the target for \url and \email.
  • The rule latex::LATEX_RULE is used on the latex-recomposed backend. It writes each link as a call of a LaTeX hook, that is, a macro that is defined in the style file and that a user can redefine: \flmHref{address}{text}, \flmUrl{address} or \flmEmail{address}. The hooks \flmUrl and \flmEmail compute the display in LaTeX, with the same result as display. See the module latex.

Each backend writes a flm.pres.link node in its own format. For instance, the HTML backend writes an <a> element whose href attribute is the target (hyperlinks in HTML). The text backend writes what the link shows, followed by the target between angle brackets, as in Click here <https://example.com>. By default, the text backend omits the target after a link written with \url or \email, since such a link already shows its address. The render options link_addresses and link_addresses_when_shown change this behavior. The text backend recognizes such a link by the field LinkPayload::shows_target, which the default rule sets for \url and \email.

Modules§

latex
The hyperlinks on the latex-recomposed backend: each command of the href feature written back as a LaTeX hook, a macro that the recomposed LaTeX writes and the style file defines with a plain default, which a user redefines in the document’s preamble (latex::preamble; see the LaTeX hooks).

Structs§

AddressNotCharacters
The error that is reported when the address of \href, \url or \email is not plain text (flm.href.address-not-characters).
Href
The href feature, which provides the commands \href, \url and \email for links to addresses outside the document. See the module documentation.
LinkRule
The implementation of the default lowering rule RULE of \href, \url and \email.

Enums§

HrefCommand
The command with which a link is written: \href, \url or \email.

Constants§

ADDRESS_ARG
The name of the address argument, address.
ADDRESS_ARG_ROLE
The name of the argument role flm.href.address, which marks an argument that contains the address of a link. The address argument ADDRESS_ARG of \href, \url and \email has this role (ArgDef::role).
DISPLAY_PREFIXES
The prefixes that display removes from the start of a link target: http://, https:// and mailto:. At most one prefix is removed.
DISPLAY_TRAILING
The characters that display removes from the end of a link target: /, # and ?. All of these characters at the end of the target are removed, in any number and in any order.
FEATURE
The feature’s registered name, href.
LINK_ELEMENT
The name of the element type of \href, \url and \email, flm.href.link. Each of the three commands has its own element value of this type, whose data is an HrefCommand.
LINK_KIND
The link kind of the links that the default rule RULE builds, url (LinkPayload::kind). This kind marks a link to an address outside the document. The HTML backend writes it as the attribute data-flm-kind="url".
RULE
The name of the default lowering rule of LINK_ELEMENT, flm.href.link.lower.default. The rule is implemented by LinkRule, and is used on every backend except latex-recomposed. On the latex-recomposed backend, the rule latex::LATEX_RULE is used instead.
TEXT_ARG
The name of the text argument of \href, text.

Functions§

display
Returns the display of the link target target, that is, the text that a link written with \url or \email shows.
feature
Returns the Href feature, ready to be listed in a package entry.
rule
Returns the default lowering rule RULE of LINK_ELEMENT, implemented by LinkRule.