v/doc: parse multi-line examples

pull/13894/head
Nick Treleaven 2022-04-01 14:25:57 +01:00
parent 02c80bd445
commit 42e4148643
2 changed files with 24 additions and 3 deletions

View File

@ -175,7 +175,7 @@ pub struct WindowAttribute {
// - `size` - snapshot size // - `size` - snapshot size
// - `step` - gap size between each snapshot, default is 1. // - `step` - gap size between each snapshot, default is 1.
// //
// Example: arrays.window([1, 2, 3, 4], size: 2) => [[1, 2], [2, 3], [3, 4]] // Example: arrays.window([1, 2, 3, 4], size: 2) // => [[1, 2], [2, 3], [3, 4]]
// Example: arrays.window([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], size: 3, step: 2) // => [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]] // Example: arrays.window([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], size: 3, step: 2) // => [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]]
pub fn window<T>(list []T, attr WindowAttribute) [][]T { pub fn window<T>(list []T, attr WindowAttribute) [][]T {
// allocate snapshot array // allocate snapshot array

View File

@ -61,9 +61,30 @@ pub fn (dc DocNode) merge_comments_without_examples() string {
// examples returns a `[]string` containing examples parsed from `DocNode.comments`. // examples returns a `[]string` containing examples parsed from `DocNode.comments`.
pub fn (dn DocNode) examples() []string { pub fn (dn DocNode) examples() []string {
mut output := []string{} mut output := []string{}
for comment in dn.comments { for i, comment in dn.comments {
if comment.is_example() { if comment.is_example() {
output << comment.example() output << comment.example()
} else if comment.text == '\x01 Example:' {
mut j := i + 1
//~ comments = dn.comments
mut ml_ex := ''
mdcode := '\x01 ```'
if j + 2 < dn.comments.len && dn.comments[j].text == mdcode + 'v'
{
j++
for j < dn.comments.len && dn.comments[j].text != mdcode {
//~ println(dn.comments[j].text)
if ml_ex.len > 0 {
ml_ex += '\n'
}
s := dn.comments[j].text
if s.len > 2 { ml_ex += s[2..] }
j++
}
println(ml_ex)
output << ml_ex
break
}
} }
} }
return output return output