From 0b2feefd870315dc704cbb8b434caa8a008e2161 Mon Sep 17 00:00:00 2001 From: Ned <7358345+nedpals@users.noreply.github.com> Date: Thu, 30 Nov 2023 00:03:58 +0800 Subject: [PATCH] vdoc: use `markdown.default_html_transformer` underneath, remove `html_tag_escape` (#19785) --- cmd/tools/vdoc/html.v | 44 +++---------------- cmd/tools/vdoc/html_tag_escape_test.v | 6 --- .../tests/testdata/output_formats/README.md | 4 ++ .../tests/testdata/output_formats/main.ansi | 8 +++- .../tests/testdata/output_formats/main.html | 4 +- .../tests/testdata/output_formats/main.text | 6 ++- .../vdoc/tests/testdata/output_formats/main.v | 4 ++ 7 files changed, 28 insertions(+), 48 deletions(-) delete mode 100644 cmd/tools/vdoc/html_tag_escape_test.v diff --git a/cmd/tools/vdoc/html.v b/cmd/tools/vdoc/html.v index 38f52972d3268f..15579833d18951 100644 --- a/cmd/tools/vdoc/html.v +++ b/cmd/tools/vdoc/html.v @@ -4,7 +4,6 @@ import os import net.urllib import strings import markdown -import regex import v.scanner import v.ast import v.token @@ -19,9 +18,6 @@ const link_svg = '', '>'] const md_script_escape_seq = ['', '`'] enum HighlightTokenTyp { @@ -508,7 +504,7 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool, // Strip markdown [TOC] directives, since we generate our own. comments.replace('[TOC]', '').replace_each(md_script_escape_seq) } else { - html_tag_escape(comments) + comments } mut renderer := markdown.HtmlRenderer{ transformer: &MdHtmlCodeHighlighter{ @@ -574,37 +570,6 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool, return dnw_str } -fn html_tag_escape(str string) string { - escaped_string := str.replace_each(html_tag_escape_seq) - mut re := html_tag_escape_re - if re.find_all_str(escaped_string).len > 0 { - return str - } - return escaped_string -} - -/* -fn js_compress(str string) string { - mut js := strings.new_builder(200) - lines := str.split_into_lines() - rules := [') {', ' = ', ', ', '{ ', ' }', ' (', '; ', ' + ', ' < ', ' - ', ' || ', ' var', - ': ', ' >= ', ' && ', ' else if', ' === ', ' !== ', ' else '] - clean := ['){', '=', ',', '{', '}', '(', ';', '+', '<', '-', '||', 'var', ':', '>=', '&&', - 'else if', '===', '!==', 'else'] - for line in lines { - mut trimmed := line.trim_space() - if trimmed.starts_with('//') || (trimmed.starts_with('/*') && trimmed.ends_with('*/')) { - continue - } - for i in 0 .. rules.len - 1 { - trimmed = trimmed.replace(rules[i], clean[i]) - } - js.write_string(trimmed) - } - js_str := js.str() - return js_str -} -*/ fn write_toc(dn doc.DocNode, mut toc strings.Builder) { mut toc_slug := if dn.name.len == 0 || dn.content.len == 0 { '' } else { slug(dn.name) } if toc_slug == '' && dn.children.len > 0 { @@ -648,12 +613,15 @@ fn (f &MdHtmlCodeHighlighter) transform_attribute(p markdown.ParentType, name st } fn (f &MdHtmlCodeHighlighter) transform_content(parent markdown.ParentType, text string) string { + // NOTE: markdown.default_html_transformer uses html.escape internally. + initial_transformed_text := markdown.default_html_transformer.transform_content(parent, + text) if parent is markdown.MD_BLOCKTYPE && parent == .md_block_code { if f.language == 'v' || f.language == 'vlang' { - return html_highlight(html_tag_escape(text), f.table) + return html_highlight(initial_transformed_text, f.table) } } - return markdown.default_html_transformer.transform_content(parent, html_tag_escape(text)) + return initial_transformed_text } fn (mut f MdHtmlCodeHighlighter) config_set(key string, val string) { diff --git a/cmd/tools/vdoc/html_tag_escape_test.v b/cmd/tools/vdoc/html_tag_escape_test.v deleted file mode 100644 index ed9c95bbcf634c..00000000000000 --- a/cmd/tools/vdoc/html_tag_escape_test.v +++ /dev/null @@ -1,6 +0,0 @@ -module main - -fn test_html_tag_escape() { - assert html_tag_escape('abc bold 123') == 'abc <b>bold</b> 123' - assert html_tag_escape('`abc bold 123`') == '`abc bold 123`' -} diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/README.md b/cmd/tools/vdoc/tests/testdata/output_formats/README.md index 1dd672c8ad876c..c139a666ea083b 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/README.md +++ b/cmd/tools/vdoc/tests/testdata/output_formats/README.md @@ -5,6 +5,10 @@ documentation in a project or module. This is a [link](https://vlang.io/) to the main V site. +This is a bold text. + +This is a script . + ## Examples: ### Processing command line args: diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi b/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi index b50c979651ff76..dd981289565112 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi +++ b/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi @@ -2,6 +2,8 @@ Description: This is an example of a an .md file, used for adding more rich text documentation in a project or module. This is a link to the main V site. +This is a bold text. +This is a script console.log('hi from README.md'); . Examples: Processing command line args: import os @@ -65,6 +67,10 @@ fn auth_verify(secret string, token string) bool { This is a [link](https://vlang.io/) to the main V site. + This is a bold text. + + This is a script . + ## Examples: ### Processing command line args: @@ -135,7 +141,7 @@ fn auth_verify(secret string, token string) bool { fn def() def - should be first fn xyz() - xyz - should be in the middle + xyz - should be in the middle a small script bold text bold end underlined text underline end a link [main v repo](https://github.com/vlang/v) fn MyXMLDocument.abc(text string) ?(string, int) MyXMLDocument.abc does something too... I just do not know what. fn MyXMLDocument.from_file(path string) !MyXMLDocument diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/main.html b/cmd/tools/vdoc/tests/testdata/output_formats/main.html index 49ffe334026681..d7199097fdc56c 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/main.html +++ b/cmd/tools/vdoc/tests/testdata/output_formats/main.html @@ -1,5 +1,5 @@
-

main #

Description:

This is an example of a an .md file, used for adding more rich text documentation in a project or module.

This is a link to the main V site.

Examples:

Processing command line args:

import os
+			

main #

Description:

This is an example of a an .md file, used for adding more rich text documentation in a project or module.

This is a link to the main V site.

This is a bold text.

This is a script console.log('hi from README.md'); .

Examples:

Processing command line args:

import os
 
 fn main() {
 dump(os.args)
@@ -83,7 +83,7 @@
 

fn xyz #

fn xyz()
-

xyz - should be in the middle

+

xyz - should be in the middle a small script bold text bold end underlined text underline end a link main v repo

diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/main.text b/cmd/tools/vdoc/tests/testdata/output_formats/main.text index 15d90af404ddc7..b837d2c3397671 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/main.text +++ b/cmd/tools/vdoc/tests/testdata/output_formats/main.text @@ -6,6 +6,10 @@ module main This is a [link](https://vlang.io/) to the main V site. + This is a bold text. + + This is a script . + ## Examples: ### Processing command line args: @@ -76,7 +80,7 @@ fn abc() fn def() def - should be first fn xyz() - xyz - should be in the middle + xyz - should be in the middle a small script bold text bold end underlined text underline end a link [main v repo](https://github.com/vlang/v) fn MyXMLDocument.abc(text string) ?(string, int) MyXMLDocument.abc does something too... I just do not know what. fn MyXMLDocument.from_file(path string) !MyXMLDocument diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/main.v b/cmd/tools/vdoc/tests/testdata/output_formats/main.v index 791928b382a2ac..f6e3527d235269 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/main.v +++ b/cmd/tools/vdoc/tests/testdata/output_formats/main.v @@ -10,6 +10,10 @@ pub fn def() { } // xyz - should be in the middle +// a small script +// bold text bold end +// underlined text underline end +// a link [main v repo](https://github.com/vlang/v) pub fn xyz() { println(2) }