diff --git a/Makefile b/Makefile index 771adc5..a65000c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: build bindir clean -build: bindir src/*.cpp +build: bindir $(CXX) -std=c++17 -l fmt -o bin/descindex src/*.cpp bindir: diff --git a/src/main.cpp b/src/main.cpp index d9255e2..8a39eab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,76 @@ +#include "template.hpp" + #include +#include +#include +#include +#include +#include + +#include +struct desc { + std::string title; + std::string description; +}; -int main() { +desc read_desc_file(const std::string& path) { + desc fres; + // Open file + std::ifstream desc_file(path); + // Check file + if (!desc_file) { + throw std::runtime_error("Failed to open "+path+" for reading"); + } + // Read first line (title) + std::getline(desc_file, fres.title); + // Read description + fres.description = std::string(std::istreambuf_iterator{desc_file}, {}); // A mess of code + // Return function result + return fres; +} + +desc handle_media_dir(const std::filesystem::path& media_dir) { + // Open output file for writing + std::ofstream output(media_dir/"index.html"); + // Get directory description + auto media_dir_desc = read_desc_file(media_dir/"desc.txt"); + // Write out top + output << fmt::format(Template::top, media_dir_desc.title, media_dir_desc.description); + // Iterate through input dir + for (const auto& file : std::filesystem::directory_iterator(media_dir)) { + // Skip .html and .txt files + auto ext = file.path().extension(); + if (ext == ".html" || ext == ".txt") { + continue; + } + // Get description + desc file_desc; + if (file.is_directory()) { + // Handle directory + file_desc = handle_media_dir(file); + } else { + // Handle file + file_desc = read_desc_file(file.path().string()+".txt"); + } + // Write out entry + output << fmt::format(Template::content, file_desc.title, file_desc.description, file.path().filename().string()); + } + // Write out bottom + output << Template::bottom; + // Return description of directory + return media_dir_desc; +} + +int main(int argc, char **argv) { + // Get args + if (argc != 2) { + // Print usage + std::cout << "Usage: " << argv[0] << " " << std::endl; + return EXIT_FAILURE; + } + // Handle given media dir + handle_media_dir(argv[1]); } diff --git a/src/template.hpp b/src/template.hpp new file mode 100644 index 0000000..22cdb48 --- /dev/null +++ b/src/template.hpp @@ -0,0 +1,30 @@ +#ifndef _TEMPLATE_HPP +#define _TEMPLATE_HPP +#include + + + +namespace Template { +constexpr std::string_view +top = R"( + + + {0} + + +

{0}

+

{1}

+
+)", +content = R"( +
+

{0}

+

{1}

+ Open +)", +bottom = R"( + + +)"; +} +#endif