[WIP] RSS support.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2020-07-05 09:06:45 +02:00
parent cfb79577ae
commit 8325da3624
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 194 additions and 1 deletions

View File

@ -1,6 +1,6 @@
include(GNUInstallDirs)
add_executable(gen_html html.cpp)
add_executable(gen_html "html.cpp")
target_link_libraries(gen_html
PRIVATE fediblock)
@ -8,3 +8,12 @@ target_link_libraries(gen_html
install(TARGETS gen_html
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
RENAME ${PROJECT_NAME}-gen_html)
add_executable(gen_rss "rss.cpp")
target_link_libraries(gen_rss
PRIVATE fediblock)
install(TARGETS gen_rss
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
RENAME ${PROJECT_NAME}-gen_rss)

120
src/generators/rss.cpp Normal file
View File

@ -0,0 +1,120 @@
/* This file is part of FediBlock-backend.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rss.hpp"
#include "time.hpp"
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <ostream>
#include <string>
#include <string_view>
#include <vector>
namespace FediBlock::rss
{
using std::getenv;
using std::ostream;
using std::string;
using std::string_view;
using std::uint8_t;
using std::chrono::system_clock;
void write_line(ostream &out, const uint8_t spaces, const string_view rsstag,
const string_view value)
{
string endtag;
// If there is a space in the rsstag, use only the part up until the space
// for the ending rsstag.
const size_t pos = rsstag.find(' ');
if (pos == string_view::npos)
{
endtag = rsstag;
}
else
{
endtag = rsstag.substr(0, pos);
}
out << string(spaces, ' ');
out << '<' << rsstag << '>' << value << "</" << endtag << ">\n";
}
void write_rss(ostream &out, const vector<entry_type> &entries,
const vector<string> &tags)
{
string selfurl;
char *env{getenv("HTTPS")};
if (env == nullptr || string(env) != "on")
{
selfurl = "http://";
}
else
{
selfurl = "https://";
}
env = getenv("SERVER_NAME");
if (env != nullptr)
{
selfurl += env;
}
env = getenv("REQUEST_URI");
if (env != nullptr)
{
selfurl += env;
}
constexpr string_view blocklist_url{
"https://fediblock-test.tastytea.de/blocklist/"};
constexpr string_view rss_time_format{"%a, %d %b %Y %T %z"};
out << R"(<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
)";
out << R"( <atom:link href=")" + selfurl
<< R"(" rel="self" type="application/rss+xml"/>)" << '\n';
write_line(out, 4, "title", "FediBlock: newest entries");
write_line(out, 4, "link", blocklist_url);
write_line(out, 4, "description", "FediBlock: Newest entries.");
write_line(out,
4,
"lastBuildDate",
time::to_string(system_clock::now(), rss_time_format));
// TODO: <item>s.
out << " </channel>\n</rss>\n";
}
} // namespace FediBlock::rss
using std::cout;
using std::string_view;
using std::vector;
using namespace FediBlock::rss;
int main(int argc, char *argv[])
{
const vector<string_view> args(argv, argv + argc);
write_rss(cout, {}, {});
return 0;
}

64
src/generators/rss.hpp Normal file
View File

@ -0,0 +1,64 @@
/* This file is part of FediBlock-backend.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FEDIBLOCK_BACKEND_RSS_HPP
#define FEDIBLOCK_BACKEND_RSS_HPP
#include "types.hpp"
#include <cstdint>
#include <ostream>
#include <string>
#include <string_view>
#include <vector>
namespace FediBlock::rss
{
using std::ostream;
using std::string;
using std::string_view;
using std::uint8_t;
using std::vector;
/*!
* @brief Write a line of RSS.
*
* @param out The ostream to use for output.
* @param spaces Preceding spaces.
* @param rsstag RSS tag.
* @param value Content of the tag.
*
* @since 0.2.0
*/
void write_line(ostream &out, uint8_t spaces, string_view rsstag,
string_view value);
/*!
* @brief Convert entries to RSS.
*
* @param out The ostream to use for output.
* @param entries The entries to write.
* @param tags Only write entries with these tags (logical or).
*
* @since 0.2.0
*/
void write_rss(ostream &out, const vector<entry_type> &entries,
const vector<string> &tags);
} // namespace FediBlock::rss
#endif // FEDIBLOCK_BACKEND_RSS_HPP