From e4f42bacc3b6b5180679cdb44f3efcd881c8a2fd Mon Sep 17 00:00:00 2001 From: EyeZiS Date: Sun, 4 Sep 2016 21:04:45 +0200 Subject: [PATCH] Actual fix for unicode not showing up correctly from xkcd --- otouto/plugins/xkcd.lua | 4 ++-- otouto/utilities.lua | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/otouto/plugins/xkcd.lua b/otouto/plugins/xkcd.lua index 4e54572..b75ba78 100644 --- a/otouto/plugins/xkcd.lua +++ b/otouto/plugins/xkcd.lua @@ -40,10 +40,10 @@ function xkcd:action(msg, config) else local data = JSON.decode(jstr) local output = string.format('*%s (*[%s](%s)*)*\n_%s_', - data.safe_title:gsub('*', '*\\**'), + utilities.fix_UTF8(data.safe_title):gsub('*', '*\\**'), data.num, data.img, - data.alt:gsub('_', '_\\__') + utilities.fix_UTF8(data.alt):gsub('_', '_\\__') ) utilities.send_message(msg.chat.id, output, false, nil, true) end diff --git a/otouto/utilities.lua b/otouto/utilities.lua index 47e6def..1e89a9c 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -321,4 +321,15 @@ utilities.style.enquote = function(title, body) return '*' .. title:gsub('*', '\\*') .. ':*\n"' .. utilities.md_escape(body) .. '"' end +-- Converts a string back into "proper" utf-8 from an ascii representation of utf-8 +-- it is only useful for fixing improper encoding caused by bad JSON escaping +function utilities.fix_UTF8(str) + local t = {} + for p, c in utf8.codes(str) do + table.insert(t, string.char(c)) + end + return table.concat(t) +end + + return utilities