@@ -9,6 +9,7 @@ const Package = @import("Package.zig");
99const Zir = @import ("Zir.zig" );
1010const Ref = Zir .Inst .Ref ;
1111const log = std .log .scoped (.autodoc );
12+ const Docgen = @import ("Docgen.zig" );
1213
1314module : * Module ,
1415doc_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`.
278320const Scope = struct {
0 commit comments