aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/Head.zig
blob: 0eb090fa3132c55ac85c0a728a6fa0d191057025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const File = @import("File.zig");
const Component = @import("Component.zig");

const z_component_code: File.Script = .minify("z-component.js");

pub const Meta = struct {
    name: []const u8,
    content: []const u8,

    pub fn to_html_string(comptime self: @This()) []const u8 {
        return "<meta name=\"" ++ self.name ++ "\" content=\"" ++ self.content ++ "\">";
    }
};

title: []const u8,
meta: []const Meta,

pub const empty: @This() = .{
    .title = "",
    .meta = &.{},
};

pub fn default(comptime title: []const u8) @This() {
    return .{
        .title = title,
        .meta = &.{
            .{
                .name = "viewport",
                .content = "width=device-width, initial-scale=1.0",
            },
        },
    };
}

pub fn to_html_string(
    comptime self: @This(),
    comptime components: []const Component,
    comptime styles: []const File.Style,
) []const u8 {
    var content: []const u8 = "<head>" ++ z_component_code.to_html_string();

    content = content ++ "<title>" ++ self.title ++ "</title>";

    inline for (components) |component| {
        content = content ++ comptime component.to_html_string();
    }

    inline for (styles) |style| {
        content = content ++ comptime style.to_html_string();
    }

    inline for (self.meta) |meta| {
        content = content ++ comptime meta.to_html_string();
    }

    content = content ++ "</head>";
    return content;
}