@@ -12,6 +12,11 @@ import v.util
12
12
import json
13
13
import term
14
14
15
+ struct Readme {
16
+ frontmatter map [string ]string
17
+ content string
18
+ }
19
+
15
20
enum OutputType {
16
21
unset
17
22
html
@@ -159,8 +164,7 @@ fn (mut vd VDoc) render_doc(d doc.Doc, out Output) (string, string) {
159
164
fn (vd &VDoc) get_file_name (mod string , out Output) string {
160
165
cfg := vd.cfg
161
166
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' {
164
168
name = 'index'
165
169
} else if ! cfg.is_multi && ! os.is_dir (out.path) {
166
170
name = os.file_name (out.path)
@@ -220,10 +224,10 @@ fn (mut vd VDoc) render(out Output) map[string]string {
220
224
return docs
221
225
}
222
226
223
- fn (vd &VDoc) get_readme (path string ) string {
227
+ fn (vd &VDoc) get_readme (path string ) Readme {
224
228
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)) {
227
231
fname = name
228
232
break
229
233
}
@@ -232,12 +236,28 @@ fn (vd &VDoc) get_readme(path string) string {
232
236
if path.all_after_last (os.path_separator) == 'src' {
233
237
return vd.get_readme (path.all_before_last (os.path_separator))
234
238
}
235
- return ''
239
+ return Readme{}
236
240
}
237
- readme_path := os.join_path (path, ' ${ fname} .md' )
241
+ readme_path := os.join_path (path, fname)
238
242
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
+ }
241
261
}
242
262
243
263
fn (vd &VDoc) emit_generate_err (err IError) {
@@ -277,7 +297,7 @@ fn (mut vd VDoc) generate_docs_from_file() {
277
297
exit (1 )
278
298
}
279
299
dir_path := if cfg.is_vlib {
280
- vroot
300
+ os. join_path ( vroot, 'vlib' )
281
301
} else if os.is_dir (cfg.input_path) {
282
302
cfg.input_path
283
303
} else {
@@ -290,18 +310,26 @@ fn (mut vd VDoc) generate_docs_from_file() {
290
310
vd.manifest = manifest
291
311
}
292
312
}
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
+ }
295
319
comment := doc.DocComment{
296
- text: readme_contents
320
+ is_readme: true
321
+ frontmatter: readme.frontmatter
322
+ text: readme.content
297
323
}
298
324
if out.typ == .ansi {
299
- println (markdown.to_plain (readme_contents ))
325
+ println (markdown.to_plain (readme.content ))
300
326
} else if out.typ == .html && cfg.is_multi {
301
327
vd.docs << doc.Doc{
302
328
head: doc.DocNode{
303
- name: 'README'
304
- comments: [comment]
329
+ is_readme: true
330
+ name: readme_name
331
+ frontmatter: readme.frontmatter
332
+ comments: [comment]
305
333
}
306
334
time_generated: time.now ()
307
335
}
@@ -327,9 +355,11 @@ fn (mut vd VDoc) generate_docs_from_file() {
327
355
continue
328
356
}
329
357
if cfg.is_multi || (! cfg.is_multi && cfg.include_readme) {
330
- readme_contents := vd.get_readme (dirpath)
358
+ readme := vd.get_readme (dirpath)
331
359
comment := doc.DocComment{
332
- text: readme_contents
360
+ is_readme: true
361
+ frontmatter: readme.frontmatter
362
+ text: readme.content
333
363
}
334
364
dcs.head.comments = [comment]
335
365
}
@@ -343,13 +373,6 @@ fn (mut vd VDoc) generate_docs_from_file() {
343
373
}
344
374
vd.docs << dcs
345
375
}
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
- }
353
376
if dirs.len == 0 && cfg.is_multi {
354
377
eprintln ('vdoc: -m requires at least 1 module folder' )
355
378
exit (1 )
0 commit comments