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 (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
"errors"
|
|
|
|
|
2022-02-26 06:52:06 -05:00
|
|
|
"github.com/gohugoio/hugo/common/text"
|
2022-03-10 02:19:03 -05:00
|
|
|
"github.com/gohugoio/hugo/common/types/hstring"
|
2019-11-27 07:42:36 -05:00
|
|
|
"github.com/gohugoio/hugo/identity"
|
2022-05-29 10:41:57 -04:00
|
|
|
"github.com/gohugoio/hugo/parser/pageparser"
|
2022-01-27 03:46:51 -05:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/spf13/cast"
|
2019-11-27 07:42:36 -05:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/markup/converter/hooks"
|
2022-11-24 06:13:19 -05:00
|
|
|
"github.com/gohugoio/hugo/markup/highlight/chromalexers"
|
2023-02-11 10:20:24 -05:00
|
|
|
"github.com/gohugoio/hugo/markup/tableofcontents"
|
2019-11-27 07:42:36 -05:00
|
|
|
|
2019-08-16 09:55:03 -04:00
|
|
|
"github.com/gohugoio/hugo/markup/converter"
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/lazy"
|
|
|
|
|
|
|
|
bp "github.com/gohugoio/hugo/bufferpool"
|
|
|
|
"github.com/gohugoio/hugo/tpl"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2019-08-03 11:27:40 -04:00
|
|
|
"github.com/gohugoio/hugo/output"
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/resources/page"
|
|
|
|
"github.com/gohugoio/hugo/resources/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
nopTargetPath = targetPathsHolder{}
|
|
|
|
nopPagePerOutput = struct {
|
|
|
|
resource.ResourceLinksProvider
|
|
|
|
page.ContentProvider
|
|
|
|
page.PageRenderProvider
|
|
|
|
page.PaginatorProvider
|
|
|
|
page.TableOfContentsProvider
|
|
|
|
page.AlternativeOutputFormatsProvider
|
|
|
|
|
|
|
|
targetPather
|
|
|
|
}{
|
|
|
|
page.NopPage,
|
|
|
|
page.NopPage,
|
|
|
|
page.NopPage,
|
|
|
|
page.NopPage,
|
|
|
|
page.NopPage,
|
|
|
|
page.NopPage,
|
|
|
|
nopTargetPath,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
var pageContentOutputDependenciesID = identity.KeyValueIdentity{Key: "pageOutput", Value: "dependencies"}
|
|
|
|
|
|
|
|
func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, error) {
|
2019-01-02 06:33:26 -05:00
|
|
|
parent := p.init
|
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
var dependencyTracker identity.Manager
|
|
|
|
if p.s.running() {
|
|
|
|
dependencyTracker = identity.NewManager(pageContentOutputDependenciesID)
|
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
cp := &pageContentOutput{
|
|
|
|
dependencyTracker: dependencyTracker,
|
|
|
|
p: p,
|
|
|
|
f: po.f,
|
2020-05-24 07:03:32 -04:00
|
|
|
renderHooks: &renderHooks{},
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
initToC := func(ctx context.Context) (err error) {
|
2019-11-27 07:42:36 -05:00
|
|
|
if p.cmap == nil {
|
|
|
|
// Nothing to do.
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-15 04:56:49 -04:00
|
|
|
|
2022-01-27 03:46:51 -05:00
|
|
|
if err := po.cp.initRenderHooks(); err != nil {
|
2019-11-27 07:42:36 -05:00
|
|
|
return err
|
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
f := po.f
|
2023-02-11 10:20:24 -05:00
|
|
|
cp.contentPlaceholders, err = p.shortcodeState.prepareShortcodesForPage(ctx, p, f)
|
2019-11-27 07:42:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
var hasVariants bool
|
|
|
|
cp.workContent, hasVariants, err = p.contentToRender(ctx, p.source.parsed, p.cmap, cp.contentPlaceholders)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hasVariants {
|
2022-02-17 07:04:00 -05:00
|
|
|
p.pageOutputTemplateVariationsState.Store(2)
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
isHTML := cp.p.m.markup == "html"
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2020-03-09 07:04:33 -04:00
|
|
|
if !isHTML {
|
2023-02-24 01:23:10 -05:00
|
|
|
createAndSetToC := func(tocProvider converter.TableOfContentsProvider) {
|
2020-03-09 07:04:33 -04:00
|
|
|
cfg := p.s.ContentSpec.Converters.GetMarkupConfig()
|
2023-02-11 10:20:24 -05:00
|
|
|
cp.tableOfContents = tocProvider.TableOfContents()
|
|
|
|
cp.tableOfContentsHTML = template.HTML(
|
|
|
|
cp.tableOfContents.ToHTML(
|
2020-03-09 07:04:33 -04:00
|
|
|
cfg.TableOfContents.StartLevel,
|
|
|
|
cfg.TableOfContents.EndLevel,
|
|
|
|
cfg.TableOfContents.Ordered,
|
|
|
|
),
|
|
|
|
)
|
2023-02-24 01:23:10 -05:00
|
|
|
}
|
|
|
|
// If the converter supports doing the parsing separately, we do that.
|
|
|
|
parseResult, ok, err := po.contentRenderer.ParseContent(ctx, cp.workContent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
// This is Goldmark.
|
|
|
|
// Store away the parse result for later use.
|
|
|
|
createAndSetToC(parseResult)
|
|
|
|
cp.astDoc = parseResult.Doc()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is Asciidoctor etc.
|
|
|
|
r, err := po.contentRenderer.ParseAndRenderContent(ctx, cp.workContent, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cp.workContent = r.Bytes()
|
|
|
|
|
|
|
|
if tocProvider, ok := r.(converter.TableOfContentsProvider); ok {
|
|
|
|
createAndSetToC(tocProvider)
|
2020-03-09 07:04:33 -04:00
|
|
|
} else {
|
|
|
|
tmpContent, tmpTableOfContents := helpers.ExtractTOC(cp.workContent)
|
2023-02-11 10:20:24 -05:00
|
|
|
cp.tableOfContentsHTML = helpers.BytesToHTML(tmpTableOfContents)
|
|
|
|
cp.tableOfContents = tableofcontents.Empty
|
2020-03-09 07:04:33 -04:00
|
|
|
cp.workContent = tmpContent
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
2020-03-09 07:04:33 -04:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
initContent := func(ctx context.Context) (err error) {
|
|
|
|
|
|
|
|
p.s.h.IncrContentRender()
|
|
|
|
|
|
|
|
if p.cmap == nil {
|
|
|
|
// Nothing to do.
|
|
|
|
return nil
|
2020-03-09 07:04:33 -04:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2023-02-24 01:23:10 -05:00
|
|
|
if cp.astDoc != nil {
|
|
|
|
// The content is parsed, but not rendered.
|
|
|
|
r, ok, err := po.contentRenderer.RenderContent(ctx, cp.workContent, cp.astDoc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return errors.New("invalid state: astDoc is set but RenderContent returned false")
|
|
|
|
}
|
|
|
|
|
|
|
|
cp.workContent = r.Bytes()
|
|
|
|
}
|
|
|
|
|
2020-03-09 07:04:33 -04:00
|
|
|
if p.cmap.hasNonMarkdownShortcode || cp.placeholdersEnabled {
|
|
|
|
// There are one or more replacement tokens to be replaced.
|
2023-02-11 10:20:24 -05:00
|
|
|
var hasShortcodeVariants bool
|
|
|
|
tokenHandler := func(ctx context.Context, token string) ([]byte, error) {
|
|
|
|
if token == tocShortcodePlaceholder {
|
|
|
|
// The Page's TableOfContents was accessed in a shortcode.
|
|
|
|
if cp.tableOfContentsHTML == "" {
|
|
|
|
cp.p.s.initInit(ctx, cp.initToC, cp.p)
|
|
|
|
}
|
|
|
|
return []byte(cp.tableOfContentsHTML), nil
|
|
|
|
}
|
|
|
|
renderer, found := cp.contentPlaceholders[token]
|
|
|
|
if found {
|
|
|
|
repl, more, err := renderer.renderShortcode(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hasShortcodeVariants = hasShortcodeVariants || more
|
|
|
|
return repl, nil
|
|
|
|
}
|
|
|
|
// This should never happen.
|
|
|
|
return nil, fmt.Errorf("unknown shortcode token %q", token)
|
|
|
|
}
|
|
|
|
|
|
|
|
cp.workContent, err = expandShortcodeTokens(ctx, cp.workContent, tokenHandler)
|
2020-03-09 07:04:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-11 10:20:24 -05:00
|
|
|
if hasShortcodeVariants {
|
|
|
|
p.pageOutputTemplateVariationsState.Store(2)
|
|
|
|
}
|
2020-03-09 07:04:33 -04:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2020-03-09 07:04:33 -04:00
|
|
|
if cp.p.source.hasSummaryDivider {
|
2023-02-11 10:20:24 -05:00
|
|
|
isHTML := cp.p.m.markup == "html"
|
2020-03-09 07:04:33 -04:00
|
|
|
if isHTML {
|
|
|
|
src := p.source.parsed.Input()
|
2019-11-27 07:42:36 -05:00
|
|
|
|
2020-03-09 07:04:33 -04:00
|
|
|
// Use the summary sections as they are provided by the user.
|
|
|
|
if p.source.posSummaryEnd != -1 {
|
|
|
|
cp.summary = helpers.BytesToHTML(src[p.source.posMainContent:p.source.posSummaryEnd])
|
|
|
|
}
|
2019-08-16 09:55:03 -04:00
|
|
|
|
2020-03-09 07:04:33 -04:00
|
|
|
if cp.p.source.posBodyStart != -1 {
|
|
|
|
cp.workContent = src[cp.p.source.posBodyStart:]
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
2020-03-09 07:04:33 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
summary, content, err := splitUserDefinedSummaryAndContent(cp.p.m.markup, cp.workContent)
|
2019-11-27 07:42:36 -05:00
|
|
|
if err != nil {
|
2020-10-21 05:17:48 -04:00
|
|
|
cp.p.s.Log.Errorf("Failed to set user defined summary for page %q: %s", cp.p.pathOrTitle(), err)
|
2020-03-09 07:04:33 -04:00
|
|
|
} else {
|
|
|
|
cp.workContent = content
|
|
|
|
cp.summary = helpers.BytesToHTML(summary)
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
2020-03-09 07:04:33 -04:00
|
|
|
} else if cp.p.m.summary != "" {
|
2023-02-24 01:23:10 -05:00
|
|
|
b, err := po.contentRenderer.ParseAndRenderContent(ctx, []byte(cp.p.m.summary), false)
|
2020-03-09 07:04:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
html := cp.p.s.ContentSpec.TrimShortHTML(b.Bytes())
|
|
|
|
cp.summary = helpers.BytesToHTML(html)
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2020-03-09 07:04:33 -04:00
|
|
|
cp.content = helpers.BytesToHTML(cp.workContent)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
cp.initToC = parent.Branch(func(ctx context.Context) (any, error) {
|
|
|
|
return nil, initToC(ctx)
|
|
|
|
})
|
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
// There may be recursive loops in shortcodes and render hooks.
|
2023-02-11 10:20:24 -05:00
|
|
|
cp.initMain = cp.initToC.BranchWithTimeout(p.s.siteCfg.timeout, func(ctx context.Context) (any, error) {
|
|
|
|
return nil, initContent(ctx)
|
2022-02-17 07:04:00 -05:00
|
|
|
})
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
cp.initPlain = cp.initMain.Branch(func(context.Context) (any, error) {
|
2022-05-25 04:56:14 -04:00
|
|
|
cp.plain = tpl.StripHTML(string(cp.content))
|
2019-11-27 07:42:36 -05:00
|
|
|
cp.plainWords = strings.Fields(cp.plain)
|
|
|
|
cp.setWordCounts(p.m.isCJKLanguage)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
if err := cp.setAutoSummary(); err != nil {
|
|
|
|
return err, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return cp, nil
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2020-05-24 07:03:32 -04:00
|
|
|
type renderHooks struct {
|
2022-02-17 07:04:00 -05:00
|
|
|
getRenderer hooks.GetRendererFunc
|
|
|
|
init sync.Once
|
2020-05-24 07:03:32 -04:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
// pageContentOutput represents the Page content for a given output format.
|
|
|
|
type pageContentOutput struct {
|
|
|
|
f output.Format
|
|
|
|
|
|
|
|
p *pageState
|
|
|
|
|
|
|
|
// Lazy load dependencies
|
2023-02-11 10:20:24 -05:00
|
|
|
initToC *lazy.Init
|
2019-01-02 06:33:26 -05:00
|
|
|
initMain *lazy.Init
|
|
|
|
initPlain *lazy.Init
|
|
|
|
|
|
|
|
placeholdersEnabled bool
|
|
|
|
placeholdersEnabledInit sync.Once
|
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
// Renders Markdown hooks.
|
2020-05-24 07:03:32 -04:00
|
|
|
renderHooks *renderHooks
|
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
workContent []byte
|
|
|
|
dependencyTracker identity.Manager // Set in server mode.
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
// Temporary storage of placeholders mapped to their content.
|
|
|
|
// These are shortcodes etc. Some of these will need to be replaced
|
|
|
|
// after any markup is rendered, so they share a common prefix.
|
2023-02-11 10:20:24 -05:00
|
|
|
contentPlaceholders map[string]shortcodeRenderer
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
// Content sections
|
2023-02-11 10:20:24 -05:00
|
|
|
content template.HTML
|
|
|
|
summary template.HTML
|
|
|
|
tableOfContents *tableofcontents.Fragments
|
|
|
|
tableOfContentsHTML template.HTML
|
2023-02-24 01:23:10 -05:00
|
|
|
// For Goldmark we split Parse and Render.
|
|
|
|
astDoc any
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
truncated bool
|
|
|
|
|
|
|
|
plainWords []string
|
|
|
|
plain string
|
|
|
|
fuzzyWordCount int
|
|
|
|
wordCount int
|
|
|
|
readingTime int
|
|
|
|
}
|
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
func (p *pageContentOutput) trackDependency(id identity.Provider) {
|
|
|
|
if p.dependencyTracker != nil {
|
|
|
|
p.dependencyTracker.Add(id)
|
|
|
|
}
|
2023-02-11 10:20:24 -05:00
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pageContentOutput) Reset() {
|
|
|
|
if p.dependencyTracker != nil {
|
|
|
|
p.dependencyTracker.Reset()
|
|
|
|
}
|
2023-02-11 10:20:24 -05:00
|
|
|
p.initToC.Reset()
|
2019-11-27 07:42:36 -05:00
|
|
|
p.initMain.Reset()
|
|
|
|
p.initPlain.Reset()
|
2020-05-24 07:03:32 -04:00
|
|
|
p.renderHooks = &renderHooks{}
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
2023-03-05 04:32:00 -05:00
|
|
|
func (p *pageContentOutput) Fragments(ctx context.Context) *tableofcontents.Fragments {
|
|
|
|
p.p.s.initInit(ctx, p.initToC, p.p)
|
|
|
|
if p.tableOfContents == nil {
|
|
|
|
return tableofcontents.Empty
|
|
|
|
}
|
|
|
|
return p.tableOfContents
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pageContentOutput) TableOfContents(ctx context.Context) template.HTML {
|
|
|
|
p.p.s.initInit(ctx, p.initToC, p.p)
|
|
|
|
return p.tableOfContentsHTML
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) Content(ctx context.Context) (any, error) {
|
|
|
|
p.p.s.initInit(ctx, p.initMain, p.p)
|
|
|
|
return p.content, nil
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) FuzzyWordCount(ctx context.Context) int {
|
|
|
|
p.p.s.initInit(ctx, p.initPlain, p.p)
|
2019-01-02 06:33:26 -05:00
|
|
|
return p.fuzzyWordCount
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) Len(ctx context.Context) int {
|
|
|
|
p.p.s.initInit(ctx, p.initMain, p.p)
|
2019-01-02 06:33:26 -05:00
|
|
|
return len(p.content)
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) Plain(ctx context.Context) string {
|
|
|
|
p.p.s.initInit(ctx, p.initPlain, p.p)
|
2019-01-02 06:33:26 -05:00
|
|
|
return p.plain
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) PlainWords(ctx context.Context) []string {
|
|
|
|
p.p.s.initInit(ctx, p.initPlain, p.p)
|
2019-01-02 06:33:26 -05:00
|
|
|
return p.plainWords
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) ReadingTime(ctx context.Context) int {
|
|
|
|
p.p.s.initInit(ctx, p.initPlain, p.p)
|
2019-01-02 06:33:26 -05:00
|
|
|
return p.readingTime
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) Summary(ctx context.Context) template.HTML {
|
|
|
|
p.p.s.initInit(ctx, p.initMain, p.p)
|
2019-01-02 06:33:26 -05:00
|
|
|
if !p.p.source.hasSummaryDivider {
|
2023-02-11 10:20:24 -05:00
|
|
|
p.p.s.initInit(ctx, p.initPlain, p.p)
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
return p.summary
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) Truncated(ctx context.Context) bool {
|
2019-01-02 06:33:26 -05:00
|
|
|
if p.p.truncated {
|
|
|
|
return true
|
|
|
|
}
|
2023-02-11 10:20:24 -05:00
|
|
|
p.p.s.initInit(ctx, p.initPlain, p.p)
|
2019-01-02 06:33:26 -05:00
|
|
|
return p.truncated
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) WordCount(ctx context.Context) int {
|
|
|
|
p.p.s.initInit(ctx, p.initPlain, p.p)
|
2019-01-02 06:33:26 -05:00
|
|
|
return p.wordCount
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) RenderString(ctx context.Context, args ...any) (template.HTML, error) {
|
2022-01-27 03:46:51 -05:00
|
|
|
if len(args) < 1 || len(args) > 2 {
|
|
|
|
return "", errors.New("want 1 or 2 arguments")
|
|
|
|
}
|
|
|
|
|
2022-05-29 10:41:57 -04:00
|
|
|
var contentToRender string
|
2022-01-27 03:46:51 -05:00
|
|
|
opts := defaultRenderStringOpts
|
|
|
|
sidx := 1
|
|
|
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
sidx = 0
|
|
|
|
} else {
|
2022-03-17 17:03:27 -04:00
|
|
|
m, ok := args[0].(map[string]any)
|
2022-01-27 03:46:51 -05:00
|
|
|
if !ok {
|
|
|
|
return "", errors.New("first argument must be a map")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mapstructure.WeakDecode(m, &opts); err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
return "", fmt.Errorf("failed to decode options: %w", err)
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-29 10:41:57 -04:00
|
|
|
contentToRenderv := args[sidx]
|
2022-03-10 02:19:03 -05:00
|
|
|
|
2022-05-29 10:41:57 -04:00
|
|
|
if _, ok := contentToRenderv.(hstring.RenderedString); ok {
|
2022-03-10 02:19:03 -05:00
|
|
|
// This content is already rendered, this is potentially
|
|
|
|
// a infinite recursion.
|
|
|
|
return "", errors.New("text is already rendered, repeating it may cause infinite recursion")
|
|
|
|
}
|
|
|
|
|
2022-01-27 03:46:51 -05:00
|
|
|
var err error
|
2022-05-29 10:41:57 -04:00
|
|
|
contentToRender, err = cast.ToStringE(contentToRenderv)
|
2022-01-27 03:46:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = p.initRenderHooks(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
conv := p.p.getContentConverter()
|
|
|
|
if opts.Markup != "" && opts.Markup != p.p.m.markup {
|
|
|
|
var err error
|
|
|
|
// TODO(bep) consider cache
|
2022-05-28 05:01:47 -04:00
|
|
|
conv, err = p.p.m.newContentConverter(p.p, opts.Markup)
|
2022-01-27 03:46:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", p.p.wrapError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-29 10:41:57 -04:00
|
|
|
var rendered []byte
|
|
|
|
|
2023-01-26 04:30:25 -05:00
|
|
|
if pageparser.HasShortcode(contentToRender) {
|
|
|
|
// String contains a shortcode.
|
2022-05-29 10:41:57 -04:00
|
|
|
parsed, err := pageparser.ParseMain(strings.NewReader(contentToRender), pageparser.Config{})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
pm := &pageContentMap{
|
|
|
|
items: make([]any, 0, 20),
|
|
|
|
}
|
|
|
|
s := newShortcodeHandler(p.p, p.p.s)
|
|
|
|
|
|
|
|
if err := p.p.mapContentForResult(
|
|
|
|
parsed,
|
|
|
|
s,
|
|
|
|
pm,
|
|
|
|
opts.Markup,
|
|
|
|
nil,
|
|
|
|
); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
placeholders, err := s.prepareShortcodesForPage(ctx, p.p, p.f)
|
2022-05-29 10:41:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
contentToRender, hasVariants, err := p.p.contentToRender(ctx, parsed, pm, placeholders)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if hasVariants {
|
2022-05-29 10:41:57 -04:00
|
|
|
p.p.pageOutputTemplateVariationsState.Store(2)
|
|
|
|
}
|
2023-02-11 10:20:24 -05:00
|
|
|
b, err := p.renderContentWithConverter(ctx, conv, contentToRender, false)
|
2022-05-29 10:41:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", p.p.wrapError(err)
|
|
|
|
}
|
|
|
|
rendered = b.Bytes()
|
2022-01-27 03:46:51 -05:00
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
if pm.hasNonMarkdownShortcode || p.placeholdersEnabled {
|
|
|
|
var hasShortcodeVariants bool
|
|
|
|
|
|
|
|
tokenHandler := func(ctx context.Context, token string) ([]byte, error) {
|
|
|
|
if token == tocShortcodePlaceholder {
|
|
|
|
// The Page's TableOfContents was accessed in a shortcode.
|
|
|
|
if p.tableOfContentsHTML == "" {
|
|
|
|
p.p.s.initInit(ctx, p.initToC, p.p)
|
|
|
|
}
|
|
|
|
return []byte(p.tableOfContentsHTML), nil
|
|
|
|
}
|
|
|
|
renderer, found := placeholders[token]
|
|
|
|
if found {
|
|
|
|
repl, more, err := renderer.renderShortcode(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hasShortcodeVariants = hasShortcodeVariants || more
|
|
|
|
return repl, nil
|
|
|
|
}
|
|
|
|
// This should not happen.
|
|
|
|
return nil, fmt.Errorf("unknown shortcode token %q", token)
|
2022-05-29 10:41:57 -04:00
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
rendered, err = expandShortcodeTokens(ctx, rendered, tokenHandler)
|
2022-05-29 10:41:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-02-11 10:20:24 -05:00
|
|
|
if hasShortcodeVariants {
|
|
|
|
p.p.pageOutputTemplateVariationsState.Store(2)
|
|
|
|
}
|
2022-05-29 10:41:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need a consolidated view in $page.HasShortcode
|
|
|
|
p.p.shortcodeState.transferNames(s)
|
|
|
|
|
|
|
|
} else {
|
2023-02-11 10:20:24 -05:00
|
|
|
c, err := p.renderContentWithConverter(ctx, conv, []byte(contentToRender), false)
|
2022-05-29 10:41:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", p.p.wrapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rendered = c.Bytes()
|
|
|
|
}
|
2022-01-27 03:46:51 -05:00
|
|
|
|
|
|
|
if opts.Display == "inline" {
|
|
|
|
// We may have to rethink this in the future when we get other
|
|
|
|
// renderers.
|
2022-05-29 10:41:57 -04:00
|
|
|
rendered = p.p.s.ContentSpec.TrimShortHTML(rendered)
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
|
|
|
|
2022-05-29 10:41:57 -04:00
|
|
|
return template.HTML(string(rendered)), nil
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) RenderWithTemplateInfo(ctx context.Context, info tpl.Info, layout ...string) (template.HTML, error) {
|
2022-01-27 03:46:51 -05:00
|
|
|
p.p.addDependency(info)
|
2023-02-11 10:20:24 -05:00
|
|
|
return p.Render(ctx, layout...)
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func (p *pageContentOutput) Render(ctx context.Context, layout ...string) (template.HTML, error) {
|
2022-01-27 03:46:51 -05:00
|
|
|
templ, found, err := p.p.resolveTemplate(layout...)
|
|
|
|
if err != nil {
|
|
|
|
return "", p.p.wrapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.p.addDependency(templ.(tpl.Info))
|
|
|
|
|
|
|
|
// Make sure to send the *pageState and not the *pageContentOutput to the template.
|
2023-02-11 10:20:24 -05:00
|
|
|
res, err := executeToString(ctx, p.p.s.Tmpl(), templ, p.p)
|
2022-01-27 03:46:51 -05:00
|
|
|
if err != nil {
|
2022-05-12 05:43:20 -04:00
|
|
|
return "", p.p.wrapError(fmt.Errorf("failed to execute template %s: %w", templ.Name(), err))
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
|
|
|
return template.HTML(res), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pageContentOutput) initRenderHooks() error {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.renderHooks.init.Do(func() {
|
2022-02-17 07:04:00 -05:00
|
|
|
if p.p.pageOutputTemplateVariationsState.Load() == 0 {
|
|
|
|
p.p.pageOutputTemplateVariationsState.Store(1)
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
type cacheKey struct {
|
|
|
|
tp hooks.RendererType
|
2022-03-17 17:03:27 -04:00
|
|
|
id any
|
2022-02-17 07:04:00 -05:00
|
|
|
f output.Format
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
renderCache := make(map[cacheKey]any)
|
2022-02-17 07:04:00 -05:00
|
|
|
var renderCacheMu sync.Mutex
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
resolvePosition := func(ctx any) text.Position {
|
2022-02-26 06:52:06 -05:00
|
|
|
var offset int
|
|
|
|
|
|
|
|
switch v := ctx.(type) {
|
|
|
|
case hooks.CodeblockContext:
|
2022-02-27 07:31:55 -05:00
|
|
|
offset = bytes.Index(p.p.source.parsed.Input(), []byte(v.Inner()))
|
2022-02-26 06:52:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pos := p.p.posFromInput(p.p.source.parsed.Input(), offset)
|
|
|
|
|
|
|
|
if pos.LineNumber > 0 {
|
|
|
|
// Move up to the code fence delimiter.
|
|
|
|
// This is in line with how we report on shortcodes.
|
|
|
|
pos.LineNumber = pos.LineNumber - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
p.renderHooks.getRenderer = func(tp hooks.RendererType, id any) any {
|
2022-02-17 07:04:00 -05:00
|
|
|
renderCacheMu.Lock()
|
|
|
|
defer renderCacheMu.Unlock()
|
|
|
|
|
|
|
|
key := cacheKey{tp: tp, id: id, f: p.f}
|
|
|
|
if r, ok := renderCache[key]; ok {
|
|
|
|
return r
|
|
|
|
}
|
2022-01-27 03:46:51 -05:00
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
layoutDescriptor := p.p.getLayoutDescriptor()
|
|
|
|
layoutDescriptor.RenderingHook = true
|
|
|
|
layoutDescriptor.LayoutOverride = false
|
|
|
|
layoutDescriptor.Layout = ""
|
|
|
|
|
|
|
|
switch tp {
|
|
|
|
case hooks.LinkRendererType:
|
|
|
|
layoutDescriptor.Kind = "render-link"
|
|
|
|
case hooks.ImageRendererType:
|
|
|
|
layoutDescriptor.Kind = "render-image"
|
|
|
|
case hooks.HeadingRendererType:
|
|
|
|
layoutDescriptor.Kind = "render-heading"
|
|
|
|
case hooks.CodeBlockRendererType:
|
|
|
|
layoutDescriptor.Kind = "render-codeblock"
|
|
|
|
if id != nil {
|
|
|
|
lang := id.(string)
|
2022-11-24 06:13:19 -05:00
|
|
|
lexer := chromalexers.Get(lang)
|
2022-02-17 07:04:00 -05:00
|
|
|
if lexer != nil {
|
|
|
|
layoutDescriptor.KindVariants = strings.Join(lexer.Config().Aliases, ",")
|
|
|
|
} else {
|
|
|
|
layoutDescriptor.KindVariants = lang
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
2022-02-17 07:04:00 -05:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 03:46:51 -05:00
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
getHookTemplate := func(f output.Format) (tpl.Template, bool) {
|
|
|
|
templ, found, err := p.p.s.Tmpl().LookupLayout(layoutDescriptor, f)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return templ, found
|
|
|
|
}
|
|
|
|
|
|
|
|
templ, found1 := getHookTemplate(p.f)
|
2022-01-27 03:46:51 -05:00
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
if p.p.reusePageOutputContent() {
|
|
|
|
// Check if some of the other output formats would give a different template.
|
|
|
|
for _, f := range p.p.s.renderFormats {
|
|
|
|
if f.Name == p.f.Name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
templ2, found2 := getHookTemplate(f)
|
|
|
|
if found2 {
|
|
|
|
if !found1 {
|
|
|
|
templ = templ2
|
|
|
|
found1 = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if templ != templ2 {
|
|
|
|
p.p.pageOutputTemplateVariationsState.Store(2)
|
|
|
|
break
|
|
|
|
}
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
2022-02-17 07:04:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found1 {
|
|
|
|
if tp == hooks.CodeBlockRendererType {
|
|
|
|
// No user provided tempplate for code blocks, so we use the native Go code version -- which is also faster.
|
|
|
|
r := p.p.s.ContentSpec.Converters.GetHighlighter()
|
|
|
|
renderCache[key] = r
|
|
|
|
return r
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
2022-02-17 07:04:00 -05:00
|
|
|
return nil
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
2022-02-17 07:04:00 -05:00
|
|
|
|
|
|
|
r := hookRendererTemplate{
|
|
|
|
templateHandler: p.p.s.Tmpl(),
|
|
|
|
SearchProvider: templ.(identity.SearchProvider),
|
|
|
|
templ: templ,
|
2022-02-26 06:52:06 -05:00
|
|
|
resolvePosition: resolvePosition,
|
2022-02-17 07:04:00 -05:00
|
|
|
}
|
|
|
|
renderCache[key] = r
|
|
|
|
return r
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
return nil
|
2022-01-27 03:46:51 -05:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func (p *pageContentOutput) setAutoSummary() error {
|
2019-04-05 13:11:04 -04:00
|
|
|
if p.p.source.hasSummaryDivider || p.p.m.summary != "" {
|
2019-01-02 06:33:26 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var summary string
|
|
|
|
var truncated bool
|
|
|
|
|
|
|
|
if p.p.m.isCJKLanguage {
|
|
|
|
summary, truncated = p.p.s.ContentSpec.TruncateWordsByRune(p.plainWords)
|
|
|
|
} else {
|
|
|
|
summary, truncated = p.p.s.ContentSpec.TruncateWordsToWholeSentence(p.plain)
|
|
|
|
}
|
|
|
|
p.summary = template.HTML(summary)
|
|
|
|
|
|
|
|
p.truncated = truncated
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-24 01:23:10 -05:00
|
|
|
func (cp *pageContentOutput) getContentConverter() (converter.Converter, error) {
|
2022-02-17 07:04:00 -05:00
|
|
|
if err := cp.initRenderHooks(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-24 01:23:10 -05:00
|
|
|
return cp.p.getContentConverter(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *pageContentOutput) ParseAndRenderContent(ctx context.Context, content []byte, renderTOC bool) (converter.ResultRender, error) {
|
|
|
|
c, err := cp.getContentConverter()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-11 10:20:24 -05:00
|
|
|
return cp.renderContentWithConverter(ctx, c, content, renderTOC)
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
2023-02-24 01:23:10 -05:00
|
|
|
func (cp *pageContentOutput) ParseContent(ctx context.Context, content []byte) (converter.ResultParse, bool, error) {
|
|
|
|
c, err := cp.getContentConverter()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
p, ok := c.(converter.ParseRenderer)
|
|
|
|
if !ok {
|
|
|
|
return nil, ok, nil
|
|
|
|
}
|
|
|
|
rctx := converter.RenderContext{
|
2023-02-25 03:24:59 -05:00
|
|
|
Ctx: ctx,
|
2023-02-24 01:23:10 -05:00
|
|
|
Src: content,
|
|
|
|
RenderTOC: true,
|
|
|
|
GetRenderer: cp.renderHooks.getRenderer,
|
|
|
|
}
|
|
|
|
r, err := p.Parse(rctx)
|
|
|
|
return r, ok, err
|
|
|
|
|
|
|
|
}
|
|
|
|
func (cp *pageContentOutput) RenderContent(ctx context.Context, content []byte, doc any) (converter.ResultRender, bool, error) {
|
|
|
|
c, err := cp.getContentConverter()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
p, ok := c.(converter.ParseRenderer)
|
|
|
|
if !ok {
|
|
|
|
return nil, ok, nil
|
|
|
|
}
|
|
|
|
rctx := converter.RenderContext{
|
2023-02-25 03:24:59 -05:00
|
|
|
Ctx: ctx,
|
2023-02-24 01:23:10 -05:00
|
|
|
Src: content,
|
|
|
|
RenderTOC: true,
|
|
|
|
GetRenderer: cp.renderHooks.getRenderer,
|
|
|
|
}
|
|
|
|
r, err := p.Render(rctx, doc)
|
|
|
|
if err == nil {
|
|
|
|
if ids, ok := r.(identity.IdentitiesProvider); ok {
|
|
|
|
for _, v := range ids.GetIdentities() {
|
|
|
|
cp.trackDependency(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, ok, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *pageContentOutput) renderContentWithConverter(ctx context.Context, c converter.Converter, content []byte, renderTOC bool) (converter.ResultRender, error) {
|
2019-11-27 07:42:36 -05:00
|
|
|
r, err := c.Convert(
|
2019-08-16 09:55:03 -04:00
|
|
|
converter.RenderContext{
|
2023-02-25 03:24:59 -05:00
|
|
|
Ctx: ctx,
|
2019-11-27 07:42:36 -05:00
|
|
|
Src: content,
|
|
|
|
RenderTOC: renderTOC,
|
2022-02-17 07:04:00 -05:00
|
|
|
GetRenderer: cp.renderHooks.getRenderer,
|
2019-08-16 09:55:03 -04:00
|
|
|
})
|
2019-11-27 07:42:36 -05:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if ids, ok := r.(identity.IdentitiesProvider); ok {
|
|
|
|
for _, v := range ids.GetIdentities() {
|
|
|
|
cp.trackDependency(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, err
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pageContentOutput) setWordCounts(isCJKLanguage bool) {
|
|
|
|
if isCJKLanguage {
|
|
|
|
p.wordCount = 0
|
|
|
|
for _, word := range p.plainWords {
|
|
|
|
runeCount := utf8.RuneCountInString(word)
|
|
|
|
if len(word) == runeCount {
|
|
|
|
p.wordCount++
|
|
|
|
} else {
|
|
|
|
p.wordCount += runeCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.wordCount = helpers.TotalWords(p.plain)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bep) is set in a test. Fix that.
|
|
|
|
if p.fuzzyWordCount == 0 {
|
|
|
|
p.fuzzyWordCount = (p.wordCount + 100) / 100 * 100
|
|
|
|
}
|
|
|
|
|
|
|
|
if isCJKLanguage {
|
|
|
|
p.readingTime = (p.wordCount + 500) / 501
|
|
|
|
} else {
|
|
|
|
p.readingTime = (p.wordCount + 212) / 213
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A callback to signal that we have inserted a placeholder into the rendered
|
|
|
|
// content. This avoids doing extra replacement work.
|
|
|
|
func (p *pageContentOutput) enablePlaceholders() {
|
|
|
|
p.placeholdersEnabledInit.Do(func() {
|
|
|
|
p.placeholdersEnabled = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// these will be shifted out when rendering a given output format.
|
|
|
|
type pagePerOutputProviders interface {
|
|
|
|
targetPather
|
|
|
|
page.PaginatorProvider
|
|
|
|
resource.ResourceLinksProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
type targetPather interface {
|
|
|
|
targetPaths() page.TargetPaths
|
|
|
|
}
|
|
|
|
|
|
|
|
type targetPathsHolder struct {
|
|
|
|
paths page.TargetPaths
|
|
|
|
page.OutputFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t targetPathsHolder) targetPaths() page.TargetPaths {
|
|
|
|
return t.paths
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:20:24 -05:00
|
|
|
func executeToString(ctx context.Context, h tpl.TemplateHandler, templ tpl.Template, data any) (string, error) {
|
2019-01-02 06:33:26 -05:00
|
|
|
b := bp.GetBuffer()
|
|
|
|
defer bp.PutBuffer(b)
|
2023-02-11 10:20:24 -05:00
|
|
|
if err := h.ExecuteWithContext(ctx, templ, b, data); err != nil {
|
2019-01-02 06:33:26 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitUserDefinedSummaryAndContent(markup string, c []byte) (summary []byte, content []byte, err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("summary split failed: %s", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
startDivider := bytes.Index(c, internalSummaryDividerBaseBytes)
|
|
|
|
|
|
|
|
if startDivider == -1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
startTag := "p"
|
|
|
|
switch markup {
|
Rework external asciidoctor integration
This commit solves the relative path problem with asciidoctor tooling. An include will resolve relatively, so you can refer easily to files in the same folder.
Also `asciidoctor-diagram` and PlantUML rendering works now, because the created temporary files will be placed in the correct folder.
This patch covers just the Ruby version of asciidoctor. The old AsciiDoc CLI EOLs in Jan 2020, so this variant is removed from code.
The configuration is completely rewritten and now available in `config.toml` under the key `[markup.asciidocext]`:
```toml
[markup.asciidocext]
extensions = ["asciidoctor-html5s", "asciidoctor-diagram"]
workingFolderCurrent = true
trace = true
[markup.asciidocext.attributes]
my-base-url = "https://example.com/"
my-attribute-name = "my value"
```
- backends, safe-modes, and extensions are now whitelisted to the popular (ruby) extensions and valid values.
- the default for extensions is to not enable any, because they're all external dependencies so the build would break if the user didn't install them beforehand.
- the default backend is html5 because html5s is an external gem dependency.
- the default safe-mode is safe, explanations of the modes: https://asciidoctor.org/man/asciidoctor/
- the config is namespaced under asciidocext_config and the parser looks at asciidocext to allow a future native Go asciidoc.
- `uglyUrls=true` option and `--source` flag are supported
- `--destination` flag is required
Follow the updated documentation under `docs/content/en/content-management/formats.md`.
This patch would be a breaking change, because you need to correct all your absolute include pathes to relative paths, so using relative paths must be configured explicitly by setting `workingFolderCurrent = true`.
2020-06-25 03:51:33 -04:00
|
|
|
case "asciidocext":
|
2019-01-02 06:33:26 -05:00
|
|
|
startTag = "div"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk back and forward to the surrounding tags.
|
|
|
|
start := bytes.LastIndex(c[:startDivider], []byte("<"+startTag))
|
|
|
|
end := bytes.Index(c[startDivider:], []byte("</"+startTag))
|
|
|
|
|
|
|
|
if start == -1 {
|
|
|
|
start = startDivider
|
|
|
|
} else {
|
|
|
|
start = startDivider - (startDivider - start)
|
|
|
|
}
|
|
|
|
|
|
|
|
if end == -1 {
|
|
|
|
end = startDivider + len(internalSummaryDividerBase)
|
|
|
|
} else {
|
|
|
|
end = startDivider + end + len(startTag) + 3
|
|
|
|
}
|
|
|
|
|
|
|
|
var addDiv bool
|
|
|
|
|
|
|
|
switch markup {
|
|
|
|
case "rst":
|
|
|
|
addDiv = true
|
|
|
|
}
|
|
|
|
|
|
|
|
withoutDivider := append(c[:start], bytes.Trim(c[end:], "\n")...)
|
|
|
|
|
|
|
|
if len(withoutDivider) > 0 {
|
|
|
|
summary = bytes.TrimSpace(withoutDivider[:start])
|
|
|
|
}
|
|
|
|
|
|
|
|
if addDiv {
|
|
|
|
// For the rst
|
|
|
|
summary = append(append([]byte(nil), summary...), []byte("</div>")...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
content = bytes.TrimSpace(withoutDivider)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|