I can understand your wish for #
as the argument delimiter, but note that this is a hard to parse for character in TeX due to its nature as the parameter token.
Anyway, the following proposes 3 solutions to make this work.
\ghprDetok
would break any macros or special characters (read: non-ASCII in pdfTeX) in your argument, but is the fastest of them
\ghprEtl
is the second fastest (but stable)
\ghprRegex
is the slowest (but stable)
In none of the three do I check whether an #
was actually part of the argument (which would result in a low level TeX error in the \ghprDetok
and \ghprEtl
and lead to an empty numeric part in \ghprRegex
). If you need detection here I can add it for all three variants.
The \ghprEtl
variant reuses the internal of the \ghprDetok
, which wouldn't be necessary but it'd need an auxiliary so why not reuse the other one.
\documentclass{article}
\usepackage{hyperref}
\usepackage{etl}
\newcommand*\ghprfastest[2]
{\href{https://github.com/my-username/#1/pull/#2}{#1\##2}}%
\newcommand*\ghprDetok[1]
{\expandafter\ghprDetokAUX\detokenize{#1}\stop}
\expanded{\unexpanded{\def\ghprDetokAUX#1}\string#\string#}#2\stop
{\ghprfastest{#1}{#2}}
\ExplSyntaxOn
\cs_new_protected_nopar:Npn \ghprEtl #1
{%
\exp_last_unbraced:Ne \ghprDetokAUX
{ \etl_token_replace_once:nNe {#1} ## { \c_hash_str\c_hash_str } }
\stop
}
\cs_generate_variant:Nn \etl_token_replace_once:nNn { nNe }
\cs_new_protected_nopar:Npn \ghprRegex #1
{
\regex_split:nnN { \# } {#1} \l_tmpa_seq
\exp_args:Nee \ghprfastest
{ \seq_item:Nn \l_tmpa_seq 1 }
{ \seq_item:Nn \l_tmpa_seq 2 }
}
\ExplSyntaxOff
\begin{document}
\ghprfastest{myrepo}{1234}
\ghprDetok{myrepo#1234}
\ghprEtl{myrepo#1234}
\ghprRegex{myrepo#1234}
\end{document}
\ghpr{mathlib/pull/8105}
; I wanted to be able to write\ghpr{mathlib#8105}
and have the macro insert the/pull/
string for me.\newcommand\ghpr[2]{\href{https://github.com/my-username/#1/pull/#2}{#1\##2}
and then\ghpr{myrepo}{1234}
.#
between the parts. If you think that's a really bad idea (or know it's impossible), I'd also accept an answer explaining why.#
? It would be easy to do with another separator, if there's a suitable alternative which won't occur in the values. For example, it would easy to make it work for something like\ghpr{mathlib:8105}
or\ghpr{mathlib:8015,mathslib:8106,mathxlib:9834}
(for 3 links). Then you can just split the argument as the first step.