Skip to content

Commit

Permalink
vdoc: update merge_doc_comments so recent fixes extend to all outpu…
Browse files Browse the repository at this point in the history
…t formats (#21289)
  • Loading branch information
ttytm committed Apr 15, 2024
1 parent 107c43a commit 0b83ea7
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 151 deletions.
33 changes: 1 addition & 32 deletions cmd/tools/vdoc/html.v
Expand Up @@ -506,43 +506,12 @@ fn html_highlight(code string, tb &ast.Table) string {
fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool, tb &ast.Table) string {
mut dnw := strings.new_builder(200)
head_tag := if head { 'h1' } else { 'h2' }
// Allow README.md to go through unescaped except for script tags
escaped_html := if head && is_module_readme(dn) {
readme_lines := dn.comments[0].text.split_into_lines()
mut merged_lines := []string{}
mut is_codeblock := false
for i := 0; i < readme_lines.len; i++ {
l := readme_lines[i]
nl := readme_lines[i + 1] or {
merged_lines << l
break
}
l_trimmed := l.trim_left('\x01').trim_space()
if l_trimmed.starts_with('```') {
is_codeblock = !is_codeblock
}
// -> if l_trimmed.len > 1 && (is_ul || is_ol)
is_list := l_trimmed.len > 1 && ((l_trimmed[1] == ` ` && l_trimmed[0] in [`*`, `-`])
|| (l_trimmed.len > 2 && l_trimmed[2] == ` ` && l_trimmed[1] == `.`
&& l_trimmed[0].is_digit()))
if !is_codeblock && l != '' && nl != '' && !is_list
&& !nl.trim_left('\x01').trim_space().starts_with('```') {
merged_lines << '${l} ${nl}'
i++
continue
}
merged_lines << l
}
merged_lines.join_lines()
} else {
dn.merge_comments_without_examples()
}
mut renderer := markdown.HtmlRenderer{
transformer: &MdHtmlCodeHighlighter{
table: tb
}
}
md_content := markdown.render(escaped_html, mut renderer) or { '' }
md_content := markdown.render(dn.merge_comments_without_examples(), mut renderer) or { '' }
highlighted_code := html_highlight(dn.content, tb)
node_class := if dn.kind == .const_group { ' const' } else { '' }
sym_name := get_sym_name(dn)
Expand Down
119 changes: 61 additions & 58 deletions cmd/tools/vdoc/tests/testdata/output_formats/main.ansi
Expand Up @@ -137,17 +137,17 @@ List point 3
import os

fn main() {
dump(os.args)
dump(os.args.len)
assert os.args.len > 0
dump(os.args)
dump(os.args.len)
assert os.args.len > 0

// Test escape characters like for `&` and `<`
mut arr := [1, 2, 3]
mut ref := &arr
arr << 4
// Test escape characters like for `&` and `<`
mut arr := [1, 2, 3]
mut ref := &arr
arr << 4

ch := chan bool{cap: 1}
ch <- true
ch := chan bool{cap: 1}
ch <- true
}
```

Expand All @@ -161,42 +161,42 @@ List point 3
import time

struct JwtHeader {
alg string
typ string
alg string
typ string
}

struct JwtPayload {
sub string
name string
iat int
sub string
name string
iat int
}

fn main() {
sw := time.new_stopwatch()
secret := 'your-256-bit-secret'
token := make_token(secret)
ok := auth_verify(secret, token)
dt := sw.elapsed().microseconds()
println('token: ${token}')
println('auth_verify(secret, token): ${ok}')
println('Elapsed time: ${dt} uS')
sw := time.new_stopwatch()
secret := 'your-256-bit-secret'
token := make_token(secret)
ok := auth_verify(secret, token)
dt := sw.elapsed().microseconds()
println('token: ${token}')
println('auth_verify(secret, token): ${ok}')
println('Elapsed time: ${dt} uS')
}

fn make_token(secret string) string {
header := base64.url_encode(json.encode(JwtHeader{'HS256', 'JWT'}).bytes())
payload := base64.url_encode(json.encode(JwtPayload{'1234567890', 'John Doe', 1516239022}).bytes())
signature := base64.url_encode(hmac.new(secret.bytes(), '${header}.${payload}'.bytes(),
sha256.sum, sha256.block_size))
jwt := '${header}.${payload}.${signature}'
return jwt
header := base64.url_encode(json.encode(JwtHeader{'HS256', 'JWT'}).bytes())
payload := base64.url_encode(json.encode(JwtPayload{'1234567890', 'John Doe', 1516239022}).bytes())
signature := base64.url_encode(hmac.new(secret.bytes(), '${header}.${payload}'.bytes(),
sha256.sum, sha256.block_size))
jwt := '${header}.${payload}.${signature}'
return jwt
}

fn auth_verify(secret string, token string) bool {
token_split := token.split('.')
signature_mirror := hmac.new(secret.bytes(), '${token_split[0]}.${token_split[1]}'.bytes(),
sha256.sum, sha256.block_size)
signature_from_token := base64.url_decode(token_split[2])
return hmac.equal(signature_from_token, signature_mirror)
token_split := token.split('.')
signature_mirror := hmac.new(secret.bytes(), '${token_split[0]}.${token_split[1]}'.bytes(),
sha256.sum, sha256.block_size)
signature_from_token := base64.url_decode(token_split[2])
return hmac.equal(signature_from_token, signature_mirror)
}
```

Expand All @@ -207,12 +207,12 @@ List point 3
#include <map>

std::map<std::string, int> my_map {
{"KEY_1", 0},
{"KEY_2", 10},
{"KEY_1", 0},
{"KEY_2", 10},
};

for (const auto &[key, value] : my_map) {
std::cout << key << ": " << value << ", ";
std::cout << key << ": " << value << ", ";
}
std::cout << "\n";
```
Expand All @@ -227,35 +227,38 @@ List point 3
```v
const html = '<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
background: linear-gradient(to right, #274060, #1B2845);
color: GhostWhite;
font-family: sans-serif;
text-align: center;
}
</style>
</head>
<body>
<h1>Your App Content!</h1>
<button onclick="callV()">Call V!</button>
</body>
<script>
async function callV() {
// Call a V function that takes an argument and returns a value.
const res = await window.my_v_func(\'Hello from JS!\');
console.log(res);
}
</script>
<head>
<style>
body {
background: linear-gradient(to right, #274060, #1B2845);
color: GhostWhite;
font-family: sans-serif;
text-align: center;
}
</style>
</head>
<body>
<h1>Your App Content!</h1>
<button onclick="callV()">Call V!</button>
</body>
<script>
async function callV() {
// Call a V function that takes an argument and returns a value.
const res = await window.my_v_func(\'Hello from JS!\');
console.log(res);
}
</script>
</html>'
```

- Regular markdown list point 1
- List point 2
- List point 3

1. Numbered markdown list point 1 2. List point 2 3. List point 3
1. Numbered markdown list point 1
2. List point 2
3. List point 3


const omega = 3 // should be first
const alpha = 5 // should be in the middle
Expand Down
119 changes: 61 additions & 58 deletions cmd/tools/vdoc/tests/testdata/output_formats/main.text
Expand Up @@ -17,17 +17,17 @@ module main
import os

fn main() {
dump(os.args)
dump(os.args.len)
assert os.args.len > 0
dump(os.args)
dump(os.args.len)
assert os.args.len > 0

// Test escape characters like for `&` and `<`
mut arr := [1, 2, 3]
mut ref := &arr
arr << 4
// Test escape characters like for `&` and `<`
mut arr := [1, 2, 3]
mut ref := &arr
arr << 4

ch := chan bool{cap: 1}
ch <- true
ch := chan bool{cap: 1}
ch <- true
}
```

Expand All @@ -41,42 +41,42 @@ module main
import time

struct JwtHeader {
alg string
typ string
alg string
typ string
}

struct JwtPayload {
sub string
name string
iat int
sub string
name string
iat int
}

fn main() {
sw := time.new_stopwatch()
secret := 'your-256-bit-secret'
token := make_token(secret)
ok := auth_verify(secret, token)
dt := sw.elapsed().microseconds()
println('token: ${token}')
println('auth_verify(secret, token): ${ok}')
println('Elapsed time: ${dt} uS')
sw := time.new_stopwatch()
secret := 'your-256-bit-secret'
token := make_token(secret)
ok := auth_verify(secret, token)
dt := sw.elapsed().microseconds()
println('token: ${token}')
println('auth_verify(secret, token): ${ok}')
println('Elapsed time: ${dt} uS')
}

fn make_token(secret string) string {
header := base64.url_encode(json.encode(JwtHeader{'HS256', 'JWT'}).bytes())
payload := base64.url_encode(json.encode(JwtPayload{'1234567890', 'John Doe', 1516239022}).bytes())
signature := base64.url_encode(hmac.new(secret.bytes(), '${header}.${payload}'.bytes(),
sha256.sum, sha256.block_size))
jwt := '${header}.${payload}.${signature}'
return jwt
header := base64.url_encode(json.encode(JwtHeader{'HS256', 'JWT'}).bytes())
payload := base64.url_encode(json.encode(JwtPayload{'1234567890', 'John Doe', 1516239022}).bytes())
signature := base64.url_encode(hmac.new(secret.bytes(), '${header}.${payload}'.bytes(),
sha256.sum, sha256.block_size))
jwt := '${header}.${payload}.${signature}'
return jwt
}

fn auth_verify(secret string, token string) bool {
token_split := token.split('.')
signature_mirror := hmac.new(secret.bytes(), '${token_split[0]}.${token_split[1]}'.bytes(),
sha256.sum, sha256.block_size)
signature_from_token := base64.url_decode(token_split[2])
return hmac.equal(signature_from_token, signature_mirror)
token_split := token.split('.')
signature_mirror := hmac.new(secret.bytes(), '${token_split[0]}.${token_split[1]}'.bytes(),
sha256.sum, sha256.block_size)
signature_from_token := base64.url_decode(token_split[2])
return hmac.equal(signature_from_token, signature_mirror)
}
```

Expand All @@ -87,12 +87,12 @@ module main
#include <map>

std::map<std::string, int> my_map {
{"KEY_1", 0},
{"KEY_2", 10},
{"KEY_1", 0},
{"KEY_2", 10},
};

for (const auto &[key, value] : my_map) {
std::cout << key << ": " << value << ", ";
std::cout << key << ": " << value << ", ";
}
std::cout << "\n";
```
Expand All @@ -107,35 +107,38 @@ module main
```v
const html = '<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
background: linear-gradient(to right, #274060, #1B2845);
color: GhostWhite;
font-family: sans-serif;
text-align: center;
}
</style>
</head>
<body>
<h1>Your App Content!</h1>
<button onclick="callV()">Call V!</button>
</body>
<script>
async function callV() {
// Call a V function that takes an argument and returns a value.
const res = await window.my_v_func(\'Hello from JS!\');
console.log(res);
}
</script>
<head>
<style>
body {
background: linear-gradient(to right, #274060, #1B2845);
color: GhostWhite;
font-family: sans-serif;
text-align: center;
}
</style>
</head>
<body>
<h1>Your App Content!</h1>
<button onclick="callV()">Call V!</button>
</body>
<script>
async function callV() {
// Call a V function that takes an argument and returns a value.
const res = await window.my_v_func(\'Hello from JS!\');
console.log(res);
}
</script>
</html>'
```

- Regular markdown list point 1
- List point 2
- List point 3

1. Numbered markdown list point 1 2. List point 2 3. List point 3
1. Numbered markdown list point 1
2. List point 2
3. List point 3


const omega = 3 // should be first
const alpha = 5 // should be in the middle
Expand Down

0 comments on commit 0b83ea7

Please sign in to comment.