Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## REQUIREMENTS:

- zig 0.13.0
- zig 0.14.0

## HOW TO EDIT WITH LIVE-PREVIEW & COMMIT DOCS

Expand Down
Binary file removed assets/main.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions assets/zml_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,3 +983,4 @@
wasmArray.set(jsArray);
}
})();

11 changes: 8 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ pub fn build(b: *std.Build) !void {

// the zine dev server needs this option!!!
const opt_debug = b.option(bool, "debug", "zine debug, unused") orelse true;
const include_drafts = b.option(
bool,
"include-drafts",
"Include drafts in zine output",
) orelse false;

const pcre2_dep = b.dependency("pcre2", .{
.target = target,
Expand All @@ -15,7 +20,7 @@ pub fn build(b: *std.Build) !void {
});

const docs_wasm = try buildDocsWasm(b, optimize);
const website_step, const serve_step = try buildWebSite(b, docs_wasm, opt_debug);
const website_step, const serve_step = try buildWebSite(b, docs_wasm, opt_debug, include_drafts);
// has to be run with zig build website
_ = website_step;
// has to be run with zig build serve
Expand Down Expand Up @@ -54,7 +59,7 @@ fn buildDocsWasm(b: *std.Build, optimize: std.builtin.OptimizeMode) !*std.Build.
return docs_wasm;
}

fn buildWebSite(b: *std.Build, docs_wasm: *std.Build.Step.Compile, debug: bool) !struct {
fn buildWebSite(b: *std.Build, docs_wasm: *std.Build.Step.Compile, debug: bool, include_drafts: bool) !struct {
*std.Build.Step,
*std.Build.Step,
} {
Expand Down Expand Up @@ -107,7 +112,7 @@ fn buildWebSite(b: *std.Build, docs_wasm: *std.Build.Step.Compile, debug: bool)
"website",
"Builds the website",
);
zine.addWebsite(b, opts, website_step, site);
zine.addWebsite(b, opts, website_step, site, include_drafts);

const serve_step = b.step(
"serve",
Expand Down
11 changes: 4 additions & 7 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
// .hash = "1220a59c2cf0e42a6f42cf1bf9b6a6c2691abfd91fcafa0e81a6282c4a197d05c41a",

// modified zine
.url = "git+https://github.com/renerocksai/zine#170567737efc5333b38886cfe83211e011267b24",
.hash = "1220ec97928f9e1f1a95ff8c39a943cf6ebab2ff2e6acb1900aeb7844f906b1ce3aa",

// rene local dev
// .path = "../../renerocksai/zine",
.url = "https://github.com/renerocksai/zine/archive/refs/tags/zig-0.14.0.tar.gz",
.hash = "1220dfe5a9352fc7c9c16b07c5c6d7417fc96ee92dbab5f27b8d4d02400ba5a9f88c",
},
.pcre2 = .{
.url = "https://github.com/renerocksai/pcre2/archive/refs/tags/pcre2-10.45.tar.gz",
.hash = "12207d56c8b27a141c793fc596af11e6eca919efbbcf49b04f7803c603e2f6081b1b",
.url = "https://github.com/renerocksai/pcre2/archive/refs/tags/zig-0.14.tar.gz",
.hash = "12202a2e5043bd8641208b0eb597bc0346fd94fdec5b1ae92ee0096e83d6ff33fc45",
},
},
.paths = .{"."},
Expand Down
36 changes: 0 additions & 36 deletions zig_docs/Decl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ file: Walk.File.Index,
/// The decl whose namespace this is in.
parent: Index,

/// Delete this to find out where URL escaping needs to be added.
const missing_feature_url_escape = true;

pub const ExtraInfo = struct {
is_pub: bool,
name: []const u8,
Expand Down Expand Up @@ -160,16 +157,6 @@ pub fn fqn(decl: *const Decl, out: *std.ArrayListUnmanaged(u8)) Oom!void {
}
}

threadlocal var _fqn_buf: [1024]u8 = undefined;

pub fn fqnAsHref(decl: *const Decl, allocator: std.mem.Allocator, out: *std.ArrayListUnmanaged(u8)) Oom!void {
var _fqn = std.ArrayListUnmanaged(u8).fromOwnedSlice(&_fqn_buf);
_fqn.items.len = 0;
try decl.fqn(&_fqn);

try renderHref(out.writer(allocator), _fqn.items);
}

pub fn reset_with_path(decl: *const Decl, list: *std.ArrayListUnmanaged(u8)) Oom!void {
list.clearRetainingCapacity();
try append_path(decl, list);
Expand Down Expand Up @@ -237,26 +224,3 @@ pub fn find(search_string: []const u8) Decl.Index {
}
return current_decl_index;
}

/// Replace a fully qualified name from Zig std symbol by an external link.
/// fqn is not allowed to alias to out.
pub fn renderHref(writer: anytype, noalias name: []const u8) !void {
std.log.debug("checking fqn: \"{s}\"", .{name});

const std_module = std.mem.indexOf(u8, name, ".std.");
const std_root = if (std.mem.endsWith(u8, name, ".std")) name.len - 4 else null;

if (std_module orelse std_root) |std_dot_start| {
const std_href = " href=\"https://ziglang.org/documentation/0.13.0/std/#";
try writer.writeAll(std_href);
_ = missing_feature_url_escape;
try writer.writeAll(name[std_dot_start + 1 ..]);
try writer.writeByte('"');
std.log.debug("replaced name: \"{s}\" -> {s}{s}", .{ name, std_href, name });
} else {
try writer.writeAll(" href=\"#");
_ = missing_feature_url_escape;
try writer.writeAll(name);
try writer.writeByte('"');
}
}
22 changes: 11 additions & 11 deletions zig_docs/Walk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const Oom = error{OutOfMemory};

pub const Decl = @import("Decl.zig");

pub var files: std.StringArrayHashMapUnmanaged(File) = .{};
pub var decls: std.ArrayListUnmanaged(Decl) = .{};
pub var modules: std.StringArrayHashMapUnmanaged(File.Index) = .{};
pub var files: std.StringArrayHashMapUnmanaged(File) = .empty;
pub var decls: std.ArrayListUnmanaged(Decl) = .empty;
pub var modules: std.StringArrayHashMapUnmanaged(File.Index) = .empty;

file: File.Index,

Expand All @@ -36,23 +36,23 @@ pub const Category = union(enum(u8)) {
/// A function that returns a type.
type_function: Ast.Node.Index,

pub const Tag = @typeInfo(Category).Union.tag_type.?;
pub const Tag = @typeInfo(Category).@"union".tag_type.?;
};

pub const File = struct {
ast: Ast,
/// Maps identifiers to the declarations they point to.
ident_decls: std.AutoArrayHashMapUnmanaged(Ast.TokenIndex, Ast.Node.Index) = .{},
ident_decls: std.AutoArrayHashMapUnmanaged(Ast.TokenIndex, Ast.Node.Index) = .empty,
/// Maps field access identifiers to the containing field access node.
token_parents: std.AutoArrayHashMapUnmanaged(Ast.TokenIndex, Ast.Node.Index) = .{},
token_parents: std.AutoArrayHashMapUnmanaged(Ast.TokenIndex, Ast.Node.Index) = .empty,
/// Maps declarations to their global index.
node_decls: std.AutoArrayHashMapUnmanaged(Ast.Node.Index, Decl.Index) = .{},
node_decls: std.AutoArrayHashMapUnmanaged(Ast.Node.Index, Decl.Index) = .empty,
/// Maps function declarations to doctests.
doctests: std.AutoArrayHashMapUnmanaged(Ast.Node.Index, Ast.Node.Index) = .{},
doctests: std.AutoArrayHashMapUnmanaged(Ast.Node.Index, Ast.Node.Index) = .empty,
/// root node => its namespace scope
/// struct/union/enum/opaque decl node => its namespace scope
/// local var decl node => its local variable scope
scopes: std.AutoArrayHashMapUnmanaged(Ast.Node.Index, *Scope) = .{},
scopes: std.AutoArrayHashMapUnmanaged(Ast.Node.Index, *Scope) = .empty,

pub fn lookup_token(file: *File, token: Ast.TokenIndex) Decl.Index {
const decl_node = file.ident_decls.get(token) orelse return .none;
Expand Down Expand Up @@ -464,8 +464,8 @@ pub const Scope = struct {
const Namespace = struct {
base: Scope = .{ .tag = .namespace },
parent: *Scope,
names: std.StringArrayHashMapUnmanaged(Ast.Node.Index) = .{},
doctests: std.StringArrayHashMapUnmanaged(Ast.Node.Index) = .{},
names: std.StringArrayHashMapUnmanaged(Ast.Node.Index) = .empty,
doctests: std.StringArrayHashMapUnmanaged(Ast.Node.Index) = .empty,
decl_index: Decl.Index,
};

Expand Down
48 changes: 39 additions & 9 deletions zig_docs/html_render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@ const Decl = Walk.Decl;
const gpa = std.heap.wasm_allocator;
const Oom = error{OutOfMemory};

/// Delete this to find out where URL escaping needs to be added.
pub const missing_feature_url_escape = true;

pub const RenderSourceOptions = struct {
skip_doc_comments: bool = false,
skip_comments: bool = false,
collapse_whitespace: bool = false,
fn_link: Decl.Index = .none,
/// Assumed to be sorted ascending.
source_location_annotations: []const Annotation = &.{},
/// Concatenated with dom_id.
annotation_prefix: []const u8 = "l",
};

pub const Annotation = struct {
file_byte_offset: u32,
/// Concatenated with annotation_prefix.
dom_id: u32,
};

pub fn fileSourceHtml(
Expand All @@ -25,7 +38,7 @@ pub fn fileSourceHtml(
const file = file_index.get();

const g = struct {
var field_access_buffer: std.ArrayListUnmanaged(u8) = .{};
var field_access_buffer: std.ArrayListUnmanaged(u8) = .empty;
};

const token_tags = ast.tokens.items(.tag);
Expand All @@ -48,6 +61,8 @@ pub fn fileSourceHtml(
}
}

var next_annotate_index: usize = 0;

for (
token_tags[start_token..end_token],
token_starts[start_token..end_token],
Expand All @@ -71,6 +86,18 @@ pub fn fileSourceHtml(
if (tag == .eof) break;
const slice = ast.tokenSlice(token_index);
cursor = start + slice.len;

// Insert annotations.
while (true) {
if (next_annotate_index >= options.source_location_annotations.len) break;
const next_annotation = options.source_location_annotations[next_annotate_index];
if (cursor <= next_annotation.file_byte_offset) break;
try out.writer(gpa).print("<span id=\"{s}{d}\"></span>", .{
options.annotation_prefix, next_annotation.dom_id,
});
next_annotate_index += 1;
}

switch (tag) {
.eof => unreachable,

Expand Down Expand Up @@ -159,8 +186,9 @@ pub fn fileSourceHtml(
const fn_link = options.fn_link.get();
const fn_token = main_tokens[fn_link.ast_node];
if (token_index == fn_token + 1) {
try out.appendSlice(gpa, "<a class=\"tok-fn\"");
try fn_link.fqnAsHref(gpa, out);
try out.appendSlice(gpa, "<a class=\"tok-fn\" href=\"#");
_ = missing_feature_url_escape;
try fn_link.fqn(out);
try out.appendSlice(gpa, "\">");
try appendEscaped(out, slice);
try out.appendSlice(gpa, "</a>");
Expand Down Expand Up @@ -193,9 +221,10 @@ pub fn fileSourceHtml(
g.field_access_buffer.clearRetainingCapacity();
try walkFieldAccesses(file_index, &g.field_access_buffer, field_access_node);
if (g.field_access_buffer.items.len > 0) {
try out.appendSlice(gpa, "<a");
try Decl.renderHref(out.writer(gpa), g.field_access_buffer.items);
try out.appendSlice(gpa, ">");
try out.appendSlice(gpa, "<a href=\"#");
_ = missing_feature_url_escape;
try out.appendSlice(gpa, g.field_access_buffer.items);
try out.appendSlice(gpa, "\">");
try appendEscaped(out, slice);
try out.appendSlice(gpa, "</a>");
} else {
Expand All @@ -208,9 +237,10 @@ pub fn fileSourceHtml(
g.field_access_buffer.clearRetainingCapacity();
try resolveIdentLink(file_index, &g.field_access_buffer, token_index);
if (g.field_access_buffer.items.len > 0) {
try out.appendSlice(gpa, "<a");
try Decl.renderHref(out.writer(gpa), g.field_access_buffer.items);
try out.appendSlice(gpa, ">");
try out.appendSlice(gpa, "<a href=\"#");
_ = missing_feature_url_escape;
try out.appendSlice(gpa, g.field_access_buffer.items);
try out.appendSlice(gpa, "\">");
try appendEscaped(out, slice);
try out.appendSlice(gpa, "</a>");
break :i;
Expand Down
Loading