@@ -12,6 +12,11 @@ import v.util
1212import json
1313import term
1414
15+ struct Readme {
16+ frontmatter map [string ]string
17+ content string
18+ }
19+
1520enum OutputType {
1621 unset
1722 html
@@ -159,8 +164,7 @@ fn (mut vd VDoc) render_doc(d doc.Doc, out Output) (string, string) {
159164fn (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
243263fn (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 )
0 commit comments