-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #897 from kiwix/nojs
A gift to javascript naysayers
- Loading branch information
Showing
25 changed files
with
996 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2023 Nikhil Tanwar <2002nikhiltanwar@gmail.com> | ||
* | ||
* 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. | ||
*/ | ||
|
||
#ifndef KIWIX_HTML_DUMPER_H | ||
#define KIWIX_HTML_DUMPER_H | ||
|
||
#include <string> | ||
|
||
#include "library_dumper.h" | ||
|
||
namespace kiwix | ||
{ | ||
|
||
/** | ||
* A class to dump Library in HTML format. | ||
*/ | ||
class HTMLDumper : public LibraryDumper | ||
{ | ||
public: | ||
HTMLDumper(const Library* library, const NameMapper* NameMapper); | ||
~HTMLDumper(); | ||
|
||
|
||
/** | ||
* Dump library in HTML | ||
* | ||
* @return HTML content | ||
*/ | ||
std::string dumpPlainHTML(kiwix::Filter filter) const; | ||
}; | ||
|
||
} | ||
|
||
#endif // KIWIX_HTML_DUMPER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2023 Nikhil Tanwar <2002nikhiltanwar@gmail.com> | ||
* Copyright 2017 Matthieu Gautier <mgautier@kymeria.fr> | ||
* | ||
* 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. | ||
*/ | ||
|
||
#ifndef KIWIX_LIBRARY_DUMPER_H | ||
#define KIWIX_LIBRARY_DUMPER_H | ||
|
||
#include <string> | ||
|
||
|
||
#include "library.h" | ||
#include "name_mapper.h" | ||
#include <mustache.hpp> | ||
|
||
namespace kiwix | ||
{ | ||
|
||
/** | ||
* A base class to dump Library in various formats. | ||
* | ||
*/ | ||
class LibraryDumper | ||
{ | ||
public: | ||
LibraryDumper(const Library* library, const NameMapper* NameMapper); | ||
~LibraryDumper(); | ||
|
||
void setLibraryId(const std::string& id) { this->libraryId = id;} | ||
|
||
/** | ||
* Set the root location used when generating url. | ||
* | ||
* @param rootLocation the root location to use. | ||
*/ | ||
void setRootLocation(const std::string& rootLocation) { this->rootLocation = rootLocation; } | ||
|
||
/** | ||
* Set some informations about the search results. | ||
* | ||
* @param totalResult the total number of results of the search. | ||
* @param startIndex the start index of the result. | ||
* @param count the number of result of the current set (or page). | ||
*/ | ||
void setOpenSearchInfo(int totalResult, int startIndex, int count); | ||
|
||
/** | ||
* Sets user default language | ||
* | ||
* @param userLang the user language to be set | ||
*/ | ||
void setUserLanguage(std::string userLang) { this->m_userLang = userLang; } | ||
|
||
/** | ||
* Get the data of categories | ||
*/ | ||
kainjow::mustache::list getCategoryData() const; | ||
|
||
/** | ||
* Get the data of languages | ||
*/ | ||
kainjow::mustache::list getLanguageData() const; | ||
|
||
protected: | ||
const kiwix::Library* const library; | ||
const kiwix::NameMapper* const nameMapper; | ||
std::string libraryId; | ||
std::string rootLocation; | ||
std::string m_userLang; | ||
int m_totalResults; | ||
int m_startIndex; | ||
int m_count; | ||
}; | ||
} | ||
|
||
#endif // KIWIX_LIBRARY_DUMPER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#include "html_dumper.h" | ||
#include "libkiwix-resources.h" | ||
#include "tools/otherTools.h" | ||
#include "tools.h" | ||
#include "tools/regexTools.h" | ||
#include "server/i18n.h" | ||
|
||
namespace kiwix | ||
{ | ||
|
||
/* Constructor */ | ||
HTMLDumper::HTMLDumper(const Library* library, const NameMapper* nameMapper) | ||
: LibraryDumper(library, nameMapper) | ||
{ | ||
} | ||
/* Destructor */ | ||
HTMLDumper::~HTMLDumper() | ||
{ | ||
} | ||
|
||
namespace { | ||
|
||
std::string humanFriendlyTitle(std::string title) | ||
{ | ||
std::string humanFriendlyString = replaceRegex(title, "_", " "); | ||
humanFriendlyString[0] = toupper(humanFriendlyString[0]); | ||
return humanFriendlyString; | ||
} | ||
|
||
kainjow::mustache::list getTagList(std::string tags) | ||
{ | ||
const auto tagsList = kiwix::split(tags, ";", true, false); | ||
kainjow::mustache::list finalTagList; | ||
for (auto tag : tagsList) { | ||
if (tag[0] != '_') | ||
finalTagList.push_back(kainjow::mustache::object{ | ||
{"tag", tag} | ||
}); | ||
} | ||
return finalTagList; | ||
} | ||
|
||
} // unnamed namespace | ||
|
||
std::string HTMLDumper::dumpPlainHTML(kiwix::Filter filter) const | ||
{ | ||
kainjow::mustache::list booksData; | ||
const auto filteredBooks = library->filter(filter); | ||
const auto searchQuery = filter.getQuery(); | ||
auto languages = getLanguageData(); | ||
auto categories = getCategoryData(); | ||
|
||
for (auto &category : categories) { | ||
const auto categoryName = category.get("name")->string_value(); | ||
if (categoryName == filter.getCategory()) { | ||
category["selected"] = true; | ||
} | ||
category["hf_name"] = humanFriendlyTitle(categoryName); | ||
} | ||
|
||
for (auto &language : languages) { | ||
if (language.get("lang_code")->string_value() == filter.getLang()) { | ||
language["selected"] = true; | ||
} | ||
} | ||
|
||
for ( const auto& bookId : filteredBooks ) { | ||
const auto bookObj = library->getBookById(bookId); | ||
const auto bookTitle = bookObj.getTitle(); | ||
std::string contentId = ""; | ||
try { | ||
contentId = urlEncode(nameMapper->getNameForId(bookId)); | ||
} catch (...) {} | ||
const auto bookDescription = bookObj.getDescription(); | ||
const auto langCode = bookObj.getCommaSeparatedLanguages(); | ||
const auto bookIconUrl = rootLocation + "/catalog/v2/illustration/" + bookId + "/?size=48"; | ||
const auto tags = bookObj.getTags(); | ||
const auto downloadAvailable = (bookObj.getUrl() != ""); | ||
std::string faviconAttr = "style=background-image:url(" + bookIconUrl + ")"; | ||
|
||
booksData.push_back(kainjow::mustache::object{ | ||
{"id", contentId}, | ||
{"title", bookTitle}, | ||
{"description", bookDescription}, | ||
{"langCode", langCode}, | ||
{"faviconAttr", faviconAttr}, | ||
{"tagList", getTagList(tags)}, | ||
{"downloadAvailable", downloadAvailable} | ||
}); | ||
} | ||
|
||
auto getTranslation = i18n::GetTranslatedStringWithMsgId(m_userLang); | ||
|
||
const auto translations = kainjow::mustache::object{ | ||
getTranslation("search"), | ||
getTranslation("download"), | ||
getTranslation("count-of-matching-books", {{"COUNT", to_string(filteredBooks.size())}}), | ||
getTranslation("book-filtering-all-categories"), | ||
getTranslation("book-filtering-all-languages"), | ||
getTranslation("powered-by-kiwix-html"), | ||
getTranslation("welcome-to-kiwix-server"), | ||
getTranslation("preview-book"), | ||
getTranslation("welcome-page-overzealous-filter", {{"URL", "?lang="}}) | ||
}; | ||
|
||
return render_template( | ||
RESOURCE::templates::no_js_library_page_html, | ||
kainjow::mustache::object{ | ||
{"root", rootLocation}, | ||
{"books", booksData }, | ||
{"searchQuery", searchQuery}, | ||
{"languages", languages}, | ||
{"categories", categories}, | ||
{"noResults", filteredBooks.size() == 0}, | ||
{"translations", translations} | ||
} | ||
); | ||
} | ||
|
||
} // namespace kiwix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.