authorgravatar for pfg@pfg.pwpfg <pfg@pfg.pw> 2021-10-01 15:43:23-04:00
committergravatar for pfg@pfg.pwpfg <pfg@pfg.pw> 2021-10-01 15:43:23-04:00
logde92925897e4bf10f58a081d850e3f7eb265e9b6
tree7f490721dbd02e938d4388523bdd9d5ae316b5f2
parentc4cd592f0e1eeff5a4056796610d97010ae4e38c

Add line numbers to langref (fixes #9870)


2 files changed, 40 insertions(+), 11 deletions(-)

doc/docgen.zig+27-11
...@@ -864,6 +864,21 @@ fn isType(name: []const u8) bool {...@@ -864,6 +864,21 @@ fn isType(name: []const u8) bool {
864 return false;864 return false;
865}865}
866866
867const start_line = "<span class=\"line\">";
868const end_line = "</span>";
869
870fn writeEscapedLines(out: anytype, text: []const u8) !void {
871 for (text) |char| {
872 if (char == '\n') {
873 try out.writeAll(end_line);
874 try out.writeAll("\n");
875 try out.writeAll(start_line);
876 } else {
877 try writeEscaped(out, &[_]u8{char});
878 }
879 }
880}
881
867fn tokenizeAndPrintRaw(882fn tokenizeAndPrintRaw(
868 allocator: *Allocator,883 allocator: *Allocator,
869 docgen_tokenizer: *Tokenizer,884 docgen_tokenizer: *Tokenizer,
...@@ -873,7 +888,8 @@ fn tokenizeAndPrintRaw(...@@ -873,7 +888,8 @@ fn tokenizeAndPrintRaw(
873) !void {888) !void {
874 const src_non_terminated = mem.trim(u8, raw_src, " \n");889 const src_non_terminated = mem.trim(u8, raw_src, " \n");
875 const src = try allocator.dupeZ(u8, src_non_terminated);890 const src = try allocator.dupeZ(u8, src_non_terminated);
876 try out.writeAll("<code>");891
892 try out.writeAll("<code>" ++ start_line);
877 var tokenizer = std.zig.Tokenizer.init(src);893 var tokenizer = std.zig.Tokenizer.init(src);
878 var index: usize = 0;894 var index: usize = 0;
879 var next_tok_is_fn = false;895 var next_tok_is_fn = false;
...@@ -888,7 +904,7 @@ fn tokenizeAndPrintRaw(...@@ -888,7 +904,7 @@ fn tokenizeAndPrintRaw(
888 const comment_end_off = mem.indexOf(u8, src[comment_start..token.loc.start], "\n");904 const comment_end_off = mem.indexOf(u8, src[comment_start..token.loc.start], "\n");
889 const comment_end = if (comment_end_off) |o| comment_start + o else token.loc.start;905 const comment_end = if (comment_end_off) |o| comment_start + o else token.loc.start;
890906
891 try writeEscaped(out, src[index..comment_start]);907 try writeEscapedLines(out, src[index..comment_start]);
892 try out.writeAll("<span class=\"tok-comment\">");908 try out.writeAll("<span class=\"tok-comment\">");
893 try writeEscaped(out, src[comment_start..comment_end]);909 try writeEscaped(out, src[comment_start..comment_end]);
894 try out.writeAll("</span>");910 try out.writeAll("</span>");
...@@ -897,7 +913,7 @@ fn tokenizeAndPrintRaw(...@@ -897,7 +913,7 @@ fn tokenizeAndPrintRaw(
897 continue;913 continue;
898 }914 }
899915
900 try writeEscaped(out, src[index..token.loc.start]);916 try writeEscapedLines(out, src[index..token.loc.start]);
901 switch (token.tag) {917 switch (token.tag) {
902 .eof => break,918 .eof => break,
903919
...@@ -1103,7 +1119,7 @@ fn tokenizeAndPrintRaw(...@@ -1103,7 +1119,7 @@ fn tokenizeAndPrintRaw(
1103 }1119 }
1104 index = token.loc.end;1120 index = token.loc.end;
1105 }1121 }
1106 try out.writeAll("</code>");1122 try out.writeAll(end_line ++ "</code>");
1107}1123}
11081124
1109fn tokenizeAndPrint(1125fn tokenizeAndPrint(
...@@ -1126,9 +1142,9 @@ fn printSourceBlock(allocator: *Allocator, docgen_tokenizer: *Tokenizer, out: an...@@ -1126,9 +1142,9 @@ fn printSourceBlock(allocator: *Allocator, docgen_tokenizer: *Tokenizer, out: an
1126 const raw_source = docgen_tokenizer.buffer[syntax_block.source_token.start..syntax_block.source_token.end];1142 const raw_source = docgen_tokenizer.buffer[syntax_block.source_token.start..syntax_block.source_token.end];
1127 const trimmed_raw_source = mem.trim(u8, raw_source, " \n");1143 const trimmed_raw_source = mem.trim(u8, raw_source, " \n");
11281144
1129 try out.writeAll("<code>");1145 try out.writeAll("<code>" ++ start_line);
1130 try writeEscaped(out, trimmed_raw_source);1146 try writeEscapedLines(out, trimmed_raw_source);
1131 try out.writeAll("</code>");1147 try out.writeAll(end_line ++ "</code>");
1132 },1148 },
1133 }1149 }
1134 try out.writeAll("</pre></figure>");1150 try out.writeAll("</pre></figure>");
...@@ -1142,15 +1158,15 @@ fn printShell(out: anytype, shell_content: []const u8) !void {...@@ -1142,15 +1158,15 @@ fn printShell(out: anytype, shell_content: []const u8) !void {
1142 while (iter.next()) |orig_line| {1158 while (iter.next()) |orig_line| {
1143 const line = mem.trimRight(u8, orig_line, " ");1159 const line = mem.trimRight(u8, orig_line, " ");
1144 if (!cmd_cont and line.len > 1 and mem.eql(u8, line[0..2], "$ ") and line[line.len - 1] != '\\') {1160 if (!cmd_cont and line.len > 1 and mem.eql(u8, line[0..2], "$ ") and line[line.len - 1] != '\\') {
1145 try out.print("$ <kbd>{s}</kbd>\n", .{std.mem.trimLeft(u8, line[1..], " ")});1161 try out.print(start_line ++ "$ <kbd>{s}</kbd>" ++ end_line ++ "\n", .{std.mem.trimLeft(u8, line[1..], " ")});
1146 } else if (!cmd_cont and line.len > 1 and mem.eql(u8, line[0..2], "$ ") and line[line.len - 1] == '\\') {1162 } else if (!cmd_cont and line.len > 1 and mem.eql(u8, line[0..2], "$ ") and line[line.len - 1] == '\\') {
1147 try out.print("$ <kbd>{s}\n", .{std.mem.trimLeft(u8, line[1..], " ")});1163 try out.print(start_line ++ "$ <kbd>{s}" ++ end_line ++ "\n", .{std.mem.trimLeft(u8, line[1..], " ")});
1148 cmd_cont = true;1164 cmd_cont = true;
1149 } else if (line.len > 0 and line[line.len - 1] != '\\' and cmd_cont) {1165 } else if (line.len > 0 and line[line.len - 1] != '\\' and cmd_cont) {
1150 try out.print("{s}</kbd>\n", .{line});1166 try out.print(start_line ++ "{s}</kbd>" ++ end_line ++ "\n", .{line});
1151 cmd_cont = false;1167 cmd_cont = false;
1152 } else {1168 } else {
1153 try out.print("{s}\n", .{line});1169 try out.print(start_line ++ "{s}" ++ end_line ++ "\n", .{line});
1154 }1170 }
1155 }1171 }
11561172
doc/langref.html.in+13
...@@ -274,6 +274,19 @@...@@ -274,6 +274,19 @@
274 color: #fff;274 color: #fff;
275 }275 }
276 }276 }
277
278 pre {
279 counter-reset: line;
280 }
281 pre span.line:before {
282 counter-increment: line;
283 content: counter(line);
284 display: inline-block;
285 padding-right: 1em;
286 width: 2em;
287 text-align: right;
288 color: #999;
289 }
277 </style>290 </style>
278</head>291</head>
279<body>292<body>