From eae3aa9bd479661987b274cd645f6c26f4a9c133 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 2 Jul 2020 09:47:56 +0200 Subject: [PATCH] HTML generator done. --- src/files.cpp | 5 +- src/generators/html.cpp | 100 +++++++++++++++++++++++++++++++++++++--- src/generators/html.hpp | 10 ++++ src/json.cpp | 1 - 4 files changed, 107 insertions(+), 9 deletions(-) diff --git a/src/files.cpp b/src/files.cpp index 2b9aa9f..3dd2872 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -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; } diff --git a/src/generators/html.cpp b/src/generators/html.cpp index 0a955a5..9085b0c 100644 --- a/src/generators/html.cpp +++ b/src/generators/html.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include 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 read_json_files() { @@ -61,27 +65,109 @@ vector 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 &entries) +{ + out << R"( + + + FediBlock prototype + + + +

Blocklist

+)"; + + for (const auto &entry : entries) + { + out << "\n"; + out << "
\n"; + out << " " << entry.instance << "\n"; + out << "

" << entry.description << "

\n"; + out << "

Tags: "; + for (const auto &tag : entry.tags) + { + if (tag != *(entry.tags.begin())) + { + out << ", "; + } + out << tag; + } + out << "

\n"; + out << "

Receipts:
"; + for (const auto &receipt : entry.receipts) + { + if (receipt != *(entry.receipts.begin())) + { + out << "
"; + } + out << "" << receipt << ""; + } + out << "

\n"; + if (!entry.screenshot_filepath.empty()) + { + out << "

Screenshot:
\n"; + out << " )" << '\n'; + out << "

\n"; + } + out << "
\n"; + } + + out << " \n\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 args(argv, argv + argc); + if (argc <= 1) + { + cerr << "usage: " << args[0] << " \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) { diff --git a/src/generators/html.hpp b/src/generators/html.hpp index 7678390..c435a03 100644 --- a/src/generators/html.hpp +++ b/src/generators/html.hpp @@ -17,17 +17,27 @@ #ifndef FEDIBLOCK_BACKEND_HTML_HPP #define FEDIBLOCK_BACKEND_HTML_HPP +#include "fs-compat.hpp" #include "types.hpp" +#include #include namespace FediBlock { +using std::ostream; using std::vector; +// Read JSON files in data repo and return vecor of entries. [[nodiscard]] vector read_json_files(); +// Convert entries to HTML and output it to an ostream. +void write_html(ostream &out, const vector &entries); + +// Copy screenshots from repo to directory. +void copy_screenshots(const fs::path &directory); + } // namespace FediBlock #endif // FEDIBLOCK_BACKEND_HTML_HPP diff --git a/src/json.cpp b/src/json.cpp index 0fbdffd..df5ea30 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -18,7 +18,6 @@ #include "fs-compat.hpp" #include "types.hpp" -#include #include namespace FediBlock::json