- // the chars_url[] characters must be handled for both, url and href
- for (int k = 0; k < 2; k++) {
+ // Use URI/URL-style percent-encoded string (hexadecimal).
+ // We exclude some characters that must not be transformed
+ // in hrefs: % # / : ? = & ! * ' ( ) ; @ + $ , [ ]
+ // or that we need to treat manually: \.
+ url = to_percent_encoding(url, from_ascii("%#\\/:?=&!*'();@+$,[]"));
+ // We handle \ manually since \\ is valid
+ for (size_t i = 0, pos;
+ (pos = url.find('\\', i)) != string::npos;
+ i = pos + 2) {
+ if (url[pos + 1] != '\\')
+ url.replace(pos, 1, from_ascii("%5C"));
+ }
+
+ // The characters in chars_url[] need to be escaped in the url
+ // field because otherwise LaTeX will fail when the hyperlink is
+ // within an argument of another command, e.g. in a \footnote. It
+ // is important that they are escaped as "\#" and not as "\#{}".
+ // FIXME this is not necessary in outside of commands.
+ for (int k = 0; k < 2; k++)