authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-01-16 14:19:58+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-01-16 14:19:58+02:00
log4aa33da189264abf07f0b0e446036060c15a79e9
tree03a926bcda759fbf55749a6760d8b8e7258c5c23
parent32544ed56c8049b27a8be75871d5a5d19fed0037
parent29d7da519c97f9a3179d37e245bfa27703e28955
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14303 from perillo/improve-docgen

Improve docgen

2 files changed, 52 insertions(+), 17 deletions(-)

build.zig+1
......@@ -43,6 +43,7 @@ pub fn build(b: *Builder) !void {
4343 ) catch unreachable;
4444 const docgen_cmd = docgen_exe.run();
4545 docgen_cmd.addArgs(&[_][]const u8{
46 "--zig",
4647 rel_zig_exe,
4748 "doc" ++ fs.path.sep_str ++ "langref.html.in",
4849 langref_out_path,
doc/docgen.zig+51-17
......@@ -17,6 +17,24 @@ const obj_ext = builtin.object_format.fileExt(builtin.cpu.arch);
1717const tmp_dir_name = "docgen_tmp";
1818const test_out_path = tmp_dir_name ++ fs.path.sep_str ++ "test" ++ exe_ext;
1919
20const usage =
21 \\Usage: docgen [--zig] [--skip-code-test] input output"
22 \\
23 \\ Generates an HTML document from a docgen template.
24 \\
25 \\Options:
26 \\ -h, --help Print this help and exit
27 \\ --skip-code-test Skip the doctests
28 \\
29;
30
31fn errorf(comptime format: []const u8, args: anytype) noreturn {
32 const stderr = io.getStdErr().writer();
33
34 stderr.print("error: " ++ format, args) catch {};
35 process.exit(1);
36}
37
2038pub fn main() !void {
2139 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
2240 defer arena.deinit();
......@@ -24,38 +42,54 @@ pub fn main() !void {
2442 const allocator = arena.allocator();
2543
2644 var args_it = try process.argsWithAllocator(allocator);
27
2845 if (!args_it.skip()) @panic("expected self arg");
2946
30 const zig_exe = args_it.next() orelse @panic("expected zig exe arg");
31 defer allocator.free(zig_exe);
32
33 const in_file_name = args_it.next() orelse @panic("expected input arg");
34 defer allocator.free(in_file_name);
35
36 const out_file_name = args_it.next() orelse @panic("expected output arg");
37 defer allocator.free(out_file_name);
38
47 var zig_exe: []const u8 = "zig";
3948 var do_code_tests = true;
40 if (args_it.next()) |arg| {
41 if (mem.eql(u8, arg, "--skip-code-tests")) {
42 do_code_tests = false;
49 var files = [_][]const u8{ "", "" };
50
51 var i: usize = 0;
52 while (args_it.next()) |arg| {
53 if (mem.startsWith(u8, arg, "-")) {
54 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
55 const stdout = io.getStdOut().writer();
56 try stdout.writeAll(usage);
57 process.exit(0);
58 } else if (mem.eql(u8, arg, "--zig")) {
59 if (args_it.next()) |param| {
60 zig_exe = param;
61 } else {
62 errorf("expected parameter after --zig\n", .{});
63 }
64 } else if (mem.eql(u8, arg, "--skip-code-tests")) {
65 do_code_tests = false;
66 } else {
67 errorf("unrecognized option: '{s}'\n", .{arg});
68 }
4369 } else {
44 @panic("unrecognized arg");
70 if (i > 1) {
71 errorf("too many arguments\n", .{});
72 }
73 files[i] = arg;
74 i += 1;
4575 }
4676 }
77 if (i < 2) {
78 errorf("not enough arguments\n", .{});
79 process.exit(1);
80 }
4781
48 var in_file = try fs.cwd().openFile(in_file_name, .{ .mode = .read_only });
82 var in_file = try fs.cwd().openFile(files[0], .{ .mode = .read_only });
4983 defer in_file.close();
5084
51 var out_file = try fs.cwd().createFile(out_file_name, .{});
85 var out_file = try fs.cwd().createFile(files[1], .{});
5286 defer out_file.close();
5387
5488 const input_file_bytes = try in_file.reader().readAllAlloc(allocator, max_doc_file_size);
5589
5690 var buffered_writer = io.bufferedWriter(out_file.writer());
5791
58 var tokenizer = Tokenizer.init(in_file_name, input_file_bytes);
92 var tokenizer = Tokenizer.init(files[0], input_file_bytes);
5993 var toc = try genToc(allocator, &tokenizer);
6094
6195 try fs.cwd().makePath(tmp_dir_name);