feat: compile-time minification and hiding of temporaries

This commit is contained in:
LordMZTE 2023-04-21 21:47:00 +02:00
parent d54340b2a9
commit bb371d6ab8
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
2 changed files with 67 additions and 18 deletions

23
src/minify.zig Normal file
View file

@ -0,0 +1,23 @@
const std = @import("std");
/// Very basic comptime HTML minification
pub fn minifyHTML(comptime html: []const u8) []const u8 {
var out: []const u8 = &[_]u8{};
var i = 0;
while (i < html.len) {
if (html[i] == '\n' or i == 0) {
if (i != 0) {
i += 1;
}
while (html[i] == ' ') {
i += 1;
}
}
out = out ++ [_]u8{html[i]};
i += 1;
}
return out;
}

View file

@ -1,6 +1,7 @@
const std = @import("std");
const status_response = @import("status_response.zig");
const mimeTypeForFilename = @import("mime_type.zig").mimeTypeForFilename;
const minifyHTML = @import("minify.zig").minifyHTML;
const DownloadQueue = @import("DownloadQueue.zig");
const State = @import("State.zig");
@ -39,8 +40,8 @@ pub fn indexRoute(
var content_buf = std.BoundedArray(u8, 1024 * 16).init(0) catch unreachable;
const w = content_buf.writer();
try w.writeAll(
\\<!DOCTYPE html>
try w.writeAll(comptime minifyHTML(
\\<!DOCTYPE html>
\\<html>
\\<head>
\\ <title>Vidzig index</title>
@ -49,7 +50,7 @@ pub fn indexRoute(
\\ <link rel="stylesheet" href="static/index.css">
\\</head>
\\<body>
);
));
var paused = false;
{
@ -60,8 +61,7 @@ pub fn indexRoute(
if (state.downloads.active_task) |active| {
try w.print(
\\<p>Active task: <b>{s}{s}{s}</b> -> <b>{s}</b></p>
\\<hr>
\\<p>Active task: <b>{s}{s}{s}</b> -> <b>{s}</b></p><hr>
,
.{
if (active.description) description_prefix_html else "",
@ -94,8 +94,7 @@ pub fn indexRoute(
}
try w.writeAll(
\\</table></tbody>
\\<hr>
\\</table></tbody><hr>
);
}
}
@ -106,7 +105,7 @@ pub fn indexRoute(
.{paused},
);
try w.writeAll(
try w.writeAll(comptime minifyHTML(
\\<form id="enqueue_form">
\\ <input type="text" placeholder="URL" id="url_inp"><br>
\\ <input type="text" placeholder="output name" id="outname_inp"><br>
@ -118,35 +117,62 @@ pub fn indexRoute(
\\<button id="pause_btn"></button>
\\<hr>
\\<ul>
);
));
var vids_dir = try std.fs.cwd().openIterableDir(state.vids_dir, .{});
defer vids_dir.close();
var hidden_entries: usize = 0;
var iter = vids_dir.iterate();
while (try iter.next()) |entry| {
files: while (try iter.next()) |entry| {
if (entry.kind != .File)
continue;
const hidden_suffixes = [_][]const u8{
".ytdl",
".psrt",
};
for (hidden_suffixes) |suf| {
if (std.mem.endsWith(u8, entry.name, suf)) {
hidden_entries += 1;
continue :files;
}
}
const quote = try std.Uri.escapeString(alloc, entry.name);
defer alloc.free(quote);
try w.print(
\\<li>
\\<button class="vid_del_btn">del</button>
\\<a href="vids/{s}">{s}</a>
\\</li>
,
comptime minifyHTML(
\\<li>
\\<button class="vid_del_btn">del</button>
\\<a href="vids/{s}">{s}</a>
\\</li>
),
.{ quote, entry.name },
);
}
try w.writeAll(
\\</ul>
try w.writeAll("</ul>");
if (hidden_entries > 0) {
try w.print(
\\<span class="gray">{} hidden {s}</span>
,
.{
hidden_entries,
if (hidden_entries == 1) "temporary" else "temporaries",
},
);
}
try w.writeAll(comptime minifyHTML(
\\<script src="static/index.js"></script>
\\</body>
\\</html>
);
));
res.transfer_encoding = .{ .content_length = content_buf.len };
try res.headers.append("Content-Type", "text/html");