Skip to content

Commit b22f478

Browse files
fix:revert back auto styling for indentations
1 parent 3ad99a1 commit b22f478

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/zimwriterfs/zimcreatorfs.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ struct GumboOutputDestructor {
260260
GumboOutput* output;
261261
};
262262

263+
inline bool isValidTitle(const std::string& title, size_t min_length)
264+
{
265+
static const std::regex alphanumeric_regex("[a-zA-Z0-9]");
266+
/* 1. Ensure the title has at least 3 characters */
267+
bool isLongEnough = title.length() >= min_length;
268+
/* 2. Ensure the title contains at least one alphanumeric character */
269+
/* (not all special chars) */
270+
bool hasAlphanumeric = std::regex_search(title, alphanumeric_regex);
271+
return isLongEnough && hasAlphanumeric;
272+
}
273+
263274
std::string ZimCreatorFS::parseAndAdaptHtml(std::string& data, std::string& title, const std::string& url)
264275
{
265276
GumboOutput* output = gumbo_parse(data.c_str());
@@ -331,6 +342,11 @@ std::string ZimCreatorFS::parseAndAdaptHtml(std::string& data, std::string& titl
331342
std::replace(title.begin(), title.end(), '_', ' ');
332343
}
333344
}
345+
if (!isValidTitle(title, 3)){
346+
std::cerr << "Warning: Generated title is invalid for URL: " << url
347+
<< std::endl;
348+
title = "Unknown title"; // or some other default title
349+
}
334350
}
335351
return "";
336352
}

test/zimwriterfs-zimcreatorfs.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,22 @@ TEST(ZimCreatorFSTest, ParseRedirect)
178178
}, std::runtime_error);
179179
}
180180
}
181+
182+
// verify the functionalities of isValidTitle function
183+
TEST(ZimCreatorFSTest, ValidateTitle)
184+
{
185+
EXPECT_TRUE(isValidTitle("Valid Title", 3));
186+
187+
EXPECT_FALSE(isValidTitle("", 3));
188+
189+
EXPECT_FALSE(isValidTitle("ab", 3));
190+
191+
EXPECT_TRUE(isValidTitle("Title123", 3));
192+
193+
EXPECT_TRUE(isValidTitle("Title!@#", 3));
194+
195+
EXPECT_TRUE(isValidTitle("Títlé123", 3));
196+
197+
EXPECT_FALSE(isValidTitle("!!!!!", 3));
198+
}
199+

0 commit comments

Comments
 (0)