Skip to content

Commit 1992734

Browse files
authored
feat(graindoc): Pretty markdown tables (#2292)
1 parent f8a8f7f commit 1992734

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+6798
-6768
lines changed

compiler/src/utils/markdown.re

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,30 @@ let code = code => {
1515
Format.sprintf("`%s`", code);
1616
};
1717

18+
let compute_column_widths = (headers, rows) => {
19+
// Minimum markdown column width is 3
20+
let column_widths =
21+
Array.of_list(
22+
List.map(header => max(String.length(header), 3), headers),
23+
);
24+
List.iter(
25+
row => {
26+
List.iteri(
27+
(idx, cell) => {
28+
column_widths[idx] = max(column_widths[idx], String.length(cell))
29+
},
30+
row,
31+
)
32+
},
33+
rows,
34+
);
35+
column_widths;
36+
};
37+
1838
let table = (~headers: list(string), rows) => {
1939
let header_len = List.length(headers);
2040
let all_match = List.for_all(row => List.length(row) == header_len, rows);
2141
if (all_match) {
22-
let header = String.concat("|", headers);
2342
let rows =
2443
List.map(
2544
row =>
@@ -29,21 +48,32 @@ let table = (~headers: list(string), rows) => {
2948
),
3049
rows,
3150
);
32-
let separator =
33-
String.concat(
34-
"|",
35-
List.init(
36-
header_len,
37-
idx => {
38-
let len = List.nth(headers, idx) |> String.length;
39-
String.init(len, _ => '-');
40-
},
41-
),
51+
let column_widths = compute_column_widths(headers, rows);
52+
let buffer = Buffer.create(512);
53+
54+
let write_row = (~pad_char=' ', row) => {
55+
List.iteri(
56+
(idx, cell) => {
57+
let width = column_widths[idx];
58+
Buffer.add_string(buffer, "| ");
59+
Buffer.add_string(buffer, cell);
60+
Buffer.add_string(
61+
buffer,
62+
String.make(width - String.length(cell), pad_char),
63+
);
64+
Buffer.add_string(buffer, " ");
65+
},
66+
row,
4267
);
43-
let body =
44-
List.map(row => Format.sprintf("|%s|", String.concat("|", row)), rows)
45-
|> String.concat("\n");
46-
Format.sprintf("|%s|\n|%s|\n%s\n\n", header, separator, body);
68+
Buffer.add_string(buffer, "|\n");
69+
};
70+
71+
write_row(headers);
72+
write_row(~pad_char='-', List.map(_ => "---", headers));
73+
List.iter(write_row, rows);
74+
Buffer.add_string(buffer, "\n");
75+
76+
Buffer.contents(buffer);
4777
} else {
4878
failwith("Your rows didn't all have the correct length");
4979
};

compiler/test/graindoc/functionDoc.expected.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ A negative index is treated as an offset from the end of the array.
3131

3232
Parameters:
3333

34-
|param|type|description|
35-
|-----|----|-----------|
36-
|`index`|`Number`|The index to access|
37-
|`array`|`Array<a>`|The array to access|
34+
| param | type | description |
35+
| ------- | ---------- | ------------------- |
36+
| `index` | `Number` | The index to access |
37+
| `array` | `Array<a>` | The array to access |
3838

3939
Returns:
4040

41-
|type|description|
42-
|----|-----------|
43-
|`a`|The element from the array|
41+
| type | description |
42+
| ---- | -------------------------- |
43+
| `a` | The element from the array |
4444

compiler/test/graindoc/types.expected.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ A record
2020

2121
Fields:
2222

23-
|name|type|description|
24-
|----|----|-----------|
25-
|`x`|`Number`|Record field x|
26-
|`y`|`String`|Record field y<br/>Second line|
27-
|`z`|`String`||
23+
| name | type | description |
24+
| ---- | -------- | ------------------------------ |
25+
| `x` | `Number` | Record field x |
26+
| `y` | `String` | Record field y<br/>Second line |
27+
| `z` | `String` | |
2828

2929
### TypeGrainDoc.**R2**
3030

@@ -83,10 +83,10 @@ Record variant
8383

8484
Fields:
8585

86-
|name|type|description|
87-
|----|----|-----------|
88-
|`a`|`Number`|Record field a|
89-
|`f`|`Number => Number`|Function|
86+
| name | type | description |
87+
| ---- | ------------------ | -------------- |
88+
| `a` | `Number` | Record field a |
89+
| `f` | `Number => Number` | Function |
9090

9191
```grain
9292
Variant5{
@@ -97,10 +97,10 @@ Variant5{
9797

9898
Fields:
9999

100-
|name|type|description|
101-
|----|----|-----------|
102-
|`a`|`Number`|Record field|
103-
|`b`|`Number`||
100+
| name | type | description |
101+
| ---- | -------- | ------------ |
102+
| `a` | `Number` | Record field |
103+
| `b` | `Number` | |
104104

105105
### TypeGrainDoc.**E2**
106106

@@ -139,10 +139,10 @@ Rec{
139139

140140
Fields:
141141

142-
|name|type|description|
143-
|----|----|-----------|
144-
|`a`|`Number`|A description|
145-
|`b`|`String`||
142+
| name | type | description |
143+
| ---- | -------- | ------------- |
144+
| `a` | `Number` | A description |
145+
| `b` | `String` | |
146146

147147
### TypeGrainDoc.**Num**
148148

0 commit comments

Comments
 (0)