fix: Wikipedia article links use _ for spaces (0.43.2)

This commit is contained in:
Johannes Findeisen 2026-07-26 02:07:13 +02:00
commit f79e4d2cd5
3 changed files with 40 additions and 3 deletions

View file

@ -1,3 +1,7 @@
2026.07.26 - Fix Wikipedia article links using wrong space encoding. (0.43.2)
- Fixed Wikipedia article links in `!wp` responses using `+` instead of `_` for spaces (e.g. `CLever+Audio+Plug-in``CLever_Audio_Plug-in`). The `wp_url_encode()` function was used for both API query parameters (where `+` is correct) and article title URLs (where Wikipedia expects `_`). Added `wp_url_encode_title()` that encodes spaces as underscores for use in article link construction, and switched all article link encodings to use it.
2026.07.26 - Improved Wikipedia results formatting and article extracts. (0.43.1)
- Changed multi-result mode from a single pipe-separated message to individual messages: each result is now sent as its own IRC message (`Title — snippet`) followed by a separate "Read full article: LINK" message. This makes results easier to read in busy channels and ensures each article link is immediately visible.

View file

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.13)
project(zynk VERSION 0.43.1 LANGUAGES C)
project(zynk VERSION 0.43.2 LANGUAGES C)
# Prefer C99; the code is compatible with C99/C11
set(CMAKE_C_STANDARD 99)

View file

@ -73,6 +73,39 @@ static void wp_url_encode(const char *src, char *dst, size_t dstsz) {
dst[di] = '\0';
}
/*
* wp_url_encode_title - Percent-encode a Wikipedia article title for use in a URL path.
*
* Like wp_url_encode() but encodes spaces as underscores instead of plus signs,
* matching Wikipedia's URL convention for article titles (e.g. "CLever Audio
* Plug-in" becomes "CLever_Audio_Plug-in" instead of "CLever+Audio+Plug-in").
*
* Parameters:
* src - The article title to encode.
* dst - Output buffer for the encoded string.
* dstsz - Size of the output buffer.
*
* Returns: void (output is written to dst, NUL-terminated).
*/
static void wp_url_encode_title(const char *src, char *dst, size_t dstsz) {
static const char hex[] = "0123456789ABCDEF";
size_t di = 0;
for (; *src && di + 4 < dstsz; src++) {
unsigned char c = (unsigned char)*src;
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
dst[di++] = c;
} else if (c == ' ') {
dst[di++] = '_';
} else {
if (di + 3 >= dstsz) break;
dst[di++] = '%';
dst[di++] = hex[c >> 4];
dst[di++] = hex[c & 0x0F];
}
}
dst[di] = '\0';
}
/*
* wp_decode_json_string - Decode JSON string escape sequences in-place.
*
@ -310,7 +343,7 @@ int cmd_wp(Session *s, const char *msg, const char *reply_target, const char *sr
return 1;
}
char enc_title[512];
wp_url_encode(title, enc_title, sizeof enc_title);
wp_url_encode_title(title, enc_title, sizeof enc_title);
char extract_url[1024];
snprintf(extract_url, sizeof extract_url,
"https://en.wikipedia.org/w/api.php?action=query&titles=%s&prop=extracts&explaintext=true&exchars=800&format=json",
@ -361,7 +394,7 @@ int cmd_wp(Session *s, const char *msg, const char *reply_target, const char *sr
wp_collapse_spaces(snippet_raw);
if (!title[0]) continue;
char enc_title[512];
wp_url_encode(title, enc_title, sizeof enc_title);
wp_url_encode_title(title, enc_title, sizeof enc_title);
char line[1024];
if (snippet_raw[0]) {
char snippet_short[256];