HTML generator done.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
tastytea 2020-07-02 09:47:56 +02:00
parent 20666bdf35
commit eae3aa9bd4
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 107 additions and 9 deletions

View File

@ -79,7 +79,10 @@ fs::path get_datadir()
#else
path /= ".fediblock-backend";
#endif
fs::create_directories(path);
if (!fs::exists(path))
{
fs::create_directories(path);
}
return path;
}

View File

@ -25,6 +25,8 @@
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string_view>
#include <vector>
using namespace FediBlock;
@ -36,7 +38,9 @@ using std::cout;
using std::exception;
using std::ifstream;
using std::runtime_error;
using std::string_view;
using std::stringstream;
using std::vector;
vector<entry_type> read_json_files()
{
@ -61,27 +65,109 @@ vector<entry_type> read_json_files()
}
stringstream ss;
ss << file.rdbuf();
entries.push_back(json::from_json(ss.str()));
auto entry{json::from_json(ss.str())};
if (!entry.screenshot_filepath.empty())
{
const fs::path sh_path{entry.screenshot_filepath};
entry.screenshot_filepath = path.stem().string() +=
sh_path.extension();
}
entries.push_back(entry);
}
return entries;
}
void write_html(ostream &out, const vector<entry_type> &entries)
{
out << R"(<!doctype html>
<html>
<head>
<title>FediBlock prototype</title>
<meta charset="utf-8">
</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>";
for (const auto &receipt : entry.receipts)
{
if (receipt != *(entry.receipts.begin()))
{
out << "<br>";
}
out << "<a href=\"" << receipt << "\">" << receipt << "</a>";
}
out << "</p>\n";
if (!entry.screenshot_filepath.empty())
{
out << " <p><strong>Screenshot:</strong><br>\n";
out << " <img src=\"" << entry.screenshot_filepath
<< R"(" height="100">)" << '\n';
out << " </p>\n";
}
out << " </details>\n";
}
out << " </body>\n</html>\n";
}
void copy_screenshots(const fs::path &directory)
{
auto dir_iter{fs::directory_iterator(files::get_tmpdir() / "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 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_libgit2_init();
try
{
git::clone();
const auto entries{read_json_files()};
for (const auto &entry : entries)
{
cout << entry.instance << " ";
cout << entry.screenshot_filepath << '\n';
}
write_html(cout, entries);
copy_screenshots(target_dir);
}
catch (const exception &e)
{

View File

@ -17,17 +17,27 @@
#ifndef FEDIBLOCK_BACKEND_HTML_HPP
#define FEDIBLOCK_BACKEND_HTML_HPP
#include "fs-compat.hpp"
#include "types.hpp"
#include <ostream>
#include <vector>
namespace FediBlock
{
using std::ostream;
using std::vector;
// Read JSON files in data repo and return vecor of entries.
[[nodiscard]] vector<entry_type> read_json_files();
// Convert entries to HTML and output it to an ostream.
void write_html(ostream &out, const vector<entry_type> &entries);
// Copy screenshots from repo to directory.
void copy_screenshots(const fs::path &directory);
} // namespace FediBlock
#endif // FEDIBLOCK_BACKEND_HTML_HPP

View File

@ -18,7 +18,6 @@
#include "fs-compat.hpp"
#include "types.hpp"
#include <nlohmann/detail/exceptions.hpp>
#include <nlohmann/json.hpp>
namespace FediBlock::json