Merge pull request #1105 from kiwix/string_slugification

Add String Slugification for Generating File Name
This commit is contained in:
Kelson 2024-08-14 20:24:08 +00:00 committed by GitHub
commit 513a8d1383
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 0 deletions

View File

@ -282,5 +282,14 @@ FeedCategories readCategoriesFromFeed(const std::string& content);
* @return full language name.
*/
std::string getLanguageSelfName(const std::string& lang);
/**
* Slugifies the filename by converting any characters reserved by the operating
* system to '_'. Note filename is only the file name and not a path.
*
* @param filename Valid UTF-8 encoded file name string.
* @return slugified string.
*/
std::string getSlugifiedFileName(const std::string& filename);
}
#endif // KIWIX_TOOLS_H

View File

@ -32,6 +32,7 @@
#include <iostream>
#include <iomanip>
#include <regex>
/* tell ICU where to find its dat file (tables) */
void kiwix::loadICUExternalTables()
@ -439,3 +440,13 @@ template<>
std::string kiwix::extractFromString(const std::string& str) {
return str;
}
std::string kiwix::getSlugifiedFileName(const std::string& filename)
{
#ifdef _WIN32
const std::regex reservedCharsReg(R"([<>:"/\\|?*])");
#else
const std::regex reservedCharsReg("/");
#endif
return std::regex_replace(filename, reservedCharsReg, "_");
}

View File

@ -19,6 +19,7 @@
#include "gtest/gtest.h"
#include "../src/tools/stringTools.h"
#include "../include/tools.h"
#include <string>
#include <vector>
@ -170,4 +171,17 @@ TEST(stringTools, stripSuffix)
EXPECT_EQ(stripSuffix("abc123", "987"), "abc123");
}
TEST(stringTools, getSlugifiedFileName)
{
EXPECT_EQ(getSlugifiedFileName("abc123.png"), "abc123.png");
EXPECT_EQ(getSlugifiedFileName("/"), "_");
EXPECT_EQ(getSlugifiedFileName("abc/123.pdf"), "abc_123.pdf");
EXPECT_EQ(getSlugifiedFileName("abc//123.yaml"), "abc__123.yaml");
EXPECT_EQ(getSlugifiedFileName("//abc//123//"), "__abc__123__");
#ifdef _WIN32
EXPECT_EQ(getSlugifiedFileName(R"(<>:"/\\|?*)"), "__________");
EXPECT_EQ(getSlugifiedFileName(R"(<abc>:"/123\\|?*<.txt>)"), "_abc____123______.txt_");
#endif
}
};