hyperref
hyperref copied to clipboard
\url default https protocol
Is there any chance of adding options to \url? I see \href has options, but not \url.
My issue that in TUGboat, as with many other journals, Barbara likes to drop the protocol to reduce url lengths. As in \url{tug.org}. Unfortunately this currently results in a local file link (file://localhost/tug.org), since it looks like a filename.
My goals are to 1) write the url in the source document only once and 2) not have to escape # and ~. As in \tuburl{example.org/~user/foo.html#section} would result in a link to https://example.org/~user/foo.html#section
Short of having \tuburl reset catcodes before calling \href (not even sure that would be sufficient), I can't devise a way to do this. Maybe one of you will have an idea?
What occurred to me is an option protocol= for \url, such that \url[protocol=https]{url} would be equivalent to \href{https://url}{url} Then I could have \def\tuburl{\url[protocol=https]} (Not necessarily using \def, but you get the idea.)
In the cases where https is incorrect, I'd specify the protocol explicitly, one way or another, i.e., not use \tuburl.
Although adding protocol= to \href would probably be pretty easy, sadly I don't think it suffices. \def\tuburl{\href[protocol=https]} wouldn't work because there's no way to double the argument url that I can see.
wdyt? --thanks, karl.
\documentclass{article}
\usepackage{hyperref}
\makeatletter
\DeclareRobustCommand*{\tuburl}{\hyper@normalise\tuburl@}
\def\tuburl@#1{\hyper@linkurl{\Hurl{#1}}{https://#1}}
\makeatother
\begin{document}
\tuburl{www.tug.org}
\tuburl{www.google.com/#searchform}
\end{document}
Or with optional argument:
\documentclass{article}
\usepackage{hyperref,xparse}
\makeatletter
\ExplSyntaxOn
\tl_new:N \l__hypurl_protocol_tl
\tl_new:N \l__hypurl_default_protocol_tl
\keys_define:nn {hyp / url}
{
protocol .choice:,
protocol / https .code:n = \tl_set:No \l__hypurl_protocol_tl {https://},
protocol / http .code:n = \tl_set:Nn \l__hypurl_protocol_tl {http://},
protocol / file .code:n = \tl_set:Nn \l__hypurl_protocol_tl {file://},
protocol / none .code:n = \tl_set:Nn \l__hypurl_protocol_tl {},
}
\keys_define:nn {hyp / urlsetup}
{
protocol-default .tl_set:N = \l__hypurl_default_protocol_tl,
protocol-default .initial:n = https
}
\NewDocumentCommand \opturl {O{}}
{
\exp_args:Nnx\keys_set:nn {hyp / url} {protocol=\l__hypurl_default_protocol_tl}
\keys_set:nn {hyp / url} {#1}
\hyper@normalise\opturl@
}
\cs_new:Npn \opturl@ #1 {\hyper@linkurl{\Hurl{#1}}{\l__hypurl_protocol_tl#1}}
\NewDocumentCommand \opturlsetup { m }
{
\keys_set:nn {hyp / urlsetup} { #1}
}
\ExplSyntaxOff
\makeatother
\begin{document}
\opturl{www.tug.org}
\opturl{www.google.com/#searchform}
\opturl[protocol=file]{test-utf8.log}
\opturlsetup{protocol-default=none}
\opturl{https://www.google.com/#searchform}
\end{document}
Wow. Thank you very much, Ulrike. \hyper@normalise, I'll have to try to remember that one : ).
I'm leaving this open, perhaps one can add the optional argument to url, or add a new command, but it needs some testing.
Hi again Ulrike. here's my next idea/request: I've been happily using \tbsurl and \tbhurl based on your code for a while now. But I've learned it would be helpful sometimes if the argument could optionally contain the leading protocol://, which then gets stripped off. This is for bib files, where it is nice to preserve the full url in the bib source, and yet I still don't want to print the protocol. In other words,
\tbsurl{https://foo.com} % same as \tbsurl{foo.com}
and same for \tbhurl and http://.
It's not important to me what happens if the protocol doesn't match - could be stripped, could be left alone (resulting in an invalid url), could be raised as an error. Whatever behavior is easiest.
For the archive, here are the definitions I've got now:
\DeclareRobustCommand*{\tbsurl}{\hyper@normalise\tbsurl@}%
\def\tbsurl@#1{\hyper@linkurl{\Hurl{#1}}{https://#1}}%
\DeclareRobustCommand*{\tbhurl}{\hyper@normalise\tbhurl@}
\def\tbhurl@#1{\hyper@linkurl{\Hurl{#1}}{http://#1}}%
Just thought I'd ask, in case it's easy for you :). Thanks!
Something like that could work. But better check the links and the catcodes ...
\documentclass{article}
\usepackage{hyperref}
\makeatletter
\DeclareRobustCommand*{\tuburl}{\hyper@normalise\tuburl@}
\ExplSyntaxOn
\def\tuburl@#1
{
\str_set:Nn\l_tmpa_str{#1}
\str_remove_once:Nn\l_tmpa_str{https://}
\expandafter\hyper@linkurl\expandafter{\expandafter\Hurl\expandafter{\l_tmpa_str}}{https://\l_tmpa_str}
}
\ExplSyntaxOff
\makeatother
\begin{document}
\tuburl{www.tug.org}
\tuburl{https://www.tug.org}
\tuburl{www.google.com/#searchform}
\tuburl{https://www.google.com/#searchform}
\tuburl{www.tug.org/~%#}
\tuburl{https://www.tug.org/~%#}
\end{document}
So far as I can tell, this works perfectly. Thank you!!
I'm sorry, Ulrike, but can I ask for one more refinement to this? Given \tuburl{http://foo} (given \tuburl definitions in your last reply here), don't insert the https, but just leave the url alone, so that it is valid after all? In your \expandafter\hyper@linkurl line above, I guess.
It turns out to be useful to \let\url\tbsurl before typesetting bibliographies, but occasionally there is an http url that cannot be changed, so it would be nice for this case to work after all.
I played around with the code for a little while, but failed. Sorry/thanks ...
Add a test:
\def\tuburl@#1
{
\str_set:Nn\l_tmpa_str{#1}
\str_if_in:NnTF \l_tmpa_str {http://}
{
\expandafter\hyper@linkurl
\expandafter{\expandafter\Hurl\expandafter{\l_tmpa_str}}{\l_tmpa_str}
}
{
\str_remove_once:Nn\l_tmpa_str{https://}
\expandafter\hyper@linkurl
\expandafter{\expandafter\Hurl\expandafter{\l_tmpa_str}}{https://\l_tmpa_str}
}
}
thanks very much.