module main import os import net.urllib import strings import markdown import regex import v.scanner import v.ast import v.token import v.doc import v.pref import v.util { tabs } const ( css_js_assets = ['doc.css', 'normalize.css', 'doc.js', 'dark-mode.js'] default_theme = os.resource_abs_path('theme') link_svg = '' single_quote = "'" double_quote = '"' no_quotes_replacement = [single_quote, '', double_quote, ''] ) enum HighlightTokenTyp { unone boolean builtin char comment function keyword name number operator punctuation string symbol none_ module_ prefix } struct SearchModuleResult { description string link string } struct SearchResult { prefix string badge string description string link string } fn (vd VDoc) render_search_index(out Output) { mut js_search_index := strings.new_builder(200) mut js_search_data := strings.new_builder(200) js_search_index.write_string('var searchModuleIndex = [') js_search_data.write_string('var searchModuleData = [') for i, title in vd.search_module_index { data := vd.search_module_data[i] js_search_index.write_string('"${title}",') js_search_data.write_string('["${data.description}","${data.link}"],') } js_search_index.writeln('];') js_search_index.write_string('var searchIndex = [') js_search_data.writeln('];') js_search_data.write_string('var searchData = [') for i, title in vd.search_index { data := vd.search_data[i] js_search_index.write_string('"${title}",') // array instead of object to reduce file size js_search_data.write_string('["${data.badge}","${data.description}","${data.link}","${data.prefix}"],') } js_search_index.writeln('];') js_search_data.writeln('];') out_file_path := os.join_path(out.path, 'search_index.js') os.write_file(out_file_path, js_search_index.str() + js_search_data.str()) or { panic(err) } } fn (mut vd VDoc) render_static_html(out Output) { vd.assets = { 'doc_css': vd.get_resource(css_js_assets[0], out) 'normalize_css': vd.get_resource(css_js_assets[1], out) 'doc_js': vd.get_resource(css_js_assets[2], out) 'dark_mode_js': vd.get_resource(css_js_assets[3], out) 'light_icon': vd.get_resource('light.svg', out) 'dark_icon': vd.get_resource('dark.svg', out) 'menu_icon': vd.get_resource('menu.svg', out) 'arrow_icon': vd.get_resource('arrow.svg', out) } } fn (vd VDoc) get_resource(name string, out Output) string { cfg := vd.cfg path := os.join_path(cfg.theme_dir, name) mut res := os.read_file(path) or { panic('vdoc: could not read ${path}') } /* if minify { if name.ends_with('.js') { res = js_compress(res) } else { res = res.split_into_lines().map(it.trim_space()).join('') } } */ // TODO: Make SVG inline for now if cfg.inline_assets || path.ends_with('.svg') { return res } else { output_path := os.join_path(out.path, name) if !os.exists(output_path) { println('Generating ${out.typ} in "${output_path}"') os.write_file(output_path, res) or { panic(err) } } return name } } fn (mut vd VDoc) collect_search_index(out Output) { cfg := vd.cfg for doc in vd.docs { mod := doc.head.name vd.search_module_index << mod comments := if cfg.include_examples { doc.head.merge_comments() } else { doc.head.merge_comments_without_examples() } vd.search_module_data << SearchModuleResult{ description: trim_doc_node_description(comments) link: vd.get_file_name(mod, out) } for _, dn in doc.contents { vd.create_search_results(mod, dn, out) } } } fn (mut vd VDoc) create_search_results(mod string, dn doc.DocNode, out Output) { cfg := vd.cfg if dn.kind == .const_group { return } comments := if cfg.include_examples { dn.merge_comments() } else { dn.merge_comments_without_examples() } dn_description := trim_doc_node_description(comments) vd.search_index << dn.name vd.search_data << SearchResult{ prefix: if dn.parent_name != '' { '${dn.kind} (${dn.parent_name})' } else { '${dn.kind} ' } description: dn_description badge: mod link: vd.get_file_name(mod, out) + '#' + get_node_id(dn) } for child in dn.children { vd.create_search_results(mod, child, out) } } fn (vd VDoc) write_content(cn &doc.DocNode, d &doc.Doc, mut hw strings.Builder) { cfg := vd.cfg base_dir := os.dir(os.real_path(cfg.input_path)) file_path_name := if cfg.is_multi { cn.file_path.replace('${base_dir}/', '') } else { os.file_name(cn.file_path) } src_link := get_src_link(vd.manifest.repo_url, file_path_name, cn.pos.line_nr + 1) if cn.content.len != 0 || cn.name == 'Constants' { hw.write_string(doc_node_html(cn, src_link, false, cfg.include_examples, d.table)) } for child in cn.children { child_file_path_name := child.file_path.replace('${base_dir}/', '') child_src_link := get_src_link(vd.manifest.repo_url, child_file_path_name, child.pos.line_nr + 1) hw.write_string(doc_node_html(child, child_src_link, false, cfg.include_examples, d.table)) } } fn (vd VDoc) gen_html(d doc.Doc) string { cfg := vd.cfg mut symbols_toc := strings.new_builder(200) mut modules_toc := strings.new_builder(200) mut contents := strings.new_builder(200) dcs_contents := d.contents.arr() // generate toc first contents.writeln(doc_node_html(d.head, '', true, cfg.include_examples, d.table)) if is_module_readme(d.head) { write_toc(d.head, mut symbols_toc) } for cn in dcs_contents { vd.write_content(&cn, &d, mut contents) write_toc(cn, mut symbols_toc) } // write head // write css mut version := if vd.manifest.version.len != 0 { vd.manifest.version } else { '' } version = [version, @VHASH].join(' ') header_name := if cfg.is_multi && vd.docs.len > 1 { os.file_name(os.real_path(cfg.input_path)) } else { d.head.name } // write nav1 if cfg.is_multi || vd.docs.len > 1 { mut submod_prefix := '' for i, dc in vd.docs { if i - 1 >= 0 && dc.head.name.starts_with(submod_prefix + '.') { continue } names := dc.head.name.split('.') submod_prefix = if names.len > 1 { names[0] } else { dc.head.name } mut href_name := './${dc.head.name}.html' if (cfg.is_vlib && dc.head.name == 'builtin' && !cfg.include_readme) || dc.head.name == 'README' { href_name = './index.html' } else if submod_prefix !in vd.docs.map(it.head.name) { href_name = '#' } submodules := vd.docs.filter(it.head.name.starts_with(submod_prefix + '.')) dropdown := if submodules.len > 0 { vd.assets['arrow_icon'] } else { '' } active_class := if dc.head.name == d.head.name { ' active' } else { '' } modules_toc.write_string('
${highlighted_code}
')
}
// do not mess with md_content further, its formatting is important, just output it 1:1 !
dnw.writeln('${md_content}\n')
// Write examples if any found
examples := dn.examples()
if include_examples && examples.len > 0 {
example_title := if examples.len > 1 { 'Examples' } else { 'Example' }
dnw.writeln('${hl_example}
')
}
dnw.writeln('