Skip to content

Commit

Permalink
Option to include Drafts in Output (#70)
Browse files Browse the repository at this point in the history
* Added Option to include Drafts in Output

* Added option to dev server
  • Loading branch information
nihklas authored Oct 16, 2024
1 parent 36e5751 commit d84acc3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
25 changes: 22 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,17 @@ pub fn website(b: *std.Build, site: Site) void {
// Setup debug flags if the user enabled Zine debug.
const opts = zine.defaultZineOptions(b, site.debug);

const include_drafts = b.option(
bool,
"include-drafts",
"Include drafts in output",
) orelse false;

const website_step = b.step(
"website",
"Builds the website",
);
zine.addWebsite(b, opts, website_step, site);
zine.addWebsite(b, opts, website_step, site, include_drafts);

// Invoking the default step also builds the website
b.getInstallStep().dependOn(website_step);
Expand All @@ -182,6 +188,7 @@ pub fn website(b: *std.Build, site: Site) void {
.website_step = website_step,
.host = "localhost",
.port = port,
.include_drafts = include_drafts,
.input_dirs = &.{
site.layouts_dir_path,
site.content_dir_path,
Expand All @@ -206,11 +213,17 @@ pub fn multilingualWebsite(b: *std.Build, multi: MultilingualSite) void {
// Setup debug flags if the user enabled Zine debug.
const opts = zine.defaultZineOptions(b, multi.debug);

const include_drafts = b.option(
bool,
"include-drafts",
"Include drafts in output",
) orelse false;

const website_step = b.step(
"website",
"Builds the website",
);
zine.addMultilingualWebsite(b, website_step, multi, opts);
zine.addMultilingualWebsite(b, website_step, multi, opts, include_drafts);

// Invoking the default step also builds the website
b.getInstallStep().dependOn(website_step);
Expand Down Expand Up @@ -253,32 +266,37 @@ pub fn addWebsite(
opts: ZineOptions,
step: *std.Build.Step,
site: Site,
include_drafts: bool,
) void {
@import("build/content.zig").addWebsiteImpl(
b,
opts,
step,
.{ .site = site },
include_drafts,
);
}
pub fn addMultilingualWebsite(
b: *std.Build,
step: *std.Build.Step,
multi: MultilingualSite,
opts: ZineOptions,
include_drafts: bool,
) void {
@import("build/content.zig").addWebsiteImpl(
b,
opts,
step,
.{ .multilingual = multi },
include_drafts,
);
}

pub const DevelopmentServerOptions = struct {
website_step: *std.Build.Step,
host: []const u8,
port: u16 = 1990,
include_drafts: bool = false,
input_dirs: []const []const u8,
};
pub fn addDevelopmentServer(
Expand All @@ -299,9 +317,10 @@ pub fn addDevelopmentServer(
run_server.addArg(b.fmt("{d}", .{server_opts.port})); // #3
run_server.addArg(server_opts.website_step.name); // #4
run_server.addArg(@tagName(zine_opts.optimize)); // #5
run_server.addArg(b.fmt("{}", .{server_opts.include_drafts})); // #6

for (server_opts.input_dirs) |dir| {
run_server.addArg(dir); // #6..
run_server.addArg(dir); // #7..
}

if (server_opts.website_step.id != .top_level) {
Expand Down
11 changes: 8 additions & 3 deletions build/content.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ pub fn addWebsiteImpl(
opts: zine.ZineOptions,
step: *std.Build.Step,
web: AddWebsiteOptions,
include_drafts: bool,
) void {
const zine_dep = project.dependencyFromBuildZig(zine, .{
.optimize = opts.optimize,
.scope = opts.scopes,
});

// Scan the content folder
scan(project, step, opts.optimize == .Debug, zine_dep, web);
scan(project, step, opts.optimize == .Debug, zine_dep, web, include_drafts);
}

fn scan(
Expand All @@ -37,6 +38,7 @@ fn scan(
debug: bool,
zine_dep: *std.Build.Dependency,
website: AddWebsiteOptions,
include_drafts: bool,
) void {
const index_dir_path = project.pathJoin(&.{
project.cache_root.path orelse ".",
Expand Down Expand Up @@ -112,6 +114,7 @@ fn scan(
debug,
v.content_dir_path,
url_path_prefix,
include_drafts,
);
sv.output_path_prefix = output_path_prefix;
sv.url_path_prefix = url_path_prefix;
Expand Down Expand Up @@ -192,6 +195,7 @@ fn scan(
debug,
s.content_dir_path,
s.output_path_prefix,
include_drafts,
);

addAllSteps(
Expand Down Expand Up @@ -394,6 +398,7 @@ pub fn scanVariant(
debug: bool,
content_dir_path: []const u8,
url_path_prefix: []const u8,
include_drafts: bool,
) ScannedVariant {
var t = std.time.Timer.start() catch unreachable;
defer if (debug) std.debug.print(
Expand Down Expand Up @@ -466,7 +471,7 @@ pub fn scanVariant(
},
};

if (fm.draft) break :blk;
if (!include_drafts and fm.draft) break :blk;

// This is going to be null only for 'content/index.md'
if (dir_entry.parent_section) |parent_section| {
Expand Down Expand Up @@ -546,7 +551,7 @@ pub fn scanVariant(
},
};

if (fm.draft) continue;
if (!include_drafts and fm.draft) continue;

current_section.pages.append(project.allocator, .{
.content_sub_path = project.dupe(dir_entry.path),
Expand Down
32 changes: 23 additions & 9 deletions src/exes/server/Reloader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ zig_exe: []const u8,
out_dir_path: []const u8,
website_step_name: []const u8,
debug: bool,
include_drafts: bool,
watcher: Watcher,

clients_lock: std.Thread.Mutex = .{},
Expand All @@ -31,6 +32,7 @@ pub fn init(
in_dir_paths: []const []const u8,
website_step_name: []const u8,
debug: bool,
include_drafts: bool,
) !Reloader {
const ws_server = try ws.Server.init(gpa, .{});

Expand All @@ -41,6 +43,7 @@ pub fn init(
.ws_server = ws_server,
.website_step_name = website_step_name,
.debug = debug,
.include_drafts = include_drafts,
.watcher = try Watcher.init(gpa, out_dir_path, in_dir_paths),
};
}
Expand All @@ -52,15 +55,26 @@ pub fn listen(self: *Reloader) !void {
pub fn onInputChange(self: *Reloader, path: []const u8, name: []const u8) void {
_ = name;
_ = path;
const args: []const []const u8 = if (self.debug) &.{
self.zig_exe,
"build",
self.website_step_name,
"-Ddebug",
} else &.{
self.zig_exe,
"build",
self.website_step_name,
const args: []const []const u8 = blk: {
var args_count: usize = 3;
var all_args: [5][]const u8 = .{
self.zig_exe,
"build",
self.website_step_name,
"",
"",
};

if (self.include_drafts) {
all_args[args_count] = "-Dinclude-drafts";
args_count += 1;
}
if (self.debug) {
all_args[args_count] = "-Ddebug";
args_count += 1;
}

break :blk all_args[0..args_count];
};
log.debug("re-building! args: {s}", .{args});

Expand Down
4 changes: 3 additions & 1 deletion src/exes/server/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,9 @@ pub fn main() !void {
};
const rebuild_step_name = args[4];
const debug = std.mem.eql(u8, args[5], "Debug");
const include_drafts = std.mem.eql(u8, args[6], "true");

const input_dirs = args[6..];
const input_dirs = args[7..];

// ensure the path exists. without this, an empty website that
// doesn't generate a zig-out/ will cause the server to error out
Expand All @@ -240,6 +241,7 @@ pub fn main() !void {
input_dirs,
rebuild_step_name,
debug,
include_drafts,
);

var server: Server = .{
Expand Down

0 comments on commit d84acc3

Please sign in to comment.