2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/output"
|
|
|
|
"github.com/gohugoio/hugo/parser/pageparser"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
internalSummaryDividerBase = "HUGOMORE42"
|
|
|
|
internalSummaryDividerBaseBytes = []byte(internalSummaryDividerBase)
|
|
|
|
internalSummaryDividerPre = []byte("\n\n" + internalSummaryDividerBase + "\n\n")
|
|
|
|
)
|
|
|
|
|
|
|
|
// The content related items on a Page.
|
|
|
|
type pageContent struct {
|
|
|
|
selfLayout string
|
2019-11-27 07:42:36 -05:00
|
|
|
truncated bool
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
cmap *pageContentMap
|
|
|
|
|
|
|
|
shortcodeState *shortcodeHandler
|
|
|
|
|
|
|
|
source rawPageContent
|
|
|
|
}
|
|
|
|
|
2022-05-28 05:01:47 -04:00
|
|
|
// returns the content to be processed by Goldmark or similar.
|
2022-05-29 10:41:57 -04:00
|
|
|
func (p pageContent) contentToRender(parsed pageparser.Result, pm *pageContentMap, renderedShortcodes map[string]string) []byte {
|
|
|
|
source := parsed.Input()
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
c := make([]byte, 0, len(source)+(len(source)/10))
|
|
|
|
|
2022-05-29 10:41:57 -04:00
|
|
|
for _, it := range pm.items {
|
2019-01-02 06:33:26 -05:00
|
|
|
switch v := it.(type) {
|
|
|
|
case pageparser.Item:
|
|
|
|
c = append(c, source[v.Pos:v.Pos+len(v.Val)]...)
|
|
|
|
case pageContentReplacement:
|
|
|
|
c = append(c, v.val...)
|
|
|
|
case *shortcode:
|
2020-03-09 07:04:33 -04:00
|
|
|
if !v.insertPlaceholder() {
|
2019-01-02 06:33:26 -05:00
|
|
|
// Insert the rendered shortcode.
|
|
|
|
renderedShortcode, found := renderedShortcodes[v.placeholder]
|
|
|
|
if !found {
|
|
|
|
// This should never happen.
|
|
|
|
panic(fmt.Sprintf("rendered shortcode %q not found", v.placeholder))
|
|
|
|
}
|
|
|
|
|
|
|
|
c = append(c, []byte(renderedShortcode)...)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Insert the placeholder so we can insert the content after
|
|
|
|
// markdown processing.
|
|
|
|
c = append(c, []byte(v.placeholder)...)
|
|
|
|
}
|
|
|
|
default:
|
2019-07-31 08:36:36 -04:00
|
|
|
panic(fmt.Sprintf("unknown item type %T", it))
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p pageContent) selfLayoutForOutput(f output.Format) string {
|
|
|
|
if p.selfLayout == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return p.selfLayout + f.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
type rawPageContent struct {
|
|
|
|
hasSummaryDivider bool
|
|
|
|
|
|
|
|
// The AST of the parsed page. Contains information about:
|
|
|
|
// shortcodes, front matter, summary indicators.
|
|
|
|
parsed pageparser.Result
|
|
|
|
|
|
|
|
// Returns the position in bytes after any front matter.
|
|
|
|
posMainContent int
|
|
|
|
|
|
|
|
// These are set if we're able to determine this from the source.
|
|
|
|
posSummaryEnd int
|
|
|
|
posBodyStart int
|
|
|
|
}
|
|
|
|
|
|
|
|
type pageContentReplacement struct {
|
|
|
|
val []byte
|
|
|
|
|
|
|
|
source pageparser.Item
|
|
|
|
}
|
|
|
|
|
|
|
|
type pageContentMap struct {
|
|
|
|
|
|
|
|
// If not, we can skip any pre-rendering of shortcodes.
|
|
|
|
hasMarkdownShortcode bool
|
|
|
|
|
|
|
|
// Indicates whether we must do placeholder replacements.
|
|
|
|
hasNonMarkdownShortcode bool
|
|
|
|
|
|
|
|
// *shortcode, pageContentReplacement or pageparser.Item
|
2022-03-17 17:03:27 -04:00
|
|
|
items []any
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pageContentMap) AddBytes(item pageparser.Item) {
|
|
|
|
p.items = append(p.items, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pageContentMap) AddReplacement(val []byte, source pageparser.Item) {
|
|
|
|
p.items = append(p.items, pageContentReplacement{val: val, source: source})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pageContentMap) AddShortcode(s *shortcode) {
|
|
|
|
p.items = append(p.items, s)
|
2019-04-24 08:05:37 -04:00
|
|
|
if s.insertPlaceholder() {
|
2019-01-02 06:33:26 -05:00
|
|
|
p.hasNonMarkdownShortcode = true
|
2019-04-24 08:05:37 -04:00
|
|
|
} else {
|
|
|
|
p.hasMarkdownShortcode = true
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
}
|