vdoc: make -run-examples compatible with all output modes, not just the plaintext one

This commit is contained in:
Delyan Angelov 2025-08-13 12:57:25 +03:00
parent ef347e79f4
commit e33dcabcc1
No known key found for this signature in database
GPG key ID: 66886C0F12D595ED
3 changed files with 16 additions and 7 deletions

View file

@ -182,14 +182,14 @@ fn (vd &VDoc) write_content(cn &doc.DocNode, d &doc.Doc, mut hw strings.Builder)
src_link := get_src_link(vd.manifest.repo_url, vd.manifest.repo_branch, file_path_name, src_link := get_src_link(vd.manifest.repo_url, vd.manifest.repo_branch, file_path_name,
cn.pos.line_nr + 1) cn.pos.line_nr + 1)
if cn.content.len != 0 || cn.name == 'Constants' { if cn.content.len != 0 || cn.name == 'Constants' {
hw.write_string(doc_node_html(cn, src_link, false, cfg.include_examples, d.table)) hw.write_string(vd.doc_node_html(cn, src_link, false, cfg.include_examples, d.table))
hw.write_string('\n') hw.write_string('\n')
} }
for child in cn.children { for child in cn.children {
child_file_path_name := child.file_path.replace('${base_dir}/', '') child_file_path_name := child.file_path.replace('${base_dir}/', '')
child_src_link := get_src_link(vd.manifest.repo_url, vd.manifest.repo_branch, child_src_link := get_src_link(vd.manifest.repo_url, vd.manifest.repo_branch,
child_file_path_name, child.pos.line_nr + 1) child_file_path_name, child.pos.line_nr + 1)
hw.write_string(doc_node_html(child, child_src_link, false, cfg.include_examples, hw.write_string(vd.doc_node_html(child, child_src_link, false, cfg.include_examples,
d.table)) d.table))
hw.write_string('\n') hw.write_string('\n')
} }
@ -202,7 +202,7 @@ fn (vd &VDoc) gen_html(d doc.Doc) string {
mut contents := strings.new_builder(200) mut contents := strings.new_builder(200)
dcs_contents := d.contents.arr() dcs_contents := d.contents.arr()
// generate toc first // generate toc first
contents.writeln(doc_node_html(d.head, '', true, cfg.include_examples, d.table)) contents.writeln(vd.doc_node_html(d.head, '', true, cfg.include_examples, d.table))
if is_module_readme(d.head) { if is_module_readme(d.head) {
write_toc(d.head, mut symbols_toc) write_toc(d.head, mut symbols_toc)
} }
@ -519,7 +519,7 @@ fn html_highlight(code string, tb &ast.Table) string {
return buf.str() return buf.str()
} }
fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool, tb &ast.Table) string { fn (vd &VDoc) doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool, tb &ast.Table) string {
mut dnw := strings.new_builder(200) mut dnw := strings.new_builder(200)
head_tag := if head { 'h1' } else { 'h2' } head_tag := if head { 'h1' } else { 'h2' }
mut renderer := markdown.HtmlRenderer{ mut renderer := markdown.HtmlRenderer{

View file

@ -2,11 +2,19 @@ module main
import document as doc import document as doc
import v.vmod import v.vmod
import strings
import os import os
import rand import rand
import term import term
fn (mut vd VDoc) process_all_examples(contents []doc.DocNode) {
for cn in contents {
if cn.content.len > 0 {
vd.run_examples(cn)
}
vd.process_all_examples(cn.children)
}
}
const normalised_default_vmodules_path = os.vmodules_dir().replace('\\', '/') const normalised_default_vmodules_path = os.vmodules_dir().replace('\\', '/')
fn get_mod_name_by_file_path(file_path string) string { fn get_mod_name_by_file_path(file_path string) string {
@ -23,7 +31,7 @@ fn get_mod_name_by_file_path(file_path string) string {
return mod_name return mod_name
} }
fn (mut vd VDoc) run_examples(dn doc.DocNode, mut pw strings.Builder) { fn (mut vd VDoc) run_examples(dn doc.DocNode) {
if dn.comments.len == 0 || !vd.cfg.run_examples { if dn.comments.len == 0 || !vd.cfg.run_examples {
return return
} }

View file

@ -143,7 +143,6 @@ fn (mut vd VDoc) write_plaintext_content(contents []doc.DocNode, mut pw strings.
} }
} }
} }
vd.run_examples(cn, mut pw)
} }
vd.write_plaintext_content(cn.children, mut pw) vd.write_plaintext_content(cn.children, mut pw)
} }
@ -157,6 +156,8 @@ fn (mut vd VDoc) render_doc(d doc.Doc, out Output) (string, string) {
.json { vd.gen_json(d) } .json { vd.gen_json(d) }
else { vd.gen_plaintext(d) } else { vd.gen_plaintext(d) }
} }
contents := d.contents.arr()
vd.process_all_examples(contents)
return name, output return name, output
} }