Skip to content

Commit 656b942

Browse files
autodoc: An attempt at generating HTML files from all imported source
files. Files generated from the standard library could be considered for placing with main.js and index.html in lib/docs. Paths should reflect packages in the future.
1 parent a12abc6 commit 656b942

File tree

3 files changed

+467
-1
lines changed

3 files changed

+467
-1
lines changed

lib/docs/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var zigAnalysis;
5151
const domHdrName = document.getElementById("hdrName");
5252
const domHelpModal = document.getElementById("helpModal");
5353
const domSearchPlaceholder = document.getElementById("searchPlaceholder");
54-
const sourceFileUrlTemplate = "/src-viewer/{{file}}#L{{line}}"
54+
const sourceFileUrlTemplate = "src-viewer/{{file}}#L{{line}}"
5555
const domLangRefLink = document.getElementById("langRefLink");
5656

5757
let lineCounter = 1;

src/Autodoc.zig

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Package = @import("Package.zig");
99
const Zir = @import("Zir.zig");
1010
const Ref = Zir.Inst.Ref;
1111
const log = std.log.scoped(.autodoc);
12+
const Docgen = @import("Docgen.zig");
1213

1314
module: *Module,
1415
doc_location: Compilation.EmitLoc,
@@ -266,13 +267,54 @@ pub fn generateZirData(self: *Autodoc) !void {
266267
try buffer.flush();
267268
}
268269

270+
output_dir.makeDir("src-viewer") catch |e| switch (e) {
271+
error.PathAlreadyExists => {},
272+
else => |err| return err,
273+
};
274+
const html_dir = try output_dir.openDir("src-viewer", .{});
275+
276+
var files_iterator = self.files.iterator();
277+
278+
while (files_iterator.next()) |entry| {
279+
const new_html_path = entry.key_ptr.*.sub_file_path;
280+
281+
const html_file = try createFromPath(html_dir, new_html_path);
282+
defer html_file.close();
283+
var buffer = std.io.bufferedWriter(html_file.writer());
284+
285+
const out = buffer.writer();
286+
287+
try Docgen.genHtml(self.module.gpa, entry.key_ptr.*, out);
288+
try buffer.flush();
289+
}
290+
269291
// copy main.js, index.html
270292
var docs_dir = try self.module.comp.zig_lib_directory.handle.openDir("docs", .{});
271293
defer docs_dir.close();
272294
try docs_dir.copyFile("main.js", output_dir, "main.js", .{});
273295
try docs_dir.copyFile("index.html", output_dir, "index.html", .{});
274296
}
275297

298+
fn createFromPath(base_dir: std.fs.Dir, path: []const u8) !std.fs.File {
299+
var path_tokens = std.mem.tokenize(u8, path, std.fs.path.sep_str);
300+
var dir = base_dir;
301+
while (path_tokens.next()) |toc| {
302+
if (path_tokens.peek() != null) {
303+
dir.makeDir(toc) catch |e| switch (e) {
304+
error.PathAlreadyExists => {},
305+
else => |err| return err,
306+
};
307+
dir = try dir.openDir(toc, .{});
308+
} else {
309+
return dir.createFile(toc, .{}) catch |e| switch (e) {
310+
error.PathAlreadyExists => try dir.openFile(toc, .{}),
311+
else => |e| return e,
312+
};
313+
}
314+
}
315+
return error.EmptyPath;
316+
}
317+
276318
/// Represents a chain of scopes, used to resolve decl references to the
277319
/// corresponding entry in `self.decls`.
278320
const Scope = struct {

0 commit comments

Comments
 (0)