vdoc: add footer, improve accessibility and highlighting

This commit is contained in:
Ned Palacios 2020-06-06 16:43:50 +08:00 committed by GitHub
parent 7815c5b108
commit a19aaf2b90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 227 additions and 227 deletions

View file

@ -14,34 +14,18 @@ import v.vmod
enum HighlightTokenTyp {
unone
atrule
attr_name
bold
boolean
builtin
char
comment
constant
cdata
deleted
doctype
entity
function
important
inserted
italic
keyword
name
number
operator
prolog
property
punctuation
selector
string
symbol
tag
url
}
const (
@ -169,7 +153,7 @@ fn (cfg DocConfig) gen_json(idx int) 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']
highlight_code := fn (tok token.Token, typ HighlightTokenTyp) string {
lit := if typ in [.unone, .operator] { tok.kind.str() } else { tok.lit }
lit := if typ in [.unone, .operator, .punctuation] { tok.kind.str() } else { tok.lit }
return if typ in [.unone, .name] { lit } else { '<span class="token $typ">$lit</span>' }
}
s := scanner.new_scanner(code, .parse_comments)
@ -202,10 +186,13 @@ fn html_highlight(code string, tb &table.Table) string {
.number {
tok_typ = .number
}
.key_true,
.key_false {
.key_true, .key_false {
tok_typ = .boolean
}
.lpar, .lcbr, .rpar, .rcbr, .lsbr,
.rsbr, .semicolon, .colon, .comma, .dot {
tok_typ = .punctuation
}
else {
if token.is_key(tok.lit) || token.is_decl(tok.kind) {
tok_typ = .keyword
@ -240,7 +227,7 @@ fn doc_node_html(dd doc.DocNode, link string, head bool, tb &table.Table) string
if dd.name != 'README' {
dnw.write('<div class="title"><$head_tag>${dd.name} <a href="#${slug(dd.name)}">#</a></$head_tag>')
if link.len != 0 {
dnw.write('<a class="link" target="_blank" href="$link">$link_svg</a>')
dnw.write('<a class="link" rel="noreferrer" target="_blank" href="$link">$link_svg</a>')
}
dnw.write('</div>')
}
@ -256,6 +243,7 @@ fn doc_node_html(dd doc.DocNode, link string, head bool, tb &table.Table) string
fn (cfg DocConfig) gen_html(idx int) string {
dcs := cfg.docs[idx]
time_gen := dcs.time_generated.day.str() + ' ' + dcs.time_generated.smonth() + ' ' + dcs.time_generated.year.str() + ' ' + dcs.time_generated.hhmmss()
mut hw := strings.new_builder(200)
mut toc := strings.new_builder(200)
// generate toc first
@ -313,7 +301,7 @@ fn (cfg DocConfig) gen_html(idx int) string {
<div class="module">${header_name}</div>
<div class="toggle-version-container">
<span>${version}</span>
<div id="dark-mode-toggle" role="checkbox">$light_icon $dark_icon</div>
<div id="dark-mode-toggle" role="switch" aria-checked="false" aria-label="Toggle dark mode">$light_icon $dark_icon</div>
</div>
$menu_icon
</div>
@ -380,7 +368,7 @@ fn (cfg DocConfig) gen_html(idx int) string {
}
}
}
hw.write('\n</div>\n')
hw.write('\n<div class="footer">Powered by vdoc. Generated on: $time_gen</div>\n</div>\n')
if cfg.is_multi && cfg.docs.len > 1 && dcs.head.name != 'README' {
hw.write('<div class="doc-toc">\n\n<ul>\n${toc.str()}</ul>\n</div>')
}
@ -537,10 +525,9 @@ fn (mut cfg DocConfig) generate_docs_from_file() {
panic(err)
}
} else {
os.rm(os.join_path(cfg.output_path, 'doc.css'))
os.rm(os.join_path(cfg.output_path, 'v-prism.css'))
os.rm(os.join_path(cfg.output_path, 'v-prism.js'))
os.rm(os.join_path(cfg.output_path, 'doc.js'))
for fname in ['doc.css', 'v-prism.css', 'doc.js'] {
os.rm(os.join_path(cfg.output_path, fname))
}
}
}
outputs := cfg.render()