|
| 1 | +//! This module is a help generator for keybind actions documentation. |
| 2 | +//! It can generate documentation in different formats (plaintext for CLI, |
| 3 | +//! markdown for website) while maintaining consistent content. |
| 4 | + |
| 5 | +const std = @import("std"); |
| 6 | +const KeybindAction = @import("Binding.zig").Action; |
| 7 | +const help_strings = @import("help_strings"); |
| 8 | + |
| 9 | +/// Format options for generating keybind actions documentation |
| 10 | +pub const Format = enum { |
| 11 | + /// Plain text output with indentation |
| 12 | + plaintext, |
| 13 | + /// Markdown formatted output |
| 14 | + markdown, |
| 15 | + |
| 16 | + fn formatFieldName(self: Format, writer: anytype, field_name: []const u8) !void { |
| 17 | + switch (self) { |
| 18 | + .plaintext => { |
| 19 | + try writer.writeAll(field_name); |
| 20 | + try writer.writeAll(":\n"); |
| 21 | + }, |
| 22 | + .markdown => { |
| 23 | + try writer.writeAll("## `"); |
| 24 | + try writer.writeAll(field_name); |
| 25 | + try writer.writeAll("`\n"); |
| 26 | + }, |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + fn formatDocLine(self: Format, writer: anytype, line: []const u8) !void { |
| 31 | + switch (self) { |
| 32 | + .plaintext => { |
| 33 | + try writer.appendSlice(" "); |
| 34 | + try writer.appendSlice(line); |
| 35 | + try writer.appendSlice("\n"); |
| 36 | + }, |
| 37 | + .markdown => { |
| 38 | + try writer.appendSlice(line); |
| 39 | + try writer.appendSlice("\n"); |
| 40 | + }, |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + fn header(self: Format) ?[]const u8 { |
| 45 | + return switch (self) { |
| 46 | + .plaintext => null, |
| 47 | + .markdown => |
| 48 | + \\--- |
| 49 | + \\title: Keybinding Action Reference |
| 50 | + \\description: Reference of all Ghostty keybinding actions. |
| 51 | + \\editOnGithubLink: https://github.com/ghostty-org/ghostty/edit/main/src/input/Binding.zig |
| 52 | + \\--- |
| 53 | + \\ |
| 54 | + \\This is a reference of all Ghostty keybinding actions. |
| 55 | + \\ |
| 56 | + \\ |
| 57 | + , |
| 58 | + }; |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +/// Generate keybind actions documentation with the specified format |
| 63 | +pub fn generate( |
| 64 | + writer: anytype, |
| 65 | + format: Format, |
| 66 | + page_allocator: std.mem.Allocator, |
| 67 | +) !void { |
| 68 | + if (format.header()) |header| { |
| 69 | + try writer.writeAll(header); |
| 70 | + } |
| 71 | + |
| 72 | + var buffer = std.ArrayList(u8).init(page_allocator); |
| 73 | + defer buffer.deinit(); |
| 74 | + |
| 75 | + const fields = @typeInfo(KeybindAction).Union.fields; |
| 76 | + inline for (fields) |field| { |
| 77 | + if (field.name[0] == '_') continue; |
| 78 | + |
| 79 | + // Write previously stored doc comment below all related actions |
| 80 | + if (@hasDecl(help_strings.KeybindAction, field.name)) { |
| 81 | + try writer.writeAll(buffer.items); |
| 82 | + try writer.writeAll("\n"); |
| 83 | + |
| 84 | + buffer.clearRetainingCapacity(); |
| 85 | + } |
| 86 | + |
| 87 | + try format.formatFieldName(writer, field.name); |
| 88 | + |
| 89 | + if (@hasDecl(help_strings.KeybindAction, field.name)) { |
| 90 | + var iter = std.mem.splitScalar( |
| 91 | + u8, |
| 92 | + @field(help_strings.KeybindAction, field.name), |
| 93 | + '\n', |
| 94 | + ); |
| 95 | + while (iter.next()) |s| { |
| 96 | + // If it is the last line and empty, then skip it. |
| 97 | + if (iter.peek() == null and s.len == 0) continue; |
| 98 | + try format.formatDocLine(&buffer, s); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Write any remaining buffered documentation |
| 104 | + if (buffer.items.len > 0) { |
| 105 | + try writer.writeAll(buffer.items); |
| 106 | + } |
| 107 | +} |
0 commit comments