The \percentifier in the code below makes sure that we can input e.g. Chinese characters into the first argument of e.g. \href, by converting strings into their percent-encoding using url_encode (for further reference, see also this answer to the OP "How to append a Chinese-character string argument from an environment to first argument of \href").
\percentifier:
\makeatletter
\directlua{function url_encode(str)
if (str) then
str = string.gsub (str, "\string\n", "\string\r\string\n")
str = string.gsub (str, "([^\@percentchar w \@percentchar -\@percentchar _\@percentchar .\@percentchar \string~])",
function (c) return string.format ("\@percentchar \@percentchar \@percentchar 02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end}
\makeatother
%
\def\percentifier#1{\directlua{
tex.print(url_encode('\detokenize{#1}'))
}}
\href:
\href{http://a.beautiful.url/?searchid=\percentifier{#1}}{Link}%
Another component of our code is the mainentry-environment. Let's assume we need such an environment in our answer. In reality, this environment will have multiple arguments, but of course I minimized/simplified it for the purpose of a MWE. Its first argument will always be used to complete an URL:
mainentry-environment:
\newenvironment {mainentry}
{\mainentryA}
%
{\clearpage}
%
\def\mainentryA#1{%
#1 \href{http://a.beautiful.url/?searchid=\percentifier{#1}}{Link}%
}{
}
In the MWE below, notice that the code works perfect for Test1 (since it is presented as Test1%. However, the code does not work well for Test2 (because Tex interprets a trailing space after it, which due to the url_encode adds a +).
Now, my intend is not to change the behavior of changing a space into a +, which should be done to have a properly functioning url_encode. Nevertheless, I do not want Tex to interpret the instance of Test2 as having a trailing space. So, perhaps the solution could be solved in the definition of \percentifier, or if not: in the definition of \mainentryA.
Please note I do NOT want to use Solution 1 or Solution 2 from this answer to another OP entitled "Why is this code outputting clickable URLs for some users, yet not for others?".
I would rather have a solution similar to this answer. Something using \cs_set_eq:NN\Trim\tl_trim_spaces:n or a similar construct. Somehow, I have not been able to do so.
MWE
\documentclass[a4paper]{article}
\usepackage{hyperref}
%%%%%%%
% \percentifier
\makeatletter
\directlua{function url_encode(str)
if (str) then
str = string.gsub (str, "\string\n", "\string\r\string\n")
str = string.gsub (str, "([^\@percentchar w \@percentchar -\@percentchar _\@percentchar .\@percentchar \string~])",
function (c) return string.format ("\@percentchar \@percentchar \@percentchar 02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end}
\makeatother
%
\def\percentifier#1{\directlua{
tex.print(url_encode('\detokenize{#1}'))
}}
%
%%%%%%%
%%%%%%%
% \mainentry
\newenvironment {mainentry}
{\mainentryA}
%
{\clearpage}
%
\def\mainentryA#1{%
#1 \href{http://a.beautiful.url/?searchid=\percentifier{#1}}{Link}%
}{
}
%
%%%%%%%
\begin{document}
\begin{mainentry}{%
Test1%
}
\end{mainentry}
\begin{mainentry}{%
Test2
}
\end{mainentry}
\end{document}
\def\removespacethenpercentify#1 \relax{\percentifier{#1}}and use\href{...\removespacethenpercentify#1\relax}{Link}. – Manuel Oct 13 '17 at 10:36