From 5b1ab3b0bb956973284ceffbad31e737eee15087 Mon Sep 17 00:00:00 2001 From: Ned Palacios Date: Wed, 21 Oct 2020 16:26:33 +0800 Subject: [PATCH] vdoc: use maps, enum-based categorization; fixes (#6659) --- cmd/tools/vdoc-resources/doc.css | 8 +- cmd/tools/vdoc.v | 251 ++++++++++-------------- vlib/v/doc/doc.v | 319 +++++++++++++++++-------------- vlib/v/doc/doc_test.v | 26 ++- 4 files changed, 305 insertions(+), 299 deletions(-) diff --git a/cmd/tools/vdoc-resources/doc.css b/cmd/tools/vdoc-resources/doc.css index 657cd92c26..c8c389eb78 100644 --- a/cmd/tools/vdoc-resources/doc.css +++ b/cmd/tools/vdoc-resources/doc.css @@ -274,6 +274,9 @@ body { word-break: break-all; /* IE11 */ word-break: break-word; } +.doc-content > .doc-node.const:nth-child(2) { + padding-bottom: 0 !important; +} .doc-content > .doc-node.const:not(:first-child) { padding-top: 4rem; } @@ -417,7 +420,7 @@ pre, code, pre code { background-color: #edf2f7; background-color: var(--code-background-color); border-radius: 0.25rem; -} +} pre code { direction: ltr; text-align: left; @@ -556,6 +559,9 @@ pre { .doc-nav .content.hidden { display: flex; } + .doc-content > .doc-node.const:nth-child(2) { + padding-bottom: 0 !important; + } .doc-content > .doc-node.const:not(:first-child) { padding-top: 0; } diff --git a/cmd/tools/vdoc.v b/cmd/tools/vdoc.v index ece7982303..9d085f331b 100644 --- a/cmd/tools/vdoc.v +++ b/cmd/tools/vdoc.v @@ -15,6 +15,7 @@ import v.table import v.token import v.vmod import v.pref +import json enum HighlightTokenTyp { unone @@ -124,10 +125,12 @@ struct ParallelDoc { i int } +[inline] fn slug(title string) string { return title.replace(' ', '-') } +[inline] fn open_url(url string) { $if windows { os.system('start $url') @@ -149,7 +152,6 @@ fn (mut cfg DocConfig) serve_html() { exit(1) } def_name := docs.keys()[0] - // server_url := 'http://localhost:' + cfg.server_port.str() server := net.listen(cfg.server_port) or { panic(err) @@ -268,7 +270,9 @@ fn js_compress(str string) string { } js.write(trimmed) } - return js.str() + js_str := js.str() + js.free() + return js_str } fn escape(str string) string { @@ -278,29 +282,9 @@ fn escape(str string) string { fn (cfg DocConfig) gen_json(idx int) string { dcs := cfg.docs[idx] mut jw := strings.new_builder(200) - jw.write('{"module_name":"$dcs.head.name","description":"${escape(dcs.head.comment)}","contents":[') - for i, cn in dcs.contents { - name := cn.name.all_after(dcs.head.name) - jw.write('{"name":"$name","signature":"${escape(cn.content)}",') - jw.write('"description":"${escape(cn.comment)}",') - jw.write('"position":[$cn.pos.line,$cn.pos.col,$cn.pos.len],') - jw.write('"file_path":"$cn.file_path",') - jw.write('"attrs":{') - mut j := 0 - for n, v in cn.attrs { - jw.write('"$n":"$v"') - if j < cn.attrs.len - 1 { - jw.write(',') - } - j++ - } - jw.write('}') - jw.write('}') - if i < dcs.contents.len - 1 { - jw.write(',') - } - } - jw.write('],"generator":"vdoc","time_generated":"$dcs.time_generated.str()"}') + jw.write('{"module_name":"$dcs.head.name","description":"${escape(dcs.head.comment)}","contents":') + jw.write(json.encode(dcs.contents.keys().map(dcs.contents[it]))) + jw.write(',"generator":"vdoc","time_generated":"$dcs.time_generated.str()"}') return jw.str() } @@ -392,27 +376,27 @@ fn doc_node_html(dd doc.DocNode, link string, head bool, tb &table.Table) string head_tag := if head { 'h1' } else { 'h2' } md_content := markdown.to_html(dd.comment) hlighted_code := html_highlight(dd.content, tb) - node_class := if dd.name == 'Constants' { ' const' } else { '' } - sym_name := if dd.attrs.exists('parent') && dd.attrs['parent'] !in ['void', '', 'Constants'] { dd.attrs['parent'] + - '.' + dd.name } else { dd.name } + node_class := if dd.kind == .const_group { ' const' } else { '' } + sym_name := if dd.parent_name.len > 0 && dd.parent_name != 'void' { dd.parent_name + '.' + dd.name } else { dd.name } node_id := slug(sym_name) hash_link := if !head { ' #' } else { '' } dnw.writeln('
') - if dd.name != 'README' && dd.attrs['parent'] != 'Constants' { + if dd.name.len > 0 { dnw.write('
<$head_tag>$sym_name$hash_link') if link.len != 0 { dnw.write('$link_svg') } dnw.write('
') } - if head { - dnw.write(md_content) - } else { + if !head && dd.content.len > 0 { dnw.writeln('
$hlighted_code
') - dnw.writeln(md_content) } - dnw.writeln('
') - return dnw.str() + dnw.writeln('$md_content\n') + dnw_str := dnw.str() + defer { + dnw.free() + } + return dnw_str } fn (cfg DocConfig) readme_idx() int { @@ -426,12 +410,11 @@ fn (cfg DocConfig) readme_idx() int { } fn write_toc(cn doc.DocNode, nodes []doc.DocNode, mut toc strings.Builder) { - toc_slug := if cn.content.len == 0 { '' } else { slug(cn.name) } + toc_slug := if cn.name.len == 0 { '' } else { slug(cn.name) } toc.write('
  • $cn.name') - children := nodes.find_children_of(cn.name) if cn.name != 'Constants' { toc.writeln('