From a55e8565d1f6b9d4eceba6f7f7863a66f4958c85 Mon Sep 17 00:00:00 2001 From: Naman Pahwa Date: Sun, 23 Feb 2025 00:19:21 +0530 Subject: [PATCH] Replace multiple comma-separated languages with 'mul' - Refactored language code handling to replace multiple comma-separated values with 'mul'. - Single-language entries remain unchanged. - created the helper function to get the lang tag - ensured the consistent behaviour for js and nojs version for the kiwix library view --- src/html_dumper.cpp | 29 +++++++++++++++++++++--- src/tools/stringTools.h | 3 +++ static/templates/no_js_library_page.html | 2 +- test/library_server.cpp | 6 ++--- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/html_dumper.cpp b/src/html_dumper.cpp index e0b7cdd7..d07d7375 100644 --- a/src/html_dumper.cpp +++ b/src/html_dumper.cpp @@ -27,6 +27,30 @@ std::string humanFriendlyTitle(std::string title) return humanFriendlyString; } +kainjow::mustache::object getLangTag(const std::vector& bookLanguages) { + std::string langShortString = ""; + std::string langFullString = "???"; + + //if more than 1 languages then show "mul" else show the language + if(bookLanguages.size() > 1) { + std::vector mulLanguages; + langShortString = "mul"; + for (const auto& lang : bookLanguages) { + const std::string fullLang = getLanguageSelfName(lang); + mulLanguages.push_back(fullLang); + } + langFullString = kiwix::join(mulLanguages, ","); + } else if(bookLanguages.size() == 1) { + langShortString = bookLanguages[0]; + langFullString = getLanguageSelfName(langShortString); + } + + kainjow::mustache::object langTag; + langTag["langShortString"] = langShortString; + langTag["langFullString"] = langFullString; + return langTag; +} + kainjow::mustache::list getTagList(std::string tags) { const auto tagsList = kiwix::split(tags, ";", true, false); @@ -72,17 +96,16 @@ std::string HTMLDumper::dumpPlainHTML(kiwix::Filter filter) const contentId = urlEncode(nameMapper->getNameForId(bookId)); } catch (...) {} const auto bookDescription = bookObj.getDescription(); - const auto langCode = bookObj.getCommaSeparatedLanguages(); const auto bookIconUrl = rootLocation + "/catalog/v2/illustration/" + bookId + "/?size=48"; const auto tags = bookObj.getTags(); const auto downloadAvailable = (bookObj.getUrl() != ""); + const auto langTagObj = getLangTag(bookObj.getLanguages()); std::string faviconAttr = "style=background-image:url(" + bookIconUrl + ")"; - booksData.push_back(kainjow::mustache::object{ {"id", contentId}, {"title", bookTitle}, {"description", bookDescription}, - {"langCode", langCode}, + {"langTag", langTagObj}, {"faviconAttr", faviconAttr}, {"tagList", getTagList(tags)}, {"downloadAvailable", downloadAvailable} diff --git a/src/tools/stringTools.h b/src/tools/stringTools.h index 97fa3473..4ddbfd6b 100644 --- a/src/tools/stringTools.h +++ b/src/tools/stringTools.h @@ -66,6 +66,9 @@ std::string ucAll(const std::string& word); std::string lcAll(const std::string& word); std::string ucFirst(const std::string& word); std::string lcFirst(const std::string& word); + +/* This function is broken, related Github issue + * https://github.com/kiwix/libkiwix/issues/1188 */ std::string toTitle(const std::string& word); std::string normalize(const std::string& word); diff --git a/static/templates/no_js_library_page.html b/static/templates/no_js_library_page.html index dd34a9b7..aaf61fa7 100644 --- a/static/templates/no_js_library_page.html +++ b/static/templates/no_js_library_page.html @@ -118,7 +118,7 @@
-
{{langCode}}
+
{{langTag.langShortString}}
{{#tagList}} {{tag}} diff --git a/test/library_server.cpp b/test/library_server.cpp index 5159ffb3..97ae5632 100644 --- a/test/library_server.cpp +++ b/test/library_server.cpp @@ -1251,7 +1251,7 @@ TEST_F(LibraryServerTest, no_name_mapper_catalog_v2_individual_entry_access) "
\n" \ " \n" \ "
\n" \ - "
fra
\n" \ + "
fra
\n" \ "
\n" \ " unittest\n" \ " wikipedia\n" \ @@ -1278,7 +1278,7 @@ TEST_F(LibraryServerTest, no_name_mapper_catalog_v2_individual_entry_access) "
\n" \ " \n" \ "
\n" \ - "
eng
\n" \ + "
eng
\n" \ "
\n" \ " public_tag_without_a_value\n" \ " wikipedia\n" \ @@ -1305,7 +1305,7 @@ TEST_F(LibraryServerTest, no_name_mapper_catalog_v2_individual_entry_access) "
\n" \ " \n" \ "
\n" \ - "
rus,eng
\n" \ + "
mul
\n" \ "
\n" \ " public_tag_with_a_value:value_of_a_public_tag\n" \ " wikipedia\n" \