163 lines
4.2 KiB
C++
163 lines
4.2 KiB
C++
/* 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 "html.hpp"
|
||
#include "files.hpp"
|
||
#include "fs-compat.hpp"
|
||
#include "git.hpp"
|
||
#include "json.hpp"
|
||
|
||
#include <exception>
|
||
#include <fstream>
|
||
#include <iostream>
|
||
#include <sstream>
|
||
#include <stdexcept>
|
||
#include <string_view>
|
||
#include <vector>
|
||
|
||
using namespace FediBlock;
|
||
|
||
namespace FediBlock
|
||
{
|
||
|
||
using std::cerr;
|
||
using std::cout;
|
||
using std::exception;
|
||
using std::ifstream;
|
||
using std::runtime_error;
|
||
using std::string_view;
|
||
using std::stringstream;
|
||
using std::vector;
|
||
|
||
void write_html(ostream &out, const vector<entry_type> &entries)
|
||
{
|
||
out << R"(<!doctype html>
|
||
<html>
|
||
<head>
|
||
<title>FediBlock – Blocklist</title>
|
||
<meta charset="utf-8">
|
||
<meta name="robots" content="noindex, nofollow">
|
||
</head>
|
||
<body>
|
||
<h1>Blocklist</h1>
|
||
)";
|
||
|
||
for (const auto &entry : entries)
|
||
{
|
||
out << "\n";
|
||
out << " <details>\n";
|
||
out << " <summary>" << entry.instance << "</summary>\n";
|
||
out << " <p>" << entry.description << "</p>\n";
|
||
out << " <p><strong>Tags:</strong> ";
|
||
for (const auto &tag : entry.tags)
|
||
{
|
||
if (tag != *(entry.tags.begin()))
|
||
{
|
||
out << ", ";
|
||
}
|
||
out << tag;
|
||
}
|
||
out << "</p>\n";
|
||
out << " <p><strong>Receipts:</strong><br>\n";
|
||
for (const auto &receipt : entry.receipts)
|
||
{
|
||
if (receipt != *(entry.receipts.begin()))
|
||
{
|
||
out << "<br>\n";
|
||
}
|
||
out << " <a href=\"" << receipt << "\">" << receipt
|
||
<< "</a>";
|
||
}
|
||
out << "\n </p>\n";
|
||
if (!entry.screenshot_filepath.empty())
|
||
{
|
||
out << " <p><strong>Screenshot:</strong><br>"
|
||
<< "<a href=\"" << entry.screenshot_filepath << "\"><img src=\""
|
||
<< entry.screenshot_filepath << R"(" height="100"></a></p>)"
|
||
<< '\n';
|
||
}
|
||
out << " </details>\n";
|
||
}
|
||
|
||
out << R"(
|
||
<p class="footer" style="text-align: right;">
|
||
<a href="https://schlomp.space/FediBlock">Sourcecode</a> licensed under the
|
||
<a href="https://www.gnu.org/licenses/agpl-3.0.html">AGPL-3.0-only</a>.
|
||
</p>
|
||
</body>
|
||
</html>
|
||
)";
|
||
}
|
||
|
||
void copy_screenshots(const fs::path &directory)
|
||
{
|
||
const auto dir_iter{fs::directory_iterator(files::get_cachedir() / "repo")};
|
||
for (const fs::path &path : dir_iter)
|
||
{
|
||
if (path.filename().string()[0] == '.')
|
||
{
|
||
continue;
|
||
}
|
||
if (path.extension() == ".json")
|
||
{
|
||
continue;
|
||
}
|
||
|
||
fs::copy_file(path, directory / path.filename(),
|
||
fs::copy_options::skip_existing);
|
||
}
|
||
}
|
||
|
||
} // namespace FediBlock
|
||
|
||
int main(int argc, char *argv[])
|
||
{
|
||
const vector<string_view> args(argv, argv + argc);
|
||
if (argc <= 1)
|
||
{
|
||
cerr << "usage: " << args[0] << " <target directory>\n";
|
||
return 1;
|
||
}
|
||
const string_view target_dir{args[1]};
|
||
|
||
git::init(true);
|
||
|
||
bool remove_lockfile{false};
|
||
try
|
||
{
|
||
try
|
||
{
|
||
git::update_cached_repo();
|
||
remove_lockfile = true;
|
||
}
|
||
catch (const runtime_error &)
|
||
{
|
||
// Ignore, use old version of repo.
|
||
}
|
||
const auto entries{files::read_json_files(true)};
|
||
write_html(cout, entries);
|
||
copy_screenshots(target_dir);
|
||
}
|
||
catch (const exception &e)
|
||
{
|
||
cerr << "Error: " << e.what() << '\n';
|
||
}
|
||
|
||
git::cleanup(remove_lockfile);
|
||
|
||
return 0;
|
||
}
|