From 4fd36b5347513db2a8d04d32188e323e104b8690 Mon Sep 17 00:00:00 2001 From: Xavier Coulon Date: Sun, 6 Sep 2020 22:15:15 +0200 Subject: [PATCH] fix(renderer): do not 'HTML escape' string elements (#752) With support for special characters in place, there is no need to using the HTML "escape" func when rendering StringElements. Fixes #741 Signed-off-by: Xavier Coulon --- .../sgml/html5/delimited_block_test.go | 2 +- .../sgml/html5/file_inclusion_test.go | 28 ++++----- pkg/renderer/sgml/html5/labeled_list_test.go | 2 +- pkg/renderer/sgml/html5/paragraph_test.go | 62 ++++++++++++------- pkg/renderer/sgml/html5/quoted_string_test.go | 4 +- pkg/renderer/sgml/html5/quoted_text_test.go | 18 +++--- pkg/renderer/sgml/html5/string.go | 2 +- pkg/renderer/sgml/html5/string_test.go | 2 +- pkg/renderer/sgml/inline_elements.go | 2 +- pkg/renderer/sgml/paragraph.go | 4 +- .../sgml/xhtml5/delimited_block_test.go | 2 +- .../sgml/xhtml5/file_inclusion_test.go | 28 ++++----- pkg/renderer/sgml/xhtml5/labeled_list_test.go | 2 +- pkg/renderer/sgml/xhtml5/paragraph_test.go | 2 +- .../sgml/xhtml5/quoted_string_test.go | 4 +- pkg/renderer/sgml/xhtml5/quoted_text_test.go | 2 +- test/fixtures/supported/basic.html | 2 +- test/fixtures/supported/sample.html | 2 +- 18 files changed, 93 insertions(+), 77 deletions(-) diff --git a/pkg/renderer/sgml/html5/delimited_block_test.go b/pkg/renderer/sgml/html5/delimited_block_test.go index 3e2846d9..a64739c5 100644 --- a/pkg/renderer/sgml/html5/delimited_block_test.go +++ b/pkg/renderer/sgml/html5/delimited_block_test.go @@ -520,7 +520,7 @@ and "more" content

bold content

-

and "more" content

+

and "more" content

diff --git a/pkg/renderer/sgml/html5/file_inclusion_test.go b/pkg/renderer/sgml/html5/file_inclusion_test.go index 23f2f71e..64d05984 100644 --- a/pkg/renderer/sgml/html5/file_inclusion_test.go +++ b/pkg/renderer/sgml/html5/file_inclusion_test.go @@ -243,11 +243,11 @@ include::../../../../test/includes/hello_world.go.txt[]`

package includes

-

import "fmt"

+

import "fmt"

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

` @@ -296,11 +296,11 @@ include::../../../../test/includes/hello_world.go.txt[]`

package includes

-

import "fmt"

+

import "fmt"

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

@@ -514,11 +514,11 @@ include::../../../../test/includes/hello_world.go.txt[]

package includes

-

import "fmt"

+

import "fmt"

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

@@ -537,11 +537,11 @@ ____`

package includes

-

import "fmt"

+

import "fmt"

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

@@ -558,10 +558,10 @@ ____` expected := `
package includes
 
-import "fmt"
+import "fmt"
 
 func helloworld() {
-	fmt.Println("hello, world!")
+	fmt.Println("hello, world!")
 }
` @@ -578,11 +578,11 @@ include::../../../../test/includes/hello_world.go.txt[]

package includes

-

import "fmt"

+

import "fmt"

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

@@ -610,7 +610,7 @@ include::../../../../test/includes/hello_world.go.txt[] source := `include::../../../../test/includes/hello_world.go.txt[lines=5..7]` expected := `

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

` @@ -624,7 +624,7 @@ include::../../../../test/includes/hello_world.go.txt[]

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

` diff --git a/pkg/renderer/sgml/html5/labeled_list_test.go b/pkg/renderer/sgml/html5/labeled_list_test.go index 31c3443b..6ea2c8f9 100644 --- a/pkg/renderer/sgml/html5/labeled_list_test.go +++ b/pkg/renderer/sgml/html5/labeled_list_test.go @@ -99,7 +99,7 @@ item 2:: description 2.`
item 1
-

<script>alert("foo!")</script>

+

<script>alert("foo!")</script>

diff --git a/pkg/renderer/sgml/html5/paragraph_test.go b/pkg/renderer/sgml/html5/paragraph_test.go index dc6d756e..3619bbde 100644 --- a/pkg/renderer/sgml/html5/paragraph_test.go +++ b/pkg/renderer/sgml/html5/paragraph_test.go @@ -65,7 +65,7 @@ and here another paragraph It("paragraph with single quotes", func() { source := `a 'subsection' paragraph.` expected := `
-

a 'subsection' paragraph.

+

a 'subsection' paragraph.

` Expect(RenderHTML(source)).To(MatchHTML(expected)) @@ -99,63 +99,81 @@ some content` ` Expect(RenderHTML(source)).To(MatchHTML(expected)) }) - }) - Context("paragraphs with line break", func() { + Context("with custom substitutions", func() { + + It("with attributes substitution", func() { + source := `:github-url: https://github.com - It("with explicit line break", func() { - source := `foo + +[subs="attributes"] +a link to https://github.com[] +another one using attribute substitution: {github-url}[]... +// a single-line comment` + expected := `
+

a link to https://github.com[] +another one using attribute substitution: https://github.com[]...

+
+` + Expect(RenderHTML(source)).To(MatchHTML(expected)) + }) + }) + + Context("with line break", func() { + + It("with explicit line break", func() { + source := `foo + bar baz` - expected := `
+ expected := `

foo
bar baz

` - Expect(RenderHTML(source)).To(MatchHTML(expected)) - }) + Expect(RenderHTML(source)).To(MatchHTML(expected)) + }) - It("with paragraph attribute", func() { + It("with paragraph attribute", func() { - source := `[%hardbreaks] + source := `[%hardbreaks] foo bar baz` - expected := `
+ expected := `

foo
bar
baz

` - Expect(RenderHTML(source)).To(MatchHTML(expected)) - }) + Expect(RenderHTML(source)).To(MatchHTML(expected)) + }) - It("with document attribute", func() { - source := `:hardbreaks: + It("with document attribute", func() { + source := `:hardbreaks: foo bar baz` - expected := `
+ expected := `

foo
bar
baz

` - Expect(RenderHTML(source)).To(MatchHTML(expected)) - }) + Expect(RenderHTML(source)).To(MatchHTML(expected)) + }) - It("paragraph with document attribute resets", func() { - source := `:author: Xavier + It("paragraph with document attribute resets", func() { + source := `:author: Xavier :!author1: :author2!: a paragraph written by {author}.` - expected := `
+ expected := `

a paragraph written by Xavier.

` - Expect(RenderHTML(source)).To(MatchHTML(expected)) + Expect(RenderHTML(source)).To(MatchHTML(expected)) + }) }) }) diff --git a/pkg/renderer/sgml/html5/quoted_string_test.go b/pkg/renderer/sgml/html5/quoted_string_test.go index 40d051c5..feb163be 100644 --- a/pkg/renderer/sgml/html5/quoted_string_test.go +++ b/pkg/renderer/sgml/html5/quoted_string_test.go @@ -39,7 +39,7 @@ var _ = Describe("quoted strings", func() { It("interior spaces with single quoted string", func() { source := "'` curly was single `' or so they say" expected := "
\n" + - "

'` curly was single ’ or so they say

\n" + + "

'` curly was single ’ or so they say

\n" + "
\n" Expect(RenderHTML(source)).To(MatchHTML(expected)) }) @@ -209,7 +209,7 @@ var _ = Describe("quoted strings", func() { It("spaces with double quoted string", func() { source := "\"` curly was single `\"" expected := "
\n" + - "

"` curly was single `"

\n" + + "

\"` curly was single `\"

\n" + "
\n" Expect(RenderHTML(source)).To(MatchHTML(expected)) }) diff --git a/pkg/renderer/sgml/html5/quoted_text_test.go b/pkg/renderer/sgml/html5/quoted_text_test.go index a74bfefe..5e524048 100644 --- a/pkg/renderer/sgml/html5/quoted_text_test.go +++ b/pkg/renderer/sgml/html5/quoted_text_test.go @@ -457,7 +457,7 @@ content.

It("bad syntax", func() { source := "[.]**bold**" expected := `
-

[.<something "wicked>]bold

+

[.<something "wicked>]bold

` Expect(RenderHTML(source)).To(MatchHTML(expected)) @@ -737,7 +737,7 @@ b

It("apostrophes in single bold", func() { source := "this *mother's mothers' mothers`'*\n" expected := "
\n" + - "

this mother’s mothers' mothers’

\n" + + "

this mother’s mothers' mothers’

\n" + "
\n" Expect(RenderHTML(source)).To(MatchHTML(expected)) }) @@ -745,7 +745,7 @@ b

It("apostrophes in double bold", func() { source := "this **mother's mothers' mothers`'**\n" expected := "
\n" + - "

this mother’s mothers' mothers’

\n" + + "

this mother’s mothers' mothers’

\n" + "
\n" Expect(RenderHTML(source)).To(MatchHTML(expected)) }) @@ -753,7 +753,7 @@ b

It("apostrophes in single italic", func() { source := "this _mother's mothers' mothers`'_\n" expected := "
\n" + - "

this mother’s mothers' mothers’

\n" + + "

this mother’s mothers' mothers’

\n" + "
\n" Expect(RenderHTML(source)).To(MatchHTML(expected)) }) @@ -761,7 +761,7 @@ b

It("apostrophes in double italic", func() { source := "this __mother's mothers' mothers`'__\n" expected := "
\n" + - "

this mother’s mothers' mothers’

\n" + + "

this mother’s mothers' mothers’

\n" + "
\n" Expect(RenderHTML(source)).To(MatchHTML(expected)) }) @@ -769,7 +769,7 @@ b

It("apostrophes in single mono", func() { source := "this `mother's mothers' day`\n" // no typographic quotes here expected := "
\n" + - "

this mother’s mothers' day

\n" + + "

this mother’s mothers' day

\n" + "
\n" Expect(RenderHTML(source)).To(MatchHTML(expected)) }) @@ -777,7 +777,7 @@ b

It("apostrophes in double mono", func() { source := "this ``mother's mothers' mothers`' day``\n" expected := "
\n" + - "

this mother’s mothers' mothers’ day

\n" + + "

this mother’s mothers' mothers’ day

\n" + "
\n" Expect(RenderHTML(source)).To(MatchHTML(expected)) }) @@ -785,7 +785,7 @@ b

It("apostrophes in single marked", func() { source := "this #mother's mothers' mothers`'#\n" expected := "
\n" + - "

this mother’s mothers' mothers’

\n" + + "

this mother’s mothers' mothers’

\n" + "
\n" Expect(RenderHTML(source)).To(MatchHTML(expected)) }) @@ -793,7 +793,7 @@ b

It("apostrophes in double marked", func() { source := "this ##mother's mothers' mothers`'##\n" expected := "
\n" + - "

this mother’s mothers' mothers’

\n" + + "

this mother’s mothers' mothers’

\n" + "
\n" Expect(RenderHTML(source)).To(MatchHTML(expected)) }) diff --git a/pkg/renderer/sgml/html5/string.go b/pkg/renderer/sgml/html5/string.go index cfeb0724..f76fee5a 100644 --- a/pkg/renderer/sgml/html5/string.go +++ b/pkg/renderer/sgml/html5/string.go @@ -1,5 +1,5 @@ package html5 const ( - stringTmpl = "{{ escape . }}" + stringTmpl = "{{ . }}" ) diff --git a/pkg/renderer/sgml/html5/string_test.go b/pkg/renderer/sgml/html5/string_test.go index b38edb01..ad735f7a 100644 --- a/pkg/renderer/sgml/html5/string_test.go +++ b/pkg/renderer/sgml/html5/string_test.go @@ -57,7 +57,7 @@ var _ = Describe("strings", func() { It("text with implicit apostrophe no match", func() { source := `Mothers' Day` expected := `
-

Mothers' Day

+

Mothers' Day

` Expect(RenderHTML(source)).To(MatchHTML(expected)) diff --git a/pkg/renderer/sgml/inline_elements.go b/pkg/renderer/sgml/inline_elements.go index a043c302..825c929a 100644 --- a/pkg/renderer/sgml/inline_elements.go +++ b/pkg/renderer/sgml/inline_elements.go @@ -43,7 +43,7 @@ func (r *sgmlRenderer) renderInlineElements(ctx *renderer.Context, elements []in if _, ok := element.(types.StringElement); ok { // TODO: only for StringElement? or for any kind of element? // trim trailing spaces before returning the line buf.WriteString(strings.TrimRight(string(renderedElement), " ")) - log.Debugf("trimmed spaces on '%v'", string(renderedElement)) + // log.Debugf("trimmed spaces on '%v'", string(renderedElement)) } else { buf.WriteString(renderedElement) } diff --git a/pkg/renderer/sgml/paragraph.go b/pkg/renderer/sgml/paragraph.go index 13a7421e..d8e86769 100644 --- a/pkg/renderer/sgml/paragraph.go +++ b/pkg/renderer/sgml/paragraph.go @@ -39,15 +39,13 @@ func (r *sgmlRenderer) renderParagraph(ctx *renderer.Context, p types.Paragraph) ID string Roles string Title string - Lines []interface{} Content string }{ Context: ctx, ID: r.renderElementID(p.Attributes), Title: r.renderElementTitle(p.Attributes), - Content: string(content), Roles: roles, - Lines: p.Lines, + Content: string(content), }) if err != nil { return "", errors.Wrap(err, "unable to render paragraph") diff --git a/pkg/renderer/sgml/xhtml5/delimited_block_test.go b/pkg/renderer/sgml/xhtml5/delimited_block_test.go index 155a0c04..4d97544d 100644 --- a/pkg/renderer/sgml/xhtml5/delimited_block_test.go +++ b/pkg/renderer/sgml/xhtml5/delimited_block_test.go @@ -520,7 +520,7 @@ and "more" content

bold content

-

and "more" content

+

and "more" content

diff --git a/pkg/renderer/sgml/xhtml5/file_inclusion_test.go b/pkg/renderer/sgml/xhtml5/file_inclusion_test.go index 52f6c2b1..ac588400 100644 --- a/pkg/renderer/sgml/xhtml5/file_inclusion_test.go +++ b/pkg/renderer/sgml/xhtml5/file_inclusion_test.go @@ -243,11 +243,11 @@ include::../../../../test/includes/hello_world.go.txt[]`

package includes

-

import "fmt"

+

import "fmt"

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

` @@ -296,11 +296,11 @@ include::../../../../test/includes/hello_world.go.txt[]`

package includes

-

import "fmt"

+

import "fmt"

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

@@ -514,11 +514,11 @@ include::../../../../test/includes/hello_world.go.txt[]

package includes

-

import "fmt"

+

import "fmt"

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

@@ -537,11 +537,11 @@ ____`

package includes

-

import "fmt"

+

import "fmt"

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

@@ -558,10 +558,10 @@ ____` expected := `
package includes
 
-import "fmt"
+import "fmt"
 
 func helloworld() {
-	fmt.Println("hello, world!")
+	fmt.Println("hello, world!")
 }
` @@ -578,11 +578,11 @@ include::../../../../test/includes/hello_world.go.txt[]

package includes

-

import "fmt"

+

import "fmt"

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

@@ -610,7 +610,7 @@ include::../../../../test/includes/hello_world.go.txt[] source := `include::../../../../test/includes/hello_world.go.txt[lines=5..7]` expected := `

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

` @@ -624,7 +624,7 @@ include::../../../../test/includes/hello_world.go.txt[]

func helloworld() { - fmt.Println("hello, world!") + fmt.Println("hello, world!") }

` diff --git a/pkg/renderer/sgml/xhtml5/labeled_list_test.go b/pkg/renderer/sgml/xhtml5/labeled_list_test.go index ccfad298..d8bed45a 100644 --- a/pkg/renderer/sgml/xhtml5/labeled_list_test.go +++ b/pkg/renderer/sgml/xhtml5/labeled_list_test.go @@ -99,7 +99,7 @@ item 2:: description 2.`
item 1
-

<script>alert("foo!")</script>

+

<script>alert("foo!")</script>

diff --git a/pkg/renderer/sgml/xhtml5/paragraph_test.go b/pkg/renderer/sgml/xhtml5/paragraph_test.go index 3c485b00..4c12f2f6 100644 --- a/pkg/renderer/sgml/xhtml5/paragraph_test.go +++ b/pkg/renderer/sgml/xhtml5/paragraph_test.go @@ -65,7 +65,7 @@ and here another paragraph It("paragraph with single quotes", func() { source := `a 'subsection' paragraph.` expected := `
-

a 'subsection' paragraph.

+

a 'subsection' paragraph.

` Expect(RenderXHTML(source)).To(MatchHTML(expected)) diff --git a/pkg/renderer/sgml/xhtml5/quoted_string_test.go b/pkg/renderer/sgml/xhtml5/quoted_string_test.go index 54e8f2f6..074a4796 100644 --- a/pkg/renderer/sgml/xhtml5/quoted_string_test.go +++ b/pkg/renderer/sgml/xhtml5/quoted_string_test.go @@ -31,7 +31,7 @@ var _ = Describe("quoted strings", func() { It("spaces with single quoted string", func() { source := "'` curly was single `' or so they say" expected := "
\n" + - "

'` curly was single ’ or so they say

\n" + + "

'` curly was single ’ or so they say

\n" + "
\n" Expect(RenderXHTML(source)).To(MatchHTML(expected)) }) @@ -201,7 +201,7 @@ var _ = Describe("quoted strings", func() { It("spaces with double quoted string", func() { source := "\"` curly was single `\"" expected := "
\n" + - "

"` curly was single `"

\n" + + "

\"` curly was single `\"

\n" + "
\n" Expect(RenderXHTML(source)).To(MatchHTML(expected)) }) diff --git a/pkg/renderer/sgml/xhtml5/quoted_text_test.go b/pkg/renderer/sgml/xhtml5/quoted_text_test.go index 1152bb31..bff2cf3c 100644 --- a/pkg/renderer/sgml/xhtml5/quoted_text_test.go +++ b/pkg/renderer/sgml/xhtml5/quoted_text_test.go @@ -261,7 +261,7 @@ var _ = Describe("quoted texts", func() { It("bad syntax", func() { source := "[.]**bold**" expected := `
-

[.<something "wicked>]bold

+

[.<something "wicked>]bold

` Expect(RenderXHTML(source)).To(MatchHTML(expected)) diff --git a/test/fixtures/supported/basic.html b/test/fixtures/supported/basic.html index d79d0d1e..0989e46e 100644 --- a/test/fixtures/supported/basic.html +++ b/test/fixtures/supported/basic.html @@ -20,7 +20,7 @@

First Section

-
puts "Hello, World!"
+
puts "Hello, World!"
diff --git a/test/fixtures/supported/sample.html b/test/fixtures/supported/sample.html index 94a5216c..596d2475 100644 --- a/test/fixtures/supported/sample.html +++ b/test/fixtures/supported/sample.html @@ -26,7 +26,7 @@

Section A

Section A Subsection

-

Section A 'subsection' paragraph.

+

Section A 'subsection' paragraph.