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
parent
875ad1f6ea
commit
226bb4bd8b
|
|
@ -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' }
|
example_title := if examples.len > 1 { 'Examples' } else { 'Example' }
|
||||||
dnw.writeln('<section class="doc-node examples"><h4>$example_title</h4>')
|
dnw.writeln('<section class="doc-node examples"><h4>$example_title</h4>')
|
||||||
for example in examples {
|
for example in examples {
|
||||||
// hl_example := html_highlight(example, tb)
|
hl_example := html_highlight(example, tb)
|
||||||
dnw.writeln('<pre><code class="language-v">$example</code></pre>')
|
dnw.writeln('<pre><code class="language-v">$hl_example</code></pre>')
|
||||||
}
|
}
|
||||||
dnw.writeln('</section>')
|
dnw.writeln('</section>')
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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'`
|
// 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
|
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
|
// Ignore the function signature. `all` does not take an actual callback. Rather, it
|
||||||
// takes an `it` expression.
|
// takes an `it` expression.
|
||||||
// It returns `false` if any element fails the test. Otherwise,
|
// It returns `false` if any element fails the test. Otherwise,
|
||||||
|
|
|
||||||
|
|
@ -516,7 +516,7 @@ pub fn (b byte) str_escaped() string {
|
||||||
|
|
||||||
// is_capital returns `true`, if the byte is a Latin capital letter.
|
// is_capital returns `true`, if the byte is a Latin capital letter.
|
||||||
// Example: assert `H`.is_capital() == true
|
// Example: assert `H`.is_capital() == true
|
||||||
// Example: assert 'h`.is_capital() == false
|
// Example: assert `h`.is_capital() == false
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (c byte) is_capital() bool {
|
pub fn (c byte) is_capital() bool {
|
||||||
return c >= `A` && c <= `Z`
|
return c >= `A` && c <= `Z`
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ pub fn (c byte) is_alnum() bool {
|
||||||
|
|
||||||
// is_capital returns `true`, if the byte is a Latin capital letter.
|
// is_capital returns `true`, if the byte is a Latin capital letter.
|
||||||
// Example: assert `H`.is_capital() == true
|
// Example: assert `H`.is_capital() == true
|
||||||
// Example: assert 'h`.is_capital() == false
|
// Example: assert `h`.is_capital() == false
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (c byte) is_capital() bool {
|
pub fn (c byte) is_capital() bool {
|
||||||
return c >= `A` && c <= `Z`
|
return c >= `A` && c <= `Z`
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ fn trace_error(x string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// error returns a default error instance containing the error given in `message`.
|
// 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]
|
[inline]
|
||||||
pub fn error(message string) IError {
|
pub fn error(message string) IError {
|
||||||
trace_error(message)
|
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`.
|
// 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]
|
[inline]
|
||||||
pub fn error_with_code(message string, code int) IError {
|
pub fn error_with_code(message string, code int) IError {
|
||||||
trace_error('$message | code: $code')
|
trace_error('$message | code: $code')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue