| 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 |
"context" |
| 18 |
"html/template" |
| 19 |
"regexp" |
| 20 |
"strings" |
| 21 |
"unicode" |
| 22 |
"unicode/utf8" |
| 23 |
|
| 24 |
"github.com/gohugoio/hugo/common/types" |
| 25 |
"github.com/gohugoio/hugo/markup/tableofcontents" |
| 26 |
"github.com/gohugoio/hugo/media" |
| 27 |
"github.com/gohugoio/hugo/tpl" |
| 28 |
) |
| 29 |
|
| 30 |
type Content interface { |
| 31 |
Content(context.Context) (template.HTML, error) |
| 32 |
ContentWithoutSummary(context.Context) (template.HTML, error) |
| 33 |
Summary(context.Context) (Summary, error) |
| 34 |
Plain(context.Context) string |
| 35 |
PlainWords(context.Context) []string |
| 36 |
WordCount(context.Context) int |
| 37 |
FuzzyWordCount(context.Context) int |
| 38 |
ReadingTime(context.Context) int |
| 39 |
Len(context.Context) int |
| 40 |
} |
| 41 |
|
| 42 |
type Markup interface { |
| 43 |
Render(context.Context) (Content, error) |
| 44 |
RenderString(ctx context.Context, args ...any) (template.HTML, error) |
| 45 |
RenderShortcodes(context.Context) (template.HTML, error) |
| 46 |
Fragments(context.Context) *tableofcontents.Fragments |
| 47 |
} |
| 48 |
|
| 49 |
var _ types.PrintableValueProvider = Summary{} |
| 50 |
|
| 51 |
const ( |
| 52 |
SummaryTypeAuto = "auto" |
| 53 |
SummaryTypeManual = "manual" |
| 54 |
SummaryTypeFrontMatter = "frontmatter" |
| 55 |
) |
| 56 |
|
| 57 |
type Summary struct { |
| 58 |
Text template.HTML |
| 59 |
Type string // "auto", "manual" or "frontmatter" |
| 60 |
Truncated bool |
| 61 |
} |
| 62 |
|
| 63 |
func (s Summary) IsZero() bool { |
| 64 |
return s.Text == "" |
| 65 |
} |
| 66 |
|
| 67 |
func (s Summary) PrintableValue() any { |
| 68 |
return s.Text |
| 69 |
} |
| 70 |
|
| 71 |
var _ types.PrintableValueProvider = (*Summary)(nil) |
| 72 |
|
| 73 |
type HtmlSummary struct { |
| 74 |
source string |
| 75 |
SummaryLowHigh types.LowHigh[string] |
| 76 |
SummaryEndTag types.LowHigh[string] |
| 77 |
WrapperStart types.LowHigh[string] |
| 78 |
WrapperEnd types.LowHigh[string] |
| 79 |
Divider types.LowHigh[string] |
| 80 |
} |
| 81 |
|
| 82 |
func (s HtmlSummary) wrap(ss string) string { |
| 83 |
if s.WrapperStart.IsZero() { |
| 84 |
return ss |
| 85 |
} |
| 86 |
return s.source[s.WrapperStart.Low:s.WrapperStart.High] + ss + s.source[s.WrapperEnd.Low:s.WrapperEnd.High] |
| 87 |
} |
| 88 |
|
| 89 |
func (s HtmlSummary) wrapLeft(ss string) string { |
| 90 |
if s.WrapperStart.IsZero() { |
| 91 |
return ss |
| 92 |
} |
| 93 |
|
| 94 |
return s.source[s.WrapperStart.Low:s.WrapperStart.High] + ss |
| 95 |
} |
| 96 |
|
| 97 |
func (s HtmlSummary) Value(l types.LowHigh[string]) string { |
| 98 |
return s.source[l.Low:l.High] |
| 99 |
} |
| 100 |
|
| 101 |
func (s HtmlSummary) trimSpace(ss string) string { |
| 102 |
return strings.TrimSpace(ss) |
| 103 |
} |
| 104 |
|
| 105 |
func (s HtmlSummary) Content() string { |
| 106 |
if s.Divider.IsZero() { |
| 107 |
return s.trimSpace(s.source) |
| 108 |
} |
| 109 |
ss := s.source[:s.Divider.Low] |
| 110 |
ss += s.source[s.Divider.High:] |
| 111 |
return s.trimSpace(ss) |
| 112 |
} |
| 113 |
|
| 114 |
func (s HtmlSummary) Summary() string { |
| 115 |
if s.Divider.IsZero() { |
| 116 |
return s.trimSpace(s.wrap(s.Value(s.SummaryLowHigh))) |
| 117 |
} |
| 118 |
ss := s.source[s.SummaryLowHigh.Low:s.Divider.Low] |
| 119 |
if s.SummaryLowHigh.High > s.Divider.High { |
| 120 |
ss += s.source[s.Divider.High:s.SummaryLowHigh.High] |
| 121 |
} |
| 122 |
if !s.SummaryEndTag.IsZero() { |
| 123 |
ss += s.Value(s.SummaryEndTag) |
| 124 |
} |
| 125 |
return s.trimSpace(s.wrap(ss)) |
| 126 |
} |
| 127 |
|
| 128 |
func (s HtmlSummary) ContentWithoutSummary() string { |
| 129 |
if s.Divider.IsZero() { |
| 130 |
if s.SummaryLowHigh.Low == s.WrapperStart.High && s.SummaryLowHigh.High == s.WrapperEnd.Low { |
| 131 |
return "" |
| 132 |
} |
| 133 |
return s.trimSpace(s.wrapLeft(s.source[s.SummaryLowHigh.High:])) |
| 134 |
} |
| 135 |
if s.SummaryEndTag.IsZero() { |
| 136 |
return s.trimSpace(s.wrapLeft(s.source[s.Divider.High:])) |
| 137 |
} |
| 138 |
return s.trimSpace(s.wrapLeft(s.source[s.SummaryEndTag.High:])) |
| 139 |
} |
| 140 |
|
| 141 |
func (s HtmlSummary) Truncated() bool { |
| 142 |
return s.Summary() != s.Content() |
| 143 |
} |
| 144 |
|
| 145 |
func (s *HtmlSummary) resolveParagraphTagAndSetWrapper(mt media.Type) tagReStartEnd { |
| 146 |
ptag := startEndP |
| 147 |
|
| 148 |
switch mt.SubType { |
| 149 |
case media.DefaultContentTypes.AsciiDoc.SubType: |
| 150 |
ptag = startEndDiv |
| 151 |
case media.DefaultContentTypes.ReStructuredText.SubType: |
| 152 |
const markerStart = "<div class=\"document\">" |
| 153 |
const markerEnd = "</div>" |
| 154 |
i1 := strings.Index(s.source, markerStart) |
| 155 |
i2 := strings.LastIndex(s.source, markerEnd) |
| 156 |
if i1 > -1 && i2 > -1 { |
| 157 |
s.WrapperStart = types.LowHigh[string]{Low: 0, High: i1 + len(markerStart)} |
| 158 |
s.WrapperEnd = types.LowHigh[string]{Low: i2, High: len(s.source)} |
| 159 |
} |
| 160 |
} |
| 161 |
return ptag |
| 162 |
} |
| 163 |
|
| 164 |
// Avoid counting words that are most likely HTML tokens. |
| 165 |
var ( |
| 166 |
isProbablyHTMLTag = regexp.MustCompile(`^<\/?[A-Za-z]+>?$`) |
| 167 |
isProablyHTMLAttribute = regexp.MustCompile(`^[A-Za-z]+=["']`) |
| 168 |
) |
| 169 |
|
| 170 |
func isProbablyHTMLToken(s string) bool { |
| 171 |
return s == ">" || isProbablyHTMLTag.MatchString(s) || isProablyHTMLAttribute.MatchString(s) |
| 172 |
} |
| 173 |
|
| 174 |
// ExtractSummaryFromHTML extracts a summary from the given HTML content. |
| 175 |
func ExtractSummaryFromHTML(mt media.Type, input string, numWords int, isCJK bool) (result HtmlSummary) { |
| 176 |
result.source = input |
| 177 |
ptag := result.resolveParagraphTagAndSetWrapper(mt) |
| 178 |
|
| 179 |
if numWords <= 0 { |
| 180 |
return result |
| 181 |
} |
| 182 |
|
| 183 |
var count int |
| 184 |
|
| 185 |
countWord := func(word string) int { |
| 186 |
word = strings.TrimSpace(word) |
| 187 |
if len(word) == 0 { |
| 188 |
return 0 |
| 189 |
} |
| 190 |
if isProbablyHTMLToken(word) { |
| 191 |
return 0 |
| 192 |
} |
| 193 |
|
| 194 |
if isCJK { |
| 195 |
word = tpl.StripHTML(word) |
| 196 |
runeCount := utf8.RuneCountInString(word) |
| 197 |
if len(word) == runeCount { |
| 198 |
return 1 |
| 199 |
} else { |
| 200 |
return runeCount |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return 1 |
| 205 |
} |
| 206 |
|
| 207 |
high := len(input) |
| 208 |
if result.WrapperEnd.Low > 0 { |
| 209 |
high = result.WrapperEnd.Low |
| 210 |
} |
| 211 |
|
| 212 |
for j := result.WrapperStart.High; j < high; { |
| 213 |
s := input[j:] |
| 214 |
closingIndex := strings.Index(s, "</"+ptag.tagName+">") |
| 215 |
|
| 216 |
if closingIndex == -1 { |
| 217 |
break |
| 218 |
} |
| 219 |
|
| 220 |
s = s[:closingIndex] |
| 221 |
|
| 222 |
// Count the words in the current paragraph. |
| 223 |
var wi int |
| 224 |
|
| 225 |
for i, r := range s { |
| 226 |
if unicode.IsSpace(r) || (i+utf8.RuneLen(r) == len(s)) { |
| 227 |
word := s[wi:i] |
| 228 |
count += countWord(word) |
| 229 |
wi = i |
| 230 |
if count >= numWords { |
| 231 |
break |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if count >= numWords { |
| 237 |
result.SummaryLowHigh = types.LowHigh[string]{ |
| 238 |
Low: result.WrapperStart.High, |
| 239 |
High: j + closingIndex + len(ptag.tagName) + 3, |
| 240 |
} |
| 241 |
return |
| 242 |
} |
| 243 |
|
| 244 |
j += closingIndex + len(ptag.tagName) + 2 |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
result.SummaryLowHigh = types.LowHigh[string]{ |
| 249 |
Low: result.WrapperStart.High, |
| 250 |
High: high, |
| 251 |
} |
| 252 |
|
| 253 |
return |
| 254 |
} |
| 255 |
|
| 256 |
// ExtractSummaryFromHTMLWithDivider extracts a summary from the given HTML content with |
| 257 |
// a manual summary divider. |
| 258 |
func ExtractSummaryFromHTMLWithDivider(mt media.Type, input, divider string) (result HtmlSummary) { |
| 259 |
result.source = input |
| 260 |
result.Divider.Low = strings.Index(input, divider) |
| 261 |
result.Divider.High = result.Divider.Low + len(divider) |
| 262 |
|
| 263 |
if result.Divider.Low == -1 { |
| 264 |
// No summary. |
| 265 |
return |
| 266 |
} |
| 267 |
|
| 268 |
ptag := result.resolveParagraphTagAndSetWrapper(mt) |
| 269 |
|
| 270 |
if !mt.IsHTML() { |
| 271 |
result.Divider, result.SummaryEndTag = expandSummaryDivider(result.source, ptag, result.Divider) |
| 272 |
} |
| 273 |
|
| 274 |
result.SummaryLowHigh = types.LowHigh[string]{ |
| 275 |
Low: result.WrapperStart.High, |
| 276 |
High: result.Divider.Low, |
| 277 |
} |
| 278 |
|
| 279 |
return |
| 280 |
} |
| 281 |
|
| 282 |
var ( |
| 283 |
pOrDiv = regexp.MustCompile(`<p[^>]?>|<div[^>]?>$`) |
| 284 |
|
| 285 |
startEndDiv = tagReStartEnd{ |
| 286 |
startEndOfString: regexp.MustCompile(`<div[^>]*?>$`), |
| 287 |
endEndOfString: regexp.MustCompile(`</div>$`), |
| 288 |
tagName: "div", |
| 289 |
} |
| 290 |
|
| 291 |
startEndP = tagReStartEnd{ |
| 292 |
startEndOfString: regexp.MustCompile(`<p[^>]*?>$`), |
| 293 |
endEndOfString: regexp.MustCompile(`</p>$`), |
| 294 |
tagName: "p", |
| 295 |
} |
| 296 |
) |
| 297 |
|
| 298 |
type tagReStartEnd struct { |
| 299 |
startEndOfString *regexp.Regexp |
| 300 |
endEndOfString *regexp.Regexp |
| 301 |
tagName string |
| 302 |
} |
| 303 |
|
| 304 |
func expandSummaryDivider(s string, re tagReStartEnd, divider types.LowHigh[string]) (types.LowHigh[string], types.LowHigh[string]) { |
| 305 |
var endMarkup types.LowHigh[string] |
| 306 |
|
| 307 |
if divider.IsZero() { |
| 308 |
return divider, endMarkup |
| 309 |
} |
| 310 |
|
| 311 |
lo, hi := divider.Low, divider.High |
| 312 |
|
| 313 |
var preserveEndMarkup bool |
| 314 |
|
| 315 |
// Find the start of the paragraph. |
| 316 |
|
| 317 |
for i := lo - 1; i >= 0; i-- { |
| 318 |
if s[i] == '>' { |
| 319 |
if match := re.startEndOfString.FindString(s[:i+1]); match != "" { |
| 320 |
lo = i - len(match) + 1 |
| 321 |
break |
| 322 |
} |
| 323 |
if match := pOrDiv.FindString(s[:i+1]); match != "" { |
| 324 |
i -= len(match) - 1 |
| 325 |
continue |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
r, _ := utf8.DecodeRuneInString(s[i:]) |
| 330 |
if !unicode.IsSpace(r) { |
| 331 |
preserveEndMarkup = true |
| 332 |
break |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
divider.Low = lo |
| 337 |
|
| 338 |
// Now walk forward to the end of the paragraph. |
| 339 |
for ; hi < len(s); hi++ { |
| 340 |
if s[hi] != '>' { |
| 341 |
continue |
| 342 |
} |
| 343 |
if match := re.endEndOfString.FindString(s[:hi+1]); match != "" { |
| 344 |
hi++ |
| 345 |
break |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
if preserveEndMarkup { |
| 350 |
endMarkup.Low = divider.High |
| 351 |
endMarkup.High = hi |
| 352 |
} else { |
| 353 |
divider.High = hi |
| 354 |
} |
| 355 |
|
| 356 |
// Consume trailing newline if any. |
| 357 |
if divider.High < len(s) && s[divider.High] == '\n' { |
| 358 |
divider.High++ |
| 359 |
} |
| 360 |
|
| 361 |
return divider, endMarkup |
| 362 |
} |