Skip to content

Commit 0b83ea7

Browse files
authored
vdoc: update merge_doc_comments so recent fixes extend to all output formats (#21289)
1 parent 107c43a commit 0b83ea7

File tree

4 files changed

+131
-151
lines changed

4 files changed

+131
-151
lines changed

cmd/tools/vdoc/html.v

+1-32
Original file line numberDiff line numberDiff line change
@@ -506,43 +506,12 @@ fn html_highlight(code string, tb &ast.Table) string {
506506
fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool, tb &ast.Table) string {
507507
mut dnw := strings.new_builder(200)
508508
head_tag := if head { 'h1' } else { 'h2' }
509-
// Allow README.md to go through unescaped except for script tags
510-
escaped_html := if head && is_module_readme(dn) {
511-
readme_lines := dn.comments[0].text.split_into_lines()
512-
mut merged_lines := []string{}
513-
mut is_codeblock := false
514-
for i := 0; i < readme_lines.len; i++ {
515-
l := readme_lines[i]
516-
nl := readme_lines[i + 1] or {
517-
merged_lines << l
518-
break
519-
}
520-
l_trimmed := l.trim_left('\x01').trim_space()
521-
if l_trimmed.starts_with('```') {
522-
is_codeblock = !is_codeblock
523-
}
524-
// -> if l_trimmed.len > 1 && (is_ul || is_ol)
525-
is_list := l_trimmed.len > 1 && ((l_trimmed[1] == ` ` && l_trimmed[0] in [`*`, `-`])
526-
|| (l_trimmed.len > 2 && l_trimmed[2] == ` ` && l_trimmed[1] == `.`
527-
&& l_trimmed[0].is_digit()))
528-
if !is_codeblock && l != '' && nl != '' && !is_list
529-
&& !nl.trim_left('\x01').trim_space().starts_with('```') {
530-
merged_lines << '${l} ${nl}'
531-
i++
532-
continue
533-
}
534-
merged_lines << l
535-
}
536-
merged_lines.join_lines()
537-
} else {
538-
dn.merge_comments_without_examples()
539-
}
540509
mut renderer := markdown.HtmlRenderer{
541510
transformer: &MdHtmlCodeHighlighter{
542511
table: tb
543512
}
544513
}
545-
md_content := markdown.render(escaped_html, mut renderer) or { '' }
514+
md_content := markdown.render(dn.merge_comments_without_examples(), mut renderer) or { '' }
546515
highlighted_code := html_highlight(dn.content, tb)
547516
node_class := if dn.kind == .const_group { ' const' } else { '' }
548517
sym_name := get_sym_name(dn)

cmd/tools/vdoc/tests/testdata/output_formats/main.ansi

+61-58
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,17 @@ List point 3
137137
import os
138138

139139
fn main() {
140-
dump(os.args)
141-
dump(os.args.len)
142-
assert os.args.len > 0
140+
dump(os.args)
141+
dump(os.args.len)
142+
assert os.args.len > 0
143143

144-
// Test escape characters like for `&` and `<`
145-
mut arr := [1, 2, 3]
146-
mut ref := &arr
147-
arr << 4
144+
// Test escape characters like for `&` and `<`
145+
mut arr := [1, 2, 3]
146+
mut ref := &arr
147+
arr << 4
148148

149-
ch := chan bool{cap: 1}
150-
ch <- true
149+
ch := chan bool{cap: 1}
150+
ch <- true
151151
}
152152
```
153153

@@ -161,42 +161,42 @@ List point 3
161161
import time
162162

163163
struct JwtHeader {
164-
alg string
165-
typ string
164+
alg string
165+
typ string
166166
}
167167

168168
struct JwtPayload {
169-
sub string
170-
name string
171-
iat int
169+
sub string
170+
name string
171+
iat int
172172
}
173173

174174
fn main() {
175-
sw := time.new_stopwatch()
176-
secret := 'your-256-bit-secret'
177-
token := make_token(secret)
178-
ok := auth_verify(secret, token)
179-
dt := sw.elapsed().microseconds()
180-
println('token: ${token}')
181-
println('auth_verify(secret, token): ${ok}')
182-
println('Elapsed time: ${dt} uS')
175+
sw := time.new_stopwatch()
176+
secret := 'your-256-bit-secret'
177+
token := make_token(secret)
178+
ok := auth_verify(secret, token)
179+
dt := sw.elapsed().microseconds()
180+
println('token: ${token}')
181+
println('auth_verify(secret, token): ${ok}')
182+
println('Elapsed time: ${dt} uS')
183183
}
184184

185185
fn make_token(secret string) string {
186-
header := base64.url_encode(json.encode(JwtHeader{'HS256', 'JWT'}).bytes())
187-
payload := base64.url_encode(json.encode(JwtPayload{'1234567890', 'John Doe', 1516239022}).bytes())
188-
signature := base64.url_encode(hmac.new(secret.bytes(), '${header}.${payload}'.bytes(),
189-
sha256.sum, sha256.block_size))
190-
jwt := '${header}.${payload}.${signature}'
191-
return jwt
186+
header := base64.url_encode(json.encode(JwtHeader{'HS256', 'JWT'}).bytes())
187+
payload := base64.url_encode(json.encode(JwtPayload{'1234567890', 'John Doe', 1516239022}).bytes())
188+
signature := base64.url_encode(hmac.new(secret.bytes(), '${header}.${payload}'.bytes(),
189+
sha256.sum, sha256.block_size))
190+
jwt := '${header}.${payload}.${signature}'
191+
return jwt
192192
}
193193

194194
fn auth_verify(secret string, token string) bool {
195-
token_split := token.split('.')
196-
signature_mirror := hmac.new(secret.bytes(), '${token_split[0]}.${token_split[1]}'.bytes(),
197-
sha256.sum, sha256.block_size)
198-
signature_from_token := base64.url_decode(token_split[2])
199-
return hmac.equal(signature_from_token, signature_mirror)
195+
token_split := token.split('.')
196+
signature_mirror := hmac.new(secret.bytes(), '${token_split[0]}.${token_split[1]}'.bytes(),
197+
sha256.sum, sha256.block_size)
198+
signature_from_token := base64.url_decode(token_split[2])
199+
return hmac.equal(signature_from_token, signature_mirror)
200200
}
201201
```
202202

@@ -207,12 +207,12 @@ List point 3
207207
#include <map>
208208

209209
std::map<std::string, int> my_map {
210-
{"KEY_1", 0},
211-
{"KEY_2", 10},
210+
{"KEY_1", 0},
211+
{"KEY_2", 10},
212212
};
213213

214214
for (const auto &[key, value] : my_map) {
215-
std::cout << key << ": " << value << ", ";
215+
std::cout << key << ": " << value << ", ";
216216
}
217217
std::cout << "\n";
218218
```
@@ -227,35 +227,38 @@ List point 3
227227
```v
228228
const html = '<!DOCTYPE html>
229229
<html lang="en">
230-
<head>
231-
<style>
232-
body {
233-
background: linear-gradient(to right, #274060, #1B2845);
234-
color: GhostWhite;
235-
font-family: sans-serif;
236-
text-align: center;
237-
}
238-
</style>
239-
</head>
240-
<body>
241-
<h1>Your App Content!</h1>
242-
<button onclick="callV()">Call V!</button>
243-
</body>
244-
<script>
245-
async function callV() {
246-
// Call a V function that takes an argument and returns a value.
247-
const res = await window.my_v_func(\'Hello from JS!\');
248-
console.log(res);
249-
}
250-
</script>
230+
<head>
231+
<style>
232+
body {
233+
background: linear-gradient(to right, #274060, #1B2845);
234+
color: GhostWhite;
235+
font-family: sans-serif;
236+
text-align: center;
237+
}
238+
</style>
239+
</head>
240+
<body>
241+
<h1>Your App Content!</h1>
242+
<button onclick="callV()">Call V!</button>
243+
</body>
244+
<script>
245+
async function callV() {
246+
// Call a V function that takes an argument and returns a value.
247+
const res = await window.my_v_func(\'Hello from JS!\');
248+
console.log(res);
249+
}
250+
</script>
251251
</html>'
252252
```
253253

254254
- Regular markdown list point 1
255255
- List point 2
256256
- List point 3
257257

258-
1. Numbered markdown list point 1 2. List point 2 3. List point 3
258+
1. Numbered markdown list point 1
259+
2. List point 2
260+
3. List point 3
261+
259262

260263
const omega = 3 // should be first
261264
const alpha = 5 // should be in the middle

cmd/tools/vdoc/tests/testdata/output_formats/main.text

+61-58
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ module main
1717
import os
1818

1919
fn main() {
20-
dump(os.args)
21-
dump(os.args.len)
22-
assert os.args.len > 0
20+
dump(os.args)
21+
dump(os.args.len)
22+
assert os.args.len > 0
2323

24-
// Test escape characters like for `&` and `<`
25-
mut arr := [1, 2, 3]
26-
mut ref := &arr
27-
arr << 4
24+
// Test escape characters like for `&` and `<`
25+
mut arr := [1, 2, 3]
26+
mut ref := &arr
27+
arr << 4
2828

29-
ch := chan bool{cap: 1}
30-
ch <- true
29+
ch := chan bool{cap: 1}
30+
ch <- true
3131
}
3232
```
3333

@@ -41,42 +41,42 @@ module main
4141
import time
4242

4343
struct JwtHeader {
44-
alg string
45-
typ string
44+
alg string
45+
typ string
4646
}
4747

4848
struct JwtPayload {
49-
sub string
50-
name string
51-
iat int
49+
sub string
50+
name string
51+
iat int
5252
}
5353

5454
fn main() {
55-
sw := time.new_stopwatch()
56-
secret := 'your-256-bit-secret'
57-
token := make_token(secret)
58-
ok := auth_verify(secret, token)
59-
dt := sw.elapsed().microseconds()
60-
println('token: ${token}')
61-
println('auth_verify(secret, token): ${ok}')
62-
println('Elapsed time: ${dt} uS')
55+
sw := time.new_stopwatch()
56+
secret := 'your-256-bit-secret'
57+
token := make_token(secret)
58+
ok := auth_verify(secret, token)
59+
dt := sw.elapsed().microseconds()
60+
println('token: ${token}')
61+
println('auth_verify(secret, token): ${ok}')
62+
println('Elapsed time: ${dt} uS')
6363
}
6464

6565
fn make_token(secret string) string {
66-
header := base64.url_encode(json.encode(JwtHeader{'HS256', 'JWT'}).bytes())
67-
payload := base64.url_encode(json.encode(JwtPayload{'1234567890', 'John Doe', 1516239022}).bytes())
68-
signature := base64.url_encode(hmac.new(secret.bytes(), '${header}.${payload}'.bytes(),
69-
sha256.sum, sha256.block_size))
70-
jwt := '${header}.${payload}.${signature}'
71-
return jwt
66+
header := base64.url_encode(json.encode(JwtHeader{'HS256', 'JWT'}).bytes())
67+
payload := base64.url_encode(json.encode(JwtPayload{'1234567890', 'John Doe', 1516239022}).bytes())
68+
signature := base64.url_encode(hmac.new(secret.bytes(), '${header}.${payload}'.bytes(),
69+
sha256.sum, sha256.block_size))
70+
jwt := '${header}.${payload}.${signature}'
71+
return jwt
7272
}
7373

7474
fn auth_verify(secret string, token string) bool {
75-
token_split := token.split('.')
76-
signature_mirror := hmac.new(secret.bytes(), '${token_split[0]}.${token_split[1]}'.bytes(),
77-
sha256.sum, sha256.block_size)
78-
signature_from_token := base64.url_decode(token_split[2])
79-
return hmac.equal(signature_from_token, signature_mirror)
75+
token_split := token.split('.')
76+
signature_mirror := hmac.new(secret.bytes(), '${token_split[0]}.${token_split[1]}'.bytes(),
77+
sha256.sum, sha256.block_size)
78+
signature_from_token := base64.url_decode(token_split[2])
79+
return hmac.equal(signature_from_token, signature_mirror)
8080
}
8181
```
8282

@@ -87,12 +87,12 @@ module main
8787
#include <map>
8888

8989
std::map<std::string, int> my_map {
90-
{"KEY_1", 0},
91-
{"KEY_2", 10},
90+
{"KEY_1", 0},
91+
{"KEY_2", 10},
9292
};
9393

9494
for (const auto &[key, value] : my_map) {
95-
std::cout << key << ": " << value << ", ";
95+
std::cout << key << ": " << value << ", ";
9696
}
9797
std::cout << "\n";
9898
```
@@ -107,35 +107,38 @@ module main
107107
```v
108108
const html = '<!DOCTYPE html>
109109
<html lang="en">
110-
<head>
111-
<style>
112-
body {
113-
background: linear-gradient(to right, #274060, #1B2845);
114-
color: GhostWhite;
115-
font-family: sans-serif;
116-
text-align: center;
117-
}
118-
</style>
119-
</head>
120-
<body>
121-
<h1>Your App Content!</h1>
122-
<button onclick="callV()">Call V!</button>
123-
</body>
124-
<script>
125-
async function callV() {
126-
// Call a V function that takes an argument and returns a value.
127-
const res = await window.my_v_func(\'Hello from JS!\');
128-
console.log(res);
129-
}
130-
</script>
110+
<head>
111+
<style>
112+
body {
113+
background: linear-gradient(to right, #274060, #1B2845);
114+
color: GhostWhite;
115+
font-family: sans-serif;
116+
text-align: center;
117+
}
118+
</style>
119+
</head>
120+
<body>
121+
<h1>Your App Content!</h1>
122+
<button onclick="callV()">Call V!</button>
123+
</body>
124+
<script>
125+
async function callV() {
126+
// Call a V function that takes an argument and returns a value.
127+
const res = await window.my_v_func(\'Hello from JS!\');
128+
console.log(res);
129+
}
130+
</script>
131131
</html>'
132132
```
133133

134134
- Regular markdown list point 1
135135
- List point 2
136136
- List point 3
137137

138-
1. Numbered markdown list point 1 2. List point 2 3. List point 3
138+
1. Numbered markdown list point 1
139+
2. List point 2
140+
3. List point 3
141+
139142

140143
const omega = 3 // should be first
141144
const alpha = 5 // should be in the middle

0 commit comments

Comments
 (0)