2020-06-02 12:10:01 +02:00
|
|
|
module main
|
|
|
|
|
|
|
|
import markdown
|
|
|
|
import net
|
|
|
|
import net.urllib
|
|
|
|
import os
|
|
|
|
import os.cmdline
|
2020-06-06 14:31:26 +02:00
|
|
|
import time
|
2020-06-02 12:10:01 +02:00
|
|
|
import strings
|
|
|
|
import v.doc
|
2020-06-06 07:56:17 +02:00
|
|
|
import v.scanner
|
|
|
|
import v.table
|
|
|
|
import v.token
|
2020-06-02 12:10:01 +02:00
|
|
|
import v.vmod
|
|
|
|
|
2020-06-06 07:56:17 +02:00
|
|
|
enum HighlightTokenTyp {
|
|
|
|
unone
|
|
|
|
boolean
|
|
|
|
builtin
|
|
|
|
char
|
|
|
|
comment
|
|
|
|
function
|
|
|
|
keyword
|
|
|
|
name
|
|
|
|
number
|
|
|
|
operator
|
|
|
|
punctuation
|
|
|
|
string
|
|
|
|
symbol
|
|
|
|
}
|
|
|
|
|
2020-06-04 23:50:59 +02:00
|
|
|
const (
|
2020-06-08 10:28:46 +02:00
|
|
|
css_js_assets = ['doc.css', 'normalize.css' 'doc.js']
|
2020-06-05 09:59:26 +02:00
|
|
|
allowed_formats = ['md', 'markdown', 'json', 'text', 'stdout', 'html', 'htm']
|
2020-06-05 14:22:50 +02:00
|
|
|
exe_path = os.executable()
|
|
|
|
exe_dir = os.dir(exe_path)
|
|
|
|
res_path = os.join_path(exe_dir, 'vdoc-resources')
|
|
|
|
vexe_path = os.base_dir(@VEXE)
|
2020-06-19 10:36:45 +02:00
|
|
|
html_content = '
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta http-equiv="x-ua-compatible" content="IE=edge" />
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>{{ title }} | vdoc</title>
|
|
|
|
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
|
|
|
|
{{ head_assets }}
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="page">
|
|
|
|
<header class="doc-nav hidden">
|
|
|
|
<div class="heading-container">
|
|
|
|
<div class="heading">
|
|
|
|
<input type="text" id="search" placeholder="Search...">
|
|
|
|
<div class="module">{{ head_name }}</div>
|
|
|
|
<div class="toggle-version-container">
|
|
|
|
<span>{{ version }}</span>
|
|
|
|
<div id="dark-mode-toggle" role="switch" aria-checked="false" aria-label="Toggle dark mode">{{ light_icon }}{{ dark_icon }}</div>
|
|
|
|
</div>
|
|
|
|
{{ menu_icon }}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<nav class="content hidden">
|
|
|
|
<ul>
|
|
|
|
{{ toc_links }}
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
</header>
|
|
|
|
<div class="doc-container">
|
|
|
|
<div class="doc-content">
|
|
|
|
{{ contents }}
|
|
|
|
<div class="footer">
|
|
|
|
{{ footer_content }}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{{ right_content }}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{{ footer_assets }}
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
'
|
2020-06-04 23:50:59 +02:00
|
|
|
)
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
enum OutputType {
|
2020-06-05 09:59:26 +02:00
|
|
|
unset
|
2020-06-02 12:10:01 +02:00
|
|
|
html
|
|
|
|
markdown
|
|
|
|
json
|
|
|
|
plaintext
|
|
|
|
stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DocConfig {
|
|
|
|
mut:
|
2020-06-05 14:22:50 +02:00
|
|
|
pub_only bool = true
|
|
|
|
show_loc bool = false // for plaintext
|
|
|
|
serve_http bool = false // for html
|
|
|
|
is_multi bool = false
|
|
|
|
is_verbose bool = false
|
2020-06-05 09:59:26 +02:00
|
|
|
include_readme bool = false
|
2020-06-07 12:27:42 +02:00
|
|
|
open_docs bool = false
|
|
|
|
server_port int = 8046
|
2020-06-05 14:22:50 +02:00
|
|
|
inline_assets bool = false
|
|
|
|
output_path string
|
|
|
|
input_path string
|
|
|
|
output_type OutputType = .unset
|
|
|
|
docs []doc.Doc
|
|
|
|
manifest vmod.Manifest
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn slug(title string) string {
|
|
|
|
return title.replace(' ', '-')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn open_url(url string) {
|
|
|
|
$if windows {
|
|
|
|
os.system('start $url')
|
|
|
|
}
|
|
|
|
$if macos {
|
|
|
|
os.system('open $url')
|
|
|
|
}
|
|
|
|
$if linux {
|
|
|
|
os.system('xdg-open $url')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut cfg DocConfig) serve_html() {
|
2020-06-05 09:59:26 +02:00
|
|
|
docs := cfg.render()
|
2020-06-20 18:33:44 +02:00
|
|
|
dkeys := docs.keys()
|
|
|
|
if dkeys.len < 1 {
|
|
|
|
eprintln('no documentation created, the module has no `pub` functions')
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
def_name := docs.keys()[0]
|
2020-06-20 18:33:44 +02:00
|
|
|
//
|
|
|
|
server_url := 'http://localhost:' + cfg.server_port.str()
|
2020-06-07 12:27:42 +02:00
|
|
|
server := net.listen(cfg.server_port) or {
|
2020-06-02 12:10:01 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-07 12:27:42 +02:00
|
|
|
println('Serving docs on: $server_url')
|
|
|
|
if cfg.open_docs {
|
|
|
|
open_url(server_url)
|
|
|
|
}
|
2020-06-19 10:36:45 +02:00
|
|
|
content_type := match cfg.output_type {
|
|
|
|
.html { 'text/html' }
|
|
|
|
.markdown { 'text/markdown' }
|
|
|
|
.json { 'application/json' }
|
|
|
|
else { 'text/plain' }
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
for {
|
|
|
|
con := server.accept() or {
|
|
|
|
server.close() or { }
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
s := con.read_line()
|
|
|
|
first_line := s.all_before('\n')
|
|
|
|
mut filename := def_name
|
|
|
|
if first_line.len != 0 {
|
|
|
|
data := first_line.split(' ')
|
|
|
|
url := urllib.parse(data[1]) or { return }
|
|
|
|
filename = if url.path == '/' { def_name } else { url.path.trim_left('/') }
|
|
|
|
}
|
2020-06-20 19:37:46 +02:00
|
|
|
html := docs[filename].trim_space()
|
|
|
|
content_length := html.len
|
|
|
|
con.send_string('HTTP/1.1 200 OK\r\nServer: VDoc\r\nContent-Type: ${content_type}\r\nContent-Length: ${content_length}\r\nConnection: close\r\n\r\n${html}') or {
|
2020-06-02 12:10:01 +02:00
|
|
|
con.close() or { return }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
con.close() or { return }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_src_link(repo_url string, file_name string, line_nr int) string {
|
|
|
|
mut url := urllib.parse(repo_url) or {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
if url.path.len <= 1 || file_name.len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
url.path = url.path.trim_right('/') + match url.host {
|
|
|
|
'github.com' { '/blob/master/$file_name' }
|
|
|
|
'gitlab.com' { '/-/blob/master/$file_name' }
|
|
|
|
'git.sir.ht' { '/tree/master/$file_name' }
|
|
|
|
else { '' }
|
|
|
|
}
|
|
|
|
if url.path == '/' { return '' }
|
|
|
|
url.fragment = 'L$line_nr'
|
|
|
|
return url.str()
|
|
|
|
}
|
|
|
|
|
2020-06-08 10:28:46 +02:00
|
|
|
fn js_compress(str string) string {
|
|
|
|
mut js := strings.new_builder(200)
|
|
|
|
lines := str.split_into_lines()
|
2020-06-19 10:36:45 +02:00
|
|
|
rules := [') {', ' = ', ', ', '{ ', ' }', ' (', '; ', ' + ', ' < ', ' - ', ' || ', ' var', ': ', ' >= ', ' && ', ' else if', ' === ', ' !== ', ' else ']
|
|
|
|
clean := ['){', '=', ',', '{', '}', '(', ';', '+', '<', '-', '||', 'var', ':', '>=', '&&', 'else if', '===', '!==', 'else']
|
2020-06-08 10:28:46 +02:00
|
|
|
for line in lines {
|
|
|
|
mut trimmed := line.trim_space()
|
|
|
|
if trimmed.starts_with('//') || (trimmed.starts_with('/*') && trimmed.ends_with('*/')) { continue }
|
2020-06-19 10:36:45 +02:00
|
|
|
for i in 0..rules.len-1 {
|
2020-06-08 10:28:46 +02:00
|
|
|
trimmed = trimmed.replace(rules[i], clean[i])
|
|
|
|
}
|
|
|
|
js.write(trimmed)
|
|
|
|
}
|
|
|
|
return js.str()
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
fn escape(str string) string {
|
|
|
|
return str.replace_each(['"', '\\"', '\r\n', '\\n', '\n', '\\n'])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cfg DocConfig) gen_json(idx int) string {
|
|
|
|
dcs := cfg.docs[idx]
|
|
|
|
mut jw := strings.new_builder(200)
|
|
|
|
jw.writeln('{\n\t"module_name": "$dcs.head.name",\n\t"description": "${escape(dcs.head.comment)}",\n\t"contents": [')
|
|
|
|
for i, cn in dcs.contents {
|
2020-06-05 09:59:26 +02:00
|
|
|
name := cn.name.all_after(dcs.head.name)
|
2020-06-02 12:10:01 +02:00
|
|
|
jw.writeln('\t\t{')
|
|
|
|
jw.writeln('\t\t\t"name": "$name",')
|
|
|
|
jw.writeln('\t\t\t"signature": "${escape(cn.content)}",')
|
|
|
|
jw.writeln('\t\t\t"description": "${escape(cn.comment)}"')
|
|
|
|
jw.write('\t\t}')
|
|
|
|
if i < dcs.contents.len-1 { jw.writeln(',') }
|
|
|
|
}
|
|
|
|
jw.writeln('\n\t],')
|
|
|
|
jw.write('\t"generator": "vdoc",\n\t"time_generated": "${dcs.time_generated.str()}"\n}')
|
|
|
|
return jw.str()
|
|
|
|
}
|
|
|
|
|
2020-06-06 07:56:17 +02:00
|
|
|
fn html_highlight(code string, tb &table.Table) string {
|
|
|
|
builtin := ['bool', 'string', 'i8', 'i16', 'int', 'i64', 'i128', 'byte', 'u16', 'u32', 'u64', 'u128', 'rune', 'f32', 'f64', 'any_int', 'any_float', 'byteptr', 'voidptr', 'any']
|
|
|
|
highlight_code := fn (tok token.Token, typ HighlightTokenTyp) string {
|
2020-06-08 10:28:46 +02:00
|
|
|
lit := if typ in [.unone, .operator, .punctuation] {
|
|
|
|
tok.kind.str()
|
|
|
|
} else if typ == .string {
|
|
|
|
"'$tok.lit'"
|
|
|
|
} else if typ == .char {
|
|
|
|
'`$tok.lit`'
|
|
|
|
} else { tok.lit }
|
2020-06-06 07:56:17 +02:00
|
|
|
return if typ in [.unone, .name] { lit } else { '<span class="token $typ">$lit</span>' }
|
|
|
|
}
|
2020-06-18 18:48:23 +02:00
|
|
|
s := scanner.new_scanner(code, .parse_comments, false)
|
2020-06-06 07:56:17 +02:00
|
|
|
mut tok := s.scan()
|
|
|
|
mut next_tok := s.scan()
|
|
|
|
mut buf := strings.new_builder(200)
|
|
|
|
mut i := 0
|
|
|
|
for i < code.len {
|
|
|
|
if i == tok.pos {
|
|
|
|
mut tok_typ := HighlightTokenTyp.unone
|
|
|
|
match tok.kind {
|
|
|
|
.name {
|
2020-06-08 10:28:46 +02:00
|
|
|
if tok.lit in builtin || tb.known_type(tok.lit) {
|
2020-06-06 07:56:17 +02:00
|
|
|
tok_typ = .builtin
|
2020-06-06 14:31:26 +02:00
|
|
|
} else if next_tok.kind == .lcbr {
|
2020-06-06 07:56:17 +02:00
|
|
|
tok_typ = .symbol
|
2020-06-06 14:31:26 +02:00
|
|
|
} else if next_tok.kind == .lpar {
|
|
|
|
tok_typ = .function
|
2020-06-06 07:56:17 +02:00
|
|
|
} else {
|
|
|
|
tok_typ = .name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.comment {
|
|
|
|
tok_typ = .comment
|
|
|
|
}
|
|
|
|
.chartoken {
|
|
|
|
tok_typ = .char
|
|
|
|
}
|
|
|
|
.string {
|
|
|
|
tok_typ = .string
|
|
|
|
}
|
|
|
|
.number {
|
|
|
|
tok_typ = .number
|
|
|
|
}
|
2020-06-06 10:43:50 +02:00
|
|
|
.key_true, .key_false {
|
2020-06-06 07:56:17 +02:00
|
|
|
tok_typ = .boolean
|
|
|
|
}
|
2020-06-06 10:43:50 +02:00
|
|
|
.lpar, .lcbr, .rpar, .rcbr, .lsbr,
|
|
|
|
.rsbr, .semicolon, .colon, .comma, .dot {
|
|
|
|
tok_typ = .punctuation
|
|
|
|
}
|
2020-06-06 07:56:17 +02:00
|
|
|
else {
|
|
|
|
if token.is_key(tok.lit) || token.is_decl(tok.kind) {
|
|
|
|
tok_typ = .keyword
|
|
|
|
} else if tok.kind == .decl_assign || tok.kind.is_assign() || tok.is_unary() || tok.kind.is_relational() || tok.kind.is_infix() {
|
|
|
|
tok_typ = .operator
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.write(highlight_code(tok, tok_typ))
|
|
|
|
if next_tok.kind != .eof {
|
|
|
|
i = tok.pos + tok.len
|
|
|
|
tok = next_tok
|
|
|
|
next_tok = s.scan()
|
|
|
|
} else {
|
|
|
|
break
|
2020-06-04 23:50:59 +02:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
} else {
|
2020-06-06 07:56:17 +02:00
|
|
|
buf.write_b(code[i])
|
|
|
|
i++
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 07:56:17 +02:00
|
|
|
return buf.str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn doc_node_html(dd doc.DocNode, link string, head bool, tb &table.Table) string {
|
|
|
|
mut dnw := strings.new_builder(200)
|
|
|
|
link_svg := '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"/><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/></svg>'
|
|
|
|
head_tag := if head { 'h1' } else { 'h2' }
|
|
|
|
md_content := markdown.to_html(dd.comment)
|
|
|
|
hlighted_code := html_highlight(dd.content, tb)
|
2020-06-07 12:27:42 +02:00
|
|
|
is_const_class := if dd.name == 'Constants' { ' const' } else { '' }
|
2020-06-06 14:31:26 +02:00
|
|
|
mut sym_name := dd.name
|
2020-06-19 10:36:45 +02:00
|
|
|
if dd.attrs.exists('parent') && dd.attrs['parent'] !in ['void', '', 'Constants'] {
|
|
|
|
sym_name = dd.attrs['parent'] + '.' + sym_name
|
2020-06-06 14:31:26 +02:00
|
|
|
}
|
|
|
|
node_id := slug(sym_name)
|
2020-06-07 12:27:42 +02:00
|
|
|
hash_link := if !head { ' <a href="#$node_id">#</a>' } else { '' }
|
|
|
|
dnw.writeln('<section id="$node_id" class="doc-node$is_const_class">')
|
2020-06-19 10:36:45 +02:00
|
|
|
if dd.name != 'README' && dd.attrs['parent'] != 'Constants' {
|
2020-06-07 01:23:56 +02:00
|
|
|
dnw.write('<div class="title"><$head_tag>$sym_name$hash_link</$head_tag>')
|
2020-06-06 07:56:17 +02:00
|
|
|
if link.len != 0 {
|
2020-06-06 10:43:50 +02:00
|
|
|
dnw.write('<a class="link" rel="noreferrer" target="_blank" href="$link">$link_svg</a>')
|
2020-06-06 07:56:17 +02:00
|
|
|
}
|
|
|
|
dnw.write('</div>')
|
|
|
|
}
|
|
|
|
if head {
|
|
|
|
dnw.write(md_content)
|
|
|
|
} else {
|
2020-06-08 13:11:55 +02:00
|
|
|
dnw.writeln('<pre class="signature"><code>$hlighted_code</code></pre>')
|
2020-06-06 07:56:17 +02:00
|
|
|
dnw.writeln(md_content)
|
|
|
|
}
|
|
|
|
dnw.writeln('</section>')
|
|
|
|
return dnw.str()
|
|
|
|
}
|
|
|
|
|
2020-06-19 10:36:45 +02:00
|
|
|
fn (cfg DocConfig) readme_idx() int {
|
|
|
|
for i, dc in cfg.docs {
|
|
|
|
if dc.head.name != 'README' { continue }
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-06-08 10:28:46 +02:00
|
|
|
fn write_toc(cn doc.DocNode, nodes []doc.DocNode, toc &strings.Builder) {
|
2020-06-19 10:36:45 +02:00
|
|
|
toc_slug := if cn.content.len == 0 { '' } else { slug(cn.name) }
|
|
|
|
toc.write('<li class="open"><a href="#$toc_slug">${cn.name}</a>')
|
2020-06-08 10:28:46 +02:00
|
|
|
children := nodes.find_children_of(cn.name)
|
2020-06-19 10:36:45 +02:00
|
|
|
if cn.name != 'Constants' {
|
2020-06-08 10:28:46 +02:00
|
|
|
toc.writeln(' <ul>')
|
|
|
|
for child in children {
|
|
|
|
cname := cn.name + '.' + child.name
|
|
|
|
toc.writeln('<li><a href="#${slug(cname)}">${child.name}</a></li>')
|
|
|
|
}
|
|
|
|
toc.writeln('</ul>')
|
|
|
|
}
|
|
|
|
toc.writeln('</li>')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cfg DocConfig) write_content(cn &doc.DocNode, dcs &doc.Doc, hw &strings.Builder) {
|
|
|
|
base_dir := os.base_dir(os.real_path(cfg.input_path))
|
2020-06-19 10:36:45 +02:00
|
|
|
file_path_name := if cfg.is_multi { cn.file_path.replace('$base_dir/', '') } else { os.file_name(cn.file_path) }
|
2020-06-08 10:28:46 +02:00
|
|
|
src_link := get_src_link(cfg.manifest.repo_url, file_path_name, cn.pos.line)
|
|
|
|
children := dcs.contents.find_children_of(cn.name)
|
2020-06-19 10:36:45 +02:00
|
|
|
if cn.content.len != 0 {
|
|
|
|
hw.write(doc_node_html(cn, src_link, false, dcs.table))
|
|
|
|
}
|
|
|
|
for child in children {
|
|
|
|
child_file_path_name := child.file_path.replace('$base_dir/', '')
|
|
|
|
child_src_link := get_src_link(cfg.manifest.repo_url, child_file_path_name, child.pos.line)
|
|
|
|
hw.write(doc_node_html(child, child_src_link, false, dcs.table))
|
2020-06-08 10:28:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 07:56:17 +02:00
|
|
|
fn (cfg DocConfig) gen_html(idx int) string {
|
|
|
|
dcs := cfg.docs[idx]
|
2020-06-07 12:27:42 +02:00
|
|
|
time_gen := '$dcs.time_generated.day $dcs.time_generated.smonth() $dcs.time_generated.year $dcs.time_generated.hhmmss()'
|
2020-06-06 07:56:17 +02:00
|
|
|
mut toc := strings.new_builder(200)
|
2020-06-19 10:36:45 +02:00
|
|
|
mut toc2 := strings.new_builder(200)
|
|
|
|
mut contents := strings.new_builder(200)
|
2020-06-02 12:10:01 +02:00
|
|
|
// generate toc first
|
2020-06-19 10:36:45 +02:00
|
|
|
contents.writeln(doc_node_html(dcs.head, '', true, dcs.table))
|
2020-06-02 12:10:01 +02:00
|
|
|
for cn in dcs.contents {
|
2020-06-19 10:36:45 +02:00
|
|
|
cfg.write_content(&cn, &dcs, &contents)
|
|
|
|
if cn.attrs['parent'] == 'Constants' || cn.attrs['category'] == 'Methods' { continue }
|
2020-06-08 10:28:46 +02:00
|
|
|
write_toc(cn, dcs.contents, &toc)
|
2020-06-02 12:10:01 +02:00
|
|
|
} // write head
|
2020-06-05 12:08:22 +02:00
|
|
|
// get resources
|
2020-06-08 10:28:46 +02:00
|
|
|
doc_css := cfg.get_resource(css_js_assets[0], true)
|
|
|
|
normalize_css := cfg.get_resource(css_js_assets[1], true)
|
|
|
|
doc_js := cfg.get_resource(css_js_assets[2], !cfg.serve_http)
|
2020-06-05 12:08:22 +02:00
|
|
|
light_icon := cfg.get_resource('light.svg', true)
|
|
|
|
dark_icon := cfg.get_resource('dark.svg', true)
|
|
|
|
menu_icon := cfg.get_resource('menu.svg', true)
|
|
|
|
arrow_icon := cfg.get_resource('arrow.svg', true)
|
2020-06-02 12:10:01 +02:00
|
|
|
// write css
|
2020-06-19 10:36:45 +02:00
|
|
|
version := if cfg.manifest.version.len != 0 { cfg.manifest.version } else { '' }
|
|
|
|
header_name := if cfg.is_multi && cfg.docs.len > 1 {
|
|
|
|
os.file_name(os.real_path(cfg.input_path))
|
|
|
|
} else if cfg.docs.len == 2 && idx+1 < cfg.docs.len && cfg.readme_idx() != -1 {
|
|
|
|
cfg.docs[cfg.readme_idx()+1].head.name
|
2020-06-05 09:59:26 +02:00
|
|
|
} else {
|
2020-06-19 10:36:45 +02:00
|
|
|
dcs.head.name
|
2020-06-05 09:59:26 +02:00
|
|
|
}
|
2020-06-04 23:50:59 +02:00
|
|
|
// write nav1
|
2020-06-19 10:36:45 +02:00
|
|
|
if cfg.is_multi || cfg.docs.len > 1 {
|
2020-06-02 12:10:01 +02:00
|
|
|
mut submod_prefix := ''
|
2020-06-11 00:52:41 +02:00
|
|
|
mut docs := cfg.docs.filter(it.head.name == 'builtin')
|
|
|
|
docs << cfg.docs.filter(it.head.name != 'builtin')
|
|
|
|
for i, doc in docs {
|
2020-06-04 23:50:59 +02:00
|
|
|
if i-1 >= 0 && doc.head.name.starts_with(submod_prefix + '.') {
|
2020-06-02 12:10:01 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
names := doc.head.name.split('.')
|
|
|
|
submod_prefix = if names.len > 1 { names[0] } else { doc.head.name }
|
2020-06-04 23:50:59 +02:00
|
|
|
href_name := if doc.head.name == 'README' {
|
|
|
|
'./index.html'
|
2020-06-02 12:10:01 +02:00
|
|
|
} else if submod_prefix !in cfg.docs.map(it.head.name) {
|
|
|
|
'#'
|
2020-06-19 10:36:45 +02:00
|
|
|
} else if cfg.docs.len == 2 && cfg.readme_idx() == -1 {
|
|
|
|
'./docs.html'
|
2020-06-04 23:50:59 +02:00
|
|
|
} else {
|
|
|
|
'./' + doc.head.name + '.html'
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
submodules := cfg.docs.filter(it.head.name.starts_with(submod_prefix + '.'))
|
2020-06-04 23:50:59 +02:00
|
|
|
dropdown := if submodules.len > 0 { arrow_icon } else { '' }
|
|
|
|
mut is_submodule_open := false
|
|
|
|
for _, cdoc in submodules {
|
|
|
|
if cdoc.head.name == dcs.head.name {
|
|
|
|
is_submodule_open = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
active_class := if doc.head.name == dcs.head.name { ' active' } else { '' }
|
2020-06-19 10:36:45 +02:00
|
|
|
toc2.write('<li class="open$active_class"><div class="menu-row">$dropdown<a href="$href_name">${submod_prefix}</a></div>')
|
2020-06-02 12:10:01 +02:00
|
|
|
for j, cdoc in submodules {
|
|
|
|
if j == 0 {
|
2020-06-19 10:36:45 +02:00
|
|
|
toc2.write('<ul>')
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
submod_name := cdoc.head.name.all_after(submod_prefix + '.')
|
2020-06-04 23:50:59 +02:00
|
|
|
sub_selected_classes := if cdoc.head.name == dcs.head.name { ' class="active"' } else { '' }
|
2020-06-19 10:36:45 +02:00
|
|
|
toc2.write('<li$sub_selected_classes><a href="./${cdoc.head.name}.html">${submod_name}</a></li>')
|
2020-06-04 23:50:59 +02:00
|
|
|
if j == submodules.len - 1 {
|
2020-06-19 10:36:45 +02:00
|
|
|
toc2.write('</ul>')
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-19 10:36:45 +02:00
|
|
|
toc2.write('</li>')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return html_content
|
|
|
|
.replace('{{ title }}', dcs.head.name)
|
|
|
|
.replace('{{ head_name }}', header_name)
|
|
|
|
.replace('{{ version }}', version)
|
|
|
|
.replace('{{ light_icon }}', light_icon)
|
|
|
|
.replace('{{ dark_icon }}', dark_icon)
|
|
|
|
.replace('{{ menu_icon }}', menu_icon)
|
|
|
|
.replace('{{ head_assets }}', if cfg.inline_assets {
|
|
|
|
'\n <style>$doc_css</style>\n <style>$normalize_css</style>'
|
|
|
|
} else {
|
|
|
|
'\n <link rel="stylesheet" href="$doc_css" />\n <link rel="stylesheet" href="$normalize_css" />'
|
|
|
|
})
|
|
|
|
.replace('{{ toc_links }}', if cfg.is_multi || cfg.docs.len > 1 { toc2.str() } else { toc.str() })
|
|
|
|
.replace('{{ contents }}', contents.str())
|
|
|
|
.replace('{{ right_content }}', if cfg.is_multi && cfg.docs.len > 1 && dcs.head.name != 'README' {
|
|
|
|
'<div class="doc-toc"><ul>' + toc.str() + '</ul></div>'
|
|
|
|
} else { '' })
|
|
|
|
.replace('{{ footer_content }}', 'Powered by vdoc. Generated on: $time_gen')
|
|
|
|
.replace('{{ footer_assets }}', if cfg.inline_assets {
|
|
|
|
'<script>$doc_js</script>'
|
|
|
|
} else {
|
|
|
|
'<script src="$doc_js"></script>'
|
|
|
|
})
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (cfg DocConfig) gen_plaintext(idx int) string {
|
|
|
|
dcs := cfg.docs[idx]
|
|
|
|
mut pw := strings.new_builder(200)
|
2020-06-05 09:59:26 +02:00
|
|
|
pw.writeln('${dcs.head.content}\n')
|
2020-06-10 11:14:03 +02:00
|
|
|
if dcs.head.comment.trim_space().len > 0 {
|
2020-06-08 08:22:10 +02:00
|
|
|
pw.writeln('// ' + dcs.head.comment.replace('\n', '\n// ') + '\n')
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
for cn in dcs.contents {
|
|
|
|
pw.writeln(cn.content)
|
|
|
|
if cn.comment.len > 0 {
|
2020-06-10 11:14:03 +02:00
|
|
|
pw.writeln('\/\/ ' + cn.comment.trim_space() + '\n')
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
if cfg.show_loc {
|
|
|
|
pw.writeln('Location: ${cn.file_path}:${cn.pos.line}:${cn.pos.col}\n\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pw.str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cfg DocConfig) gen_markdown(idx int, with_toc bool) string {
|
|
|
|
dcs := cfg.docs[idx]
|
|
|
|
mut hw := strings.new_builder(200)
|
|
|
|
mut cw := strings.new_builder(200)
|
|
|
|
hw.writeln('# ${dcs.head.content}\n${dcs.head.comment}\n')
|
|
|
|
if with_toc {
|
|
|
|
hw.writeln('## Contents')
|
|
|
|
}
|
|
|
|
for cn in dcs.contents {
|
2020-06-07 12:27:42 +02:00
|
|
|
name := cn.name.all_after(dcs.head.name + '.')
|
2020-06-02 12:10:01 +02:00
|
|
|
|
|
|
|
if with_toc {
|
|
|
|
hw.writeln('- [#$name](${slug(name)})')
|
|
|
|
}
|
|
|
|
cw.writeln('## $name')
|
|
|
|
cw.writeln('```v\n${cn.content}\n```${cn.comment}\n')
|
|
|
|
cw.writeln('[\[Return to contents\]](#Contents)\n')
|
|
|
|
}
|
|
|
|
cw.writeln('#### Generated by vdoc. Last generated: ${dcs.time_generated.str()}')
|
|
|
|
return hw.str() + '\n' + cw.str()
|
|
|
|
}
|
|
|
|
|
2020-06-05 09:59:26 +02:00
|
|
|
fn (cfg DocConfig) render() map[string]string {
|
2020-06-02 12:10:01 +02:00
|
|
|
mut docs := map[string]string
|
|
|
|
for i, doc in cfg.docs {
|
2020-06-11 00:52:41 +02:00
|
|
|
// since builtin is generated first, ignore it
|
2020-06-19 10:36:45 +02:00
|
|
|
mut name := if doc.head.name == 'README' || cfg.docs.len == 1 {
|
2020-06-02 12:10:01 +02:00
|
|
|
'index'
|
2020-06-05 09:59:26 +02:00
|
|
|
} else if !cfg.is_multi && !os.is_dir(cfg.output_path) {
|
|
|
|
os.file_name(cfg.output_path)
|
2020-06-19 10:36:45 +02:00
|
|
|
} else if i-1 >= 0 && cfg.readme_idx() != -1 && cfg.docs.len == 2 {
|
|
|
|
'docs'
|
2020-06-02 12:10:01 +02:00
|
|
|
} else {
|
|
|
|
doc.head.name
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
name = name + match cfg.output_type {
|
2020-06-02 12:10:01 +02:00
|
|
|
.html { '.html' }
|
|
|
|
.markdown { '.md' }
|
|
|
|
.json { '.json' }
|
|
|
|
else { '.txt' }
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
docs[name] = match cfg.output_type {
|
2020-06-02 12:10:01 +02:00
|
|
|
.html { cfg.gen_html(i) }
|
|
|
|
.markdown { cfg.gen_markdown(i, true) }
|
|
|
|
.json { cfg.gen_json(i) }
|
|
|
|
else { cfg.gen_plaintext(i) }
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
cfg.vprintln('Rendered: ' + docs.keys().str())
|
2020-06-02 12:10:01 +02:00
|
|
|
return docs
|
|
|
|
}
|
|
|
|
|
2020-06-07 12:27:42 +02:00
|
|
|
fn (cfg DocConfig) get_readme(path string) string {
|
|
|
|
mut fname := ''
|
|
|
|
for name in ['readme', 'README'] {
|
|
|
|
if os.exists(os.join_path(path, '${name}.md')) {
|
|
|
|
fname = name
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fname == '' {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
readme_path := os.join_path(path, '${fname}.md')
|
|
|
|
cfg.vprintln('Reading README file from $readme_path')
|
|
|
|
readme_contents := os.read_file(readme_path) or { '' }
|
|
|
|
return readme_contents
|
|
|
|
}
|
|
|
|
|
2020-06-05 09:59:26 +02:00
|
|
|
fn (mut cfg DocConfig) generate_docs_from_file() {
|
|
|
|
if cfg.output_path.len == 0 {
|
|
|
|
if cfg.output_type == .unset {
|
|
|
|
cfg.output_type = .stdout
|
2020-06-02 12:10:01 +02:00
|
|
|
} else {
|
2020-06-05 09:59:26 +02:00
|
|
|
cfg.vprintln('No output path has detected. Using input path instead.')
|
|
|
|
cfg.output_path = cfg.input_path
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
} else if cfg.output_type == .unset {
|
|
|
|
cfg.vprintln('Output path detected. Identifying output type..')
|
|
|
|
ext := os.file_ext(cfg.output_path)
|
|
|
|
cfg.set_output_type_from_str(ext.all_after('.'))
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
if cfg.include_readme && cfg.output_type !in [.html, .stdout] {
|
2020-06-02 22:56:13 +02:00
|
|
|
eprintln('vdoc: Including README.md for doc generation is supported on HTML output, or when running directly in the terminal.')
|
2020-06-02 12:10:01 +02:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
is_vlib := 'vlib' in cfg.input_path
|
|
|
|
dir_path := if is_vlib {
|
2020-06-05 14:22:50 +02:00
|
|
|
vexe_path
|
2020-06-05 09:59:26 +02:00
|
|
|
} else if os.is_dir(cfg.input_path) {
|
|
|
|
cfg.input_path
|
|
|
|
} else {
|
|
|
|
os.base_dir(cfg.input_path)
|
|
|
|
}
|
|
|
|
manifest_path := os.join_path(dir_path, 'v.mod')
|
|
|
|
if os.exists(manifest_path) {
|
|
|
|
cfg.vprintln('Reading v.mod info from $manifest_path')
|
2020-06-02 12:10:01 +02:00
|
|
|
if manifest := vmod.from_file(manifest_path) {
|
2020-06-05 09:59:26 +02:00
|
|
|
cfg.manifest = manifest
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-07 12:27:42 +02:00
|
|
|
if cfg.include_readme {
|
|
|
|
readme_contents := cfg.get_readme(dir_path)
|
2020-06-05 09:59:26 +02:00
|
|
|
if cfg.output_type == .stdout {
|
|
|
|
println(markdown.to_plain(readme_contents))
|
2020-06-02 22:56:13 +02:00
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
if cfg.output_type == .html {
|
|
|
|
cfg.docs << doc.Doc{
|
2020-06-02 22:56:13 +02:00
|
|
|
head: doc.DocNode{
|
|
|
|
name: 'README',
|
|
|
|
comment: readme_contents
|
|
|
|
}
|
2020-06-06 14:31:26 +02:00
|
|
|
time_generated: time.now()
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-02 22:56:13 +02:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
dirs := if cfg.is_multi { get_modules_list(cfg.input_path) } else { [cfg.input_path] }
|
|
|
|
for dirpath in dirs {
|
|
|
|
cfg.vprintln('Generating docs for ${dirpath}...')
|
2020-06-06 17:47:16 +02:00
|
|
|
mut dcs := doc.generate(dirpath, cfg.pub_only, true) or {
|
2020-06-10 11:13:42 +02:00
|
|
|
mut err_msg := err
|
|
|
|
if errcode == 1 {
|
2020-06-19 10:36:45 +02:00
|
|
|
mod_list := get_modules_list(cfg.input_path)
|
|
|
|
println('Available modules:\n==================')
|
|
|
|
for mod in mod_list {
|
|
|
|
println(mod.all_after('vlib/').all_after('modules/').replace('/', '.'))
|
|
|
|
}
|
|
|
|
err_msg += ' Use the `-m` flag if you are generating docs of a directory containing multiple modules.'
|
2020-06-10 11:13:42 +02:00
|
|
|
}
|
|
|
|
eprintln(err_msg)
|
|
|
|
exit(1)
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
if dcs.contents.len == 0 { continue }
|
2020-06-07 12:27:42 +02:00
|
|
|
if cfg.is_multi {
|
|
|
|
readme_contents := cfg.get_readme(dirpath)
|
|
|
|
dcs.head.comment = readme_contents
|
|
|
|
}
|
2020-06-06 14:31:26 +02:00
|
|
|
if cfg.pub_only {
|
|
|
|
for i, c in dcs.contents {
|
|
|
|
dcs.contents[i].content = c.content.all_after('pub ')
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
cfg.docs << dcs
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
if cfg.serve_http {
|
|
|
|
cfg.serve_html()
|
2020-06-02 12:10:01 +02:00
|
|
|
return
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
cfg.vprintln('Rendering docs...')
|
|
|
|
if cfg.output_path.len == 0 {
|
|
|
|
outputs := cfg.render()
|
2020-06-08 08:22:10 +02:00
|
|
|
if outputs.size == 0 {
|
|
|
|
println('No documentation for $dirs')
|
|
|
|
} else {
|
|
|
|
first := outputs.keys()[0]
|
|
|
|
println(outputs[first])
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
} else {
|
2020-06-05 09:59:26 +02:00
|
|
|
if !os.is_dir(cfg.output_path) {
|
|
|
|
cfg.output_path = os.real_path('.')
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-19 10:36:45 +02:00
|
|
|
if !os.exists(cfg.output_path) {
|
|
|
|
os.mkdir(cfg.output_path) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
if cfg.is_multi {
|
|
|
|
cfg.output_path = os.join_path(cfg.output_path, '_docs')
|
|
|
|
if !os.exists(cfg.output_path) {
|
|
|
|
os.mkdir(cfg.output_path) or {
|
2020-06-02 12:10:01 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
} else {
|
2020-06-08 10:28:46 +02:00
|
|
|
for fname in css_js_assets {
|
2020-06-06 10:43:50 +02:00
|
|
|
os.rm(os.join_path(cfg.output_path, fname))
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
outputs := cfg.render()
|
2020-06-02 12:10:01 +02:00
|
|
|
for file_name, content in outputs {
|
2020-06-05 09:59:26 +02:00
|
|
|
output_path := os.join_path(cfg.output_path, file_name)
|
|
|
|
println('Generating ${output_path}...')
|
|
|
|
os.write_file(output_path, content)
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 09:59:26 +02:00
|
|
|
fn (mut cfg DocConfig) set_output_type_from_str(format string) {
|
|
|
|
match format {
|
|
|
|
'htm', 'html' {
|
|
|
|
cfg.output_type = .html
|
|
|
|
}
|
|
|
|
'md', 'markdown' {
|
|
|
|
cfg.output_type = .markdown
|
|
|
|
}
|
|
|
|
'json' {
|
|
|
|
cfg.output_type = .json
|
|
|
|
}
|
|
|
|
'stdout' {
|
|
|
|
cfg.output_type = .stdout
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cfg.output_type = .plaintext
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.vprintln('Setting output type to "$cfg.output_type"')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cfg DocConfig) vprintln(str string) {
|
|
|
|
if cfg.is_verbose {
|
|
|
|
println('vdoc: $str')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
fn lookup_module(mod string) ?string {
|
2020-06-05 09:59:26 +02:00
|
|
|
mod_path := mod.replace('.', os.path_separator)
|
2020-06-02 12:10:01 +02:00
|
|
|
compile_dir := os.real_path(os.base_dir('.'))
|
|
|
|
modules_dir := os.join_path(compile_dir, 'modules', mod_path)
|
|
|
|
vlib_path := os.join_path(vexe_path, 'vlib', mod_path)
|
|
|
|
vmodules_path := os.join_path(os.home_dir(), '.vmodules', mod_path)
|
|
|
|
paths := [modules_dir, vlib_path, vmodules_path]
|
|
|
|
for path in paths {
|
|
|
|
if os.is_dir_empty(path) { continue }
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return error('vdoc: Module "${mod}" not found.')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_modules_list(path string) []string {
|
|
|
|
files := os.walk_ext(path, 'v')
|
|
|
|
mut dirs := []string{}
|
|
|
|
for file in files {
|
2020-06-19 10:36:45 +02:00
|
|
|
if 'vlib' in path && ('examples' in file || 'test' in file || 'js' in file || 'x64' in file || 'bare' in file || 'uiold' in file || 'vweb' in file) { continue }
|
2020-06-02 12:10:01 +02:00
|
|
|
dirname := os.base_dir(file)
|
|
|
|
if dirname in dirs { continue }
|
|
|
|
dirs << dirname
|
|
|
|
}
|
|
|
|
dirs.sort()
|
|
|
|
return dirs
|
|
|
|
}
|
|
|
|
|
2020-06-05 09:59:26 +02:00
|
|
|
fn (cfg DocConfig) get_resource(name string, minify bool) string {
|
2020-06-04 23:50:59 +02:00
|
|
|
path := os.join_path(res_path, name)
|
2020-06-05 09:59:26 +02:00
|
|
|
mut res := os.read_file(path) or { panic('could not read $path') }
|
2020-06-04 23:50:59 +02:00
|
|
|
if minify {
|
2020-06-08 10:28:46 +02:00
|
|
|
res = if name.ends_with('.js') { js_compress(res) } else { res.replace('\n', ' ') }
|
2020-06-05 09:59:26 +02:00
|
|
|
}
|
|
|
|
// TODO: Make SVG inline for now
|
|
|
|
if cfg.inline_assets || path.ends_with('.svg') {
|
|
|
|
return res
|
|
|
|
} else {
|
|
|
|
output_path := os.join_path(cfg.output_path, name)
|
|
|
|
if !os.exists(output_path) {
|
|
|
|
println('Generating ${output_path}...')
|
|
|
|
os.write_file(output_path, res)
|
|
|
|
}
|
|
|
|
return name
|
2020-06-04 23:50:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
fn main() {
|
2020-06-05 09:59:26 +02:00
|
|
|
args := os.args[2..]
|
2020-06-06 17:41:44 +02:00
|
|
|
if args.len == 0 || args[0] in ['help', '-h', '--help'] {
|
2020-06-05 15:57:16 +02:00
|
|
|
os.system('${@VEXE} help doc')
|
2020-06-02 12:10:01 +02:00
|
|
|
exit(0)
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
mut cfg := DocConfig{
|
2020-06-02 12:10:01 +02:00
|
|
|
manifest: vmod.Manifest{ repo_url: '' }
|
|
|
|
}
|
2020-06-05 09:59:26 +02: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', '')
|
|
|
|
allowed_str := allowed_formats.join(', ')
|
|
|
|
if format !in allowed_formats {
|
|
|
|
eprintln('vdoc: "$format" is not a valid format. Only $allowed_str are allowed.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
cfg.set_output_type_from_str(format)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
'-inline-assets' {
|
|
|
|
cfg.inline_assets = true
|
|
|
|
}
|
|
|
|
'-l' {
|
|
|
|
cfg.show_loc = true
|
|
|
|
}
|
|
|
|
'-m' {
|
|
|
|
cfg.is_multi = true
|
|
|
|
}
|
|
|
|
'-o' {
|
|
|
|
opath := cmdline.option(current_args, '-o', '')
|
|
|
|
cfg.output_path = os.real_path(opath)
|
|
|
|
i++
|
|
|
|
}
|
2020-06-07 12:27:42 +02:00
|
|
|
'-open' {
|
|
|
|
cfg.open_docs = true
|
|
|
|
}
|
|
|
|
'-p' {
|
|
|
|
s_port := cmdline.option(current_args, '-o', '')
|
|
|
|
s_port_int := s_port.int()
|
|
|
|
if s_port.len == 0 {
|
|
|
|
eprintln('vdoc: No port number specified on "-p".')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
if s_port != s_port_int.str() {
|
|
|
|
eprintln('vdoc: Invalid port number.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
cfg.server_port = s_port_int
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
'-s' {
|
|
|
|
cfg.inline_assets = true
|
|
|
|
cfg.serve_http = true
|
2020-06-19 10:36:45 +02:00
|
|
|
if cfg.output_type == .unset {
|
|
|
|
cfg.output_type = .html
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
}
|
2020-06-19 10:36:45 +02:00
|
|
|
'-readme' {
|
2020-06-05 09:59:26 +02:00
|
|
|
cfg.include_readme = true
|
|
|
|
}
|
|
|
|
'-v' {
|
|
|
|
cfg.is_verbose = true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cfg.input_path = arg
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cfg.input_path.len == 0 {
|
|
|
|
eprintln('vdoc: No input path found.')
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-06-19 10:36:45 +02:00
|
|
|
$if windows {
|
|
|
|
cfg.input_path = cfg.input_path.replace('/', os.path_separator)
|
|
|
|
} $else {
|
|
|
|
cfg.input_path = cfg.input_path.replace('\\', os.path_separator)
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
is_path := cfg.input_path.ends_with('.v') || cfg.input_path.split(os.path_separator).len > 1 || cfg.input_path == '.'
|
|
|
|
if cfg.input_path == 'vlib' {
|
|
|
|
cfg.is_multi = true
|
2020-06-05 14:22:50 +02:00
|
|
|
cfg.input_path = os.join_path(vexe_path, 'vlib')
|
2020-06-05 09:59:26 +02:00
|
|
|
} else if !is_path {
|
|
|
|
cfg.vprintln('Input "$cfg.input_path" is not a valid path. Looking for modules named "$cfg.input_path"...')
|
|
|
|
mod_path := lookup_module(cfg.input_path) or {
|
2020-06-02 12:10:01 +02:00
|
|
|
eprintln(err)
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
cfg.input_path = mod_path
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
cfg.generate_docs_from_file()
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|