mirror of
https://github.com/gohugoio/hugo.git
synced 2024-05-13 04:52:48 +00:00
Compare commits
16 commits
e027192787
...
0fd5e274bd
Author | SHA1 | Date | |
---|---|---|---|
0fd5e274bd | |||
4e483f5d4a | |||
06d248910c | |||
004b694390 | |||
da6112fc65 | |||
faf9fedc3d | |||
fbbcca40c8 | |||
addc311e56 | |||
6c8f472712 | |||
c38633a0c4 | |||
35461c167f | |||
aaed290b02 | |||
f1b1e5f41b | |||
769439cc5b | |||
6de4f5cde5 | |||
d362fae970 |
16
cache/dynacache/dynacache.go
vendored
16
cache/dynacache/dynacache.go
vendored
|
@ -140,16 +140,25 @@ func (c *Cache) DrainEvictedIdentities() []identity.Identity {
|
|||
}
|
||||
|
||||
// ClearMatching clears all partition for which the predicate returns true.
|
||||
func (c *Cache) ClearMatching(predicate func(k, v any) bool) {
|
||||
func (c *Cache) ClearMatching(predicatePartition func(k string, p PartitionManager) bool, predicateValue func(k, v any) bool) {
|
||||
if predicatePartition == nil {
|
||||
predicatePartition = func(k string, p PartitionManager) bool { return true }
|
||||
}
|
||||
if predicateValue == nil {
|
||||
panic("nil predicateValue")
|
||||
}
|
||||
g := rungroup.Run[PartitionManager](context.Background(), rungroup.Config[PartitionManager]{
|
||||
NumWorkers: len(c.partitions),
|
||||
Handle: func(ctx context.Context, partition PartitionManager) error {
|
||||
partition.clearMatching(predicate)
|
||||
partition.clearMatching(predicateValue)
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
for _, p := range c.partitions {
|
||||
for k, p := range c.partitions {
|
||||
if !predicatePartition(k, p) {
|
||||
continue
|
||||
}
|
||||
g.Enqueue(p)
|
||||
}
|
||||
|
||||
|
@ -356,6 +365,7 @@ func GetOrCreatePartition[K comparable, V any](c *Cache, name string, opts Optio
|
|||
trace: c.opts.Log.Logger().WithLevel(logg.LevelTrace).WithField("partition", name),
|
||||
opts: opts,
|
||||
}
|
||||
|
||||
c.partitions[name] = partition
|
||||
|
||||
return partition
|
||||
|
|
2
cache/dynacache/dynacache_test.go
vendored
2
cache/dynacache/dynacache_test.go
vendored
|
@ -156,7 +156,7 @@ func TestClear(t *testing.T) {
|
|||
|
||||
cache = newTestCache(t)
|
||||
|
||||
cache.ClearMatching(func(k, v any) bool {
|
||||
cache.ClearMatching(nil, func(k, v any) bool {
|
||||
return k.(string) == "clearOnRebuild"
|
||||
})
|
||||
|
||||
|
|
|
@ -128,6 +128,7 @@ type rootCommand struct {
|
|||
verbose bool
|
||||
debug bool
|
||||
quiet bool
|
||||
devMode bool // Hidden flag.
|
||||
|
||||
renderToMemory bool
|
||||
|
||||
|
@ -423,29 +424,33 @@ func (r *rootCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
|
|||
func (r *rootCommand) createLogger(running bool) (loggers.Logger, error) {
|
||||
level := logg.LevelWarn
|
||||
|
||||
if r.logLevel != "" {
|
||||
switch strings.ToLower(r.logLevel) {
|
||||
case "debug":
|
||||
level = logg.LevelDebug
|
||||
case "info":
|
||||
level = logg.LevelInfo
|
||||
case "warn", "warning":
|
||||
level = logg.LevelWarn
|
||||
case "error":
|
||||
level = logg.LevelError
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid log level: %q, must be one of debug, warn, info or error", r.logLevel)
|
||||
}
|
||||
if r.devMode {
|
||||
level = logg.LevelTrace
|
||||
} else {
|
||||
if r.verbose {
|
||||
hugo.Deprecate("--verbose", "use --logLevel info", "v0.114.0")
|
||||
hugo.Deprecate("--verbose", "use --logLevel info", "v0.114.0")
|
||||
level = logg.LevelInfo
|
||||
}
|
||||
if r.logLevel != "" {
|
||||
switch strings.ToLower(r.logLevel) {
|
||||
case "debug":
|
||||
level = logg.LevelDebug
|
||||
case "info":
|
||||
level = logg.LevelInfo
|
||||
case "warn", "warning":
|
||||
level = logg.LevelWarn
|
||||
case "error":
|
||||
level = logg.LevelError
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid log level: %q, must be one of debug, warn, info or error", r.logLevel)
|
||||
}
|
||||
} else {
|
||||
if r.verbose {
|
||||
hugo.Deprecate("--verbose", "use --logLevel info", "v0.114.0")
|
||||
hugo.Deprecate("--verbose", "use --logLevel info", "v0.114.0")
|
||||
level = logg.LevelInfo
|
||||
}
|
||||
|
||||
if r.debug {
|
||||
hugo.Deprecate("--debug", "use --logLevel debug", "v0.114.0")
|
||||
level = logg.LevelDebug
|
||||
if r.debug {
|
||||
hugo.Deprecate("--debug", "use --logLevel debug", "v0.114.0")
|
||||
level = logg.LevelDebug
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -505,10 +510,13 @@ Complete documentation is available at https://gohugo.io/.`
|
|||
|
||||
cmd.PersistentFlags().BoolVarP(&r.verbose, "verbose", "v", false, "verbose output")
|
||||
cmd.PersistentFlags().BoolVarP(&r.debug, "debug", "", false, "debug output")
|
||||
cmd.PersistentFlags().BoolVarP(&r.devMode, "devMode", "", false, "only used for internal testing, flag hidden.")
|
||||
cmd.PersistentFlags().StringVar(&r.logLevel, "logLevel", "", "log level (debug|info|warn|error)")
|
||||
_ = cmd.RegisterFlagCompletionFunc("logLevel", cobra.FixedCompletions([]string{"debug", "info", "warn", "error"}, cobra.ShellCompDirectiveNoFileComp))
|
||||
cmd.Flags().BoolVarP(&r.buildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
|
||||
|
||||
cmd.PersistentFlags().MarkHidden("devMode")
|
||||
|
||||
// Configure local flags
|
||||
applyLocalFlagsBuild(cmd, r)
|
||||
|
||||
|
|
|
@ -45,9 +45,10 @@ func newGenCommand() *genCommand {
|
|||
genmandir string
|
||||
|
||||
// Chroma flags.
|
||||
style string
|
||||
highlightStyle string
|
||||
linesStyle string
|
||||
style string
|
||||
highlightStyle string
|
||||
lineNumbersInlineStyle string
|
||||
lineNumbersTableStyle string
|
||||
)
|
||||
|
||||
newChromaStyles := func() simplecobra.Commander {
|
||||
|
@ -63,8 +64,11 @@ See https://xyproto.github.io/splash/docs/all.html for a preview of the availabl
|
|||
if highlightStyle != "" {
|
||||
builder.Add(chroma.LineHighlight, highlightStyle)
|
||||
}
|
||||
if linesStyle != "" {
|
||||
builder.Add(chroma.LineNumbers, linesStyle)
|
||||
if lineNumbersInlineStyle != "" {
|
||||
builder.Add(chroma.LineNumbers, lineNumbersInlineStyle)
|
||||
}
|
||||
if lineNumbersTableStyle != "" {
|
||||
builder.Add(chroma.LineNumbersTable, lineNumbersTableStyle)
|
||||
}
|
||||
style, err := builder.Build()
|
||||
if err != nil {
|
||||
|
@ -80,8 +84,10 @@ See https://xyproto.github.io/splash/docs/all.html for a preview of the availabl
|
|||
_ = cmd.RegisterFlagCompletionFunc("style", cobra.NoFileCompletions)
|
||||
cmd.PersistentFlags().StringVar(&highlightStyle, "highlightStyle", "", `foreground and background colors for highlighted lines, e.g. --highlightStyle "#fff000 bg:#000fff"`)
|
||||
_ = cmd.RegisterFlagCompletionFunc("highlightStyle", cobra.NoFileCompletions)
|
||||
cmd.PersistentFlags().StringVar(&linesStyle, "linesStyle", "", `foreground and background colors for inline line numbers, e.g. --linesStyle "#fff000 bg:#000fff"`)
|
||||
_ = cmd.RegisterFlagCompletionFunc("linesStyle", cobra.NoFileCompletions)
|
||||
cmd.PersistentFlags().StringVar(&lineNumbersInlineStyle, "lineNumbersInlineStyle", "", `foreground and background colors for inline line numbers, e.g. --lineNumbersInlineStyle "#fff000 bg:#000fff"`)
|
||||
_ = cmd.RegisterFlagCompletionFunc("lineNumbersInlineStyle", cobra.NoFileCompletions)
|
||||
cmd.PersistentFlags().StringVar(&lineNumbersTableStyle, "lineNumbersTableStyle", "", `foreground and background colors for table line numbers, e.g. --lineNumbersTableStyle "#fff000 bg:#000fff"`)
|
||||
_ = cmd.RegisterFlagCompletionFunc("lineNumbersTableStyle", cobra.NoFileCompletions)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ package hugo
|
|||
// This should be the only one.
|
||||
var CurrentVersion = Version{
|
||||
Major: 0,
|
||||
Minor: 126,
|
||||
PatchLevel: 0,
|
||||
Suffix: "-DEV",
|
||||
Minor: 125,
|
||||
PatchLevel: 2,
|
||||
Suffix: "",
|
||||
}
|
||||
|
|
|
@ -20,10 +20,11 @@ hugo gen chromastyles [flags] [args]
|
|||
### Options
|
||||
|
||||
```
|
||||
-h, --help help for chromastyles
|
||||
--highlightStyle string style used for highlighting lines (see https://github.com/alecthomas/chroma)
|
||||
--linesStyle string style used for line numbers (see https://github.com/alecthomas/chroma)
|
||||
--style string highlighter style (see https://xyproto.github.io/splash/docs/) (default "friendly")
|
||||
-h, --help help for chromastyles
|
||||
--highlightStyle string foreground and background colors for highlighted lines, e.g. --highlightStyle "#fff000 bg:#000fff"
|
||||
--lineNumbersInlineStyle string foreground and background colors for inline line numbers, e.g. --lineNumbersInlineStyle "#fff000 bg:#000fff"
|
||||
--lineNumbersTableStyle string foreground and background colors for table line numbers, e.g. --lineNumbersTableStyle "#fff000 bg:#000fff"
|
||||
--style string highlighter style (see https://xyproto.github.io/splash/docs/) (default "friendly")
|
||||
```
|
||||
|
||||
### Options inherited from parent commands
|
||||
|
|
|
@ -16,6 +16,7 @@ package hqt
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
|
@ -38,6 +39,11 @@ var IsSameType qt.Checker = &typeChecker{
|
|||
argNames: []string{"got", "want"},
|
||||
}
|
||||
|
||||
// IsSameFloat64 asserts that two float64 values are equal within a small delta.
|
||||
var IsSameFloat64 = qt.CmpEquals(cmp.Comparer(func(a, b float64) bool {
|
||||
return math.Abs(a-b) < 0.0001
|
||||
}))
|
||||
|
||||
type argNames []string
|
||||
|
||||
func (a argNames) ArgNames() []string {
|
||||
|
|
|
@ -1084,7 +1084,7 @@ func (h *HugoSites) resolveAndClearStateForIdentities(
|
|||
return b
|
||||
}
|
||||
|
||||
h.MemCache.ClearMatching(shouldDelete)
|
||||
h.MemCache.ClearMatching(nil, shouldDelete)
|
||||
|
||||
return ll, nil
|
||||
}); err != nil {
|
||||
|
|
|
@ -23,6 +23,7 @@ import (
|
|||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/bep/logg"
|
||||
|
@ -46,6 +47,7 @@ import (
|
|||
"github.com/gohugoio/hugo/resources/page"
|
||||
"github.com/gohugoio/hugo/resources/page/siteidentities"
|
||||
"github.com/gohugoio/hugo/resources/postpub"
|
||||
"github.com/gohugoio/hugo/resources/resource"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
|
||||
|
@ -758,15 +760,45 @@ func (h *HugoSites) processPartial(ctx context.Context, l logg.LevelLogger, conf
|
|||
}
|
||||
}
|
||||
case files.ComponentFolderAssets:
|
||||
logger.Println("Asset changed", pathInfo.Path())
|
||||
p := pathInfo.Path()
|
||||
logger.Println("Asset changed", p)
|
||||
|
||||
var matches []any
|
||||
var mu sync.Mutex
|
||||
|
||||
h.MemCache.ClearMatching(
|
||||
func(k string, pm dynacache.PartitionManager) bool {
|
||||
// Avoid going through everything.
|
||||
return strings.HasPrefix(k, "/res")
|
||||
},
|
||||
func(k, v any) bool {
|
||||
if strings.Contains(k.(string), p) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
switch vv := v.(type) {
|
||||
case resource.Resources:
|
||||
// GetMatch/Match.
|
||||
for _, r := range vv {
|
||||
matches = append(matches, r)
|
||||
}
|
||||
return true
|
||||
default:
|
||||
matches = append(matches, vv)
|
||||
return true
|
||||
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
var hasID bool
|
||||
r, _ := h.ResourceSpec.ResourceCache.Get(context.Background(), dynacache.CleanKey(pathInfo.Base()))
|
||||
identity.WalkIdentitiesShallow(r, func(level int, rid identity.Identity) bool {
|
||||
hasID = true
|
||||
changes = append(changes, rid)
|
||||
return false
|
||||
})
|
||||
for _, r := range matches {
|
||||
identity.WalkIdentitiesShallow(r, func(level int, rid identity.Identity) bool {
|
||||
hasID = true
|
||||
changes = append(changes, rid)
|
||||
return false
|
||||
})
|
||||
}
|
||||
if !hasID {
|
||||
changes = append(changes, pathInfo)
|
||||
}
|
||||
|
|
|
@ -676,3 +676,37 @@ menu: main
|
|||
b.AssertFileContent("public/fr/index.html", `<a href="/fr/p1/">p1</a>`)
|
||||
b.AssertLogNotContains("WARN")
|
||||
}
|
||||
|
||||
func TestSectionPagesIssue12399(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
files := `
|
||||
-- hugo.toml --
|
||||
disableKinds = ['rss','sitemap','taxonomy','term']
|
||||
capitalizeListTitles = false
|
||||
pluralizeListTitles = false
|
||||
sectionPagesMenu = 'main'
|
||||
-- content/p1.md --
|
||||
---
|
||||
title: p1
|
||||
---
|
||||
-- content/s1/p2.md --
|
||||
---
|
||||
title: p2
|
||||
menus: main
|
||||
---
|
||||
-- content/s1/p3.md --
|
||||
---
|
||||
title: p3
|
||||
---
|
||||
-- layouts/_default/list.html --
|
||||
{{ range site.Menus.main }}<a href="{{ .URL }}">{{ .Name }}</a>{{ end }}
|
||||
-- layouts/_default/single.html --
|
||||
{{ .Title }}
|
||||
`
|
||||
|
||||
b := Test(t, files)
|
||||
|
||||
b.AssertFileExists("public/index.html", true)
|
||||
b.AssertFileContent("public/index.html", `<a href="/s1/p2/">p2</a><a href="/s1/">s1</a>`)
|
||||
}
|
||||
|
|
|
@ -659,7 +659,7 @@ func (s *Site) assembleMenus() error {
|
|||
|
||||
if sectionPagesMenu != "" {
|
||||
if err := s.pageMap.forEachPage(pagePredicates.ShouldListGlobal, func(p *pageState) (bool, error) {
|
||||
if p.IsHome() || !p.m.shouldBeCheckedForMenuDefinitions() {
|
||||
if p.Kind() != kinds.KindSection || !p.m.shouldBeCheckedForMenuDefinitions() {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ import (
|
|||
"github.com/gohugoio/hugo/markup/asciidocext/asciidocext_config"
|
||||
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
|
||||
"github.com/gohugoio/hugo/markup/highlight"
|
||||
"github.com/gohugoio/hugo/markup/pandoc/pandoc_config"
|
||||
"github.com/gohugoio/hugo/markup/tableofcontents"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
@ -39,6 +40,7 @@ type Config struct {
|
|||
|
||||
// Configuration for the Asciidoc external markdown engine.
|
||||
AsciidocExt asciidocext_config.Config
|
||||
Pandoc pandoc_config.Config
|
||||
}
|
||||
|
||||
func Decode(cfg config.Provider) (conf Config, err error) {
|
||||
|
@ -105,4 +107,5 @@ var Default = Config{
|
|||
|
||||
Goldmark: goldmark_config.Default,
|
||||
AsciidocExt: asciidocext_config.Default,
|
||||
Pandoc: pandoc_config.Default,
|
||||
}
|
||||
|
|
|
@ -15,12 +15,16 @@
|
|||
package pandoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/gohugoio/hugo/common/hexec"
|
||||
"github.com/gohugoio/hugo/htesting"
|
||||
"github.com/gohugoio/hugo/identity"
|
||||
"golang.org/x/net/html"
|
||||
|
||||
"github.com/gohugoio/hugo/markup/converter"
|
||||
"github.com/gohugoio/hugo/markup/internal"
|
||||
"github.com/gohugoio/hugo/markup/tableofcontents"
|
||||
)
|
||||
|
||||
// Provider is the package entry point.
|
||||
|
@ -37,17 +41,33 @@ func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error)
|
|||
}), nil
|
||||
}
|
||||
|
||||
type pandocResult struct {
|
||||
converter.ResultRender
|
||||
toc *tableofcontents.Fragments
|
||||
}
|
||||
|
||||
func (r pandocResult) TableOfContents() *tableofcontents.Fragments {
|
||||
return r.toc
|
||||
}
|
||||
|
||||
type pandocConverter struct {
|
||||
ctx converter.DocumentContext
|
||||
cfg converter.ProviderConfig
|
||||
}
|
||||
|
||||
func (c *pandocConverter) Convert(ctx converter.RenderContext) (converter.ResultRender, error) {
|
||||
b, err := c.getPandocContent(ctx.Src, c.ctx)
|
||||
contentWithToc, err := c.getPandocContent(ctx.Src, c.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return converter.Bytes(b), nil
|
||||
content, toc, err := c.extractTOC(contentWithToc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pandocResult{
|
||||
ResultRender: converter.Bytes(content),
|
||||
toc: toc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *pandocConverter) Supports(feature identity.Identity) bool {
|
||||
|
@ -63,7 +83,7 @@ func (c *pandocConverter) getPandocContent(src []byte, ctx converter.DocumentCon
|
|||
" Leaving pandoc content unrendered.")
|
||||
return src, nil
|
||||
}
|
||||
args := []string{"--mathjax"}
|
||||
args := []string{"--mathjax", "--toc", "-s", "--metadata", "title=dummy"}
|
||||
return internal.ExternallyRenderContent(c.cfg, ctx, src, binaryName, args)
|
||||
}
|
||||
|
||||
|
@ -76,6 +96,164 @@ func getPandocBinaryName() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
// extractTOC extracts the toc from the given src html.
|
||||
// It returns the html without the TOC, and the TOC data
|
||||
func (a *pandocConverter) extractTOC(src []byte) ([]byte, *tableofcontents.Fragments, error) {
|
||||
var buf bytes.Buffer
|
||||
buf.Write(src)
|
||||
node, err := html.Parse(&buf)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
f func(*html.Node) bool
|
||||
body *html.Node
|
||||
toc *tableofcontents.Fragments
|
||||
toVisit []*html.Node
|
||||
)
|
||||
|
||||
// find body
|
||||
f = func(n *html.Node) bool {
|
||||
if n.Type == html.ElementNode && n.Data == "body" {
|
||||
body = n
|
||||
return true
|
||||
}
|
||||
if n.FirstChild != nil {
|
||||
toVisit = append(toVisit, n.FirstChild)
|
||||
}
|
||||
if n.NextSibling != nil && f(n.NextSibling) {
|
||||
return true
|
||||
}
|
||||
for len(toVisit) > 0 {
|
||||
nv := toVisit[0]
|
||||
toVisit = toVisit[1:]
|
||||
if f(nv) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
if !f(node) {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// remove by pandoc generated title
|
||||
f = func(n *html.Node) bool {
|
||||
if n.Type == html.ElementNode && n.Data == "header" && attr(n, "id") == "title-block-header" {
|
||||
n.Parent.RemoveChild(n)
|
||||
return true
|
||||
}
|
||||
if n.FirstChild != nil {
|
||||
toVisit = append(toVisit, n.FirstChild)
|
||||
}
|
||||
if n.NextSibling != nil && f(n.NextSibling) {
|
||||
return true
|
||||
}
|
||||
for len(toVisit) > 0 {
|
||||
nv := toVisit[0]
|
||||
toVisit = toVisit[1:]
|
||||
if f(nv) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
f(body)
|
||||
|
||||
// find toc
|
||||
f = func(n *html.Node) bool {
|
||||
if n.Type == html.ElementNode && n.Data == "nav" && attr(n, "id") == "TOC" {
|
||||
toc = parseTOC(n)
|
||||
if !a.cfg.MarkupConfig().Pandoc.PreserveTOC {
|
||||
n.Parent.RemoveChild(n)
|
||||
}
|
||||
return true
|
||||
}
|
||||
if n.FirstChild != nil {
|
||||
toVisit = append(toVisit, n.FirstChild)
|
||||
}
|
||||
if n.NextSibling != nil && f(n.NextSibling) {
|
||||
return true
|
||||
}
|
||||
for len(toVisit) > 0 {
|
||||
nv := toVisit[0]
|
||||
toVisit = toVisit[1:]
|
||||
if f(nv) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
f(body)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
buf.Reset()
|
||||
err = html.Render(&buf, body)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
// ltrim <html><head></head><body>\n\n and rtrim \n\n</body></html> which are added by html.Render
|
||||
res := buf.Bytes()[8:]
|
||||
res = res[:len(res)-9]
|
||||
return res, toc, nil
|
||||
}
|
||||
|
||||
// parseTOC returns a TOC root from the given toc Node
|
||||
func parseTOC(doc *html.Node) *tableofcontents.Fragments {
|
||||
var (
|
||||
toc tableofcontents.Builder
|
||||
f func(*html.Node, int, int)
|
||||
)
|
||||
f = func(n *html.Node, row, level int) {
|
||||
if n.Type == html.ElementNode {
|
||||
switch n.Data {
|
||||
case "ul":
|
||||
if level == 0 {
|
||||
row++
|
||||
}
|
||||
level++
|
||||
f(n.FirstChild, row, level)
|
||||
case "li":
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
if c.Type != html.ElementNode || c.Data != "a" {
|
||||
continue
|
||||
}
|
||||
href := attr(c, "href")[1:]
|
||||
toc.AddAt(&tableofcontents.Heading{
|
||||
Title: nodeContent(c),
|
||||
ID: href,
|
||||
}, row, level)
|
||||
}
|
||||
f(n.FirstChild, row, level)
|
||||
}
|
||||
}
|
||||
if n.NextSibling != nil {
|
||||
f(n.NextSibling, row, level)
|
||||
}
|
||||
}
|
||||
f(doc.FirstChild, -1, 0)
|
||||
return toc.Build()
|
||||
}
|
||||
|
||||
func attr(node *html.Node, key string) string {
|
||||
for _, a := range node.Attr {
|
||||
if a.Key == key {
|
||||
return a.Val
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func nodeContent(node *html.Node) string {
|
||||
var buf bytes.Buffer
|
||||
for c := node.FirstChild; c != nil; c = c.NextSibling {
|
||||
html.Render(&buf, c)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// Supports returns whether Pandoc is installed on this computer.
|
||||
func Supports() bool {
|
||||
hasBin := getPandocBinaryName() != ""
|
||||
|
|
85
markup/pandoc/integration_test.go
Normal file
85
markup/pandoc/integration_test.go
Normal file
|
@ -0,0 +1,85 @@
|
|||
// Copyright 2021 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 pandoc_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gohugoio/hugo/hugolib"
|
||||
)
|
||||
|
||||
func TestBasicConversion(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
files := `
|
||||
-- config.toml --
|
||||
-- content/p1.md --
|
||||
testContent
|
||||
-- layouts/_default/single.html --
|
||||
{{ .Content }}
|
||||
`
|
||||
b := hugolib.NewIntegrationTestBuilder(
|
||||
hugolib.IntegrationTestConfig{
|
||||
T: t,
|
||||
TxtarString: files,
|
||||
NeedsOsFS: true,
|
||||
},
|
||||
).Build()
|
||||
|
||||
b.AssertFileContent("public/p1/index.html", `<p>testContent</p>`)
|
||||
}
|
||||
|
||||
func TestConversionWithHeader(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
files := `
|
||||
-- config.toml --
|
||||
-- content/p1.md --
|
||||
# testContent
|
||||
-- layouts/_default/single.html --
|
||||
{{ .Content }}
|
||||
`
|
||||
b := hugolib.NewIntegrationTestBuilder(
|
||||
hugolib.IntegrationTestConfig{
|
||||
T: t,
|
||||
TxtarString: files,
|
||||
NeedsOsFS: true,
|
||||
},
|
||||
).Build()
|
||||
|
||||
b.AssertFileContent("public/p1/index.html", `<h1 id="testcontent">testContent</h1>`)
|
||||
}
|
||||
|
||||
func TestConversionWithExtractedToc(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
files := `
|
||||
-- config.toml --
|
||||
-- content/p1.md --
|
||||
# title 1
|
||||
## title 2
|
||||
-- layouts/_default/single.html --
|
||||
{{ .TableOfContents }}
|
||||
{{ .Content }}
|
||||
`
|
||||
b := hugolib.NewIntegrationTestBuilder(
|
||||
hugolib.IntegrationTestConfig{
|
||||
T: t,
|
||||
TxtarString: files,
|
||||
NeedsOsFS: true,
|
||||
},
|
||||
).Build()
|
||||
|
||||
b.AssertFileContent("public/p1/index.html", "<nav id=\"TableOfContents\">\n <ul>\n <li><a href=\"#title-2\">title 2</a></li>\n </ul>\n</nav>\n<h1 id=\"title-1\">title 1</h1>\n<h2 id=\"title-2\">title 2</h2>")
|
||||
}
|
27
markup/pandoc/pandoc_config/config.go
Normal file
27
markup/pandoc/pandoc_config/config.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2020 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 pandocdoc_config holds pandoc related configuration.
|
||||
package pandoc_config
|
||||
|
||||
var (
|
||||
// Default holds Hugo's default pandoc configuration.
|
||||
Default = Config{
|
||||
PreserveTOC: false,
|
||||
}
|
||||
)
|
||||
|
||||
// Config configures pandoc.
|
||||
type Config struct {
|
||||
PreserveTOC bool
|
||||
}
|
|
@ -18,6 +18,7 @@ import (
|
|||
"testing"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/htesting/hqt"
|
||||
)
|
||||
|
||||
func TestHexStringToColor(t *testing.T) {
|
||||
|
@ -119,8 +120,8 @@ func TestReplaceColorInPalette(t *testing.T) {
|
|||
|
||||
func TestColorLuminance(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
c.Assert(hexStringToColor("#000000").Luminance(), qt.Equals, 0.0)
|
||||
c.Assert(hexStringToColor("#768a9a").Luminance(), qt.Equals, 0.24361603589088263)
|
||||
c.Assert(hexStringToColor("#d5bc9f").Luminance(), qt.Equals, 0.5261577672685374)
|
||||
c.Assert(hexStringToColor("#ffffff").Luminance(), qt.Equals, 1.0)
|
||||
c.Assert(hexStringToColor("#000000").Luminance(), hqt.IsSameFloat64, 0.0)
|
||||
c.Assert(hexStringToColor("#768a9a").Luminance(), hqt.IsSameFloat64, 0.24361603589088263)
|
||||
c.Assert(hexStringToColor("#d5bc9f").Luminance(), hqt.IsSameFloat64, 0.5261577672685374)
|
||||
c.Assert(hexStringToColor("#ffffff").Luminance(), hqt.IsSameFloat64, 1.0)
|
||||
}
|
||||
|
|
|
@ -327,3 +327,34 @@ Styles: {{ $r.RelPermalink }}
|
|||
|
||||
b.AssertFileContent("public/index.html", "Styles: /scss/main.css")
|
||||
}
|
||||
|
||||
func TestRebuildAssetGetMatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !scss.Supports() {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
files := `
|
||||
-- assets/scss/main.scss --
|
||||
b {
|
||||
color: red;
|
||||
}
|
||||
-- layouts/index.html --
|
||||
{{ $r := resources.GetMatch "scss/main.scss" | toCSS }}
|
||||
T1: {{ $r.Content }}
|
||||
`
|
||||
|
||||
b := hugolib.NewIntegrationTestBuilder(
|
||||
hugolib.IntegrationTestConfig{
|
||||
T: t,
|
||||
TxtarString: files,
|
||||
NeedsOsFS: true,
|
||||
Running: true,
|
||||
}).Build()
|
||||
|
||||
b.AssertFileContent("public/index.html", `color: red`)
|
||||
|
||||
b.EditFiles("assets/scss/main.scss", `b { color: blue; }`).Build()
|
||||
|
||||
b.AssertFileContent("public/index.html", `color: blue`)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue