How to append a Chinese-character string argument from an environment to first argument of \href

P.S.: The answered OP "Automatically convert unicode to percent encoding in href" might be of interest.

A good answer must be able to be:

• compiled using LuaLaTeX

The MWE below creates a great URL for the first instance, namely http://a.beautiful.url/?searchid=Test, but it doesn't do so in the second instance, where it merely creates http://a.beautiful.url/?searchid=.

Now, this is because the URL is not being internationalized. But please note that the URL will depend on the (first and only) argument of the mainentry environment. In a good answer, the argument of this environment must be:

• input using genuine Chinese characters, not their percent-encoding as one would do when converting them for an internationalized URL

So, what matters to me is that they are at least input using genuine Chinese characters, e.g.

even though they might need to be converted to percent-encoding when using them in http://a.beautiful.url/?searchid= e.g. into

%E7%96%8A%E9%9F%B3%E8%A9%9E

Lastly, a good answer will need to

• use a way of changing the inter-spacing between the Chinese characters throughout the document. In this case, we are using \ltjsetparameter{kanjiskip=1em} and \ltjsetparameter{xkanjiskip=1em} for that purpose. And thus we are bound to the package \usepackage{luatexja-fontspec}.

MWE

\documentclass[a4paper]{article}

\usepackage{hyperref}
\usepackage{xparse}

% Necessary for the fonts
\usepackage{luatexja-fontspec}
\setmainjfont{HanWangKaiMediumChuIn_wp010-08}
\ltjsetparameter{kanjiskip=1em}
\ltjsetparameter{xkanjiskip=1em}

\ExplSyntaxOn
\cs_set_eq:NN
\IfEmptyTF
\tl_if_blank:nTF
\ExplSyntaxOff

\NewDocumentEnvironment {mainentry} { m } {%
\href{http://a.beautiful.url/?searchid=#1}{Click on me}%
}

\begin{document}

Tester

\begin{mainentry}{%
Test%
}
\end{mainentry}

\begin{mainentry}{%

}
\end{mainentry}

\end{document}


Adapted from the answer by user David Carlisle and the comment by User guest in the OP "Automatically convert unicode to percent encoding in href"

\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}'))
}}


Modified code:

  \href{http://a.beautiful.url/?searchid=#1}{Click on me}%


into

  \href{http://a.beautiful.url/?searchid=\percentifier{#1}}{Link}%


MWE

\documentclass[a4paper]{article}

\usepackage{hyperref}
\usepackage{xparse}

%%%%%%%%%%%%%%%
% MODIFIED CODE
\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}'))
}}
% MODIFIED CODE
%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%
% Necessary for the fonts

\usepackage{luatexja-fontspec}
\setmainjfont{MingLiU}
\ltjsetparameter{kanjiskip=1em}
\ltjsetparameter{xkanjiskip=1em}
%%%%%%%%%%%%%%%

\ExplSyntaxOn
\cs_set_eq:NN
\IfEmptyTF
\tl_if_blank:nTF
\ExplSyntaxOff

\NewDocumentEnvironment {mainentry} { m } {%
}

\begin{document}

Tester

\begin{mainentry}{%
Test%
}
\end{mainentry}

\begin{mainentry}{%

}
\end{mainentry}

\end{document}