Better Kiwix Serve Taskbar insertion (after charset definition)

This commit is contained in:
Emmanuel Engelhart 2021-01-18 11:18:53 +01:00
parent a61c94ef10
commit a8bf9dd5b4
3 changed files with 24 additions and 2 deletions

View File

@ -29,5 +29,8 @@ std::string replaceRegex(const std::string& content,
std::string appendToFirstOccurence(const std::string& content,
const std::string& regex,
const std::string& replacement);
std::string prependToFirstOccurence(const std::string& content,
const std::string& regex,
const std::string& replacement);
#endif

View File

@ -203,9 +203,9 @@ void ContentResponse::introduce_taskbar()
data.set("title", m_bookTitle);
data.set("withlibrarybutton", m_withLibraryButton);
auto head_content = render_template(RESOURCE::templates::head_part_html, data);
m_content = appendToFirstOccurence(
m_content = prependToFirstOccurence(
m_content,
"<head[^>]*>",
"</head[^>]*>",
head_content);
auto taskbar_part = render_template(RESOURCE::templates::taskbar_part_html, data);

View File

@ -95,3 +95,22 @@ std::string appendToFirstOccurence(const std::string& content,
return content;
}
std::string prependToFirstOccurence(const std::string& content,
const std::string& regex,
const std::string& replacement)
{
ucnv_setDefaultName("UTF-8");
icu::UnicodeString ucontent(content.c_str());
icu::UnicodeString ureplacement(replacement.c_str());
auto matcher = buildMatcher(regex, ucontent);
if (matcher->find()) {
UErrorCode status = U_ZERO_ERROR;
ucontent.insert(matcher->start(status), ureplacement);
std::string tmp;
ucontent.toUTF8String(tmp);
return tmp;
}
return content;
}