From 9707694be62b0df023bb22ef5c6e9807f9354bdd Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 15 Oct 2020 04:42:25 +0200 Subject: [PATCH] Add support for RSS tags. Also add cgi::tolower(). --- .drone.yml | 4 ++-- CMakeLists.txt | 1 + README.adoc | 2 ++ src/CMakeLists.txt | 4 ++-- src/cgi.cpp | 30 ++++++++++++++++++++++++++++++ src/cgi.hpp | 12 ++++++++++++ src/generators/rss.cpp | 3 ++- 7 files changed, 51 insertions(+), 5 deletions(-) diff --git a/.drone.yml b/.drone.yml index 877187e..c352cd1 100644 --- a/.drone.yml +++ b/.drone.yml @@ -26,7 +26,7 @@ steps: - alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get' - apt-get update -q - apt-get install -qq build-essential cmake g++-10 clang pkg-config - - apt-get install -qq catch libcgicc-dev nlohmann-json3-dev libgit2-dev libcurl4-openssl-dev + - apt-get install -qq catch libcgicc-dev nlohmann-json3-dev libgit2-dev libcurl4-openssl-dev libicu-dev - rm -rf build && mkdir -p build && cd build - cmake -G "Unix Makefiles" -DWITH_TESTS=YES .. - make VERBOSE=1 @@ -62,7 +62,7 @@ steps: - alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get' - apt-get update -q - apt-get install -qq build-essential cmake clang pkg-config - - apt-get install -qq catch libcgicc-dev nlohmann-json-dev libgit2-dev libcurl4-openssl-dev + - apt-get install -qq catch libcgicc-dev nlohmann-json-dev libgit2-dev libcurl4-openssl-dev libicu-dev - rm -rf build && mkdir -p build && cd build - cmake -G "Unix Makefiles" -DWITH_TESTS=YES .. - make VERBOSE=1 diff --git a/CMakeLists.txt b/CMakeLists.txt index 09a6c1d..6830c25 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ find_package(nlohmann_json REQUIRED CONFIG) find_package(Filesystem REQUIRED COMPONENTS Final Experimental) pkg_check_modules(libgit2 REQUIRED IMPORTED_TARGET libgit2) find_package(CURL 7.56 REQUIRED) +find_package(ICU REQUIRED COMPONENTS uc) add_subdirectory(src) add_subdirectory(src/generators) diff --git a/README.adoc b/README.adoc index 61a56e8..7196c0c 100644 --- a/README.adoc +++ b/README.adoc @@ -16,6 +16,7 @@ :uri-nlohmann-json: https://nlohmann.github.io/json/ :uri-libgit2: https://libgit2.org/ :uri-libcurl: https://curl.haxx.se/libcurl/ +:uti-icu: http://site.icu-project.org/ *{project}* turns form data into JSON and opens a pull request on link:https://schlomp.space/FediBlock/data[FediBlock/data]. Also included are @@ -87,6 +88,7 @@ Existing screenshots will not be overwritten. * link:{uri-nlohmann-json}[nlohmann-json] (tested: 3.7 / 2.1) * link:{uri-libgit2}[libgit2] with SSH support (tested: 1.0 / 0.26) * link:{uri-libcurl}[libcurl] (at least: 7.56) +* link:{uri-icu}[icu] (tested: 67.1 / 60.2) * Optional ** Tests: link:{uri-catch}[Catch] (tested: 2.5 / 1.10) ** DEB package: link:{uri-dpkg}[dpkg] (tested: 1.19) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 29be4d8..51744e2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,9 +21,9 @@ target_link_libraries(fediblock PkgConfig::libgit2) if(${CMAKE_VERSION} VERSION_LESS 3.12) - target_link_libraries(fediblock PUBLIC ${CURL_LIBRARIES}) + target_link_libraries(fediblock PUBLIC ${CURL_LIBRARIES} ICU::uc) else() - target_link_libraries(fediblock PUBLIC CURL::libcurl) + target_link_libraries(fediblock PUBLIC CURL::libcurl ICU::uc) endif() add_executable(${PROJECT_NAME} main.cpp) diff --git a/src/cgi.cpp b/src/cgi.cpp index c94866b..f1a7cdb 100644 --- a/src/cgi.cpp +++ b/src/cgi.cpp @@ -20,11 +20,14 @@ #include "time.hpp" #include +#include +#include #include #include #include #include +#include #include #include #include @@ -33,6 +36,7 @@ namespace FediBlock::cgi { +using std::back_inserter; using std::getline; using std::ios; using std::ofstream; @@ -40,6 +44,7 @@ using std::runtime_error; using std::string; using std::string_view; using std::stringstream; +using std::transform; using std::vector; using std::chrono::system_clock; @@ -50,6 +55,8 @@ entry_type parse_formdata() cgicc::Cgicc cgi; entry.instance = cgi("instance"); entry.tags = string_to_vector(cgi("tags")); + transform(entry.tags.begin(), entry.tags.end(), entry.tags.begin(), + [](const auto &tag) { return tolower(tag); }); entry.receipts = string_to_vector(cgi("receipts")); entry.description = cgi("description"); entry.report_time = time::to_string(system_clock::now()); @@ -96,4 +103,27 @@ vector string_to_vector(const string_view str) return vec; } +vector get_tags() +{ + cgicc::Cgicc cgi; + vector tags_form; + vector tags_string; + + cgi.getElement("tags[]", tags_form); + transform(tags_form.begin(), tags_form.end(), back_inserter(tags_string), + [](const auto &element) { return tolower(element.getValue()); }); + + return tags_string; +} + +string tolower(const string_view str) +{ + string result; + + const auto unistr{icu::UnicodeString(str.data(), "UTF-8").toLower()}; + unistr.toUTF8String(result); + + return result; +} + } // namespace FediBlock::cgi diff --git a/src/cgi.hpp b/src/cgi.hpp index 35fc3d1..1f06f45 100644 --- a/src/cgi.hpp +++ b/src/cgi.hpp @@ -36,6 +36,18 @@ using std::vector; // Split a string at commas, return a vector. [[nodiscard]] vector string_to_vector(string_view str); +/*! + * @brief Read tags from QUERY_STRING or stdin and return it as a vector. + * + * For use in rss.cpp. + * + * @since 0.1.0 + */ +[[nodiscard]] vector get_tags(); + +// Return str in lowercase. +[[nodiscard]] string tolower(string_view str); + } // namespace FediBlock::cgi #endif // FEDIBLOCK_BACKEND_CGI_HPP diff --git a/src/generators/rss.cpp b/src/generators/rss.cpp index 2e4f7e9..fd92246 100644 --- a/src/generators/rss.cpp +++ b/src/generators/rss.cpp @@ -15,6 +15,7 @@ */ #include "rss.hpp" +#include "cgi.hpp" #include "files.hpp" #include "git.hpp" #include "time.hpp" @@ -218,7 +219,7 @@ int main(int argc, char *argv[]) } const auto entries{files::read_json_files(true)}; cout << "Content-Type: application/rss+xml\r\n\r\n"; - write_rss(cout, entries, {}); + write_rss(cout, entries, cgi::get_tags()); } catch (const exception &e) {