File indexing completed on 2025-11-30 09:40:28
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include <filesystem>
0012 #include <fstream>
0013 #include <functional>
0014 #include <numeric>
0015 #include <string>
0016
0017 #include "actsvg/core/svg.hpp"
0018 #include "actsvg/web/webpage_text.hpp"
0019
0020 namespace actsvg::web {
0021
0022 namespace compare {
0023
0024 bool alphanumeric(const actsvg::svg::object& svg1,
0025 const actsvg::svg::object& svg2);
0026 }
0027
0028
0029
0030
0031
0032 class web_builder {
0033 public:
0034 using comparison_function = bool (*)(const actsvg::svg::object&,
0035 const actsvg::svg::object&);
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 template <typename iterator_t>
0046 void build(const std::filesystem::path& output_directory,
0047 const iterator_t& svgs, comparison_function order_comparator) {
0048 create_template(output_directory);
0049 configure_svgs(output_directory, svgs, order_comparator);
0050 }
0051
0052
0053
0054
0055
0056
0057
0058 template <typename iterator_t>
0059 void build(const std::filesystem::path& output_directory,
0060 const iterator_t& svgs) {
0061 create_template(output_directory);
0062 configure_svgs(output_directory, svgs);
0063 }
0064
0065 private:
0066 void create_template(const std::filesystem::path& output_directory);
0067
0068
0069
0070 template <typename iterator_t>
0071 void configure_svgs(const std::filesystem::path& output_directory,
0072 const iterator_t& it_svgs) {
0073 std::vector<std::string> file_names;
0074
0075 for (const auto& svg : it_svgs) {
0076 svg::file output_file;
0077 output_file.add_object(svg);
0078
0079 const std::string file_name = svg._id + ".svg";
0080
0081 write_file(output_directory / "svgs" / file_name, output_file);
0082
0083 file_names.push_back(file_name);
0084 }
0085
0086
0087 write_file(output_directory / "config.json", get_config(file_names));
0088 }
0089
0090
0091
0092 template <typename iterator_t>
0093 void configure_svgs(const std::filesystem::path& output_directory,
0094 const iterator_t& it_svgs,
0095 comparison_function order_comparator) {
0096 auto svgs = std::vector(it_svgs.cbegin(), it_svgs.cend());
0097 std::sort(svgs.begin(), svgs.end(), order_comparator);
0098 configure_svgs(output_directory, svgs);
0099 }
0100
0101
0102 template <typename content_t>
0103 void write_file(const std::filesystem::path& path,
0104 const content_t& content) {
0105 std::ofstream rstream;
0106 rstream.open(path);
0107 rstream << content;
0108 rstream.close();
0109 }
0110
0111
0112 template <typename iterator_t>
0113 std::string get_config(const iterator_t& file_names_it) {
0114 std::string res;
0115 for (const auto& file_name : file_names_it) {
0116 if (res.size() == 0) {
0117 res += "\"" + file_name + "\"";
0118 } else {
0119 res += ", \"" + file_name + "\"";
0120 }
0121 }
0122 return "[" + res + "]";
0123 }
0124
0125
0126 void create_directory_structure(
0127 const std::filesystem::path& directory_path);
0128 };
0129
0130 }