vdoc: highlight inline examples for `-f html`

This doesn't include multi-line examples or README.md example.
Part of https://github.com/vlang/v/issues/13851.

If the example isn't valid V:
```v
// Example: `if ouch { return error('an error occurred') }`
```
You'll get an error like:
```
internal_memory:1:47: error: invalid character literal `if ouch { return error('an error occurred') }` => `if ouch { return error('an error occurred') }` ([`i`, `f`, ` `, `o`, `u`, `c`, `h`, ` `, `{`, ` `, `r`, `e`, `t`, `u`, `r`, `n`, ` `, `e`, `r`, `r`, `o`, `r`, `(`, `'`, `a`, `n`, ` `, `e`, `r`, `r`, `o`, `r`, ` `, `o`, `c`, `c`, `u`, `r`, `r`, `e`, `d`, `'`, `)`, ` `, `}`]) (more than one character)
internal_memory:1:1: details: use quotes for strings, backticks for characters
```

This one is less informative about the source line:
```v
// Example: assert 'h`.is_capital() == false
```
```
internal_memory:1:33: error: unfinished string literal
```
pull/13879/head
Nick Treleaven 2022-03-30 17:54:49 +01:00
parent 875ad1f6ea
commit 226bb4bd8b
5 changed files with 7 additions and 7 deletions

View File

@ -426,8 +426,8 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
example_title := if examples.len > 1 { 'Examples' } else { 'Example' }
dnw.writeln('<section class="doc-node examples"><h4>$example_title</h4>')
for example in examples {
// hl_example := html_highlight(example, tb)
dnw.writeln('<pre><code class="language-v">$example</code></pre>')
hl_example := html_highlight(example, tb)
dnw.writeln('<pre><code class="language-v">$hl_example</code></pre>')
}
dnw.writeln('</section>')
}

View File

@ -690,7 +690,7 @@ pub fn (a array) filter(predicate fn (voidptr) bool) array
// Example: array.any(it.name == 'Bob') // will yield `true` if any element has `.name == 'Bob'`
pub fn (a array) any(predicate fn (voidptr) bool) bool
// all tests whether all elements in the array pass the test
// all tests whether all elements in the array pass the test.
// Ignore the function signature. `all` does not take an actual callback. Rather, it
// takes an `it` expression.
// It returns `false` if any element fails the test. Otherwise,

View File

@ -516,7 +516,7 @@ pub fn (b byte) str_escaped() string {
// is_capital returns `true`, if the byte is a Latin capital letter.
// Example: assert `H`.is_capital() == true
// Example: assert 'h`.is_capital() == false
// Example: assert `h`.is_capital() == false
[inline]
pub fn (c byte) is_capital() bool {
return c >= `A` && c <= `Z`

View File

@ -72,7 +72,7 @@ pub fn (c byte) is_alnum() bool {
// is_capital returns `true`, if the byte is a Latin capital letter.
// Example: assert `H`.is_capital() == true
// Example: assert 'h`.is_capital() == false
// Example: assert `h`.is_capital() == false
[inline]
pub fn (c byte) is_capital() bool {
return c >= `A` && c <= `Z`

View File

@ -85,7 +85,7 @@ fn trace_error(x string) {
}
// error returns a default error instance containing the error given in `message`.
// Example: `if ouch { return error('an error occurred') }`
// Example: if ouch { return error('an error occurred') }
[inline]
pub fn error(message string) IError {
trace_error(message)
@ -95,7 +95,7 @@ pub fn error(message string) IError {
}
// error_with_code returns a default error instance containing the given `message` and error `code`.
// `if ouch { return error_with_code('an error occurred', 1) }`
// Example: if ouch { return error_with_code('an error occurred', 1) }
[inline]
pub fn error_with_code(message string, code int) IError {
trace_error('$message | code: $code')