| 1 | module main |
| 2 | |
| 3 | import document as doc |
| 4 | |
| 5 | @[inline] |
| 6 | fn slug(title string) string { |
| 7 | return title.replace(' ', '-') |
| 8 | } |
| 9 | |
| 10 | fn escape(str string) string { |
| 11 | return str.replace_each(['"', '\\"', '\r\n', '\\n', '\n', '\\n', '\t', '\\t']) |
| 12 | } |
| 13 | |
| 14 | fn get_sym_name(dn doc.DocNode) string { |
| 15 | if dn.is_readme { |
| 16 | if title := dn.frontmatter['title'] { |
| 17 | return title |
| 18 | } |
| 19 | } |
| 20 | if dn.parent_name.len > 0 && dn.parent_name != 'void' { |
| 21 | return '(${dn.parent_name}) ${dn.name}' |
| 22 | } |
| 23 | return dn.name |
| 24 | } |
| 25 | |
| 26 | fn get_node_id(dn doc.DocNode) string { |
| 27 | tag := if dn.parent_name.len > 0 && dn.parent_name != 'void' { |
| 28 | '${dn.parent_name}.${dn.name}' |
| 29 | } else { |
| 30 | dn.name |
| 31 | } |
| 32 | return slug(tag) |
| 33 | } |
| 34 | |
| 35 | fn is_module_readme(dn doc.DocNode) bool { |
| 36 | return dn.is_readme || (dn.comments.len > 0 && dn.content == 'module ${dn.name}') |
| 37 | } |
| 38 | |
| 39 | // trim_doc_node_description returns the nodes trimmed description. |
| 40 | // An example use are the descriptions of the search results in the sidebar. |
| 41 | fn trim_doc_node_description(mod_name string, desc string) string { |
| 42 | mut dn_desc := desc.replace_each(['\r\n', '\n', '"', '\\"']) |
| 43 | // Get the first "descriptive" line. |
| 44 | if dn_desc.starts_with('#') { |
| 45 | // Handle module READMEs. |
| 46 | for l in dn_desc.split_into_lines()[1..] { |
| 47 | if l != '' && !l.starts_with('#') { |
| 48 | quoted_mod_name := '`${mod_name}`' |
| 49 | if l.starts_with(quoted_mod_name) { |
| 50 | // Omit the module name in the description as it is redundant since the name is displayed as well. |
| 51 | // "`arrays` is a module that..." -> "is a module that..." |
| 52 | dn_desc = l.all_after(quoted_mod_name).trim_left(' ') |
| 53 | } else { |
| 54 | dn_desc = l |
| 55 | } |
| 56 | break |
| 57 | } |
| 58 | } |
| 59 | } else { |
| 60 | dn_desc = dn_desc.all_before('\n') |
| 61 | } |
| 62 | // 80 is enough to fill one line. |
| 63 | if dn_desc.len > 80 { |
| 64 | dn_desc = dn_desc[..80] |
| 65 | } |
| 66 | // If `\` is the last character, it ends with `\"` which leads to a JS error. |
| 67 | return dn_desc.trim_string_right('\\') |
| 68 | } |
| 69 | |
| 70 | fn set_output_type_from_str(format string) OutputType { |
| 71 | return match format { |
| 72 | 'htm', 'html' { OutputType.html } |
| 73 | 'md', 'markdown' { .markdown } |
| 74 | 'json' { .json } |
| 75 | 'text' { .plaintext } |
| 76 | 'none' { .none } |
| 77 | else { .ansi } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | fn gen_footer_text(d &doc.Doc, include_timestamp bool) string { |
| 82 | footer_text := 'Powered by vdoc.' |
| 83 | if !include_timestamp { |
| 84 | return footer_text |
| 85 | } |
| 86 | generated_time := d.time_generated |
| 87 | time_str := '${generated_time.day} ${generated_time.smonth()} ${generated_time.year} ${generated_time.hhmmss()}' |
| 88 | return '${footer_text} Generated on: ${time_str}' |
| 89 | } |
| 90 | |