mirror of
https://gitlab.com/niansa/descindex.git
synced 2025-03-06 20:48:44 +01:00
Added previewing
This commit is contained in:
parent
6684d9d846
commit
33903cf549
2 changed files with 57 additions and 5 deletions
25
src/main.cpp
25
src/main.cpp
|
@ -32,13 +32,28 @@ desc read_desc_file(const std::string& path) {
|
|||
return fres;
|
||||
}
|
||||
|
||||
void handle_media_file(const std::filesystem::path& media_file, const desc& media_desc) {
|
||||
// Open output file for writing
|
||||
std::ofstream output(media_file.string()+"_preview.html");
|
||||
// Write out doctype
|
||||
output << Template::doctype;
|
||||
// Write out top
|
||||
output << fmt::format(Template::Preview::top, media_desc.title, media_desc.description);
|
||||
// Write out content
|
||||
output << fmt::format(Template::Preview::content, media_file.filename().string());
|
||||
// Write out bottom
|
||||
output << Template::Preview::bottom;
|
||||
}
|
||||
|
||||
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 doctype
|
||||
output << Template::doctype;
|
||||
// Write out top
|
||||
output << fmt::format(Template::top, media_dir_desc.title, media_dir_desc.description);
|
||||
output << fmt::format(Template::Listing::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
|
||||
|
@ -48,18 +63,22 @@ desc handle_media_dir(const std::filesystem::path& media_dir) {
|
|||
}
|
||||
// Get description
|
||||
desc file_desc;
|
||||
std::string_view content_template;
|
||||
if (file.is_directory()) {
|
||||
// Handle directory
|
||||
file_desc = handle_media_dir(file);
|
||||
content_template = Template::Listing::content_dir;
|
||||
} else {
|
||||
// Handle file
|
||||
file_desc = read_desc_file(file.path().string()+".txt");
|
||||
handle_media_file(file.path(), file_desc);
|
||||
content_template = Template::Listing::content_file;
|
||||
}
|
||||
// Write out entry
|
||||
output << fmt::format(Template::content, file_desc.title, file_desc.description, file.path().filename().string());
|
||||
output << fmt::format(content_template, file_desc.title, file_desc.description, file.path().filename().string());
|
||||
}
|
||||
// Write out bottom
|
||||
output << Template::bottom;
|
||||
output << Template::Listing::bottom;
|
||||
// Return description of directory
|
||||
return media_dir_desc;
|
||||
}
|
||||
|
|
|
@ -6,6 +6,10 @@
|
|||
|
||||
namespace Template {
|
||||
constexpr std::string_view
|
||||
doctype = R"(<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">)";
|
||||
|
||||
namespace Listing {
|
||||
constexpr std::string_view
|
||||
top = R"(
|
||||
<html>
|
||||
<head>
|
||||
|
@ -16,15 +20,44 @@ top = R"(
|
|||
<p>{1}</p>
|
||||
<br />
|
||||
)",
|
||||
content = R"(
|
||||
content_file = R"(
|
||||
<br />
|
||||
<h3>{0}</h3>
|
||||
<p>{1}</p>
|
||||
<a href="{2}">Open</a>
|
||||
<p><a href="{2}">Open</a> - <a href="{2}_preview.html">Preview</a></p>
|
||||
)",
|
||||
content_dir = R"(
|
||||
<br />
|
||||
<h3>[DIR] {0}</h3>
|
||||
<p>{1}</p>
|
||||
<p><a href="{2}">Open</a></p>
|
||||
)",
|
||||
bottom = R"(
|
||||
</body>
|
||||
</html>
|
||||
)";
|
||||
}
|
||||
|
||||
namespace Preview {
|
||||
constexpr std::string_view
|
||||
top = R"(
|
||||
<html>
|
||||
<head>
|
||||
<title>{0} - Preview</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{0}</h1>
|
||||
<p>{1}</p>
|
||||
<br />
|
||||
)",
|
||||
content = R"(
|
||||
<p><a href="{0}">Open</a></p>
|
||||
<iframe src="{0}"></iframe>
|
||||
)",
|
||||
bottom = R"(
|
||||
</body>
|
||||
</html>
|
||||
)";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue