Skip to content

Commit

Permalink
More mime
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 12, 2024
1 parent bab274e commit b17969d
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 79 deletions.
2 changes: 1 addition & 1 deletion commands/hugobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,
h.BaseFs.SourceFilesystems,
dynamicEvents)

onePageName := pickOneWriteOrCreatePath(partitionedEvents.ContentEvents)
onePageName := pickOneWriteOrCreatePath(h.Conf.ContentTypes(), partitionedEvents.ContentEvents)

c.printChangeDetected("")
c.changeDetector.PrepareNew()
Expand Down
7 changes: 3 additions & 4 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import (
"github.com/fsnotify/fsnotify"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/media"

"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/common/urls"
Expand Down Expand Up @@ -1189,16 +1188,16 @@ func partitionDynamicEvents(sourceFs *filesystems.SourceFilesystems, events []fs
return
}

func pickOneWriteOrCreatePath(events []fsnotify.Event) string {
func pickOneWriteOrCreatePath(contentTypes config.ContentTypesProvider, events []fsnotify.Event) string {
name := ""

for _, ev := range events {
if ev.Op&fsnotify.Write == fsnotify.Write || ev.Op&fsnotify.Create == fsnotify.Create {
if media.IsIndexContentFile(ev.Name) {
if contentTypes.IsIndexContentFile(ev.Name) {
return ev.Name
}

if media.IsContentFile(ev.Name) {
if contentTypes.IsContentFile(ev.Name) {
name = ev.Name
}

Expand Down
12 changes: 0 additions & 12 deletions common/paths/pathparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ import (
"github.com/gohugoio/hugo/identity"
)

var DefaultPathParser = &PathParser{
IsContentExt: func(ext string) bool {
// TODO1
return ext == "md"
},
}

// PathParser parses a path into a Path.
type PathParser struct {
// Maps the language code to its index in the languages/sites slice.
Expand All @@ -44,11 +37,6 @@ type PathParser struct {
IsContentExt func(string) bool
}

// Parse parses component c with path s into Path using the default path parser.
func Parse(c, s string) *Path {
return DefaultPathParser.Parse(c, s)
}

// NormalizePathString returns a normalized path string using the very basic Hugo rules.
func NormalizePathStringBasic(s string) string {
// All lower case.
Expand Down
2 changes: 1 addition & 1 deletion config/allconfig/allconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ func (c *Configs) Init() error {
c.Languages = languages
c.LanguagesDefaultFirst = languagesDefaultFirst

c.ContentPathParser = &paths.PathParser{LanguageIndex: languagesDefaultFirst.AsIndexSet(), IsLangDisabled: c.Base.IsLangDisabled, IsContentExt: media.IsContentSuffix}
c.ContentPathParser = &paths.PathParser{LanguageIndex: languagesDefaultFirst.AsIndexSet(), IsLangDisabled: c.Base.IsLangDisabled, IsContentExt: c.Base.C.ContentTypes.IsContentSuffix}

c.configLangs = make([]config.AllProvider, len(c.Languages))
for i, l := range c.LanguagesDefaultFirst {
Expand Down
4 changes: 4 additions & 0 deletions config/allconfig/configlanguage.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func (c ConfigLanguage) NewIdentityManager(name string) identity.Manager {
return identity.NewManager(name)
}

func (c ConfigLanguage) ContentTypes() config.ContentTypesProvider {
return c.config.C.ContentTypes
}

// GetConfigSection is mostly used in tests. The switch statement isn't complete, but what's in use.
func (c ConfigLanguage) GetConfigSection(s string) any {
switch s {
Expand Down
10 changes: 10 additions & 0 deletions config/configProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type AllProvider interface {
Dirs() CommonDirs
Quiet() bool
DirsBase() CommonDirs
ContentTypes() ContentTypesProvider
GetConfigSection(string) any
GetConfig() any
CanonifyURLs() bool
Expand Down Expand Up @@ -75,6 +76,15 @@ type AllProvider interface {
EnableEmoji() bool
}

// We cannot import the media package as that would create a circular dependency.
// This interface defineds a sub set of what media.ContentTypes provides.
type ContentTypesProvider interface {
IsContentSuffix(suffix string) bool
IsContentFile(filename string) bool
IsIndexContentFile(filename string) bool
IsHTMLSuffix(suffix string) bool
}

// Provider provides the configuration settings for Hugo.
type Provider interface {
GetString(key string) string
Expand Down
3 changes: 1 addition & 2 deletions create/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"strings"

"github.com/gohugoio/hugo/hugofs/glob"
"github.com/gohugoio/hugo/media"

"github.com/gohugoio/hugo/common/hexec"
"github.com/gohugoio/hugo/common/hstrings"
Expand Down Expand Up @@ -97,7 +96,7 @@ func NewContent(h *hugolib.HugoSites, kind, targetPath string, force bool) error
return "", fmt.Errorf("failed to resolve %q to an archetype template", targetPath)
}

if !media.IsContentFile(b.targetPath) {
if !h.Conf.ContentTypes().IsContentFile(b.targetPath) {
return "", fmt.Errorf("target path %q is not a known content format", b.targetPath)
}

Expand Down
3 changes: 2 additions & 1 deletion hugofs/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/media"

"github.com/spf13/afero"
)
Expand Down Expand Up @@ -74,7 +75,7 @@ func NewWalkway(cfg WalkwayConfig) *Walkway {
}

if cfg.PathParser == nil {
cfg.PathParser = paths.DefaultPathParser
cfg.PathParser = media.DefaultPathParser
}

logger := cfg.Logger
Expand Down
12 changes: 7 additions & 5 deletions markup/markup.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ func NewConverterProvider(cfg converter.ProviderConfig) (ConverterProvider, erro
return nil
}

if err := add(goldmark.Provider, media.Builtin.MarkdownType.SubType, media.Builtin.MarkdownType.Suffixes()...); err != nil {
contentTypes := cfg.Conf.ContentTypes().(media.ContentTypes)

if err := add(goldmark.Provider, contentTypes.Markdown.SubType, contentTypes.Markdown.Suffixes()...); err != nil {
return nil, err
}
if err := add(asciidocext.Provider, media.Builtin.AsciiDocType.SubType, media.Builtin.AsciiDocType.Suffixes()...); err != nil {
if err := add(asciidocext.Provider, contentTypes.AsciiDoc.SubType, contentTypes.AsciiDoc.Suffixes()...); err != nil {
return nil, err
}
if err := add(rst.Provider, media.Builtin.ReStructuredTextType.SubType, media.Builtin.ReStructuredTextType.Suffixes()...); err != nil {
if err := add(rst.Provider, contentTypes.ReStructuredText.SubType, contentTypes.ReStructuredText.Suffixes()...); err != nil {
return nil, err
}
if err := add(pandoc.Provider, media.Builtin.PandocType.SubType, media.Builtin.PandocType.Suffixes()...); err != nil {
if err := add(pandoc.Provider, contentTypes.Pandoc.SubType, contentTypes.Pandoc.Suffixes()...); err != nil {
return nil, err
}
if err := add(org.Provider, media.Builtin.EmacsOrgModeType.SubType, media.Builtin.EmacsOrgModeType.Suffixes()...); err != nil {
if err := add(org.Provider, contentTypes.EmacsOrgMode.SubType, contentTypes.EmacsOrgMode.Suffixes()...); err != nil {
return nil, err
}

Expand Down
38 changes: 0 additions & 38 deletions media/builtin.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package media

import (
"path/filepath"
"strings"
)

type BuiltinTypes struct {
CalendarType Type
CSSType Type
Expand Down Expand Up @@ -172,36 +167,3 @@ var defaultMediaTypesConfig = map[string]any{

"application/octet-stream": map[string]any{},
}

// IsContentSuffix returns whether the given suffix is a content media type.
func IsContentSuffix(suffix string) bool {
return contentMediaTypesExtensionsSet[suffix]
}

// IsContentFile returns whether the given filename is a content file.
func IsContentFile(filename string) bool {
return IsContentSuffix(strings.TrimPrefix(filepath.Ext(filename), "."))
}

// IsIndexContentFile returns whether the given filename is an index content file.
func IsIndexContentFile(filename string) bool {
if !IsContentFile(filename) {
return false
}

base := filepath.Base(filename)

return strings.HasPrefix(base, "index.") || strings.HasPrefix(base, "_index.")
}

// IsHTMLSuffix returns whether the given suffix is a HTML media type.
func IsHTMLSuffix(suffix string) bool {
for _, suffix := range Builtin.HTMLType.Suffixes() {
if suffix == suffix {
return true
}
}
return false
}

var contentMediaTypesExtensionsSet map[string]bool
25 changes: 18 additions & 7 deletions media/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/config"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -68,13 +69,6 @@ func init() {
}

DefaultContentTypes.init()

contentMediaTypesExtensionsSet = make(map[string]bool)
for _, mt := range DefaultContentTypes.Types() {
for _, suffix := range mt.Suffixes() {
contentMediaTypesExtensionsSet[suffix] = true
}
}
}

var DefaultContentTypes ContentTypes
Expand Down Expand Up @@ -123,6 +117,16 @@ func (t ContentTypes) IsIndexContentFile(filename string) bool {
return strings.HasPrefix(base, "index.") || strings.HasPrefix(base, "_index.")
}

// IsHTMLSuffix returns whether the given suffix is a HTML media type.
func (t ContentTypes) IsHTMLSuffix(suffix string) bool {
for _, suffix := range t.HTML.Suffixes() {
if suffix == suffix {

Check failure on line 123 in media/config.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

identical expressions on the left and right side of the '==' operator (SA4000)

Check failure on line 123 in media/config.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

identical expressions on the left and right side of the '==' operator (SA4000)
return true
}
}
return false
}

// Types is a slice of media types.
func (t ContentTypes) Types() Types {
return t.types
Expand Down Expand Up @@ -209,3 +213,10 @@ func DecodeTypes(in map[string]any) (*config.ConfigNamespace[map[string]MediaTyp
}
return ns, nil
}

// TODO(bep) get rid of this.
var DefaultPathParser = &paths.PathParser{
IsContentExt: func(ext string) bool {
return DefaultContentTypes.IsContentSuffix(ext)
},
}
6 changes: 3 additions & 3 deletions media/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ func TestDefaultTypes(t *testing.T) {
{Builtin.HTMLType, "text", "html", "html,htm", "text/html", "text/html"},
{Builtin.MarkdownType, "text", "markdown", "md,mdown,markdown", "text/markdown", "text/markdown"},
{Builtin.EmacsOrgModeType, "text", "org", "org", "text/org", "text/org"},
{Builtin.PandocType, "text", "x-pandoc", "pandoc,pdc", "text/pandoc", "text/pandoc"},
{Builtin.ReStructuredTextType, "text", "x-restructuredtext", "rst", "text/x-rst", "text/x-rst"},
{Builtin.PandocType, "text", "pandoc", "pandoc,pdc", "text/pandoc", "text/pandoc"},
{Builtin.ReStructuredTextType, "text", "x-rst", "rst", "text/x-rst", "text/x-rst"},
{Builtin.GoTmplType, "text", "x-gotmpl", "gotmpl", "text/x-gotmpl", "text/x-gotmpl"},
{Builtin.AsciiDocType, "text", "x-asciidoc", "adoc,asciidoc,ad", "text/asciidoc", "text/asciidoc"},
{Builtin.AsciiDocType, "text", "asciidoc", "adoc,asciidoc,ad", "text/asciidoc", "text/asciidoc"},
{Builtin.JavascriptType, "text", "javascript", "js,jsm,mjs", "text/javascript", "text/javascript"},
{Builtin.TypeScriptType, "text", "typescript", "ts", "text/typescript", "text/typescript"},
{Builtin.TSXType, "text", "tsx", "tsx", "text/tsx", "text/tsx"},
Expand Down
6 changes: 3 additions & 3 deletions media/mediaType_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func BenchmarkTypeOps(b *testing.B) {
func TestIsContentFile(t *testing.T) {
c := qt.New(t)

c.Assert(IsContentFile(filepath.FromSlash("my/file.md")), qt.Equals, true)
c.Assert(IsContentFile(filepath.FromSlash("my/file.ad")), qt.Equals, true)
c.Assert(IsContentFile(filepath.FromSlash("textfile.txt")), qt.Equals, false)
c.Assert(DefaultContentTypes.IsContentFile(filepath.FromSlash("my/file.md")), qt.Equals, true)
c.Assert(DefaultContentTypes.IsContentFile(filepath.FromSlash("my/file.ad")), qt.Equals, true)
c.Assert(DefaultContentTypes.IsContentFile(filepath.FromSlash("textfile.txt")), qt.Equals, false)
}
2 changes: 1 addition & 1 deletion resources/page/page_nop.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var (
// PageNop implements Page, but does nothing.
type nopPage int

var noOpPathInfo = paths.Parse(files.ComponentFolderContent, "no-op.md")
var noOpPathInfo = media.DefaultPathParser.Parse(files.ComponentFolderContent, "no-op.md")

func (p *nopPage) Err() resource.ResourceError {
return nil
Expand Down
3 changes: 2 additions & 1 deletion source/fileInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/bep/gitmap"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/media"

"github.com/gohugoio/hugo/common/hugio"

Expand Down Expand Up @@ -141,7 +142,7 @@ func (fi *File) p() *paths.Path {
func NewFileInfoFrom(path, filename string) *File {
meta := &hugofs.FileMeta{
Filename: filename,
PathInfo: paths.Parse("", filepath.ToSlash(path)),
PathInfo: media.DefaultPathParser.Parse("", filepath.ToSlash(path)),
}

return NewFileInfo(hugofs.NewFileMetaInfo(nil, meta))
Expand Down

0 comments on commit b17969d

Please sign in to comment.