[API Change] Convert tags to the new convention.

Use the new convention describe here : https://wiki.openzim.org/wiki/Tags
This commit is contained in:
Matthieu Gautier 2019-09-16 10:45:41 +02:00
parent 660d5d7fb7
commit 2a6772b76d
4 changed files with 119 additions and 5 deletions

View File

@ -229,9 +229,11 @@ class Reader
/** /**
* Get the tags of the zim file. * Get the tags of the zim file.
* *
* @return The tags of the zim file as specified in the zim metadata. * @param original If true, return the original tags as specified in the zim metadata.
* Else, try to convert it to the new 'normalized' format.
* @return The tags of the zim file.
*/ */
string getTags() const; string getTags(bool original=false) const;
/** /**
* Get the relations of the zim file. * Get the relations of the zim file.

View File

@ -352,9 +352,52 @@ string Reader::getLicense() const
METADATA("License") METADATA("License")
} }
string Reader::getTags() const std::vector<std::string> convertTags(const std::string& tags_str)
{ {
METADATA("Tags") auto tags = split(tags_str, ";");
std::vector<std::string> tagsList;
bool picSeen(false), vidSeen(false), detSeen(false), indexSeen(false);
for (auto tag: tags) {
picSeen |= (tag == "nopic" || startsWith(tag, "_pictures:"));
vidSeen |= (tag == "novid" || startsWith(tag, "_videos:"));
detSeen |= (tag == "nodet" || startsWith(tag, "_details:"));
indexSeen |= startsWith(tag, "_ftindex");
if (tag == "nopic") {
tagsList.push_back("_pictures:no");
} else if (tag == "novid") {
tagsList.push_back("_videos:no");
} else if (tag == "nodet") {
tagsList.push_back("_details:no");
} else if (tag == "_ftindex") {
tagsList.push_back("_ftindex:yes");
} else {
tagsList.push_back(tag);
}
}
if (!indexSeen) {
tagsList.push_back("_ftindex:no");
}
if (!picSeen) {
tagsList.push_back("_pictures:yes");
}
if (!vidSeen) {
tagsList.push_back("_videos:yes");
}
if (!detSeen) {
tagsList.push_back("_details:yes");
}
return tagsList;
}
string Reader::getTags(bool original) const
{
string tags_str;
getMetadata("Tags", tags_str);
if (original) {
return tags_str;
}
auto tags = convertTags(tags_str);
return join(tags, ";");
} }
string Reader::getRelation() const string Reader::getRelation() const

View File

@ -3,7 +3,8 @@
tests = [ tests = [
'parseUrl', 'parseUrl',
'library', 'library',
'regex' 'regex',
'tagParsing'
] ]

68
test/tagParsing.cpp Normal file
View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 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 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "gtest/gtest.h"
#include <string>
#include <vector>
namespace kiwix {
std::vector<std::string> convertTags(const std::string& tags);
std::string getTagValueFromTagList(const std::vector<std::string>& tagList, const std::string& tagName);
};
using namespace kiwix;
#define parse_tag getTagValueFromTagList
namespace
{
TEST(ParseTagTest, convert)
{
{
std::string tagStr = "";
std::vector<std::string> tagList = {"_ftindex:no", "_pictures:yes", "_videos:yes", "_details:yes"};
ASSERT_EQ(convertTags(tagStr), tagList);
}
{
std::string tagStr = "_category:foo;bar";
std::vector<std::string> tagList = {"_category:foo", "bar", "_ftindex:no", "_pictures:yes", "_videos:yes", "_details:yes"};
ASSERT_EQ(convertTags(tagStr), tagList);
}
{
std::string tagStr = "_ftindex:no;_pictures:yes;_videos:yes;_details:yes;_category:foo;bar";
std::vector<std::string> tagList = {"_ftindex:no", "_pictures:yes", "_videos:yes", "_details:yes", "_category:foo", "bar"};
ASSERT_EQ(convertTags(tagStr), tagList);
}
{
std::string tagStr = "_ftindex:yes;_pictures:no;_videos:no;_details:no;_category:foo;bar";
std::vector<std::string> tagList = {"_ftindex:yes", "_pictures:no", "_videos:no", "_details:no", "_category:foo", "bar"};
ASSERT_EQ(convertTags(tagStr), tagList);
}
{
std::string tagStr = "_ftindex;nopic;novid;nodet;foo;bar";
std::vector<std::string> tagList = {"_ftindex:yes", "_pictures:no", "_videos:no", "_details:no", "foo", "bar"};
ASSERT_EQ(convertTags(tagStr), tagList);
}
}
};
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}