2020-06-02 13:10:01 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import markdown
|
|
|
|
import os
|
|
|
|
import os.cmdline
|
2020-06-06 15:31:26 +03:00
|
|
|
import time
|
2020-06-02 13:10:01 +03:00
|
|
|
import strings
|
2020-09-23 21:50:51 +03:00
|
|
|
import sync
|
|
|
|
import runtime
|
2020-06-02 13:10:01 +03:00
|
|
|
import v.doc
|
2020-07-11 13:04:05 +03:00
|
|
|
import v.pref
|
2021-01-08 13:25:22 +03:00
|
|
|
import v.vmod
|
2020-10-21 11:26:33 +03:00
|
|
|
import json
|
2021-03-15 14:21:19 +03:00
|
|
|
import term
|
2020-06-06 08:56:17 +03:00
|
|
|
|
2020-06-05 00:50:59 +03:00
|
|
|
const (
|
2020-06-05 10:59:26 +03:00
|
|
|
allowed_formats = ['md', 'markdown', 'json', 'text', 'stdout', 'html', 'htm']
|
2020-11-30 20:31:37 +03:00
|
|
|
vexe = pref.vexe_path()
|
|
|
|
vroot = os.dir(vexe)
|
2021-01-02 12:11:34 +03:00
|
|
|
tabs = ['\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t']
|
2020-06-05 00:50:59 +03:00
|
|
|
)
|
|
|
|
|
2020-06-02 13:10:01 +03:00
|
|
|
enum OutputType {
|
2020-06-05 10:59:26 +03:00
|
|
|
unset
|
2020-06-02 13:10:01 +03:00
|
|
|
html
|
|
|
|
markdown
|
|
|
|
json
|
|
|
|
plaintext
|
|
|
|
stdout
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
struct VDoc {
|
2021-01-12 06:38:43 +03:00
|
|
|
cfg Config [required]
|
2020-06-02 13:10:01 +03:00
|
|
|
mut:
|
2020-12-29 14:51:24 +03:00
|
|
|
docs []doc.Doc
|
|
|
|
assets map[string]string
|
2021-01-08 13:25:22 +03:00
|
|
|
manifest vmod.Manifest
|
2020-12-29 14:51:24 +03:00
|
|
|
search_index []string
|
|
|
|
search_data []SearchResult
|
|
|
|
search_module_index []string // search results are split into a module part and the rest
|
|
|
|
search_module_data []SearchModuleResult
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
struct Config {
|
|
|
|
mut:
|
|
|
|
pub_only bool = true
|
|
|
|
show_loc bool // for plaintext
|
2021-03-15 14:21:19 +03:00
|
|
|
is_color bool
|
2021-01-08 13:25:22 +03:00
|
|
|
is_multi bool
|
|
|
|
is_vlib bool
|
|
|
|
is_verbose bool
|
|
|
|
include_readme bool
|
|
|
|
include_examples bool = true
|
2021-08-19 10:20:43 +03:00
|
|
|
include_comments bool // for plaintext
|
2021-01-08 13:25:22 +03:00
|
|
|
inline_assets bool
|
2022-02-21 13:08:46 +03:00
|
|
|
theme_dir string = default_theme
|
2021-01-08 13:25:22 +03:00
|
|
|
no_timestamp bool
|
|
|
|
output_path string
|
|
|
|
output_type OutputType = .unset
|
|
|
|
input_path string
|
|
|
|
symbol_name string
|
2021-03-27 11:50:06 +03:00
|
|
|
platform doc.Platform
|
2021-01-08 13:25:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
struct Output {
|
|
|
|
mut:
|
|
|
|
path string
|
|
|
|
typ OutputType = .unset
|
2020-09-23 21:50:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ParallelDoc {
|
2021-01-08 13:25:22 +03:00
|
|
|
d doc.Doc
|
|
|
|
out Output
|
2020-06-21 17:41:43 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (vd VDoc) gen_json(d doc.Doc) string {
|
|
|
|
cfg := vd.cfg
|
2020-06-02 13:10:01 +03:00
|
|
|
mut jw := strings.new_builder(200)
|
2021-01-06 21:43:43 +03:00
|
|
|
comments := if cfg.include_examples {
|
2021-01-08 13:25:22 +03:00
|
|
|
d.head.merge_comments()
|
2021-01-06 21:43:43 +03:00
|
|
|
} else {
|
2021-01-08 13:25:22 +03:00
|
|
|
d.head.merge_comments_without_examples()
|
2021-01-06 21:43:43 +03:00
|
|
|
}
|
2021-02-22 16:33:03 +03:00
|
|
|
jw.write_string('{"module_name":"$d.head.name","description":"${escape(comments)}","contents":')
|
|
|
|
jw.write_string(json.encode(d.contents.keys().map(d.contents[it])))
|
|
|
|
jw.write_string(',"generator":"vdoc","time_generated":"$d.time_generated.str()"}')
|
2020-06-02 13:10:01 +03:00
|
|
|
return jw.str()
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (vd VDoc) gen_plaintext(d doc.Doc) string {
|
|
|
|
cfg := vd.cfg
|
2020-06-02 13:10:01 +03:00
|
|
|
mut pw := strings.new_builder(200)
|
2021-03-15 14:21:19 +03:00
|
|
|
if cfg.is_color {
|
|
|
|
content_arr := d.head.content.split(' ')
|
2021-03-16 13:24:34 +03:00
|
|
|
pw.writeln('${term.bright_blue(content_arr[0])} ${term.green(content_arr[1])}\n')
|
2021-03-15 14:21:19 +03:00
|
|
|
} else {
|
|
|
|
pw.writeln('$d.head.content\n')
|
|
|
|
}
|
2021-08-19 10:20:43 +03:00
|
|
|
if cfg.include_comments {
|
|
|
|
comments := if cfg.include_examples {
|
|
|
|
d.head.merge_comments()
|
|
|
|
} else {
|
|
|
|
d.head.merge_comments_without_examples()
|
|
|
|
}
|
|
|
|
if comments.trim_space().len > 0 {
|
|
|
|
pw.writeln(comments.split_into_lines().map(' ' + it).join('\n'))
|
|
|
|
}
|
2020-06-08 09:22:10 +03:00
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.write_plaintext_content(d.contents.arr(), mut pw)
|
2020-11-20 13:02:52 +03:00
|
|
|
return pw.str()
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (vd VDoc) write_plaintext_content(contents []doc.DocNode, mut pw strings.Builder) {
|
|
|
|
cfg := vd.cfg
|
2020-11-20 13:02:52 +03:00
|
|
|
for cn in contents {
|
|
|
|
if cn.content.len > 0 {
|
2021-03-15 14:21:19 +03:00
|
|
|
if cfg.is_color {
|
|
|
|
pw.writeln(color_highlight(cn.content, vd.docs[0].table))
|
|
|
|
} else {
|
|
|
|
pw.writeln(cn.content)
|
|
|
|
}
|
2021-08-19 10:20:43 +03:00
|
|
|
if cn.comments.len > 0 && cfg.include_comments {
|
2021-01-06 21:43:43 +03:00
|
|
|
comments := if cfg.include_examples {
|
|
|
|
cn.merge_comments()
|
|
|
|
} else {
|
|
|
|
cn.merge_comments_without_examples()
|
|
|
|
}
|
|
|
|
pw.writeln(comments.trim_space().split_into_lines().map(' ' + it).join('\n'))
|
2020-11-20 13:02:52 +03:00
|
|
|
}
|
|
|
|
if cfg.show_loc {
|
2021-03-23 11:07:09 +03:00
|
|
|
pw.writeln('Location: $cn.file_path:${cn.pos.line_nr + 1}\n')
|
2020-11-20 13:02:52 +03:00
|
|
|
}
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.write_plaintext_content(cn.children, mut pw)
|
2020-11-20 13:02:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (vd VDoc) render_doc(d doc.Doc, out Output) (string, string) {
|
|
|
|
name := vd.get_file_name(d.head.name, out)
|
|
|
|
output := match out.typ {
|
|
|
|
.html { vd.gen_html(d) }
|
|
|
|
.markdown { vd.gen_markdown(d, true) }
|
|
|
|
.json { vd.gen_json(d) }
|
|
|
|
else { vd.gen_plaintext(d) }
|
2020-12-29 14:51:24 +03:00
|
|
|
}
|
|
|
|
return name, output
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_file_name returns the final file name from a module name
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (vd VDoc) get_file_name(mod string, out Output) string {
|
|
|
|
cfg := vd.cfg
|
2020-12-29 14:51:24 +03:00
|
|
|
mut name := mod
|
2020-09-23 21:50:51 +03:00
|
|
|
// since builtin is generated first, ignore it
|
2020-12-29 14:51:24 +03:00
|
|
|
if (cfg.is_vlib && mod == 'builtin' && !cfg.include_readme) || mod == 'README' {
|
2020-10-29 19:21:37 +03:00
|
|
|
name = 'index'
|
2021-01-08 13:25:22 +03:00
|
|
|
} else if !cfg.is_multi && !os.is_dir(out.path) {
|
|
|
|
name = os.file_name(out.path)
|
2020-09-23 21:50:51 +03:00
|
|
|
}
|
2022-01-06 05:05:23 +03:00
|
|
|
if name == '' {
|
|
|
|
name = 'index'
|
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
name = name + match out.typ {
|
2020-09-23 21:50:51 +03:00
|
|
|
.html { '.html' }
|
|
|
|
.markdown { '.md' }
|
|
|
|
.json { '.json' }
|
|
|
|
else { '.txt' }
|
|
|
|
}
|
2020-12-29 14:51:24 +03:00
|
|
|
return name
|
2020-09-23 21:50:51 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (vd VDoc) work_processor(mut work sync.Channel, mut wg sync.WaitGroup) {
|
2020-09-23 21:50:51 +03:00
|
|
|
for {
|
|
|
|
mut pdoc := ParallelDoc{}
|
|
|
|
if !work.pop(&pdoc) {
|
|
|
|
break
|
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
file_name, content := vd.render_doc(pdoc.d, pdoc.out)
|
|
|
|
output_path := os.join_path(pdoc.out.path, file_name)
|
|
|
|
println('Generating $pdoc.out.typ in "$output_path"')
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file(output_path, content) or { panic(err) }
|
2020-09-23 21:50:51 +03:00
|
|
|
}
|
|
|
|
wg.done()
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (vd VDoc) render_parallel(out Output) {
|
2020-09-23 21:50:51 +03:00
|
|
|
vjobs := runtime.nr_jobs()
|
2021-07-04 19:15:15 +03:00
|
|
|
mut work := sync.new_channel<ParallelDoc>(u32(vd.docs.len))
|
2020-09-23 21:50:51 +03:00
|
|
|
mut wg := sync.new_waitgroup()
|
2021-01-08 13:25:22 +03:00
|
|
|
for i in 0 .. vd.docs.len {
|
|
|
|
p_doc := ParallelDoc{vd.docs[i], out}
|
2020-09-23 21:50:51 +03:00
|
|
|
work.push(&p_doc)
|
|
|
|
}
|
|
|
|
work.close()
|
|
|
|
wg.add(vjobs)
|
|
|
|
for _ in 0 .. vjobs {
|
2021-01-08 13:25:22 +03:00
|
|
|
go vd.work_processor(mut work, mut wg)
|
2020-09-23 21:50:51 +03:00
|
|
|
}
|
|
|
|
wg.wait()
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (vd VDoc) render(out Output) map[string]string {
|
2020-09-21 17:40:39 +03:00
|
|
|
mut docs := map[string]string{}
|
2021-01-08 13:25:22 +03:00
|
|
|
for doc in vd.docs {
|
|
|
|
name, output := vd.render_doc(doc, out)
|
2020-06-21 17:41:43 +03:00
|
|
|
docs[name] = output.trim_space()
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.vprintln('Rendered: ' + docs.keys().str())
|
2020-06-02 13:10:01 +03:00
|
|
|
return docs
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (vd VDoc) get_readme(path string) string {
|
2020-06-07 13:27:42 +03:00
|
|
|
mut fname := ''
|
|
|
|
for name in ['readme', 'README'] {
|
|
|
|
if os.exists(os.join_path(path, '${name}.md')) {
|
|
|
|
fname = name
|
|
|
|
break
|
2020-07-11 13:04:05 +03:00
|
|
|
}
|
2020-06-07 13:27:42 +03:00
|
|
|
}
|
|
|
|
if fname == '' {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
readme_path := os.join_path(path, '${fname}.md')
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.vprintln('Reading README file from $readme_path')
|
2020-12-07 04:43:25 +03:00
|
|
|
readme_contents := os.read_file(readme_path) or { '' }
|
2020-06-07 13:27:42 +03:00
|
|
|
return readme_contents
|
|
|
|
}
|
|
|
|
|
2021-03-13 20:13:50 +03:00
|
|
|
fn (vd VDoc) emit_generate_err(err IError) {
|
2021-01-08 13:25:22 +03:00
|
|
|
cfg := vd.cfg
|
2021-02-28 22:24:29 +03:00
|
|
|
mut err_msg := err.msg
|
|
|
|
if err.code == 1 {
|
2020-10-21 11:26:33 +03:00
|
|
|
mod_list := get_modules_list(cfg.input_path, []string{})
|
|
|
|
println('Available modules:\n==================')
|
|
|
|
for mod in mod_list {
|
2020-10-21 14:36:16 +03:00
|
|
|
println(mod.all_after('vlib/').all_after('modules/').replace('/', '.'))
|
2020-10-21 11:26:33 +03:00
|
|
|
}
|
|
|
|
err_msg += ' Use the `-m` flag when generating docs from a directory that has multiple modules.'
|
|
|
|
}
|
|
|
|
eprintln(err_msg)
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (mut vd VDoc) generate_docs_from_file() {
|
|
|
|
cfg := vd.cfg
|
|
|
|
mut out := Output{
|
|
|
|
path: cfg.output_path
|
|
|
|
typ: cfg.output_type
|
|
|
|
}
|
|
|
|
if out.path.len == 0 {
|
2020-06-05 10:59:26 +03:00
|
|
|
if cfg.output_type == .unset {
|
2021-01-08 13:25:22 +03:00
|
|
|
out.typ = .stdout
|
2020-06-02 13:10:01 +03:00
|
|
|
} else {
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.vprintln('No output path has detected. Using input path instead.')
|
|
|
|
out.path = cfg.input_path
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
} else if out.typ == .unset {
|
|
|
|
vd.vprintln('Output path detected. Identifying output type..')
|
|
|
|
ext := os.file_ext(out.path)
|
|
|
|
out.typ = set_output_type_from_str(ext.all_after('.'))
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
if cfg.include_readme && out.typ !in [.html, .stdout] {
|
2020-06-02 23:56:13 +03:00
|
|
|
eprintln('vdoc: Including README.md for doc generation is supported on HTML output, or when running directly in the terminal.')
|
2020-06-02 13:10:01 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-10-29 19:21:37 +03:00
|
|
|
dir_path := if cfg.is_vlib {
|
2021-01-25 14:08:43 +03:00
|
|
|
vroot
|
2020-07-11 13:04:05 +03:00
|
|
|
} else if os.is_dir(cfg.input_path) {
|
2020-06-05 10:59:26 +03:00
|
|
|
cfg.input_path
|
2020-07-11 13:04:05 +03:00
|
|
|
} else {
|
2020-10-01 02:30:22 +03:00
|
|
|
os.dir(cfg.input_path)
|
2020-06-05 10:59:26 +03:00
|
|
|
}
|
|
|
|
manifest_path := os.join_path(dir_path, 'v.mod')
|
|
|
|
if os.exists(manifest_path) {
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.vprintln('Reading v.mod info from $manifest_path')
|
2020-06-02 13:10:01 +03:00
|
|
|
if manifest := vmod.from_file(manifest_path) {
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.manifest = manifest
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-07 13:27:42 +03:00
|
|
|
if cfg.include_readme {
|
2021-01-08 13:25:22 +03:00
|
|
|
readme_contents := vd.get_readme(dir_path)
|
2021-01-06 21:43:43 +03:00
|
|
|
comment := doc.DocComment{
|
|
|
|
text: readme_contents
|
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
if out.typ == .stdout {
|
2020-06-05 10:59:26 +03:00
|
|
|
println(markdown.to_plain(readme_contents))
|
2021-01-08 13:25:22 +03:00
|
|
|
} else if out.typ == .html && cfg.is_multi {
|
|
|
|
vd.docs << doc.Doc{
|
2020-06-02 23:56:13 +03:00
|
|
|
head: doc.DocNode{
|
2020-09-21 17:40:39 +03:00
|
|
|
name: 'README'
|
2021-01-06 21:43:43 +03:00
|
|
|
comments: [comment]
|
2020-06-02 23:56:13 +03:00
|
|
|
}
|
2020-06-06 15:31:26 +03:00
|
|
|
time_generated: time.now()
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
2020-07-04 15:29:00 +03:00
|
|
|
}
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
2021-09-21 16:20:09 +03:00
|
|
|
dirs := if cfg.is_multi { get_modules_list(cfg.input_path, []string{}) } else { [
|
|
|
|
cfg.input_path,
|
|
|
|
] }
|
2020-06-05 10:59:26 +03:00
|
|
|
for dirpath in dirs {
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.vprintln('Generating $out.typ docs for "$dirpath"')
|
2021-03-27 11:50:06 +03:00
|
|
|
mut dcs := doc.generate(dirpath, cfg.pub_only, true, cfg.platform, cfg.symbol_name) or {
|
2021-03-13 08:49:03 +03:00
|
|
|
vd.emit_generate_err(err)
|
|
|
|
exit(1)
|
2020-10-21 11:26:33 +03:00
|
|
|
}
|
|
|
|
if dcs.contents.len == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-13 08:49:03 +03:00
|
|
|
if cfg.is_multi || (!cfg.is_multi && cfg.include_readme) {
|
|
|
|
readme_contents := vd.get_readme(dirpath)
|
|
|
|
comment := doc.DocComment{
|
|
|
|
text: readme_contents
|
2020-09-21 17:40:39 +03:00
|
|
|
}
|
2021-03-13 08:49:03 +03:00
|
|
|
dcs.head.comments = [comment]
|
|
|
|
}
|
|
|
|
if cfg.pub_only {
|
|
|
|
for name, dc in dcs.contents {
|
|
|
|
dcs.contents[name].content = dc.content.all_after('pub ')
|
|
|
|
for i, cc in dc.children {
|
|
|
|
dcs.contents[name].children[i].content = cc.content.all_after('pub ')
|
2020-09-21 17:40:39 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 15:31:26 +03:00
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.docs << dcs
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
// Important. Let builtin be in the top of the module list
|
|
|
|
// if we are generating docs for vlib.
|
2020-10-29 19:21:37 +03:00
|
|
|
if cfg.is_vlib {
|
2021-01-08 13:25:22 +03:00
|
|
|
mut docs := vd.docs.filter(it.head.name == 'builtin')
|
|
|
|
docs << vd.docs.filter(it.head.name != 'builtin')
|
|
|
|
vd.docs = docs
|
2020-06-29 12:03:09 +03:00
|
|
|
}
|
2021-10-09 11:56:30 +03:00
|
|
|
if dirs.len == 0 && cfg.is_multi {
|
|
|
|
eprintln('vdoc: -m requires at least 1 module folder')
|
|
|
|
exit(1)
|
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.vprintln('Rendering docs...')
|
|
|
|
if out.path.len == 0 || out.path == 'stdout' {
|
|
|
|
if out.typ == .html {
|
2021-01-10 00:47:58 +03:00
|
|
|
vd.render_static_html(out)
|
2021-01-08 13:25:22 +03:00
|
|
|
}
|
|
|
|
outputs := vd.render(out)
|
2020-06-21 17:51:02 +03:00
|
|
|
if outputs.len == 0 {
|
2021-10-09 11:56:30 +03:00
|
|
|
if dirs.len == 0 {
|
|
|
|
eprintln('vdoc: No documentation found')
|
|
|
|
} else {
|
|
|
|
eprintln('vdoc: No documentation found for ${dirs[0]}')
|
|
|
|
}
|
2021-03-18 16:04:53 +03:00
|
|
|
exit(1)
|
2020-06-08 09:22:10 +03:00
|
|
|
} else {
|
|
|
|
first := outputs.keys()[0]
|
|
|
|
println(outputs[first])
|
|
|
|
}
|
2020-06-02 13:10:01 +03:00
|
|
|
} else {
|
2021-01-08 13:25:22 +03:00
|
|
|
if !os.exists(out.path) {
|
2021-04-12 15:59:40 +03:00
|
|
|
os.mkdir_all(out.path) or { panic(err) }
|
|
|
|
} else if !os.is_dir(out.path) {
|
|
|
|
out.path = os.real_path('.')
|
2020-06-19 11:36:45 +03:00
|
|
|
}
|
2020-06-05 10:59:26 +03:00
|
|
|
if cfg.is_multi {
|
2021-01-08 13:25:22 +03:00
|
|
|
out.path = os.join_path(out.path, '_docs')
|
|
|
|
if !os.exists(out.path) {
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mkdir(out.path) or { panic(err) }
|
2020-06-05 10:59:26 +03:00
|
|
|
} else {
|
2020-06-08 11:28:46 +03:00
|
|
|
for fname in css_js_assets {
|
2021-04-21 14:32:34 +03:00
|
|
|
existing_asset_path := os.join_path(out.path, fname)
|
|
|
|
if os.exists(existing_asset_path) {
|
|
|
|
os.rm(existing_asset_path) or { panic(err) }
|
|
|
|
}
|
2020-06-06 11:43:50 +03:00
|
|
|
}
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
if out.typ == .html {
|
2021-01-10 00:47:58 +03:00
|
|
|
vd.render_static_html(out)
|
2020-12-18 11:41:34 +03:00
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.render_parallel(out)
|
|
|
|
if out.typ == .html {
|
2021-02-13 10:04:47 +03:00
|
|
|
println('Creating search index...')
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.collect_search_index(out)
|
|
|
|
vd.render_search_index(out)
|
|
|
|
// move favicons to target directory
|
|
|
|
println('Copying favicons...')
|
2022-02-21 13:08:46 +03:00
|
|
|
|
|
|
|
favicons_path := os.join_path(cfg.theme_dir, 'favicons')
|
|
|
|
|
2021-03-01 02:18:14 +03:00
|
|
|
favicons := os.ls(favicons_path) or { panic(err) }
|
2021-01-08 13:25:22 +03:00
|
|
|
for favicon in favicons {
|
|
|
|
favicon_path := os.join_path(favicons_path, favicon)
|
|
|
|
destination_path := os.join_path(out.path, favicon)
|
2021-03-01 02:18:14 +03:00
|
|
|
os.cp(favicon_path, destination_path) or { panic(err) }
|
2020-09-21 17:40:39 +03:00
|
|
|
}
|
2020-06-29 12:03:09 +03:00
|
|
|
}
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (vd VDoc) vprintln(str string) {
|
|
|
|
if vd.cfg.is_verbose {
|
|
|
|
println('vdoc: $str')
|
2020-06-05 00:50:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn parse_arguments(args []string) Config {
|
|
|
|
mut cfg := Config{}
|
2021-03-15 14:21:19 +03:00
|
|
|
cfg.is_color = term.can_show_color_on_stdout()
|
2020-06-05 10:59:26 +03:00
|
|
|
for i := 0; i < args.len; i++ {
|
|
|
|
arg := args[i]
|
|
|
|
current_args := args[i..]
|
|
|
|
match arg {
|
|
|
|
'-all' {
|
|
|
|
cfg.pub_only = false
|
|
|
|
}
|
|
|
|
'-f' {
|
|
|
|
format := cmdline.option(current_args, '-f', '')
|
2021-01-25 14:08:43 +03:00
|
|
|
if format !in allowed_formats {
|
|
|
|
allowed_str := allowed_formats.join(', ')
|
2020-06-05 10:59:26 +03:00
|
|
|
eprintln('vdoc: "$format" is not a valid format. Only $allowed_str are allowed.')
|
|
|
|
exit(1)
|
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
cfg.output_type = set_output_type_from_str(format)
|
2020-06-05 10:59:26 +03:00
|
|
|
i++
|
|
|
|
}
|
2021-03-15 14:21:19 +03:00
|
|
|
'-color' {
|
|
|
|
cfg.is_color = true
|
|
|
|
}
|
|
|
|
'-no-color' {
|
|
|
|
cfg.is_color = false
|
|
|
|
}
|
2020-06-05 10:59:26 +03:00
|
|
|
'-inline-assets' {
|
|
|
|
cfg.inline_assets = true
|
|
|
|
}
|
2022-02-21 13:08:46 +03:00
|
|
|
'-theme-dir' {
|
|
|
|
cfg.theme_dir = cmdline.option(current_args, '-theme-dir', default_theme)
|
|
|
|
}
|
2020-06-05 10:59:26 +03:00
|
|
|
'-l' {
|
|
|
|
cfg.show_loc = true
|
|
|
|
}
|
2021-08-19 10:20:43 +03:00
|
|
|
'-comments' {
|
|
|
|
cfg.include_comments = true
|
|
|
|
}
|
2020-06-05 10:59:26 +03:00
|
|
|
'-m' {
|
|
|
|
cfg.is_multi = true
|
|
|
|
}
|
|
|
|
'-o' {
|
|
|
|
opath := cmdline.option(current_args, '-o', '')
|
2020-09-21 17:40:39 +03:00
|
|
|
cfg.output_path = if opath == 'stdout' { opath } else { os.real_path(opath) }
|
2020-06-05 10:59:26 +03:00
|
|
|
i++
|
|
|
|
}
|
2021-03-27 11:50:06 +03:00
|
|
|
'-os' {
|
|
|
|
platform_str := cmdline.option(current_args, '-os', '')
|
|
|
|
if platform_str == 'cross' {
|
|
|
|
eprintln('`v doc -os cross` is not supported yet.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
selected_platform := doc.platform_from_string(platform_str) or {
|
|
|
|
eprintln(err.msg)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
cfg.platform = selected_platform
|
|
|
|
i++
|
|
|
|
}
|
2020-10-19 22:30:37 +03:00
|
|
|
'-no-timestamp' {
|
|
|
|
cfg.no_timestamp = true
|
|
|
|
}
|
2021-01-06 21:43:43 +03:00
|
|
|
'-no-examples' {
|
|
|
|
cfg.include_examples = false
|
|
|
|
}
|
2020-06-19 11:36:45 +03:00
|
|
|
'-readme' {
|
2020-06-05 10:59:26 +03:00
|
|
|
cfg.include_readme = true
|
|
|
|
}
|
|
|
|
'-v' {
|
|
|
|
cfg.is_verbose = true
|
|
|
|
}
|
|
|
|
else {
|
2020-09-21 17:40:39 +03:00
|
|
|
if cfg.input_path.len < 1 {
|
|
|
|
cfg.input_path = arg
|
2021-03-09 14:05:50 +03:00
|
|
|
} else if !cfg.is_multi {
|
|
|
|
// Symbol name filtering should not be enabled
|
|
|
|
// in multi-module documentation mode.
|
2020-09-21 17:40:39 +03:00
|
|
|
cfg.symbol_name = arg
|
|
|
|
}
|
|
|
|
if i == args.len - 1 {
|
|
|
|
break
|
|
|
|
}
|
2020-06-05 10:59:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
// Correct from configuration from user input
|
2020-10-21 11:26:33 +03:00
|
|
|
if cfg.output_path == 'stdout' && cfg.output_type == .html {
|
|
|
|
cfg.inline_assets = true
|
|
|
|
}
|
2020-06-19 11:36:45 +03:00
|
|
|
$if windows {
|
|
|
|
cfg.input_path = cfg.input_path.replace('/', os.path_separator)
|
|
|
|
} $else {
|
|
|
|
cfg.input_path = cfg.input_path.replace('\\', os.path_separator)
|
|
|
|
}
|
2021-01-23 11:33:22 +03:00
|
|
|
is_path := cfg.input_path.ends_with('.v') || cfg.input_path.split(os.path_separator).len > 1
|
|
|
|
|| cfg.input_path == '.'
|
2020-11-17 17:08:55 +03:00
|
|
|
if cfg.input_path.trim_right('/') == 'vlib' {
|
2020-10-29 19:21:37 +03:00
|
|
|
cfg.is_vlib = true
|
2020-06-05 10:59:26 +03:00
|
|
|
cfg.is_multi = true
|
2021-01-25 14:08:43 +03:00
|
|
|
cfg.input_path = os.join_path(vroot, 'vlib')
|
2020-06-05 10:59:26 +03:00
|
|
|
} else if !is_path {
|
2021-01-08 13:25:22 +03:00
|
|
|
// TODO vd.vprintln('Input "$cfg.input_path" is not a valid path. Looking for modules named "$cfg.input_path"...')
|
2020-10-21 11:26:33 +03:00
|
|
|
mod_path := doc.lookup_module(cfg.input_path) or {
|
|
|
|
eprintln('vdoc: $err')
|
2020-06-02 13:10:01 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-06-05 10:59:26 +03:00
|
|
|
cfg.input_path = mod_path
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
return cfg
|
2020-06-02 13:10:01 +03:00
|
|
|
}
|
2021-01-05 03:40:21 +03:00
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn main() {
|
2021-03-08 18:21:40 +03:00
|
|
|
if os.args.len < 2 || '-h' in os.args || '-help' in os.args || '--help' in os.args
|
|
|
|
|| os.args[1..] == ['doc', 'help'] {
|
2022-01-22 22:13:16 +03:00
|
|
|
os.system('${os.quoted_path(vexe)} help doc')
|
2021-01-08 13:25:22 +03:00
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
args := os.args[2..].clone()
|
|
|
|
cfg := parse_arguments(args)
|
|
|
|
if cfg.input_path.len == 0 {
|
|
|
|
eprintln('vdoc: No input path found.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
// Config is immutable from this point on
|
|
|
|
mut vd := VDoc{
|
|
|
|
cfg: cfg
|
|
|
|
manifest: vmod.Manifest{
|
|
|
|
repo_url: ''
|
|
|
|
}
|
2021-01-05 03:40:21 +03:00
|
|
|
}
|
2021-01-08 13:25:22 +03:00
|
|
|
vd.vprintln('Setting output type to "$cfg.output_type"')
|
|
|
|
vd.generate_docs_from_file()
|
2021-01-05 03:40:21 +03:00
|
|
|
}
|