Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-30 09:40:28

0001 // This file is part of the actsvg package.
0002 //
0003 // Copyright (C) 2022 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
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 // Function to compare two strings alphanumerically.
0024 bool alphanumeric(const actsvg::svg::object& svg1,
0025                   const actsvg::svg::object& svg2);
0026 }  // namespace compare
0027 
0028 /// @brief Class for generating a web page to view and merge svgs.
0029 /// @note When used a debugging tool and rebuilding multiple times,
0030 /// if the webpage does not refresh as expected it is likely caused by browser
0031 /// caching.
0032 class web_builder {
0033     public:
0034     using comparison_function = bool (*)(const actsvg::svg::object&,
0035                                          const actsvg::svg::object&);
0036 
0037     /// @brief Generates a webpage configured with the given svgs.
0038     /// @param output_directory the root of the web page.
0039     /// @param svgs the svgs available for selection on the web page.
0040     /// @param order_comparator a compartor function to determine
0041     /// the display order of the svgs.
0042     /// @note When used a debugging tool and rebuilding multiple times,
0043     /// if the webpage does not refresh as expected it is likely caused by
0044     /// browser caching.
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     /// @brief Generates a webpage configured with the given svgs.
0053     /// @param output_directory the root of the web page.
0054     /// @param svgs the svgs available for selection on the web page.
0055     /// @note When used a debugging tool and rebuilding multiple times,
0056     /// if the webpage does not refresh as expected it is likely caused by
0057     /// browser caching.
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     /// @brief Writes the svgs to the svgs folder in the output directory and
0069     /// generates the config.json file.
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         // Write the svgs.
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         // Write the config.
0087         write_file(output_directory / "config.json", get_config(file_names));
0088     }
0089 
0090     /// @brief Writes the svgs to the svgs folder in the output directory and
0091     /// generates the config.json file.
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     /// @brief Writes a file.
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     /// @returns the json of the config file.
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     /// @brief Creates the folder structure for the webpage.
0126     void create_directory_structure(
0127         const std::filesystem::path& directory_path);
0128 };
0129 
0130 }  // namespace actsvg::web