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
|
2020-09-23 20:50:51 +02:00
|
|
|
import sync
|
|
|
|
import runtime
|
2020-06-02 12:10:01 +02:00
|
|
|
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-07-11 12:04:05 +02:00
|
|
|
import v.pref
|
2020-10-21 10:26:33 +02:00
|
|
|
import json
|
2020-11-15 21:54:47 +01:00
|
|
|
import io
|
2020-06-02 12:10:01 +02:00
|
|
|
|
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-12-17 10:59:12 +01:00
|
|
|
css_js_assets = ['doc.css', 'normalize.css', 'doc.js', 'dark-mode.js']
|
2020-06-05 09:59:26 +02:00
|
|
|
allowed_formats = ['md', 'markdown', 'json', 'text', 'stdout', 'html', 'htm']
|
2020-12-27 13:43:23 +01:00
|
|
|
res_path = os.resource_abs_path('resources')
|
2020-12-18 09:41:34 +01:00
|
|
|
favicons_path = os.join_path(res_path, 'favicons')
|
2020-11-30 18:31:37 +01:00
|
|
|
vexe = pref.vexe_path()
|
|
|
|
vroot = os.dir(vexe)
|
2021-01-02 10:11:34 +01:00
|
|
|
html_content = '<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
2020-06-19 10:36:45 +02:00
|
|
|
<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">
|
2020-07-11 12:04:05 +02:00
|
|
|
<title>{{ title }} | vdoc</title>
|
2020-12-25 18:04:46 +01:00
|
|
|
<link rel="preconnect" href="https://fonts.gstatic.com">
|
2020-06-19 10:36:45 +02:00
|
|
|
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
|
2020-12-25 18:04:46 +01:00
|
|
|
<link href="https://fonts.googleapis.com/css2?family=Work+Sans:wght@400;500;600&display=swap" rel="stylesheet">
|
2020-12-18 09:41:34 +01:00
|
|
|
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
|
|
|
|
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
|
|
|
|
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
|
|
|
|
<link rel="manifest" href="site.webmanifest">
|
|
|
|
<link rel="mask-icon" href="safari-pinned-tab.svg" color="#5bbad5">
|
|
|
|
<meta name="msapplication-TileColor" content="#da532c">
|
|
|
|
<meta name="theme-color" content="#ffffff">
|
2020-06-19 10:36:45 +02:00
|
|
|
{{ head_assets }}
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="page">
|
|
|
|
<header class="doc-nav hidden">
|
|
|
|
<div class="heading-container">
|
|
|
|
<div class="heading">
|
2020-12-29 12:51:24 +01:00
|
|
|
<input type="text" id="search" placeholder="Search... (beta)" autocomplete="off">
|
2020-06-19 10:36:45 +02:00
|
|
|
<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>
|
2020-12-29 12:51:24 +01:00
|
|
|
<nav class="search"></nav>
|
2020-06-19 10:36:45 +02:00
|
|
|
<nav class="content hidden">
|
|
|
|
<ul>
|
|
|
|
{{ toc_links }}
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
</header>
|
2020-12-27 15:14:16 +01:00
|
|
|
<div class="doc-scrollview">
|
|
|
|
<div class="doc-container">
|
|
|
|
<div class="doc-content">
|
2021-01-02 10:11:34 +01:00
|
|
|
{{ contents }}
|
2020-12-27 15:14:16 +01:00
|
|
|
<div class="footer">
|
|
|
|
{{ footer_content }}
|
|
|
|
</div>
|
2020-06-19 10:36:45 +02:00
|
|
|
</div>
|
2020-12-27 15:14:16 +01:00
|
|
|
{{ right_content }}
|
2020-06-19 10:36:45 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{{ footer_assets }}
|
2020-12-29 12:51:24 +01:00
|
|
|
<script async src="search_index.js" type="text/javascript"></script>
|
2020-06-19 10:36:45 +02:00
|
|
|
</body>
|
2021-01-02 10:11:34 +01:00
|
|
|
</html>'
|
|
|
|
tabs = ['\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t']
|
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-12-29 12:51:24 +01:00
|
|
|
is_local bool
|
|
|
|
local_filename string
|
|
|
|
local_pos int
|
|
|
|
pub_only bool = true
|
|
|
|
show_loc bool // for plaintext
|
|
|
|
serve_http bool // for html
|
|
|
|
is_multi bool
|
|
|
|
is_vlib bool
|
|
|
|
is_verbose bool
|
|
|
|
include_readme bool
|
2021-01-06 19:43:43 +01:00
|
|
|
include_examples bool = true
|
2020-12-29 12:51:24 +01:00
|
|
|
open_docs bool
|
|
|
|
server_port int = 8046
|
|
|
|
inline_assets bool
|
|
|
|
no_timestamp bool
|
|
|
|
output_path string
|
|
|
|
input_path string
|
|
|
|
symbol_name string
|
|
|
|
output_type OutputType = .unset
|
|
|
|
docs []doc.Doc
|
|
|
|
manifest vmod.Manifest
|
|
|
|
assets map[string]string
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SearchModuleResult {
|
|
|
|
description string
|
|
|
|
link string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SearchResult {
|
|
|
|
prefix string
|
|
|
|
badge string
|
|
|
|
description string
|
|
|
|
link string
|
2020-09-23 20:50:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ParallelDoc {
|
|
|
|
d doc.Doc
|
|
|
|
i int
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 10:26:33 +02:00
|
|
|
[inline]
|
2020-06-02 12:10:01 +02:00
|
|
|
fn slug(title string) string {
|
|
|
|
return title.replace(' ', '-')
|
|
|
|
}
|
|
|
|
|
2020-10-21 10:26:33 +02:00
|
|
|
[inline]
|
2020-06-02 12:10:01 +02:00
|
|
|
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-09-23 20:50:51 +02:00
|
|
|
cfg.render_static()
|
2020-06-05 09:59:26 +02:00
|
|
|
docs := cfg.render()
|
2020-07-11 12:04:05 +02:00
|
|
|
dkeys := docs.keys()
|
2020-06-20 18:33:44 +02:00
|
|
|
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-12-07 02:43:25 +01:00
|
|
|
server := net.listen_tcp(cfg.server_port) or { 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-09-21 16:40:39 +02:00
|
|
|
server_context := VdocHttpServerContext{
|
2020-06-21 16:41:43 +02:00
|
|
|
docs: docs
|
|
|
|
content_type: content_type
|
|
|
|
default_filename: def_name
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
for {
|
2020-11-15 21:54:47 +01:00
|
|
|
mut conn := server.accept() or {
|
2020-06-02 12:10:01 +02:00
|
|
|
server.close() or { }
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
handle_http_connection(mut conn, server_context)
|
2020-12-07 02:43:25 +01:00
|
|
|
conn.close() or { eprintln('error closing the connection: $err') }
|
2020-06-21 16:41:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct VdocHttpServerContext {
|
2020-09-21 16:40:39 +02:00
|
|
|
docs map[string]string
|
|
|
|
content_type string
|
2020-06-21 16:41:43 +02:00
|
|
|
default_filename string
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:54:47 +01:00
|
|
|
fn handle_http_connection(mut con net.TcpConn, ctx &VdocHttpServerContext) {
|
2020-12-07 02:43:25 +01:00
|
|
|
mut reader := io.new_buffered_reader(reader: io.make_reader(con))
|
2020-11-15 21:54:47 +01:00
|
|
|
first_line := reader.read_line() or {
|
2020-06-21 16:41:43 +02:00
|
|
|
send_http_response(mut con, 501, ctx.content_type, 'bad request')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
request_parts := first_line.split(' ')
|
|
|
|
if request_parts.len != 3 {
|
|
|
|
send_http_response(mut con, 501, ctx.content_type, 'bad request')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
urlpath := request_parts[1]
|
2020-12-27 14:20:30 +01:00
|
|
|
filename := if urlpath == '/' {
|
|
|
|
ctx.default_filename.trim_left('/')
|
|
|
|
} else {
|
|
|
|
urlpath.trim_left('/')
|
|
|
|
}
|
2020-06-21 16:41:43 +02:00
|
|
|
if ctx.docs[filename].len == 0 {
|
|
|
|
send_http_response(mut con, 404, ctx.content_type, 'file not found')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
send_http_response(mut con, 200, ctx.content_type, ctx.docs[filename])
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:54:47 +01:00
|
|
|
fn send_http_response(mut con net.TcpConn, http_code int, content_type string, html string) {
|
2020-06-21 16:41:43 +02:00
|
|
|
content_length := html.len.str()
|
|
|
|
shttp_code := http_code.str()
|
|
|
|
mut http_response := strings.new_builder(20000)
|
|
|
|
http_response.write('HTTP/1.1 ')
|
|
|
|
http_response.write(shttp_code)
|
|
|
|
http_response.write(' OK\r\n')
|
|
|
|
http_response.write('Server: VDoc\r\n')
|
|
|
|
http_response.write('Content-Type: ')
|
|
|
|
http_response.write(content_type)
|
|
|
|
http_response.write('\r\n')
|
|
|
|
http_response.write('Content-Length: ')
|
|
|
|
http_response.write(content_length)
|
|
|
|
http_response.write('\r\n')
|
|
|
|
http_response.write('Connection: close\r\n')
|
|
|
|
http_response.write('\r\n')
|
|
|
|
http_response.write(html)
|
|
|
|
sresponse := http_response.str()
|
2020-12-07 02:43:25 +01:00
|
|
|
con.write_str(sresponse) or { eprintln('error sending http response: $err') }
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 15:17:52 +02:00
|
|
|
fn get_src_link(repo_url string, file_name string, line_nr int) string {
|
2020-12-07 02:43:25 +01:00
|
|
|
mut url := urllib.parse(repo_url) or { return '' }
|
2020-06-02 12:10:01 +02:00
|
|
|
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 { '' }
|
|
|
|
}
|
2020-09-21 16:40:39 +02:00
|
|
|
if url.path == '/' {
|
|
|
|
return ''
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
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-09-21 16:40:39 +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()
|
2020-09-21 16:40:39 +02:00
|
|
|
if trimmed.starts_with('//') || (trimmed.starts_with('/*') && trimmed.ends_with('*/')) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
js_str := js.str()
|
|
|
|
js.free()
|
|
|
|
return js_str
|
2020-06-08 10:28:46 +02:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
fn escape(str string) string {
|
2020-09-21 16:40:39 +02:00
|
|
|
return str.replace_each(['"', '\\"', '\r\n', '\\n', '\n', '\\n', '\t', '\\t'])
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (cfg DocConfig) gen_json(idx int) string {
|
|
|
|
dcs := cfg.docs[idx]
|
|
|
|
mut jw := strings.new_builder(200)
|
2021-01-06 19:43:43 +01:00
|
|
|
comments := if cfg.include_examples {
|
|
|
|
dcs.head.merge_comments()
|
|
|
|
} else {
|
|
|
|
dcs.head.merge_comments_without_examples()
|
|
|
|
}
|
|
|
|
jw.write('{"module_name":"$dcs.head.name","description":"${escape(comments)}","contents":')
|
2020-10-21 10:26:33 +02:00
|
|
|
jw.write(json.encode(dcs.contents.keys().map(dcs.contents[it])))
|
|
|
|
jw.write(',"generator":"vdoc","time_generated":"$dcs.time_generated.str()"}')
|
2020-06-02 12:10:01 +02:00
|
|
|
return jw.str()
|
|
|
|
}
|
|
|
|
|
2020-06-06 07:56:17 +02:00
|
|
|
fn html_highlight(code string, tb &table.Table) string {
|
2020-09-21 16:40:39 +02:00
|
|
|
builtin := ['bool', 'string', 'i8', 'i16', 'int', 'i64', 'i128', 'byte', 'u16', 'u32', 'u64',
|
|
|
|
'u128', 'rune', 'f32', 'f64', 'any_int', 'any_float', 'byteptr', 'voidptr', 'any']
|
2020-06-06 07:56:17 +02:00
|
|
|
highlight_code := fn (tok token.Token, typ HighlightTokenTyp) string {
|
2020-07-11 12:04:05 +02:00
|
|
|
lit := if typ in [.unone, .operator, .punctuation] {
|
|
|
|
tok.kind.str()
|
|
|
|
} else if typ == .string {
|
2020-06-08 10:28:46 +02:00
|
|
|
"'$tok.lit'"
|
|
|
|
} else if typ == .char {
|
|
|
|
'`$tok.lit`'
|
2020-09-21 16:40:39 +02:00
|
|
|
} else {
|
|
|
|
tok.lit
|
|
|
|
}
|
|
|
|
return if typ in [.unone, .name] {
|
|
|
|
lit
|
|
|
|
} else {
|
|
|
|
'<span class="token $typ">$lit</span>'
|
|
|
|
}
|
2020-06-06 07:56:17 +02:00
|
|
|
}
|
2020-07-24 11:30:38 +02:00
|
|
|
mut s := scanner.new_scanner(code, .parse_comments, &pref.Preferences{})
|
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-09-21 16:40:39 +02:00
|
|
|
.lpar, .lcbr, .rpar, .rcbr, .lsbr, .rsbr, .semicolon, .colon, .comma, .dot {
|
2020-06-06 10:43:50 +02:00
|
|
|
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
|
2020-09-21 16:40:39 +02:00
|
|
|
} else if tok.kind == .decl_assign || tok.kind.is_assign() || tok.is_unary() ||
|
|
|
|
tok.kind.is_relational() || tok.kind.is_infix() {
|
2020-06-06 07:56:17 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-01-06 19:43:43 +01:00
|
|
|
fn doc_node_html(dd doc.DocNode, link string, head bool, include_examples bool, tb &table.Table) string {
|
2020-06-06 07:56:17 +02:00
|
|
|
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' }
|
2021-01-06 19:43:43 +01:00
|
|
|
comments := dd.merge_comments_without_examples()
|
|
|
|
md_content := markdown.to_html(html_tag_escape(comments))
|
2020-06-06 07:56:17 +02:00
|
|
|
hlighted_code := html_highlight(dd.content, tb)
|
2020-10-21 10:26:33 +02:00
|
|
|
node_class := if dd.kind == .const_group { ' const' } else { '' }
|
2020-12-29 12:51:24 +01:00
|
|
|
sym_name := get_sym_name(dd)
|
2021-01-05 01:40:21 +01:00
|
|
|
mut node_id := get_node_id(dd)
|
|
|
|
mut hash_link := if !head { ' <a href="#$node_id">#</a>' } else { '' }
|
|
|
|
if head && is_module_readme(dd) {
|
|
|
|
node_id = 'readme_$node_id'
|
|
|
|
hash_link = ' <a href="#$node_id">#</a>'
|
|
|
|
}
|
2021-01-02 18:52:45 +01:00
|
|
|
dnw.writeln('${tabs[1]}<section id="$node_id" class="doc-node$node_class">')
|
2020-10-21 10:26:33 +02:00
|
|
|
if dd.name.len > 0 {
|
2020-12-26 07:55:35 +01:00
|
|
|
if dd.kind == .const_group {
|
2021-01-02 10:11:34 +01:00
|
|
|
dnw.write('${tabs[2]}<div class="title"><$head_tag>$sym_name$hash_link</$head_tag>')
|
2020-12-26 07:55:35 +01:00
|
|
|
} else {
|
2021-01-02 10:11:34 +01:00
|
|
|
dnw.write('${tabs[2]}<div class="title"><$head_tag>$dd.kind $sym_name$hash_link</$head_tag>')
|
2020-12-26 07:55:35 +01:00
|
|
|
}
|
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>')
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
if !head && dd.content.len > 0 {
|
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
|
|
|
}
|
2021-01-02 22:17:54 +01:00
|
|
|
// do not mess with md_content further, its formatting is important, just output it 1:1 !
|
2021-01-06 19:43:43 +01:00
|
|
|
dnw.writeln('$md_content\n')
|
|
|
|
// Write examples if any found
|
|
|
|
examples := dd.examples()
|
|
|
|
if include_examples && examples.len > 0 {
|
|
|
|
example_title := if examples.len > 1 { 'Examples' } else { 'Example' }
|
|
|
|
dnw.writeln('<section class="doc-node examples"><h4>$example_title</h4>')
|
|
|
|
for example in examples {
|
|
|
|
// hl_example := html_highlight(example, tb)
|
|
|
|
dnw.writeln('<pre><code class="language-v">$example</code></pre>')
|
|
|
|
}
|
|
|
|
dnw.writeln('</section>')
|
|
|
|
}
|
|
|
|
dnw.writeln('</section>')
|
2020-10-21 10:26:33 +02:00
|
|
|
dnw_str := dnw.str()
|
|
|
|
defer {
|
|
|
|
dnw.free()
|
|
|
|
}
|
|
|
|
return dnw_str
|
2020-06-06 07:56:17 +02:00
|
|
|
}
|
|
|
|
|
2021-01-05 19:16:56 +01:00
|
|
|
fn html_tag_escape(str string) string {
|
|
|
|
return str.replace_each(['<', '<', '>', '>'])
|
|
|
|
}
|
|
|
|
|
2020-12-29 12:51:24 +01:00
|
|
|
fn get_sym_name(dn doc.DocNode) string {
|
|
|
|
sym_name := if dn.parent_name.len > 0 && dn.parent_name != 'void' {
|
|
|
|
'($dn.parent_name) $dn.name'
|
|
|
|
} else {
|
|
|
|
dn.name
|
|
|
|
}
|
|
|
|
return sym_name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_node_id(dn doc.DocNode) string {
|
|
|
|
tag := if dn.parent_name.len > 0 && dn.parent_name != 'void' {
|
|
|
|
'${dn.parent_name}.$dn.name'
|
|
|
|
} else {
|
|
|
|
dn.name
|
|
|
|
}
|
|
|
|
return slug(tag)
|
|
|
|
}
|
|
|
|
|
2020-06-19 10:36:45 +02:00
|
|
|
fn (cfg DocConfig) readme_idx() int {
|
|
|
|
for i, dc in cfg.docs {
|
2020-09-21 16:40:39 +02:00
|
|
|
if dc.head.name != 'README' {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-19 10:36:45 +02:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2021-01-05 01:40:21 +01:00
|
|
|
fn write_toc(dn doc.DocNode, mut toc strings.Builder) {
|
2020-12-26 07:55:35 +01:00
|
|
|
mut toc_slug := if dn.name.len == 0 || dn.content.len == 0 { '' } else { slug(dn.name) }
|
|
|
|
if toc_slug == '' && dn.children.len > 0 {
|
2021-01-02 19:18:17 +01:00
|
|
|
if dn.children[0].name == '' {
|
|
|
|
toc_slug = slug(dn.name)
|
|
|
|
} else {
|
|
|
|
toc_slug = slug(dn.name + '.' + dn.children[0].name)
|
|
|
|
}
|
2020-12-25 16:01:43 +01:00
|
|
|
}
|
2021-01-05 01:40:21 +01:00
|
|
|
if is_module_readme(dn) {
|
|
|
|
toc.write('<li class="open"><a href="#readme_$toc_slug">README</a>')
|
|
|
|
} else if dn.name != 'Constants' {
|
2020-12-26 07:55:35 +01:00
|
|
|
toc.write('<li class="open"><a href="#$toc_slug">$dn.kind $dn.name</a>')
|
2020-06-08 10:28:46 +02:00
|
|
|
toc.writeln(' <ul>')
|
2020-12-26 07:55:35 +01:00
|
|
|
for child in dn.children {
|
|
|
|
cname := dn.name + '.' + child.name
|
|
|
|
toc.writeln('<li><a href="#${slug(cname)}">$child.kind $child.name</a></li>')
|
2020-06-08 10:28:46 +02:00
|
|
|
}
|
|
|
|
toc.writeln('</ul>')
|
2020-12-26 07:55:35 +01:00
|
|
|
} else {
|
|
|
|
toc.write('<li class="open"><a href="#$toc_slug">$dn.name</a>')
|
2020-06-08 10:28:46 +02:00
|
|
|
}
|
|
|
|
toc.writeln('</li>')
|
|
|
|
}
|
|
|
|
|
2020-07-24 11:30:38 +02:00
|
|
|
fn (cfg DocConfig) write_content(cn &doc.DocNode, dcs &doc.Doc, mut hw strings.Builder) {
|
2020-10-01 01:30:22 +02:00
|
|
|
base_dir := os.dir(os.real_path(cfg.input_path))
|
2020-12-27 14:20:30 +01: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)
|
2020-10-21 10:26:33 +02:00
|
|
|
if cn.content.len != 0 || (cn.name == 'Constants') {
|
2021-01-06 19:43:43 +01:00
|
|
|
hw.write(doc_node_html(cn, src_link, false, cfg.include_examples, dcs.table))
|
2020-06-19 10:36:45 +02:00
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
for child in cn.children {
|
2020-06-19 10:36:45 +02:00
|
|
|
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)
|
2021-01-06 19:43:43 +01:00
|
|
|
hw.write(doc_node_html(child, child_src_link, false, cfg.include_examples, 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 {
|
2020-10-21 10:26:33 +02:00
|
|
|
mut symbols_toc := strings.new_builder(200)
|
|
|
|
mut modules_toc := strings.new_builder(200)
|
2020-06-19 10:36:45 +02:00
|
|
|
mut contents := strings.new_builder(200)
|
2020-10-21 10:26:33 +02:00
|
|
|
dcs := cfg.docs[idx]
|
|
|
|
dcs_contents := dcs.contents.arr()
|
2020-06-02 12:10:01 +02:00
|
|
|
// generate toc first
|
2021-01-06 19:43:43 +01:00
|
|
|
contents.writeln(doc_node_html(dcs.head, '', true, cfg.include_examples, dcs.table))
|
2021-01-05 01:40:21 +01:00
|
|
|
if is_module_readme(dcs.head) {
|
|
|
|
write_toc(dcs.head, mut symbols_toc)
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
for cn in dcs_contents {
|
2020-09-27 15:58:36 +02:00
|
|
|
cfg.write_content(&cn, &dcs, mut contents)
|
2021-01-05 01:40:21 +01:00
|
|
|
write_toc(cn, mut symbols_toc)
|
2020-09-21 16:40:39 +02:00
|
|
|
} // write head
|
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 { '' }
|
2020-12-27 14:20:30 +01:00
|
|
|
header_name := if cfg.is_multi && cfg.docs.len > 1 {
|
|
|
|
os.file_name(os.real_path(cfg.input_path))
|
|
|
|
} else {
|
|
|
|
dcs.head.name
|
|
|
|
}
|
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-29 11:03:09 +02:00
|
|
|
for i, doc in cfg.docs {
|
2020-09-21 16:40:39 +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-10-29 17:21:37 +01:00
|
|
|
mut href_name := './${doc.head.name}.html'
|
|
|
|
if (cfg.is_vlib && doc.head.name == 'builtin' && !cfg.include_readme) ||
|
2020-09-21 16:40:39 +02:00
|
|
|
doc.head.name == 'README' {
|
2020-10-29 17:21:37 +01:00
|
|
|
href_name = './index.html'
|
2020-06-02 12:10:01 +02:00
|
|
|
} else if submod_prefix !in cfg.docs.map(it.head.name) {
|
2020-10-29 17:21:37 +01:00
|
|
|
href_name = '#'
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
submodules := cfg.docs.filter(it.head.name.starts_with(submod_prefix + '.'))
|
2020-09-23 20:50:51 +02:00
|
|
|
dropdown := if submodules.len > 0 { cfg.assets['arrow_icon'] } else { '' }
|
2020-06-04 23:50:59 +02:00
|
|
|
active_class := if doc.head.name == dcs.head.name { ' active' } else { '' }
|
2020-10-21 10:26:33 +02:00
|
|
|
modules_toc.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-10-21 10:26:33 +02:00
|
|
|
modules_toc.write('<ul>')
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
submod_name := cdoc.head.name.all_after(submod_prefix + '.')
|
2020-12-27 14:20:30 +01:00
|
|
|
sub_selected_classes := if cdoc.head.name == dcs.head.name {
|
|
|
|
' class="active"'
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
modules_toc.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-10-21 10:26:33 +02:00
|
|
|
modules_toc.write('</ul>')
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
modules_toc.write('</li>')
|
2020-06-19 10:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
modules_toc_str := modules_toc.str()
|
|
|
|
symbols_toc_str := symbols_toc.str()
|
|
|
|
modules_toc.free()
|
|
|
|
symbols_toc.free()
|
2020-09-21 16:40:39 +02:00
|
|
|
return html_content.replace('{{ title }}', dcs.head.name).replace('{{ head_name }}',
|
2020-09-23 20:50:51 +02:00
|
|
|
header_name).replace('{{ version }}', version).replace('{{ light_icon }}', cfg.assets['light_icon']).replace('{{ dark_icon }}',
|
2020-10-15 15:17:52 +02:00
|
|
|
cfg.assets['dark_icon']).replace('{{ menu_icon }}', cfg.assets['menu_icon']).replace('{{ head_assets }}',
|
|
|
|
if cfg.inline_assets {
|
2021-01-02 10:11:34 +01:00
|
|
|
'\n${tabs[0]}<style>' + cfg.assets['doc_css'] + '</style>\n${tabs[0]}<style>' + cfg.assets['normalize_css'] +
|
|
|
|
'</style>\n${tabs[0]}<script>' + cfg.assets['dark_mode_js'] + '</script>'
|
2020-09-21 16:40:39 +02:00
|
|
|
} else {
|
2021-01-02 10:11:34 +01:00
|
|
|
'\n${tabs[0]}<link rel="stylesheet" href="' + cfg.assets['doc_css'] + '" />\n${tabs[0]}<link rel="stylesheet" href="' +
|
|
|
|
cfg.assets['normalize_css'] + '" />\n${tabs[0]}<script src="' + cfg.assets['dark_mode_js'] + '"></script>'
|
2020-09-21 16:40:39 +02:00
|
|
|
}).replace('{{ toc_links }}', if cfg.is_multi || cfg.docs.len > 1 {
|
2020-10-21 10:26:33 +02:00
|
|
|
modules_toc_str
|
2020-09-21 16:40:39 +02:00
|
|
|
} else {
|
2020-10-21 10:26:33 +02:00
|
|
|
symbols_toc_str
|
2020-09-21 16:40:39 +02:00
|
|
|
}).replace('{{ contents }}', contents.str()).replace('{{ right_content }}', if cfg.is_multi &&
|
|
|
|
cfg.docs.len > 1 && dcs.head.name != 'README' {
|
2020-10-21 10:26:33 +02:00
|
|
|
'<div class="doc-toc"><ul>' + symbols_toc_str + '</ul></div>'
|
2020-09-21 16:40:39 +02:00
|
|
|
} else {
|
|
|
|
''
|
2020-10-19 21:30:37 +02:00
|
|
|
}).replace('{{ footer_content }}', cfg.gen_footer_text(idx)).replace('{{ footer_assets }}',
|
2020-09-21 16:40:39 +02:00
|
|
|
if cfg.inline_assets {
|
2020-10-15 15:17:52 +02:00
|
|
|
'<script>' + cfg.assets['doc_js'] + '</script>'
|
2020-09-21 16:40:39 +02:00
|
|
|
} else {
|
2020-10-15 15:17:52 +02:00
|
|
|
'<script src="' + cfg.assets['doc_js'] + '"></script>'
|
2020-09-21 16:40:39 +02:00
|
|
|
})
|
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-09-21 16:40:39 +02:00
|
|
|
pw.writeln('$dcs.head.content\n')
|
2021-01-06 19:43:43 +01:00
|
|
|
comments := if cfg.include_examples {
|
|
|
|
dcs.head.merge_comments()
|
|
|
|
} else {
|
|
|
|
dcs.head.merge_comments_without_examples()
|
|
|
|
}
|
|
|
|
if comments.trim_space().len > 0 && !cfg.pub_only {
|
|
|
|
pw.writeln(comments.split_into_lines().map(' ' + it).join('\n'))
|
2020-06-08 08:22:10 +02:00
|
|
|
}
|
2020-11-20 11:02:52 +01:00
|
|
|
cfg.write_plaintext_content(dcs.contents.arr(), mut pw)
|
|
|
|
return pw.str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cfg DocConfig) write_plaintext_content(contents []doc.DocNode, mut pw strings.Builder) {
|
|
|
|
for cn in contents {
|
|
|
|
if cn.content.len > 0 {
|
|
|
|
pw.writeln(cn.content)
|
2021-01-06 19:43:43 +01:00
|
|
|
if cn.comments.len > 0 && !cfg.pub_only {
|
|
|
|
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 11:02:52 +01:00
|
|
|
}
|
|
|
|
if cfg.show_loc {
|
|
|
|
pw.writeln('Location: $cn.file_path:$cn.pos.line\n')
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-11-20 11:02:52 +01:00
|
|
|
cfg.write_plaintext_content(cn.children, mut pw)
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-11-20 11:02:52 +01:00
|
|
|
hw.writeln('# $dcs.head.content\n')
|
2021-01-06 19:43:43 +01:00
|
|
|
if dcs.head.comments.len > 0 {
|
|
|
|
comments := if cfg.include_examples {
|
|
|
|
dcs.head.merge_comments()
|
|
|
|
} else {
|
|
|
|
dcs.head.merge_comments_without_examples()
|
|
|
|
}
|
|
|
|
hw.writeln('$comments\n')
|
2020-11-20 11:02:52 +01:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
if with_toc {
|
|
|
|
hw.writeln('## Contents')
|
|
|
|
}
|
2020-11-20 11:02:52 +01:00
|
|
|
cfg.write_markdown_content(dcs.contents.arr(), mut cw, mut hw, 0, with_toc)
|
2020-10-19 21:30:37 +02:00
|
|
|
footer_text := cfg.gen_footer_text(idx)
|
|
|
|
cw.writeln('#### $footer_text')
|
2020-06-02 12:10:01 +02:00
|
|
|
return hw.str() + '\n' + cw.str()
|
|
|
|
}
|
|
|
|
|
2020-11-20 11:02:52 +01:00
|
|
|
fn (cfg DocConfig) write_markdown_content(contents []doc.DocNode, mut cw strings.Builder, mut hw strings.Builder, indent int, with_toc bool) {
|
|
|
|
for cn in contents {
|
|
|
|
if with_toc && cn.name.len > 0 {
|
|
|
|
hw.writeln(' '.repeat(2 * indent) + '- [#$cn.name](${slug(cn.name)})')
|
|
|
|
cw.writeln('## $cn.name')
|
|
|
|
}
|
|
|
|
if cn.content.len > 0 {
|
2021-01-06 19:43:43 +01:00
|
|
|
comments := cn.merge_comments_without_examples()
|
|
|
|
cw.writeln('```v\n$cn.content\n```\n$comments\n')
|
|
|
|
// Write examples if any found
|
|
|
|
examples := cn.examples()
|
|
|
|
if cfg.include_examples && examples.len > 0 {
|
|
|
|
example_title := if examples.len > 1 { 'Examples' } else { 'Example' }
|
|
|
|
cw.writeln('$example_title\n```v\n')
|
|
|
|
for example in examples {
|
|
|
|
cw.writeln('$example\n')
|
|
|
|
}
|
|
|
|
cw.writeln('```\n')
|
|
|
|
}
|
2020-11-20 11:02:52 +01:00
|
|
|
cw.writeln('[\[Return to contents\]](#Contents)\n')
|
|
|
|
}
|
|
|
|
cfg.write_markdown_content(cn.children, mut cw, mut hw, indent + 1, with_toc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 21:30:37 +02:00
|
|
|
fn (cfg DocConfig) gen_footer_text(idx int) string {
|
|
|
|
current_doc := cfg.docs[idx]
|
|
|
|
footer_text := 'Powered by vdoc.'
|
|
|
|
if cfg.no_timestamp {
|
|
|
|
return footer_text
|
|
|
|
}
|
|
|
|
generated_time := current_doc.time_generated
|
|
|
|
time_str := '$generated_time.day $generated_time.smonth() $generated_time.year $generated_time.hhmmss()'
|
|
|
|
return '$footer_text Generated on: $time_str'
|
|
|
|
}
|
|
|
|
|
2020-09-23 20:50:51 +02:00
|
|
|
fn (cfg DocConfig) render_doc(doc doc.Doc, i int) (string, string) {
|
2020-12-29 12:51:24 +01:00
|
|
|
name := cfg.get_file_name(doc.head.name)
|
|
|
|
output := match cfg.output_type {
|
|
|
|
.html { cfg.gen_html(i) }
|
|
|
|
.markdown { cfg.gen_markdown(i, true) }
|
|
|
|
.json { cfg.gen_json(i) }
|
|
|
|
else { cfg.gen_plaintext(i) }
|
|
|
|
}
|
|
|
|
return name, output
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_file_name returns the final file name from a module name
|
|
|
|
fn (cfg DocConfig) get_file_name(mod string) string {
|
|
|
|
mut name := mod
|
2020-09-23 20:50:51 +02:00
|
|
|
// since builtin is generated first, ignore it
|
2020-12-29 12:51:24 +01:00
|
|
|
if (cfg.is_vlib && mod == 'builtin' && !cfg.include_readme) || mod == 'README' {
|
2020-10-29 17:21:37 +01:00
|
|
|
name = 'index'
|
2020-09-23 20:50:51 +02:00
|
|
|
} else if !cfg.is_multi && !os.is_dir(cfg.output_path) {
|
2020-10-29 17:21:37 +01:00
|
|
|
name = os.file_name(cfg.output_path)
|
2020-09-23 20:50:51 +02:00
|
|
|
}
|
|
|
|
name = name + match cfg.output_type {
|
|
|
|
.html { '.html' }
|
|
|
|
.markdown { '.md' }
|
|
|
|
.json { '.json' }
|
|
|
|
else { '.txt' }
|
|
|
|
}
|
2020-12-29 12:51:24 +01:00
|
|
|
return name
|
2020-09-23 20:50:51 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 15:17:52 +02:00
|
|
|
fn (cfg DocConfig) work_processor(mut work sync.Channel, mut wg sync.WaitGroup) {
|
2020-09-23 20:50:51 +02:00
|
|
|
for {
|
|
|
|
mut pdoc := ParallelDoc{}
|
|
|
|
if !work.pop(&pdoc) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
file_name, content := cfg.render_doc(pdoc.d, pdoc.i)
|
|
|
|
output_path := os.join_path(cfg.output_path, file_name)
|
2020-10-21 13:36:16 +02:00
|
|
|
println('Generating $output_path')
|
2020-09-23 20:50:51 +02:00
|
|
|
os.write_file(output_path, content)
|
|
|
|
}
|
|
|
|
wg.done()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cfg DocConfig) render_parallel() {
|
|
|
|
vjobs := runtime.nr_jobs()
|
|
|
|
mut work := sync.new_channel<ParallelDoc>(cfg.docs.len)
|
|
|
|
mut wg := sync.new_waitgroup()
|
|
|
|
for i in 0 .. cfg.docs.len {
|
|
|
|
p_doc := ParallelDoc{cfg.docs[i], i}
|
|
|
|
work.push(&p_doc)
|
|
|
|
}
|
|
|
|
work.close()
|
|
|
|
wg.add(vjobs)
|
|
|
|
for _ in 0 .. vjobs {
|
|
|
|
go cfg.work_processor(mut work, mut wg)
|
|
|
|
}
|
|
|
|
wg.wait()
|
|
|
|
}
|
|
|
|
|
2020-06-05 09:59:26 +02:00
|
|
|
fn (cfg DocConfig) render() map[string]string {
|
2020-09-21 16:40:39 +02:00
|
|
|
mut docs := map[string]string{}
|
2020-06-02 12:10:01 +02:00
|
|
|
for i, doc in cfg.docs {
|
2020-09-23 20:50:51 +02:00
|
|
|
name, output := cfg.render_doc(doc, i)
|
2020-06-21 16:41:43 +02:00
|
|
|
docs[name] = output.trim_space()
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
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-12-29 12:51:24 +01:00
|
|
|
fn (mut cfg DocConfig) collect_search_index() {
|
|
|
|
if cfg.output_type != .html {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for doc in cfg.docs {
|
|
|
|
mod := doc.head.name
|
|
|
|
cfg.search_module_index << mod
|
2021-01-06 19:43:43 +01:00
|
|
|
comments := if cfg.include_examples {
|
|
|
|
doc.head.merge_comments()
|
|
|
|
} else {
|
|
|
|
doc.head.merge_comments_without_examples()
|
|
|
|
}
|
2020-12-29 12:51:24 +01:00
|
|
|
cfg.search_module_data << SearchModuleResult{
|
2021-01-06 19:43:43 +01:00
|
|
|
description: trim_doc_node_description(comments)
|
2020-12-29 12:51:24 +01:00
|
|
|
link: cfg.get_file_name(mod)
|
|
|
|
}
|
|
|
|
for _, dn in doc.contents {
|
|
|
|
cfg.create_search_results(mod, dn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut cfg DocConfig) create_search_results(mod string, dn doc.DocNode) {
|
|
|
|
if dn.kind == .const_group {
|
|
|
|
return
|
|
|
|
}
|
2021-01-06 19:43:43 +01:00
|
|
|
comments := if cfg.include_examples {
|
|
|
|
dn.merge_comments()
|
|
|
|
} else {
|
|
|
|
dn.merge_comments_without_examples()
|
|
|
|
}
|
|
|
|
dn_description := trim_doc_node_description(comments)
|
2020-12-29 12:51:24 +01:00
|
|
|
cfg.search_index << dn.name
|
|
|
|
cfg.search_data << SearchResult{
|
2020-12-29 20:01:12 +01:00
|
|
|
prefix: if dn.parent_name != '' {
|
|
|
|
'$dn.kind ($dn.parent_name)'
|
|
|
|
} else {
|
|
|
|
'$dn.kind '
|
|
|
|
}
|
2020-12-29 12:51:24 +01:00
|
|
|
description: dn_description
|
|
|
|
badge: mod
|
|
|
|
link: cfg.get_file_name(mod) + '#' + get_node_id(dn)
|
|
|
|
}
|
|
|
|
for child in dn.children {
|
|
|
|
cfg.create_search_results(mod, child)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trim_doc_node_description(description string) string {
|
|
|
|
mut dn_description := description.replace_each(['\r\n', '\n', '"', '\\"'])
|
|
|
|
// 80 is enough to fill one line
|
|
|
|
if dn_description.len > 80 {
|
|
|
|
dn_description = dn_description[..80]
|
|
|
|
}
|
|
|
|
if '\n' in dn_description {
|
|
|
|
dn_description = dn_description.split('\n')[0]
|
|
|
|
}
|
|
|
|
// if \ is last character, it ends with \" which leads to a JS error
|
|
|
|
if dn_description.ends_with('\\') {
|
|
|
|
dn_description = dn_description.trim_right('\\')
|
|
|
|
}
|
|
|
|
return dn_description
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cfg DocConfig) render_search_index() {
|
|
|
|
mut js_search_index := strings.new_builder(200)
|
|
|
|
mut js_search_data := strings.new_builder(200)
|
|
|
|
js_search_index.write('var searchModuleIndex = [')
|
|
|
|
js_search_data.write('var searchModuleData = [')
|
|
|
|
for i, title in cfg.search_module_index {
|
|
|
|
data := cfg.search_module_data[i]
|
|
|
|
js_search_index.write('"$title",')
|
|
|
|
js_search_data.write('["$data.description","$data.link"],')
|
|
|
|
}
|
|
|
|
js_search_index.writeln('];')
|
|
|
|
js_search_index.write('var searchIndex = [')
|
|
|
|
js_search_data.writeln('];')
|
|
|
|
js_search_data.write('var searchData = [')
|
|
|
|
for i, title in cfg.search_index {
|
|
|
|
data := cfg.search_data[i]
|
|
|
|
js_search_index.write('"$title",')
|
|
|
|
// array instead of object to reduce file size
|
|
|
|
js_search_data.write('["$data.badge","$data.description","$data.link","$data.prefix"],')
|
|
|
|
}
|
|
|
|
js_search_index.writeln('];')
|
|
|
|
js_search_data.writeln('];')
|
|
|
|
out_file_path := os.join_path(cfg.output_path, 'search_index.js')
|
|
|
|
os.write_file(out_file_path, js_search_index.str() + js_search_data.str())
|
|
|
|
}
|
|
|
|
|
2020-09-23 20:50:51 +02:00
|
|
|
fn (mut cfg DocConfig) render_static() {
|
2020-10-21 13:36:16 +02:00
|
|
|
if cfg.output_type != .html {
|
|
|
|
return
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
cfg.assets = {
|
2020-12-07 02:43:25 +01:00
|
|
|
'doc_css': cfg.get_resource(css_js_assets[0], true)
|
2020-10-21 13:36:16 +02:00
|
|
|
'normalize_css': cfg.get_resource(css_js_assets[1], true)
|
2020-12-07 02:43:25 +01:00
|
|
|
'doc_js': cfg.get_resource(css_js_assets[2], !cfg.serve_http)
|
2020-12-17 10:59:12 +01:00
|
|
|
'dark_mode_js': cfg.get_resource(css_js_assets[3], !cfg.serve_http)
|
2020-12-07 02:43:25 +01: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-09-23 20:50:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2020-07-11 12:04:05 +02:00
|
|
|
}
|
2020-06-07 12:27:42 +02:00
|
|
|
}
|
|
|
|
if fname == '' {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
readme_path := os.join_path(path, '${fname}.md')
|
|
|
|
cfg.vprintln('Reading README file from $readme_path')
|
2020-12-07 02:43:25 +01:00
|
|
|
readme_contents := os.read_file(readme_path) or { '' }
|
2020-06-07 12:27:42 +02:00
|
|
|
return readme_contents
|
|
|
|
}
|
|
|
|
|
2020-10-21 10:26:33 +02:00
|
|
|
fn (cfg DocConfig) emit_generate_err(err string, errcode int) {
|
|
|
|
mut err_msg := err
|
|
|
|
if errcode == 1 {
|
|
|
|
mod_list := get_modules_list(cfg.input_path, []string{})
|
|
|
|
println('Available modules:\n==================')
|
|
|
|
for mod in mod_list {
|
2020-10-21 13:36:16 +02:00
|
|
|
println(mod.all_after('vlib/').all_after('modules/').replace('/', '.'))
|
2020-10-21 10:26:33 +02:00
|
|
|
}
|
|
|
|
err_msg += ' Use the `-m` flag when generating docs from a directory that has multiple modules.'
|
|
|
|
}
|
|
|
|
eprintln(err_msg)
|
|
|
|
}
|
|
|
|
|
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-10-29 17:21:37 +01:00
|
|
|
dir_path := if cfg.is_vlib {
|
2020-11-30 18:31:37 +01:00
|
|
|
vroot
|
2020-07-11 12:04:05 +02:00
|
|
|
} else if os.is_dir(cfg.input_path) {
|
2020-06-05 09:59:26 +02:00
|
|
|
cfg.input_path
|
2020-07-11 12:04:05 +02:00
|
|
|
} else {
|
2020-10-01 01:30:22 +02:00
|
|
|
os.dir(cfg.input_path)
|
2020-06-05 09:59:26 +02:00
|
|
|
}
|
|
|
|
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)
|
2021-01-06 19:43:43 +01:00
|
|
|
comment := doc.DocComment{
|
|
|
|
text: readme_contents
|
|
|
|
}
|
2020-07-04 14:29:00 +02:00
|
|
|
if cfg.output_type == .stdout {
|
2020-06-05 09:59:26 +02:00
|
|
|
println(markdown.to_plain(readme_contents))
|
2020-07-04 14:29:00 +02:00
|
|
|
} else if cfg.output_type == .html && cfg.is_multi {
|
2020-06-05 09:59:26 +02:00
|
|
|
cfg.docs << doc.Doc{
|
2020-06-02 22:56:13 +02:00
|
|
|
head: doc.DocNode{
|
2020-09-21 16:40:39 +02:00
|
|
|
name: 'README'
|
2021-01-06 19:43:43 +01:00
|
|
|
comments: [comment]
|
2020-06-02 22:56:13 +02:00
|
|
|
}
|
2020-06-06 14:31:26 +02:00
|
|
|
time_generated: time.now()
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-07-04 14:29:00 +02:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-12-27 14:20:30 +01:00
|
|
|
dirs := if cfg.is_multi {
|
|
|
|
get_modules_list(cfg.input_path, []string{})
|
|
|
|
} else {
|
|
|
|
[cfg.input_path]
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
is_local_and_single := cfg.is_local && !cfg.is_multi
|
2020-06-05 09:59:26 +02:00
|
|
|
for dirpath in dirs {
|
2020-10-21 10:26:33 +02:00
|
|
|
mut dcs := doc.Doc{}
|
2020-10-21 13:36:16 +02:00
|
|
|
cfg.vprintln('Generating docs for $dirpath')
|
2020-10-21 10:26:33 +02:00
|
|
|
if is_local_and_single {
|
2020-10-29 17:21:37 +01:00
|
|
|
dcs = doc.generate_with_pos(dirpath, cfg.local_filename, cfg.local_pos) or {
|
2020-10-21 10:26:33 +02:00
|
|
|
cfg.emit_generate_err(err, errcode)
|
2020-09-21 16:40:39 +02:00
|
|
|
exit(1)
|
2020-06-10 11:13:42 +02:00
|
|
|
}
|
2020-09-21 16:40:39 +02:00
|
|
|
} else {
|
2020-10-21 10:26:33 +02:00
|
|
|
dcs = doc.generate(dirpath, cfg.pub_only, true) or {
|
|
|
|
cfg.emit_generate_err(err, errcode)
|
2020-09-21 16:40:39 +02:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
}
|
|
|
|
if dcs.contents.len == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !is_local_and_single {
|
2020-09-21 16:40:39 +02:00
|
|
|
if cfg.is_multi || (!cfg.is_multi && cfg.include_readme) {
|
|
|
|
readme_contents := cfg.get_readme(dirpath)
|
2021-01-06 19:43:43 +01:00
|
|
|
comment := doc.DocComment{
|
|
|
|
text: readme_contents
|
|
|
|
}
|
|
|
|
dcs.head.comments = [comment]
|
2020-09-21 16:40:39 +02:00
|
|
|
}
|
|
|
|
if cfg.pub_only {
|
2020-10-31 13:00:04 +01:00
|
|
|
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-10-21 10:26:33 +02:00
|
|
|
}
|
2020-09-21 16:40:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !cfg.is_multi && cfg.symbol_name.len > 0 {
|
2020-10-21 10:26:33 +02:00
|
|
|
if cfg.symbol_name in dcs.contents {
|
2020-10-31 13:00:04 +01:00
|
|
|
for _, c in dcs.contents[cfg.symbol_name].children {
|
|
|
|
dcs.contents[c.name] = c
|
2020-09-21 16:40:39 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 14:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
cfg.docs << dcs
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-10-29 17:21:37 +01:00
|
|
|
if cfg.is_vlib {
|
2020-06-29 11:03:09 +02:00
|
|
|
mut docs := cfg.docs.filter(it.head.name == 'builtin')
|
|
|
|
docs << cfg.docs.filter(it.head.name != 'builtin')
|
|
|
|
cfg.docs = docs
|
|
|
|
}
|
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...')
|
2020-09-21 16:40:39 +02:00
|
|
|
if cfg.output_path.len == 0 || cfg.output_path == 'stdout' {
|
2020-09-23 20:50:51 +02:00
|
|
|
cfg.render_static()
|
2020-06-05 09:59:26 +02:00
|
|
|
outputs := cfg.render()
|
2020-06-21 16:51:02 +02:00
|
|
|
if outputs.len == 0 {
|
2020-06-08 08:22:10 +02:00
|
|
|
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) {
|
2020-12-07 02:43:25 +01:00
|
|
|
os.mkdir(cfg.output_path) or { panic(err) }
|
2020-06-19 10:36:45 +02:00
|
|
|
}
|
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) {
|
2020-12-07 02:43:25 +01:00
|
|
|
os.mkdir(cfg.output_path) or { 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-09-23 20:50:51 +02:00
|
|
|
cfg.render_static()
|
|
|
|
cfg.render_parallel()
|
2020-12-29 12:51:24 +01:00
|
|
|
println('Creating search index...')
|
|
|
|
cfg.collect_search_index()
|
|
|
|
cfg.render_search_index()
|
2020-12-18 09:41:34 +01:00
|
|
|
// move favicons to target directory
|
|
|
|
println('Copying favicons...')
|
|
|
|
favicons := os.ls(favicons_path) or { panic(err) }
|
|
|
|
for favicon in favicons {
|
|
|
|
favicon_path := os.join_path(favicons_path, favicon)
|
|
|
|
destination_path := os.join_path(cfg.output_path, favicon)
|
|
|
|
os.cp(favicon_path, destination_path)
|
|
|
|
}
|
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 {
|
2020-09-21 16:40:39 +02:00
|
|
|
'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 }
|
2020-06-05 09:59:26 +02:00
|
|
|
}
|
|
|
|
cfg.vprintln('Setting output type to "$cfg.output_type"')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cfg DocConfig) vprintln(str string) {
|
|
|
|
if cfg.is_verbose {
|
|
|
|
println('vdoc: $str')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 11:03:09 +02:00
|
|
|
fn get_ignore_paths(path string) ?[]string {
|
|
|
|
ignore_file_path := os.join_path(path, '.vdocignore')
|
|
|
|
ignore_content := os.read_file(ignore_file_path) or {
|
|
|
|
return error_with_code('ignore file not found.', 1)
|
|
|
|
}
|
2020-10-03 16:40:22 +02:00
|
|
|
mut res := []string{}
|
2020-06-29 11:03:09 +02:00
|
|
|
if ignore_content.trim_space().len > 0 {
|
|
|
|
rules := ignore_content.split_into_lines().map(it.trim_space())
|
|
|
|
mut final := []string{}
|
|
|
|
for rule in rules {
|
|
|
|
if rule.contains('*.') || rule.contains('**') {
|
|
|
|
println('vdoc: Wildcards in ignore rules are not allowed for now.')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
final << rule
|
|
|
|
}
|
2020-10-03 16:40:22 +02:00
|
|
|
res = final.map(os.join_path(path, it.trim_right('/')))
|
2020-06-29 11:03:09 +02:00
|
|
|
} else {
|
2020-12-07 02:43:25 +01:00
|
|
|
mut dirs := os.ls(path) or { return []string{} }
|
2020-10-03 16:40:22 +02:00
|
|
|
res = dirs.map(os.join_path(path, it)).filter(os.is_dir(it))
|
2020-06-29 11:03:09 +02:00
|
|
|
}
|
2020-10-03 16:40:22 +02:00
|
|
|
return res.map(it.replace('/', os.path_separator))
|
2020-06-29 11:03:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_included(path string, ignore_paths []string) bool {
|
|
|
|
if path.len == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for ignore_path in ignore_paths {
|
2020-09-21 16:40:39 +02:00
|
|
|
if ignore_path !in path {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-29 11:03:09 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_modules_list(path string, ignore_paths2 []string) []string {
|
2020-12-07 02:43:25 +01:00
|
|
|
files := os.ls(path) or { return []string{} }
|
|
|
|
mut ignore_paths := get_ignore_paths(path) or { []string{} }
|
2020-06-29 11:03:09 +02:00
|
|
|
ignore_paths << ignore_paths2
|
2020-06-02 12:10:01 +02:00
|
|
|
mut dirs := []string{}
|
|
|
|
for file in files {
|
2020-06-29 11:03:09 +02:00
|
|
|
fpath := os.join_path(path, file)
|
|
|
|
if os.is_dir(fpath) && is_included(fpath, ignore_paths) && !os.is_link(path) {
|
|
|
|
dirs << get_modules_list(fpath, ignore_paths.filter(it.starts_with(fpath)))
|
|
|
|
} else if fpath.ends_with('.v') && !fpath.ends_with('_test.v') {
|
2020-09-21 16:40:39 +02:00
|
|
|
if path in dirs {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-29 11:03:09 +02:00
|
|
|
dirs << path
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
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-12-07 02:43:25 +01:00
|
|
|
mut res := os.read_file(path) or { panic('vdoc: could not read $path') }
|
2020-06-04 23:50:59 +02:00
|
|
|
if minify {
|
2020-06-29 11:03:09 +02:00
|
|
|
if name.ends_with('.js') {
|
2020-07-11 12:04:05 +02:00
|
|
|
res = js_compress(res)
|
|
|
|
} else {
|
2020-06-29 11:03:09 +02:00
|
|
|
res = res.split_into_lines().map(it.trim_space()).join('')
|
|
|
|
}
|
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) {
|
2020-10-21 13:36:16 +02:00
|
|
|
println('Generating $output_path')
|
2020-06-05 09:59:26 +02:00
|
|
|
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-11-30 18:31:37 +01:00
|
|
|
if os.args.len < 2 || '-h' in os.args || '--help' in os.args || os.args[1..] == ['doc', 'help'] {
|
|
|
|
os.system('$vexe help doc')
|
2020-06-02 12:10:01 +02:00
|
|
|
exit(0)
|
|
|
|
}
|
2020-11-30 18:31:37 +01:00
|
|
|
args := os.args[2..].clone()
|
2020-06-05 09:59:26 +02:00
|
|
|
mut cfg := DocConfig{
|
2020-09-21 16:40:39 +02:00
|
|
|
manifest: vmod.Manifest{
|
|
|
|
repo_url: ''
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
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
|
|
|
|
}
|
2020-09-21 16:40:39 +02:00
|
|
|
'-filename' {
|
|
|
|
cfg.is_local = true
|
|
|
|
cfg.local_filename = cmdline.option(current_args, '-filename', '')
|
|
|
|
i++
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
'-f' {
|
|
|
|
format := cmdline.option(current_args, '-f', '')
|
|
|
|
if format !in allowed_formats {
|
2020-09-23 20:50:51 +02:00
|
|
|
allowed_str := allowed_formats.join(', ')
|
2020-06-05 09:59:26 +02:00
|
|
|
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', '')
|
2020-09-21 16:40:39 +02:00
|
|
|
cfg.output_path = if opath == 'stdout' { opath } else { os.real_path(opath) }
|
2020-06-05 09:59:26 +02:00
|
|
|
i++
|
|
|
|
}
|
2020-06-07 12:27:42 +02:00
|
|
|
'-open' {
|
|
|
|
cfg.open_docs = true
|
|
|
|
}
|
2020-09-21 16:40:39 +02:00
|
|
|
'-pos' {
|
|
|
|
if !cfg.is_local {
|
|
|
|
eprintln('vdoc: `-pos` is only allowed with `-filename` flag.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
cfg.local_pos = cmdline.option(current_args, '-pos', '').int()
|
|
|
|
i++
|
|
|
|
}
|
2020-06-07 12:27:42 +02:00
|
|
|
'-p' {
|
2020-12-15 16:21:57 +01:00
|
|
|
s_port := cmdline.option(current_args, '-p', '')
|
2020-06-07 12:27:42 +02:00
|
|
|
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-12-15 16:21:57 +01:00
|
|
|
i++
|
2020-06-07 12:27:42 +02:00
|
|
|
}
|
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-10-19 21:30:37 +02:00
|
|
|
'-no-timestamp' {
|
|
|
|
cfg.no_timestamp = true
|
|
|
|
}
|
2021-01-06 19:43:43 +01:00
|
|
|
'-no-examples' {
|
|
|
|
cfg.include_examples = false
|
|
|
|
}
|
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 {
|
2020-09-21 16:40:39 +02:00
|
|
|
if cfg.input_path.len < 1 {
|
|
|
|
cfg.input_path = arg
|
|
|
|
} else {
|
|
|
|
cfg.symbol_name = arg
|
|
|
|
}
|
|
|
|
if i == args.len - 1 {
|
|
|
|
break
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cfg.input_path.len == 0 {
|
|
|
|
eprintln('vdoc: No input path found.')
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
if cfg.output_path == 'stdout' && cfg.output_type == .html {
|
|
|
|
cfg.inline_assets = true
|
|
|
|
}
|
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-09-21 16:40:39 +02:00
|
|
|
is_path := cfg.input_path.ends_with('.v') || cfg.input_path.split(os.path_separator).len >
|
|
|
|
1 || cfg.input_path == '.'
|
2020-11-17 15:08:55 +01:00
|
|
|
if cfg.input_path.trim_right('/') == 'vlib' {
|
2020-10-29 17:21:37 +01:00
|
|
|
cfg.is_vlib = true
|
2020-06-05 09:59:26 +02:00
|
|
|
cfg.is_multi = true
|
2020-11-30 18:31:37 +01:00
|
|
|
cfg.input_path = os.join_path(vroot, '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"...')
|
2020-10-21 10:26:33 +02:00
|
|
|
mod_path := doc.lookup_module(cfg.input_path) or {
|
|
|
|
eprintln('vdoc: $err')
|
2020-06-02 12:10:01 +02:00
|
|
|
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
|
|
|
}
|
2021-01-05 01:40:21 +01:00
|
|
|
|
|
|
|
fn is_module_readme(dn doc.DocNode) bool {
|
2021-01-06 19:43:43 +01:00
|
|
|
if dn.comments.len > 0 && dn.content == 'module $dn.name' {
|
2021-01-05 01:40:21 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|