| ... | @@ -17,6 +17,24 @@ const obj_ext = builtin.object_format.fileExt(builtin.cpu.arch); | ... | @@ -17,6 +17,24 @@ const obj_ext = builtin.object_format.fileExt(builtin.cpu.arch); |
| 17 | const tmp_dir_name = "docgen_tmp"; | 17 | const tmp_dir_name = "docgen_tmp"; |
| 18 | const test_out_path = tmp_dir_name ++ fs.path.sep_str ++ "test" ++ exe_ext; | 18 | const test_out_path = tmp_dir_name ++ fs.path.sep_str ++ "test" ++ exe_ext; |
| 19 | | 19 | |
| | 20 | const 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 | |
| | 31 | fn 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 | |
| 20 | pub fn main() !void { | 38 | pub fn main() !void { |
| 21 | var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | 39 | var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); |
| 22 | defer arena.deinit(); | 40 | defer arena.deinit(); |
| ... | @@ -24,33 +42,54 @@ pub fn main() !void { | ... | @@ -24,33 +42,54 @@ pub fn main() !void { |
| 24 | const allocator = arena.allocator(); | 42 | const allocator = arena.allocator(); |
| 25 | | 43 | |
| 26 | var args_it = try process.argsWithAllocator(allocator); | 44 | var args_it = try process.argsWithAllocator(allocator); |
| 27 | | | |
| 28 | if (!args_it.skip()) @panic("expected self arg"); | 45 | if (!args_it.skip()) @panic("expected self arg"); |
| 29 | | 46 | |
| 30 | const zig_exe = args_it.next() orelse @panic("expected zig exe arg"); | 47 | var zig_exe: []const u8 = "zig"; |
| 31 | const in_file_name = args_it.next() orelse @panic("expected input arg"); | | |
| 32 | const out_file_name = args_it.next() orelse @panic("expected output arg"); | | |
| 33 | | | |
| 34 | var do_code_tests = true; | 48 | var do_code_tests = true; |
| 35 | if (args_it.next()) |arg| { | 49 | var files = [_][]const u8{ "", "" }; |
| 36 | if (mem.eql(u8, arg, "--skip-code-tests")) { | 50 | |
| 37 | do_code_tests = false; | 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 | } |
| 38 | } else { | 69 | } else { |
| 39 | @panic("unrecognized arg"); | 70 | if (i > 1) { |
| | 71 | errorf("too many arguments\n", .{}); |
| | 72 | } |
| | 73 | files[i] = arg; |
| | 74 | i += 1; |
| 40 | } | 75 | } |
| 41 | } | 76 | } |
| | 77 | if (i < 2) { |
| | 78 | errorf("not enough arguments\n", .{}); |
| | 79 | process.exit(1); |
| | 80 | } |
| 42 | | 81 | |
| 43 | 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 }); |
| 44 | defer in_file.close(); | 83 | defer in_file.close(); |
| 45 | | 84 | |
| 46 | var out_file = try fs.cwd().createFile(out_file_name, .{}); | 85 | var out_file = try fs.cwd().createFile(files[1], .{}); |
| 47 | defer out_file.close(); | 86 | defer out_file.close(); |
| 48 | | 87 | |
| 49 | const input_file_bytes = try in_file.reader().readAllAlloc(allocator, max_doc_file_size); | 88 | const input_file_bytes = try in_file.reader().readAllAlloc(allocator, max_doc_file_size); |
| 50 | | 89 | |
| 51 | var buffered_writer = io.bufferedWriter(out_file.writer()); | 90 | var buffered_writer = io.bufferedWriter(out_file.writer()); |
| 52 | | 91 | |
| 53 | var tokenizer = Tokenizer.init(in_file_name, input_file_bytes); | 92 | var tokenizer = Tokenizer.init(files[0], input_file_bytes); |
| 54 | var toc = try genToc(allocator, &tokenizer); | 93 | var toc = try genToc(allocator, &tokenizer); |
| 55 | | 94 | |
| 56 | try fs.cwd().makePath(tmp_dir_name); | 95 | try fs.cwd().makePath(tmp_dir_name); |