v doc: add attribute tags to `v doc -f html` (#10778)

pull/10786/head
shadowninja55 2021-07-13 01:27:20 -04:00 committed by GitHub
parent 7694afa44c
commit d1f1c5ae51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 6 deletions

View File

@ -438,9 +438,12 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
html_tag_escape(comments) html_tag_escape(comments)
} }
md_content := markdown.to_html(escaped_html) md_content := markdown.to_html(escaped_html)
hlighted_code := html_highlight(dn.content, tb) highlighted_code := html_highlight(dn.content, tb)
node_class := if dn.kind == .const_group { ' const' } else { '' } node_class := if dn.kind == .const_group { ' const' } else { '' }
sym_name := get_sym_name(dn) sym_name := get_sym_name(dn)
mut tags := dn.tags
tags.sort()
tags_str := ' ' + tags.map('<span class="$it-attribute-tag">[$it]</span>').join('')
mut node_id := get_node_id(dn) mut node_id := get_node_id(dn)
mut hash_link := if !head { ' <a href="#$node_id">#</a>' } else { '' } mut hash_link := if !head { ' <a href="#$node_id">#</a>' } else { '' }
if head && is_module_readme(dn) { if head && is_module_readme(dn) {
@ -450,9 +453,9 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
dnw.writeln('${tabs[1]}<section id="$node_id" class="doc-node$node_class">') dnw.writeln('${tabs[1]}<section id="$node_id" class="doc-node$node_class">')
if dn.name.len > 0 { if dn.name.len > 0 {
if dn.kind == .const_group { if dn.kind == .const_group {
dnw.write_string('${tabs[2]}<div class="title"><$head_tag>$sym_name$hash_link</$head_tag>') dnw.write_string('${tabs[2]}<div class="title"><$head_tag>$sym_name$tags_str$hash_link</$head_tag>')
} else { } else {
dnw.write_string('${tabs[2]}<div class="title"><$head_tag>$dn.kind $sym_name$hash_link</$head_tag>') dnw.write_string('${tabs[2]}<div class="title"><$head_tag>$dn.kind $sym_name$tags_str<$hash_link/$head_tag>')
} }
if link.len != 0 { if link.len != 0 {
dnw.write_string('<a class="link" rel="noreferrer" target="_blank" href="$link">$link_svg</a>') dnw.write_string('<a class="link" rel="noreferrer" target="_blank" href="$link">$link_svg</a>')
@ -460,7 +463,7 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
dnw.write_string('</div>') dnw.write_string('</div>')
} }
if !head && dn.content.len > 0 { if !head && dn.content.len > 0 {
dnw.writeln('<pre class="signature"><code>$hlighted_code</code></pre>') dnw.writeln('<pre class="signature"><code>$highlighted_code</code></pre>')
} }
// do not mess with md_content further, its formatting is important, just output it 1:1 ! // do not mess with md_content further, its formatting is important, just output it 1:1 !
dnw.writeln('$md_content\n') dnw.writeln('$md_content\n')

View File

@ -578,6 +578,11 @@ pre {
color: var(--code-function-text-color); color: var(--code-function-text-color);
} }
/* Attribute tag colors */
.attribute-tag {
color: red !important;
}
/* Medium screen and up */ /* Medium screen and up */
@media (min-width: 768px) { @media (min-width: 768px) {
*::-webkit-scrollbar { *::-webkit-scrollbar {

View File

@ -370,6 +370,7 @@ pub:
is_conditional bool // true for `[if abc] fn abc(){}` is_conditional bool // true for `[if abc] fn abc(){}`
is_exported bool // true for `[export: 'exact_C_name']` is_exported bool // true for `[export: 'exact_C_name']`
is_keep_alive bool // passed memory must not be freed (by GC) before function returns is_keep_alive bool // passed memory must not be freed (by GC) before function returns
is_unsafe bool // true, when [unsafe] is used on a fn
receiver StructField // TODO this is not a struct field receiver StructField // TODO this is not a struct field
receiver_pos token.Position // `(u User)` in `fn (u User) name()` position receiver_pos token.Position // `(u User)` in `fn (u User) name()` position
is_method bool is_method bool

View File

@ -127,7 +127,7 @@ pub mut:
pos token.Position pos token.Position
file_path string file_path string
kind SymbolKind kind SymbolKind
deprecated bool tags []string
parent_name string parent_name string
return_type string return_type string
children []DocNode children []DocNode
@ -257,7 +257,15 @@ pub fn (mut d Doc) stmt(stmt ast.Stmt, filename string) ?DocNode {
node.kind = .typedef node.kind = .typedef
} }
ast.FnDecl { ast.FnDecl {
node.deprecated = stmt.is_deprecated if stmt.is_deprecated {
node.tags << 'deprecated'
}
if stmt.is_unsafe {
node.tags << 'unsafe'
}
if node.tags.len > 0 {
eprintln(node.tags)
}
node.kind = .function node.kind = .function
node.return_type = d.type_to_str(stmt.return_type) node.return_type = d.type_to_str(stmt.return_type)
if stmt.receiver.typ !in [0, 1] { if stmt.receiver.typ !in [0, 1] {

View File

@ -467,6 +467,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
is_main: is_main is_main: is_main
is_test: is_test is_test: is_test
is_keep_alive: is_keep_alive is_keep_alive: is_keep_alive
is_unsafe: is_unsafe
// //
attrs: p.attrs attrs: p.attrs
is_conditional: conditional_ctdefine_idx != -1 is_conditional: conditional_ctdefine_idx != -1