Skip to content

Commit e5153e7

Browse files
authored
tools,v.doc: let https://modules.vlang.io use vlib/README.md as the index page. (#23480)
1 parent d680c42 commit e5153e7

File tree

6 files changed

+87
-40
lines changed

6 files changed

+87
-40
lines changed

cmd/tools/vdoc/html.v

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,18 @@ fn (vd &VDoc) gen_html(d doc.Doc) string {
222222
if cfg.is_multi || vd.docs.len > 1 {
223223
mut used_submod_prefixes := map[string]bool{}
224224
for dc in vd.docs {
225-
submod_prefix := dc.head.name.all_before('.')
225+
mut submod_prefix := dc.head.name.all_before('.')
226+
if index := dc.head.frontmatter['index'] {
227+
if dc.head.name == 'index' {
228+
submod_prefix = index
229+
}
230+
}
226231
if used_submod_prefixes[submod_prefix] {
227232
continue
228233
}
229234
used_submod_prefixes[submod_prefix] = true
230235
mut href_name := './${dc.head.name}.html'
231-
if (cfg.is_vlib && dc.head.name == 'builtin' && !cfg.include_readme)
232-
|| dc.head.name == 'README' {
236+
if dc.head.name in ['README', 'index'] {
233237
href_name = './index.html'
234238
} else if submod_prefix !in vd.docs.map(it.head.name) {
235239
href_name = '#'

cmd/tools/vdoc/utils.v

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ fn escape(str string) string {
1212
}
1313

1414
fn get_sym_name(dn doc.DocNode) string {
15-
sym_name := if dn.parent_name.len > 0 && dn.parent_name != 'void' {
16-
'(${dn.parent_name}) ${dn.name}'
17-
} else {
18-
dn.name
15+
if dn.is_readme {
16+
if title := dn.frontmatter['title'] {
17+
return title
18+
}
19+
}
20+
if dn.parent_name.len > 0 && dn.parent_name != 'void' {
21+
return '(${dn.parent_name}) ${dn.name}'
1922
}
20-
return sym_name
23+
return dn.name
2124
}
2225

2326
fn get_node_id(dn doc.DocNode) string {
@@ -30,7 +33,7 @@ fn get_node_id(dn doc.DocNode) string {
3033
}
3134

3235
fn is_module_readme(dn doc.DocNode) bool {
33-
return dn.comments.len > 0 && (dn.content == 'module ${dn.name}' || dn.name == 'README')
36+
return dn.is_readme || (dn.comments.len > 0 && dn.content == 'module ${dn.name}')
3437
}
3538

3639
// trim_doc_node_description returns the nodes trimmed description.

cmd/tools/vdoc/vdoc.v

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import v.util
1212
import json
1313
import term
1414

15+
struct Readme {
16+
frontmatter map[string]string
17+
content string
18+
}
19+
1520
enum OutputType {
1621
unset
1722
html
@@ -159,8 +164,7 @@ fn (mut vd VDoc) render_doc(d doc.Doc, out Output) (string, string) {
159164
fn (vd &VDoc) get_file_name(mod string, out Output) string {
160165
cfg := vd.cfg
161166
mut name := mod
162-
// since builtin is generated first, ignore it
163-
if (cfg.is_vlib && mod == 'builtin' && !cfg.include_readme) || mod == 'README' {
167+
if mod == 'README' {
164168
name = 'index'
165169
} else if !cfg.is_multi && !os.is_dir(out.path) {
166170
name = os.file_name(out.path)
@@ -220,10 +224,10 @@ fn (mut vd VDoc) render(out Output) map[string]string {
220224
return docs
221225
}
222226

223-
fn (vd &VDoc) get_readme(path string) string {
227+
fn (vd &VDoc) get_readme(path string) Readme {
224228
mut fname := ''
225-
for name in ['readme', 'README'] {
226-
if os.exists(os.join_path(path, '${name}.md')) {
229+
for name in ['readme.md', 'README.md'] {
230+
if os.exists(os.join_path(path, name)) {
227231
fname = name
228232
break
229233
}
@@ -232,12 +236,28 @@ fn (vd &VDoc) get_readme(path string) string {
232236
if path.all_after_last(os.path_separator) == 'src' {
233237
return vd.get_readme(path.all_before_last(os.path_separator))
234238
}
235-
return ''
239+
return Readme{}
236240
}
237-
readme_path := os.join_path(path, '${fname}.md')
241+
readme_path := os.join_path(path, fname)
238242
vd.vprintln('Reading README file from ${readme_path}')
239-
readme_contents := os.read_file(readme_path) or { '' }
240-
return readme_contents
243+
mut readme_contents := os.read_file(readme_path) or { '' }
244+
mut readme_frontmatter := map[string]string{}
245+
if readme_contents.starts_with('---\n') {
246+
if frontmatter_lines_end_idx := readme_contents.index('\n---\n') {
247+
front_matter_lines := readme_contents#[4..frontmatter_lines_end_idx].trim_space().split_into_lines()
248+
for line in front_matter_lines {
249+
x := line.split(': ')
250+
if x.len == 2 {
251+
readme_frontmatter[x[0]] = x[1]
252+
}
253+
}
254+
readme_contents = readme_contents#[5 + frontmatter_lines_end_idx..]
255+
}
256+
}
257+
return Readme{
258+
frontmatter: readme_frontmatter
259+
content: readme_contents
260+
}
241261
}
242262

243263
fn (vd &VDoc) emit_generate_err(err IError) {
@@ -277,7 +297,7 @@ fn (mut vd VDoc) generate_docs_from_file() {
277297
exit(1)
278298
}
279299
dir_path := if cfg.is_vlib {
280-
vroot
300+
os.join_path(vroot, 'vlib')
281301
} else if os.is_dir(cfg.input_path) {
282302
cfg.input_path
283303
} else {
@@ -290,18 +310,26 @@ fn (mut vd VDoc) generate_docs_from_file() {
290310
vd.manifest = manifest
291311
}
292312
}
293-
if cfg.include_readme {
294-
readme_contents := vd.get_readme(dir_path)
313+
if cfg.include_readme || cfg.is_vlib {
314+
mut readme_name := 'README'
315+
readme := vd.get_readme(dir_path)
316+
if page := readme.frontmatter['page'] {
317+
readme_name = page
318+
}
295319
comment := doc.DocComment{
296-
text: readme_contents
320+
is_readme: true
321+
frontmatter: readme.frontmatter
322+
text: readme.content
297323
}
298324
if out.typ == .ansi {
299-
println(markdown.to_plain(readme_contents))
325+
println(markdown.to_plain(readme.content))
300326
} else if out.typ == .html && cfg.is_multi {
301327
vd.docs << doc.Doc{
302328
head: doc.DocNode{
303-
name: 'README'
304-
comments: [comment]
329+
is_readme: true
330+
name: readme_name
331+
frontmatter: readme.frontmatter
332+
comments: [comment]
305333
}
306334
time_generated: time.now()
307335
}
@@ -327,9 +355,11 @@ fn (mut vd VDoc) generate_docs_from_file() {
327355
continue
328356
}
329357
if cfg.is_multi || (!cfg.is_multi && cfg.include_readme) {
330-
readme_contents := vd.get_readme(dirpath)
358+
readme := vd.get_readme(dirpath)
331359
comment := doc.DocComment{
332-
text: readme_contents
360+
is_readme: true
361+
frontmatter: readme.frontmatter
362+
text: readme.content
333363
}
334364
dcs.head.comments = [comment]
335365
}
@@ -343,13 +373,6 @@ fn (mut vd VDoc) generate_docs_from_file() {
343373
}
344374
vd.docs << dcs
345375
}
346-
// Important. Let builtin be in the top of the module list
347-
// if we are generating docs for vlib.
348-
if cfg.is_vlib {
349-
mut docs := vd.docs.filter(it.head.name == 'builtin')
350-
docs << vd.docs.filter(it.head.name != 'builtin')
351-
vd.docs = docs
352-
}
353376
if dirs.len == 0 && cfg.is_multi {
354377
eprintln('vdoc: -m requires at least 1 module folder')
355378
exit(1)

vlib/README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1-
# `vlib` Documentation
1+
---
2+
page: index
3+
title: V standard library documentation
4+
index: V stdlib
5+
---
6+
This site contains the documentation for the standard library of modules
7+
included with the [V language](https://vlang.io). Also commonly referred
8+
to as `vlib`, as that is the root directory for these modules in the V
9+
repository.
210

3-
`vlib` is the term for all modules included by default with V and
4-
maintained as part of the V source code repository.
11+
If you were looking for documentation for the language itself, the
12+
builtin types, operators, et. al., please use the
13+
[V documentation](https://docs.vlang.io/introduction.html) link.
514

615
Some included modules depend on third party libraries, and these are kept
716
separate in the `thirdparty` directory at the root level of the source
817
repository.
18+
19+
Note that the [builtin](https://modules.vlang.io/builtin.html) module is
20+
implicitly imported by default in V, so you do not need to have a specific
21+
`import` statement to use any of the features of that module.

vlib/v/doc/comment.v

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ const example_pattern = '\x01 Example: '
66

77
pub struct DocComment {
88
pub mut:
9-
text string // Raw text content of the comment, excluding the comment token chars ('//, /*, */')
10-
is_multi bool // Is a block / multi-line comment
11-
pos token.Pos
9+
text string // Raw text content of the comment, excluding the comment token chars ('//, /*, */')
10+
is_multi bool // Is a block / multi-line comment
11+
pos token.Pos
12+
is_readme bool
13+
frontmatter map[string]string
1214
}
1315

1416
// is_example returns true if the contents of this comment is an inline doc example.

vlib/v/doc/doc.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ pub mut:
143143
from_scope bool
144144
is_pub bool @[json: public]
145145
platform Platform
146+
is_readme bool
147+
frontmatter map[string]string
146148
}
147149

148150
// new_vdoc_preferences creates a new instance of pref.Preferences tailored for v.doc.

0 commit comments

Comments
 (0)