diff --git a/include/tools/regexTools.h b/include/tools/regexTools.h index ef7a3a52b..50ce28647 100644 --- a/include/tools/regexTools.h +++ b/include/tools/regexTools.h @@ -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 diff --git a/src/server/response.cpp b/src/server/response.cpp index 8ed4cc23d..cb0d82304 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -1,5 +1,21 @@ - - +/* + * Copyright 2019 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ #include "response.h" #include "request_context.h" @@ -187,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_content); auto taskbar_part = render_template(RESOURCE::templates::taskbar_part_html, data); diff --git a/src/tools/regexTools.cpp b/src/tools/regexTools.cpp index 1b70198f1..72fc29aa8 100644 --- a/src/tools/regexTools.cpp +++ b/src/tools/regexTools.cpp @@ -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; +}