From 42e41486432917885aeadab3063a3bd75e363136 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Fri, 1 Apr 2022 14:25:57 +0100 Subject: [PATCH] v/doc: parse multi-line examples --- vlib/arrays/arrays.v | 4 ++-- vlib/v/doc/node.v | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/vlib/arrays/arrays.v b/vlib/arrays/arrays.v index 1fffd5fd42..dc099714eb 100644 --- a/vlib/arrays/arrays.v +++ b/vlib/arrays/arrays.v @@ -175,7 +175,7 @@ pub struct WindowAttribute { // - `size` - snapshot size // - `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]] pub fn window(list []T, attr WindowAttribute) [][]T { // allocate snapshot array @@ -394,7 +394,7 @@ pub fn binary_search(arr []T, target T) ?int { // Example: // ```v // mut x := [1,2,3,4,5,6] -// arrays.rotate_left(mut x,2) +// arrays.rotate_left(mut x, 2) // println(x) // [3, 4, 5, 6, 1, 2] // ``` pub fn rotate_left(mut arr []T, mid int) { diff --git a/vlib/v/doc/node.v b/vlib/v/doc/node.v index 736067f276..c43be887ac 100644 --- a/vlib/v/doc/node.v +++ b/vlib/v/doc/node.v @@ -61,9 +61,30 @@ pub fn (dc DocNode) merge_comments_without_examples() string { // examples returns a `[]string` containing examples parsed from `DocNode.comments`. pub fn (dn DocNode) examples() []string { mut output := []string{} - for comment in dn.comments { + for i, comment in dn.comments { if comment.is_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