mirror of https://github.com/kiwix/libkiwix.git
Merge pull request #1105 from kiwix/string_slugification
Add String Slugification for Generating File Name
This commit is contained in:
commit
513a8d1383
|
@ -282,5 +282,14 @@ FeedCategories readCategoriesFromFeed(const std::string& content);
|
||||||
* @return full language name.
|
* @return full language name.
|
||||||
*/
|
*/
|
||||||
std::string getLanguageSelfName(const std::string& lang);
|
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
|
#endif // KIWIX_TOOLS_H
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
/* tell ICU where to find its dat file (tables) */
|
/* tell ICU where to find its dat file (tables) */
|
||||||
void kiwix::loadICUExternalTables()
|
void kiwix::loadICUExternalTables()
|
||||||
|
@ -439,3 +440,13 @@ template<>
|
||||||
std::string kiwix::extractFromString(const std::string& str) {
|
std::string kiwix::extractFromString(const std::string& str) {
|
||||||
return 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, "_");
|
||||||
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "../src/tools/stringTools.h"
|
#include "../src/tools/stringTools.h"
|
||||||
|
#include "../include/tools.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -170,4 +171,17 @@ TEST(stringTools, stripSuffix)
|
||||||
EXPECT_EQ(stripSuffix("abc123", "987"), "abc123");
|
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
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue