doc: fix compilation and format

pull/5801/head
Alexander Medvednikov 2020-07-11 12:04:05 +02:00
parent d94a02ba79
commit 54da8371ac
1 changed files with 28 additions and 27 deletions

View File

@ -12,6 +12,7 @@ import v.scanner
import v.table import v.table
import v.token import v.token
import v.vmod import v.vmod
import v.pref
enum HighlightTokenTyp { enum HighlightTokenTyp {
unone unone
@ -43,7 +44,7 @@ const (
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge" /> <meta http-equiv="x-ua-compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ title }} | vdoc</title> <title>{{ title }} | vdoc</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
{{ head_assets }} {{ head_assets }}
</head> </head>
@ -128,7 +129,7 @@ fn open_url(url string) {
fn (mut cfg DocConfig) serve_html() { fn (mut cfg DocConfig) serve_html() {
docs := cfg.render() docs := cfg.render()
dkeys := docs.keys() dkeys := docs.keys()
if dkeys.len < 1 { if dkeys.len < 1 {
eprintln('no documentation created, the module has no `pub` functions') eprintln('no documentation created, the module has no `pub` functions')
exit(1) exit(1)
@ -275,16 +276,16 @@ fn (cfg DocConfig) gen_json(idx int) string {
fn html_highlight(code string, tb &table.Table) string { 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'] 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 { highlight_code := fn (tok token.Token, typ HighlightTokenTyp) string {
lit := if typ in [.unone, .operator, .punctuation] { lit := if typ in [.unone, .operator, .punctuation] {
tok.kind.str() tok.kind.str()
} else if typ == .string { } else if typ == .string {
"'$tok.lit'" "'$tok.lit'"
} else if typ == .char { } else if typ == .char {
'`$tok.lit`' '`$tok.lit`'
} else { tok.lit } } else { tok.lit }
return if typ in [.unone, .name] { lit } else { '<span class="token $typ">$lit</span>' } return if typ in [.unone, .name] { lit } else { '<span class="token $typ">$lit</span>' }
} }
s := scanner.new_scanner(code, .parse_comments, false) s := scanner.new_scanner(code, .parse_comments, &pref.Preferences{})
mut tok := s.scan() mut tok := s.scan()
mut next_tok := s.scan() mut next_tok := s.scan()
mut buf := strings.new_builder(200) mut buf := strings.new_builder(200)
@ -319,7 +320,7 @@ fn html_highlight(code string, tb &table.Table) string {
.key_true, .key_false { .key_true, .key_false {
tok_typ = .boolean tok_typ = .boolean
} }
.lpar, .lcbr, .rpar, .rcbr, .lsbr, .lpar, .lcbr, .rpar, .rcbr, .lsbr,
.rsbr, .semicolon, .colon, .comma, .dot { .rsbr, .semicolon, .colon, .comma, .dot {
tok_typ = .punctuation tok_typ = .punctuation
} }
@ -354,7 +355,7 @@ fn doc_node_html(dd doc.DocNode, link string, head bool, tb &table.Table) string
md_content := markdown.to_html(dd.comment) md_content := markdown.to_html(dd.comment)
hlighted_code := html_highlight(dd.content, tb) hlighted_code := html_highlight(dd.content, tb)
node_class := if dd.name == 'Constants' { ' const' } else { '' } node_class := if dd.name == 'Constants' { ' const' } else { '' }
sym_name := if dd.attrs.exists('parent') && dd.attrs['parent'] !in ['void', '', 'Constants'] { sym_name := if dd.attrs.exists('parent') && dd.attrs['parent'] !in ['void', '', 'Constants'] {
dd.attrs['parent'] + '.' + dd.name dd.attrs['parent'] + '.' + dd.name
} else { } else {
dd.name dd.name
@ -440,8 +441,8 @@ fn (cfg DocConfig) gen_html(idx int) string {
arrow_icon := cfg.get_resource('arrow.svg', true) arrow_icon := cfg.get_resource('arrow.svg', true)
// write css // write css
version := if cfg.manifest.version.len != 0 { cfg.manifest.version } else { '' } version := if cfg.manifest.version.len != 0 { cfg.manifest.version } else { '' }
header_name := if cfg.is_multi && cfg.docs.len > 1 { header_name := if cfg.is_multi && cfg.docs.len > 1 {
os.file_name(os.real_path(cfg.input_path)) os.file_name(os.real_path(cfg.input_path))
} else { } else {
dcs.head.name dcs.head.name
} }
@ -587,7 +588,7 @@ fn (cfg DocConfig) get_readme(path string) string {
if os.exists(os.join_path(path, '${name}.md')) { if os.exists(os.join_path(path, '${name}.md')) {
fname = name fname = name
break break
} }
} }
if fname == '' { if fname == '' {
return '' return ''
@ -618,10 +619,10 @@ fn (mut cfg DocConfig) generate_docs_from_file() {
is_vlib := 'vlib' in cfg.input_path is_vlib := 'vlib' in cfg.input_path
dir_path := if is_vlib { dir_path := if is_vlib {
vexe_path vexe_path
} else if os.is_dir(cfg.input_path) { } else if os.is_dir(cfg.input_path) {
cfg.input_path cfg.input_path
} else { } else {
os.base_dir(cfg.input_path) os.base_dir(cfg.input_path)
} }
manifest_path := os.join_path(dir_path, 'v.mod') manifest_path := os.join_path(dir_path, 'v.mod')
if os.exists(manifest_path) { if os.exists(manifest_path) {
@ -644,7 +645,7 @@ fn (mut cfg DocConfig) generate_docs_from_file() {
} }
} }
} }
dirs := if cfg.is_multi { get_modules_list(cfg.input_path, []string{}) } else { [cfg.input_path] } dirs := if cfg.is_multi { get_modules_list(cfg.input_path, []string{}) } else { [cfg.input_path] }
for dirpath in dirs { for dirpath in dirs {
cfg.vprintln('Generating docs for ${dirpath}...') cfg.vprintln('Generating docs for ${dirpath}...')
mut dcs := doc.generate(dirpath, cfg.pub_only, true) or { mut dcs := doc.generate(dirpath, cfg.pub_only, true) or {
@ -667,7 +668,7 @@ fn (mut cfg DocConfig) generate_docs_from_file() {
} }
if cfg.pub_only { if cfg.pub_only {
for i, c in dcs.contents { for i, c in dcs.contents {
dcs.contents[i].content = c.content.all_after('pub ') dcs.contents[i].content = c.content.all_after('pub ')
} }
} }
cfg.docs << dcs cfg.docs << dcs
@ -722,20 +723,20 @@ fn (mut cfg DocConfig) generate_docs_from_file() {
fn (mut cfg DocConfig) set_output_type_from_str(format string) { fn (mut cfg DocConfig) set_output_type_from_str(format string) {
match format { match format {
'htm', 'html' { 'htm', 'html' {
cfg.output_type = .html cfg.output_type = .html
} }
'md', 'markdown' { 'md', 'markdown' {
cfg.output_type = .markdown cfg.output_type = .markdown
} }
'json' { 'json' {
cfg.output_type = .json cfg.output_type = .json
} }
'stdout' { 'stdout' {
cfg.output_type = .stdout cfg.output_type = .stdout
} }
else { else {
cfg.output_type = .plaintext cfg.output_type = .plaintext
} }
} }
cfg.vprintln('Setting output type to "$cfg.output_type"') cfg.vprintln('Setting output type to "$cfg.output_type"')
@ -819,8 +820,8 @@ fn (cfg DocConfig) get_resource(name string, minify bool) string {
mut res := os.read_file(path) or { panic('could not read $path') } mut res := os.read_file(path) or { panic('could not read $path') }
if minify { if minify {
if name.ends_with('.js') { if name.ends_with('.js') {
res = js_compress(res) res = js_compress(res)
} else { } else {
res = res.split_into_lines().map(it.trim_space()).join('') res = res.split_into_lines().map(it.trim_space()).join('')
} }
} }