page_markup_test.go - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
page_markup_test.go (10648B)
---
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package page
15
16 import (
17 "strings"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/common/types"
22 "github.com/gohugoio/hugo/media"
23 )
24
25 func TestExtractSummaryFromHTML(t *testing.T) {
26 c := qt.New(t)
27
28 tests := []struct {
29 mt media.Type
30 input string
31 isCJK bool
32 numWords int
33 expectSummary string
34 expectContentWithoutSummary string
35 }{
36 {media.Builtin.ReStructuredTextType, "<div class=\"document\">\n\n\n<p>Simple Page</p>\n</div>", false, 70, "<div class=\"document\">\n\n\n<p>Simple Page</p>\n</div>", ""},
37 {media.Builtin.ReStructuredTextType, "<div class=\"document\"><p>First paragraph</p><p>Second paragraph</p></div>", false, 2, `<div class="document"><p>First paragraph</p></div>`, "<div class=\"document\"><p>Second paragraph</p></div>"},
38 {media.Builtin.MarkdownType, "<p>First paragraph</p>", false, 10, "<p>First paragraph</p>", ""},
39 {media.Builtin.MarkdownType, "<p>First paragraph</p><p>Second paragraph</p>", false, 2, "<p>First paragraph</p>", "<p>Second paragraph</p>"},
40 {media.Builtin.MarkdownType, "<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p>", false, 3, "<p>First paragraph</p><p>Second paragraph</p>", "<p>Third paragraph</p>"},
41 {media.Builtin.AsciiDocType, "<div><p>First paragraph</p></div><div><p>Second paragraph</p></div>", false, 2, "<div><p>First paragraph</p></div>", "<div><p>Second paragraph</p></div>"},
42 {media.Builtin.MarkdownType, "<p>这是中文,全中文</p><p>a这是中文,全中文</p>", true, 5, "<p>这是中文,全中文</p>", "<p>a这是中文,全中文</p>"},
43 }
44
45 for i, test := range tests {
46 summary := ExtractSummaryFromHTML(test.mt, test.input, test.numWords, test.isCJK)
47 c.Assert(summary.Summary(), qt.Equals, test.expectSummary, qt.Commentf("Summary %d", i))
48 c.Assert(summary.ContentWithoutSummary(), qt.Equals, test.expectContentWithoutSummary, qt.Commentf("ContentWithoutSummary %d", i))
49 }
50 }
51
52 // See https://discourse.gohugo.io/t/automatic-summarys-summarylength-seems-broken-in-the-case-of-plainify/51466/4
53 // Also issue 12837
54 func TestExtractSummaryFromHTMLLotsOfHTMLInSummary(t *testing.T) {
55 c := qt.New(t)
56
57 input := `
58 <p>
59 <div>
60 <picture>
61 <img src="imgs/1.jpg" alt="1"/>
62 </picture>
63 <picture>
64 <img src="imgs/2.jpg" alt="2"/>
65 </picture>
66 <picture>
67 <img src="imgs/3.jpg" alt="3"/>
68 </picture>
69 <picture>
70 <img src="imgs/4.jpg" alt="4"/>
71 </picture>
72 <picture>
73 <img src="imgs/5.jpg" alt="5"/>
74 </picture>
75 </div>
76 </p>
77 <p>
78 This is a story about a cat.
79 </p>
80 <p>
81 The cat was white and fluffy.
82 </p>
83 <p>
84 And it liked milk.
85 </p>
86 `
87
88 summary := ExtractSummaryFromHTML(media.Builtin.MarkdownType, input, 10, false)
89 c.Assert(strings.HasSuffix(summary.Summary(), "<p>\nThis is a story about a cat.\n</p>\n<p>\nThe cat was white and fluffy.\n</p>"), qt.IsTrue)
90 }
91
92 func TestExtractSummaryFromHTMLWithDivider(t *testing.T) {
93 c := qt.New(t)
94
95 const divider = "FOOO"
96
97 tests := []struct {
98 mt media.Type
99 input string
100 expectSummary string
101 expectContentWithoutSummary string
102 expectContent string
103 }{
104 {media.Builtin.MarkdownType, "<p>First paragraph</p><p>FOOO</p><p>Second paragraph</p>", "<p>First paragraph</p>", "<p>Second paragraph</p>", "<p>First paragraph</p><p>Second paragraph</p>"},
105 {media.Builtin.MarkdownType, "<p>First paragraph</p>\n<p>FOOO</p>\n<p>Second paragraph</p>", "<p>First paragraph</p>", "<p>Second paragraph</p>", "<p>First paragraph</p>\n<p>Second paragraph</p>"},
106 {media.Builtin.MarkdownType, "<p>FOOO</p>\n<p>First paragraph</p>", "", "<p>First paragraph</p>", "<p>First paragraph</p>"},
107 {media.Builtin.MarkdownType, "<p>First paragraph</p><p>Second paragraphFOOO</p><p>Third paragraph</p>", "<p>First paragraph</p><p>Second paragraph</p>", "<p>Third paragraph</p>", "<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p>"},
108 {media.Builtin.MarkdownType, "<p>这是中文,全中文FOOO</p><p>a这是中文,全中文</p>", "<p>这是中文,全中文</p>", "<p>a这是中文,全中文</p>", "<p>这是中文,全中文</p><p>a这是中文,全中文</p>"},
109 {media.Builtin.MarkdownType, `<p>a <strong>b</strong>` + "\v" + ` c</p>` + "\n<p>FOOO</p>", "<p>a <strong>b</strong>\v c</p>", "", "<p>a <strong>b</strong>\v c</p>"},
110
111 {media.Builtin.HTMLType, "<p>First paragraph</p>FOOO<p>Second paragraph</p>", "<p>First paragraph</p>", "<p>Second paragraph</p>", "<p>First paragraph</p><p>Second paragraph</p>"},
112
113 {media.Builtin.ReStructuredTextType, "<div class=\"document\">\n\n\n<p>This is summary.</p>\n<p>FOOO</p>\n<p>This is content.</p>\n</div>", "<div class=\"document\">\n\n\n<p>This is summary.</p>\n</div>", "<div class=\"document\"><p>This is content.</p>\n</div>", "<div class=\"document\">\n\n\n<p>This is summary.</p>\n<p>This is content.</p>\n</div>"},
114 {media.Builtin.ReStructuredTextType, "<div class=\"document\"><p>First paragraphFOOO</p><p>Second paragraph</p></div>", "<div class=\"document\"><p>First paragraph</p></div>", "<div class=\"document\"><p>Second paragraph</p></div>", `<div class="document"><p>First paragraph</p><p>Second paragraph</p></div>`},
115
116 {media.Builtin.AsciiDocType, "<div class=\"paragraph\"><p>Summary Next Line</p></div><div class=\"paragraph\"><p>FOOO</p></div><div class=\"paragraph\"><p>Some more text</p></div>", "<div class=\"paragraph\"><p>Summary Next Line</p></div>", "<div class=\"paragraph\"><p>Some more text</p></div>", "<div class=\"paragraph\"><p>Summary Next Line</p></div><div class=\"paragraph\"><p>Some more text</p></div>"},
117 {media.Builtin.AsciiDocType, "<div class=\"paragraph\">\n<p>Summary Next Line</p>\n</div>\n<div class=\"paragraph\">\n<p>FOOO</p>\n</div>\n<div class=\"paragraph\">\n<p>Some more text</p>\n</div>\n", "<div class=\"paragraph\">\n<p>Summary Next Line</p>\n</div>", "<div class=\"paragraph\">\n<p>Some more text</p>\n</div>", "<div class=\"paragraph\">\n<p>Summary Next Line</p>\n</div>\n<div class=\"paragraph\">\n<p>Some more text</p>\n</div>"},
118 {media.Builtin.AsciiDocType, "<div><p>FOOO</p></div><div><p>First paragraph</p></div>", "", "<div><p>First paragraph</p></div>", "<div><p>First paragraph</p></div>"},
119 {media.Builtin.AsciiDocType, "<div><p>First paragraphFOOO</p></div><div><p>Second paragraph</p></div>", "<div><p>First paragraph</p></div>", "<div><p>Second paragraph</p></div>", "<div><p>First paragraph</p></div><div><p>Second paragraph</p></div>"},
120 }
121
122 for i, test := range tests {
123 summary := ExtractSummaryFromHTMLWithDivider(test.mt, test.input, divider)
124 c.Assert(summary.Summary(), qt.Equals, test.expectSummary, qt.Commentf("Summary %d", i))
125 c.Assert(summary.ContentWithoutSummary(), qt.Equals, test.expectContentWithoutSummary, qt.Commentf("ContentWithoutSummary %d", i))
126 c.Assert(summary.Content(), qt.Equals, test.expectContent, qt.Commentf("Content %d", i))
127 }
128 }
129
130 func TestExpandDivider(t *testing.T) {
131 c := qt.New(t)
132
133 for i, test := range []struct {
134 input string
135 divider string
136 ptag tagReStartEnd
137 expect string
138 expectEndMarkup string
139 }{
140 {"<p>First paragraph</p>\n<p>FOOO</p>\n<p>Second paragraph</p>", "FOOO", startEndP, "<p>FOOO</p>\n", ""},
141 {"<div class=\"paragraph\">\n<p>FOOO</p>\n</div>", "FOOO", startEndDiv, "<div class=\"paragraph\">\n<p>FOOO</p>\n</div>", ""},
142 {"<div><p>FOOO</p></div><div><p>Second paragraph</p></div>", "FOOO", startEndDiv, "<div><p>FOOO</p></div>", ""},
143 {"<div><p>First paragraphFOOO</p></div><div><p>Second paragraph</p></div>", "FOOO", startEndDiv, "FOOO", "</p></div>"},
144 {" <p> abc FOOO </p> ", "FOOO", startEndP, "FOOO", " </p>"},
145 {" <p> FOOO </p> ", "FOOO", startEndP, "<p> FOOO </p>", ""},
146 {" <p>\n \nFOOO </p> ", "FOOO", startEndP, "<p>\n \nFOOO </p>", ""},
147 {" <div> FOOO </div> ", "FOOO", startEndDiv, "<div> FOOO </div>", ""},
148 } {
149
150 l := types.LowHigh[string]{Low: strings.Index(test.input, test.divider), High: strings.Index(test.input, test.divider) + len(test.divider)}
151 e, t := expandSummaryDivider(test.input, test.ptag, l)
152 c.Assert(test.input[e.Low:e.High], qt.Equals, test.expect, qt.Commentf("[%d] Test.expect %q", i, test.input))
153 c.Assert(test.input[t.Low:t.High], qt.Equals, test.expectEndMarkup, qt.Commentf("[%d] Test.expectEndMarkup %q", i, test.input))
154 }
155 }
156
157 func TestIsProbablyHTMLToken(t *testing.T) {
158 c := qt.New(t)
159
160 for i, test := range []struct {
161 input string
162 expect bool
163 }{
164 {"<p>", true},
165 {"<p", true},
166 {"width=\"32\"", true},
167 {"width='32'", true},
168 {"<p>Æøå", false},
169 } {
170 c.Assert(isProbablyHTMLToken(test.input), qt.Equals, test.expect, qt.Commentf("[%d] Test.expect %q", i, test.input))
171 }
172 }
173
174 func BenchmarkSummaryFromHTML(b *testing.B) {
175 b.StopTimer()
176 input := "<p>First paragraph</p><p>Second paragraph</p>"
177 b.StartTimer()
178 for i := 0; i < b.N; i++ {
179 summary := ExtractSummaryFromHTML(media.Builtin.MarkdownType, input, 2, false)
180 if s := summary.Content(); s != input {
181 b.Fatalf("unexpected content: %q", s)
182 }
183 if s := summary.ContentWithoutSummary(); s != "<p>Second paragraph</p>" {
184 b.Fatalf("unexpected content without summary: %q", s)
185 }
186 if s := summary.Summary(); s != "<p>First paragraph</p>" {
187 b.Fatalf("unexpected summary: %q", s)
188 }
189 }
190 }
191
192 func BenchmarkSummaryFromHTMLWithDivider(b *testing.B) {
193 b.StopTimer()
194 input := "<p>First paragraph</p><p>FOOO</p><p>Second paragraph</p>"
195 b.StartTimer()
196 for i := 0; i < b.N; i++ {
197 summary := ExtractSummaryFromHTMLWithDivider(media.Builtin.MarkdownType, input, "FOOO")
198 if s := summary.Content(); s != "<p>First paragraph</p><p>Second paragraph</p>" {
199 b.Fatalf("unexpected content: %q", s)
200 }
201 if s := summary.ContentWithoutSummary(); s != "<p>Second paragraph</p>" {
202 b.Fatalf("unexpected content without summary: %q", s)
203 }
204 if s := summary.Summary(); s != "<p>First paragraph</p>" {
205 b.Fatalf("unexpected summary: %q", s)
206 }
207 }
208 }