Skip to content

Commit

Permalink
refactor project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoff-it committed Jul 25, 2024
1 parent e548112 commit f3f355c
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 59 deletions.
106 changes: 79 additions & 27 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -170,43 +170,36 @@ pub fn build(b: *std.Build) !void {
break :blk options.createModule();
};

const server = b.addExecutable(.{
.name = "server",
.root_source_file = b.path("server/main.zig"),
.target = target,
.optimize = optimize,
});

if (target.result.os.tag == .macos) {
server.linkFramework("CoreServices");
}

const mime = b.dependency("mime", mode);
const ws = b.dependency("ws", mode);
// dummy comment
const super = b.dependency("superhtml", mode);
const scripty = super.builder.dependency("scripty", .{});
const ziggy = b.dependency("ziggy", mode);
const zeit = b.dependency("zeit", mode);
const syntax = b.dependency("flow-syntax", mode);
const ts = syntax.builder.dependency("tree-sitter", mode);

server.root_module.addImport("options", options);
server.root_module.addImport("mime", mime.module("mime"));
server.root_module.addImport("ws", ws.module("websocket"));
const zine = b.addModule("zine", .{
.root_source_file = b.path("src/root.zig"),
});
zine.addImport("ziggy", ziggy.module("ziggy"));
zine.addImport("zeit", zeit.module("zeit"));
zine.addImport("syntax", syntax.module("syntax"));
zine.addImport("scripty", scripty.module("scripty"));
zine.addImport("treez", ts.module("treez"));
zine.addImport("superhtml", super.module("superhtml"));

b.installArtifact(server);
setupServer(b, options, target, optimize);

const layout = b.addExecutable(.{
.name = "layout",
.root_source_file = b.path("src/layout.zig"),
.root_source_file = b.path("src/exes/layout.zig"),
.target = target,
.optimize = optimize,
// .strip = true,

});

// dummy comment
const super = b.dependency("superhtml", mode);
const scripty = super.builder.dependency("scripty", .{});
const ziggy = b.dependency("ziggy", mode);
const zeit = b.dependency("zeit", mode);
const syntax = b.dependency("flow-syntax", mode);
const ts = syntax.builder.dependency("tree-sitter", mode);

layout.root_module.addImport("zine", zine);
layout.root_module.addImport("options", options);
layout.root_module.addImport("superhtml", super.module("superhtml"));
layout.root_module.addImport("scripty", scripty.module("scripty"));
Expand All @@ -225,19 +218,21 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = .Debug,
});
docgen.root_module.addImport("zine", zine);
docgen.root_module.addImport("zeit", zeit.module("zeit"));
docgen.root_module.addImport("ziggy", ziggy.module("ziggy"));
b.installArtifact(docgen);

const md_renderer = b.addExecutable(.{
.name = "markdown-renderer",
.root_source_file = b.path("src/markdown-renderer.zig"),
.root_source_file = b.path("src/exes/markdown-renderer.zig"),
.target = b.resolveTargetQuery(.{}),
.optimize = optimize,
});

const gfm = b.dependency("gfm", mode);

md_renderer.root_module.addImport("zine", zine);
md_renderer.root_module.addImport("ziggy", ziggy.module("ziggy"));
md_renderer.root_module.addImport("zeit", zeit.module("zeit"));
md_renderer.root_module.addImport("syntax", syntax.module("syntax"));
Expand All @@ -248,4 +243,61 @@ pub fn build(b: *std.Build) !void {
md_renderer.linkLibC();

b.installArtifact(md_renderer);

const fuzz = b.step("fuzz", "build fuzzing executables");
setupFuzzing(b, target, optimize, fuzz);
}

fn setupServer(
b: *std.Build,
options: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) void {
const server = b.addExecutable(.{
.name = "server",
.root_source_file = b.path("src/exes/server/main.zig"),
.target = target,
.optimize = optimize,
});

if (target.result.os.tag == .macos) {
server.linkFramework("CoreServices");
}

const mime = b.dependency("mime", .{
.target = target,
.optimize = optimize,
});
const ws = b.dependency("ws", .{
.target = target,
.optimize = optimize,
});

server.root_module.addImport("options", options);
server.root_module.addImport("mime", mime.module("mime"));
server.root_module.addImport("ws", ws.module("websocket"));

b.installArtifact(server);
}

fn setupFuzzing(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
fuzz: *std.Build.Step,
) void {
const afl = @import("zig-afl-kit");

const scripty_afl_obj = b.addObject(.{
.name = "scripty",
.root_source_file = b.path("src/fuzz/scripty.zig"),
.target = b.resolveTargetQuery(.{}),
.optimize = .Debug,
});
scripty_afl_obj.root_module.stack_check = false;
scripty_afl_obj.root_module.link_libc = true;

const afl_exe = afl.addInstrumentedExe(b, target, optimize, scripty_afl_obj);
fuzz.dependOn(&b.addInstallFile(afl_exe, "scripty-afl").step);
}
8 changes: 6 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
.path = "frontmatter",
},
.superhtml = .{
.url = "git+https://github.com/kristoff-it/superhtml#186b4bc3d56a67e1b898834393128b2d8b9a1bc6",
.hash = "122075eb33b79c21eb478c8dd6d5930e325d75dda619bcb7408fd0586b762c22885a",
.url = "git+https://github.com/kristoff-it/superhtml#7aea5cf0a32a4aa4dce4190219adde60388aaf98",
.hash = "122032ed35c90f3accd3ac19041c392dfbad817597643fc6df3a88bcd6b46ff11c7d",
},
.ziggy = .{
.url = "git+https://github.com/MFAshby/ziggy.git#2f34b58ca2c5e72c7dbd3ad3756df537d03d79e0",
Expand All @@ -33,6 +33,10 @@
.url = "git+https://github.com/neurocyte/flow-syntax#26b99478af9a1f09e0af8fd77fbdbe44e2015da4",
.hash = "1220f993fa053a2a76355906e74e1559f770fd612b303c8b4a1bafa116ab157b5efa",
},
.@"zig-afl-kit" = .{
.url = "git+https://github.com/kristoff-it/zig-afl-kit#f003bfe714f2964c90939fdc940d5993190a66ec",
.hash = "1220f2d8402bb7bbc4786b9c0aad73910929ea209cbd3b063842371d68abfed33c1e",
},
},
.paths = .{"."},
}
6 changes: 3 additions & 3 deletions content.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const std = @import("std");
const ziggy = @import("ziggy");
const templating = @import("templating.zig");
const contexts = @import("src/contexts.zig");
const context = @import("src/context.zig");
const zine = @import("build.zig");

const FrontParser = ziggy.frontmatter.Parser(contexts.Page);
const FrontParser = ziggy.frontmatter.Parser(context.Page);
const TranslationIndex = std.StringArrayHashMap(TranslationIndexEntry);
const TranslationIndexEntry = struct {
index_file: std.Build.LazyPath = undefined,
Expand Down Expand Up @@ -665,7 +665,7 @@ const Section = struct {
const Page = struct {
content_sub_path: []const u8,
md_name: []const u8,
fm: contexts.Page,
fm: context.Page,

// Present if this page is an 'index.md' and set
// to the section defined by this page.
Expand Down
File renamed without changes.
28 changes: 14 additions & 14 deletions src/docgen.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const ziggy = @import("ziggy");
const contexts = @import("contexts.zig");
const Value = contexts.Value;
const context = @import("context.zig");
const Value = context.Value;

pub const Signature = struct {
params: []const ScriptyParam = &.{},
Expand Down Expand Up @@ -35,17 +35,17 @@ pub const ScriptyParam = union(enum) {

pub fn fromType(t: type) ScriptyParam {
return switch (t) {
contexts.Page, *contexts.Page => .Page,
context.Page, *context.Page => .Page,
[]const u8 => .str,
[]const []const u8 => .{ .many = .str },
contexts.DateTime => .date,
context.DateTime => .date,
usize => .int,
bool => .bool,
ziggy.dynamic.Value => .dyn,
contexts.Page.Alternative => .Alternative,
[]const contexts.Page.Alternative => .{ .many = .Alternative },
contexts.Page.Translation => .Translation,
[]const contexts.Page.Translation => .{ .many = .Translation },
context.Page.Alternative => .Alternative,
[]const context.Page.Alternative => .{ .many = .Alternative },
context.Page.Translation => .Translation,
[]const context.Page.Translation => .{ .many = .Translation },

else => @compileError("TODO: add support for " ++ @typeName(t)),
};
Expand Down Expand Up @@ -110,8 +110,8 @@ pub fn main() !void {
);

const globals = .{
.{ .name = "$site", .type_name = "Site", .desc = contexts.Site.description },
.{ .name = "$page", .type_name = "Page", .desc = contexts.Page.description },
.{ .name = "$site", .type_name = "Site", .desc = context.Site.description },
.{ .name = "$page", .type_name = "Page", .desc = context.Page.description },
.{
.name = "$loop",
.type_name = "?Loop",
Expand Down Expand Up @@ -145,10 +145,10 @@ pub fn main() !void {
\\
);
const types = .{
.{ .name = "Site", .t = contexts.Site, .builtins = Value.builtinsFor(.site) },
.{ .name = "Page", .t = contexts.Page, .builtins = Value.builtinsFor(.page) },
.{ .name = "Alternative", .t = contexts.Page.Alternative, .builtins = Value.builtinsFor(.alternative) },
.{ .name = "Translation", .t = contexts.Page.Translation, .builtins = Value.builtinsFor(.translation) },
.{ .name = "Site", .t = context.Site, .builtins = Value.builtinsFor(.site) },
.{ .name = "Page", .t = context.Page, .builtins = Value.builtinsFor(.page) },
.{ .name = "Alternative", .t = context.Page.Alternative, .builtins = Value.builtinsFor(.alternative) },
.{ .name = "Translation", .t = context.Page.Translation, .builtins = Value.builtinsFor(.translation) },
.{ .name = "str", .builtins = Value.builtinsFor(.string) },
.{ .name = "date", .builtins = Value.builtinsFor(.date) },
.{ .name = "int", .builtins = Value.builtinsFor(.int) },
Expand Down
21 changes: 11 additions & 10 deletions src/layout.zig → src/exes/layout.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const builtin = @import("builtin");
const options = @import("options");
const ziggy = @import("ziggy");
const super = @import("superhtml");
const contexts = @import("contexts.zig");
const zine = @import("zine");
const context = zine.context;

const log = std.log.scoped(.layout);
pub const std_options: std.Options = .{
Expand Down Expand Up @@ -115,7 +116,7 @@ pub fn main() !void {
};
};

const ti: []const contexts.Page.Translation = blk: {
const ti: []const context.Page.Translation = blk: {
if (std.mem.eql(u8, translation_index_path, "null")) break :blk &.{};
const bytes = readFile(translation_index_path, arena) catch |err| {
fatal("error while opening the translation index file:\n{s}\n{s}\n", .{
Expand All @@ -124,7 +125,7 @@ pub fn main() !void {
});
};

const ti = ziggy.parseLeaky([]const contexts.Page.Translation, arena, bytes, .{}) catch {
const ti = ziggy.parseLeaky([]const context.Page.Translation, arena, bytes, .{}) catch {
@panic("TODO: error message when a ziggy translation index fails to parse.");
};

Expand All @@ -145,33 +146,33 @@ pub fn main() !void {
var out_buf_writer = std.io.bufferedWriter(out_file.writer());
const out_writer = out_buf_writer.writer();

const site: contexts.Site = .{ .host_url = site_host_url, .title = site_title };
const site: context.Site = .{ .host_url = site_host_url, .title = site_title };

const page = try ziggy.parseLeaky(contexts.Page, arena, page_meta, .{});
const page = try ziggy.parseLeaky(context.Page, arena, page_meta, .{});

var ctx: contexts.Template = .{
var ctx: context.Template = .{
.site = site,
.page = page,
.i18n = i18n,
};
ctx.page.content = rendered_md_string;

if (subpages_meta) |sub| {
ctx.page._meta.subpages = try ziggy.parseLeaky([]const contexts.Page, arena, sub, .{});
ctx.page._meta.subpages = try ziggy.parseLeaky([]const context.Page, arena, sub, .{});
ctx.page._meta.is_section = true;
}

if (prev_meta) |prev| {
ctx.page._meta.prev = try ziggy.parseLeaky(*contexts.Page, arena, prev, .{});
ctx.page._meta.prev = try ziggy.parseLeaky(*context.Page, arena, prev, .{});
}

if (next_meta) |next| {
ctx.page._meta.next = try ziggy.parseLeaky(*contexts.Page, arena, next, .{});
ctx.page._meta.next = try ziggy.parseLeaky(*context.Page, arena, next, .{});
}

ctx.page._meta.translations = ti;

var super_vm = super.VM(contexts.Template, contexts.Value).init(
var super_vm = super.VM(context.Template, context.Value).init(
arena,
&ctx,
layout_name,
Expand Down
7 changes: 4 additions & 3 deletions src/markdown-renderer.zig → src/exes/markdown-renderer.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const std = @import("std");
const ziggy = @import("ziggy");
const contexts = @import("contexts.zig");
const hl = @import("highlight.zig");
const zine = @import("zine");
const context = zine.context;
const hl = zine.highlight;
const highlightCode = hl.highlightCode;
const HtmlSafe = hl.HtmlSafe;

Expand Down Expand Up @@ -52,7 +53,7 @@ pub fn main() !void {

var buf_reader = std.io.bufferedReader(in_file.reader());
const r = buf_reader.reader();
const result = try ziggy.frontmatter.Parser(contexts.Page).parse(arena, r, null);
const result = try ziggy.frontmatter.Parser(context.Page).parse(arena, r, null);
var page = switch (result) {
.success => |s| s.header,
else => unreachable,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions src/fuzz/scripty.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const std = @import("std");
const zine = @import("zine");

export fn zig_fuzz_init() void {}

export fn zig_fuzz_test(buf: [*]u8, len: isize) void {}
2 changes: 2 additions & 0 deletions src/root.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub const context = @import("context.zig");
pub const highlight = @import("highlight.zig");

0 comments on commit f3f355c

Please sign in to comment.