Skip to content

Commit 0b2feef

Browse files
authored
vdoc: use markdown.default_html_transformer underneath, remove html_tag_escape (#19785)
1 parent 883eaa6 commit 0b2feef

File tree

7 files changed

+28
-48
lines changed

7 files changed

+28
-48
lines changed

cmd/tools/vdoc/html.v

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import os
44
import net.urllib
55
import strings
66
import markdown
7-
import regex
87
import v.scanner
98
import v.ast
109
import v.token
@@ -19,9 +18,6 @@ const link_svg = '<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0
1918
const single_quote = "'"
2019
const double_quote = '"'
2120
const no_quotes_replacement = [single_quote, '', double_quote, '']
22-
23-
const html_tag_escape_re = regex.regex_opt(r'`.+[(&lt;)(&gt;)].+`') or { panic(err) }
24-
const html_tag_escape_seq = ['<', '&lt;', '>', '&gt;']
2521
const md_script_escape_seq = ['<script>', '`', '</script>', '`']
2622

2723
enum HighlightTokenTyp {
@@ -508,7 +504,7 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
508504
// Strip markdown [TOC] directives, since we generate our own.
509505
comments.replace('[TOC]', '').replace_each(md_script_escape_seq)
510506
} else {
511-
html_tag_escape(comments)
507+
comments
512508
}
513509
mut renderer := markdown.HtmlRenderer{
514510
transformer: &MdHtmlCodeHighlighter{
@@ -574,37 +570,6 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
574570
return dnw_str
575571
}
576572

577-
fn html_tag_escape(str string) string {
578-
escaped_string := str.replace_each(html_tag_escape_seq)
579-
mut re := html_tag_escape_re
580-
if re.find_all_str(escaped_string).len > 0 {
581-
return str
582-
}
583-
return escaped_string
584-
}
585-
586-
/*
587-
fn js_compress(str string) string {
588-
mut js := strings.new_builder(200)
589-
lines := str.split_into_lines()
590-
rules := [') {', ' = ', ', ', '{ ', ' }', ' (', '; ', ' + ', ' < ', ' - ', ' || ', ' var',
591-
': ', ' >= ', ' && ', ' else if', ' === ', ' !== ', ' else ']
592-
clean := ['){', '=', ',', '{', '}', '(', ';', '+', '<', '-', '||', 'var', ':', '>=', '&&',
593-
'else if', '===', '!==', 'else']
594-
for line in lines {
595-
mut trimmed := line.trim_space()
596-
if trimmed.starts_with('//') || (trimmed.starts_with('/*') && trimmed.ends_with('*/')) {
597-
continue
598-
}
599-
for i in 0 .. rules.len - 1 {
600-
trimmed = trimmed.replace(rules[i], clean[i])
601-
}
602-
js.write_string(trimmed)
603-
}
604-
js_str := js.str()
605-
return js_str
606-
}
607-
*/
608573
fn write_toc(dn doc.DocNode, mut toc strings.Builder) {
609574
mut toc_slug := if dn.name.len == 0 || dn.content.len == 0 { '' } else { slug(dn.name) }
610575
if toc_slug == '' && dn.children.len > 0 {
@@ -648,12 +613,15 @@ fn (f &MdHtmlCodeHighlighter) transform_attribute(p markdown.ParentType, name st
648613
}
649614

650615
fn (f &MdHtmlCodeHighlighter) transform_content(parent markdown.ParentType, text string) string {
616+
// NOTE: markdown.default_html_transformer uses html.escape internally.
617+
initial_transformed_text := markdown.default_html_transformer.transform_content(parent,
618+
text)
651619
if parent is markdown.MD_BLOCKTYPE && parent == .md_block_code {
652620
if f.language == 'v' || f.language == 'vlang' {
653-
return html_highlight(html_tag_escape(text), f.table)
621+
return html_highlight(initial_transformed_text, f.table)
654622
}
655623
}
656-
return markdown.default_html_transformer.transform_content(parent, html_tag_escape(text))
624+
return initial_transformed_text
657625
}
658626

659627
fn (mut f MdHtmlCodeHighlighter) config_set(key string, val string) {

cmd/tools/vdoc/html_tag_escape_test.v

Lines changed: 0 additions & 6 deletions
This file was deleted.

cmd/tools/vdoc/tests/testdata/output_formats/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ documentation in a project or module.
55

66
This is a [link](https://vlang.io/) to the main V site.
77

8+
This is a <b>bold text</b>.
9+
10+
This is a script <script>console.log('hi from README.md');</script> .
11+
812
## Examples:
913

1014
### Processing command line args:

cmd/tools/vdoc/tests/testdata/output_formats/main.ansi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Description:
22
This is an example of a an .md file, used for adding more rich text
33
documentation in a project or module.
44
This is a link to the main V site.
5+
This is a bold text.
6+
This is a script console.log('hi from README.md'); .
57
Examples:
68
Processing command line args:
79
import os
@@ -65,6 +67,10 @@ fn auth_verify(secret string, token string) bool {
6567

6668
This is a [link](https://vlang.io/) to the main V site.
6769

70+
This is a <b>bold text</b>.
71+
72+
This is a script <script>console.log('hi from README.md');</script> .
73+
6874
## Examples:
6975

7076
### Processing command line args:
@@ -135,7 +141,7 @@ fn auth_verify(secret string, token string) bool {
135141
fn def()
136142
def - should be first
137143
fn xyz()
138-
xyz - should be in the middle
144+
xyz - should be in the middle a small script <script>console.log('hello');</script> bold text <b>bold</b> end underlined text <u>underline</u> end a link [main v repo](https://github.com/vlang/v)
139145
fn MyXMLDocument.abc(text string) ?(string, int)
140146
MyXMLDocument.abc does something too... I just do not know what.
141147
fn MyXMLDocument.from_file(path string) !MyXMLDocument

cmd/tools/vdoc/tests/testdata/output_formats/main.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<section id="readme_main" class="doc-node">
2-
<div class="title"><h1> main <a href="#readme_main">#</a></h1></div><h2>Description:</h2><p>This is an example of a an .md file, used for adding more rich text documentation in a project or module.</p><p>This is a <a href="https://vlang.io/">link</a> to the main V site.</p><h2>Examples:</h2><h3>Processing command line args:</h3><pre><code class="language-v"><span class="token keyword">import</span> os
2+
<div class="title"><h1> main <a href="#readme_main">#</a></h1></div><h2>Description:</h2><p>This is an example of a an .md file, used for adding more rich text documentation in a project or module.</p><p>This is a <a href="https://vlang.io/">link</a> to the main V site.</p><p>This is a <b>bold text</b>.</p><p>This is a script <code>console.log('hi from README.md');</code> .</p><h2>Examples:</h2><h3>Processing command line args:</h3><pre><code class="language-v"><span class="token keyword">import</span> os
33

44
<span class="token keyword">fn</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
55
<span class="token keyword">dump</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>args<span class="token punctuation">)</span>
@@ -83,7 +83,7 @@
8383
</section>
8484
<section id="xyz" class="doc-node">
8585
<div class="title"><h2>fn xyz <a href="#xyz">#</a></h2></div><pre class="signature"><code><span class="token keyword">fn</span> <span class="token function">xyz</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
86-
<p>xyz - should be in the middle</p>
86+
<p>xyz - should be in the middle a small script <script>console.log('hello');</script> bold text <b>bold</b> end underlined text <u>underline</u> end a link <a href="https://github.com/vlang/v">main v repo</a></p>
8787

8888
</section>
8989
<section id="MyXMLDocument.abc" class="doc-node">

cmd/tools/vdoc/tests/testdata/output_formats/main.text

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module main
66

77
This is a [link](https://vlang.io/) to the main V site.
88

9+
This is a <b>bold text</b>.
10+
11+
This is a script <script>console.log('hi from README.md');</script> .
12+
913
## Examples:
1014

1115
### Processing command line args:
@@ -76,7 +80,7 @@ fn abc()
7680
fn def()
7781
def - should be first
7882
fn xyz()
79-
xyz - should be in the middle
83+
xyz - should be in the middle a small script <script>console.log('hello');</script> bold text <b>bold</b> end underlined text <u>underline</u> end a link [main v repo](https://github.com/vlang/v)
8084
fn MyXMLDocument.abc(text string) ?(string, int)
8185
MyXMLDocument.abc does something too... I just do not know what.
8286
fn MyXMLDocument.from_file(path string) !MyXMLDocument

cmd/tools/vdoc/tests/testdata/output_formats/main.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ pub fn def() {
1010
}
1111

1212
// xyz - should be in the middle
13+
// a small script <script>console.log('hello');</script>
14+
// bold text <b>bold</b> end
15+
// underlined text <u>underline</u> end
16+
// a link [main v repo](https://github.com/vlang/v)
1317
pub fn xyz() {
1418
println(2)
1519
}

0 commit comments

Comments
 (0)