"github.com/bep/logg"
"github.com/gohugoio/hugo/common/hmaps"
- "github.com/gohugoio/hugo/htesting"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/config/allconfig"
+ "github.com/gohugoio/hugo/htesting"
qt "github.com/frankban/quicktest"
"github.com/spf13/afero"
// Use ["**"] to include all fields.
Fields []string
- // Hugo extracts the "photo taken" date/time into .Date by default.
- // Set this to true to turn it off.
- DisableDate bool
-
- // Hugo extracts the "photo taken where" (GPS latitude and longitude) into
- // .Long and .Lat. Set this to true to turn it off.
- DisableLatLong bool
-
// Which metadata sources to include.
// Valid values are "exif", "iptc", "xmp".
// Default is ["exif", "iptc"] (XMP is excluded for performance reasons).
m := cfg.Config.Imaging.Meta
metaDecoder, err := meta.NewDecoder(
- meta.WithDateDisabled(m.DisableDate),
- meta.WithLatLongDisabled(m.DisableLatLong),
meta.WithFields(m.Fields),
meta.WithSources(m.Sources...),
meta.WithWarnLogger(warnl),
}
shouldHandleTag := func(ti imagemeta.TagInfo) bool {
- // Always include time and GPS tags (needed for Date, Lat, Long extraction).
- // These may be in EXIF, XMP, or IPTC depending on the image.
- if !d.noDate && isTimeTag(ti.Tag) {
- return true
- }
- if !d.noLatLong && isGPSTag(ti.Tag) {
- return true
- }
-
// For EXIF, only include tags from IFD0 (skip thumbnail data).
if ti.Source == imagemeta.EXIF {
if !strings.HasPrefix(ti.Namespace, "IFD0") {
return nil, err
}
- var tm time.Time
- var lat, long float64
-
- if !d.noDate {
- tm, _ = tagInfos.GetDateTime()
- }
-
- if !d.noLatLong {
- lat, long, _ = tagInfos.GetLatLong()
- }
+ tm, _ := tagInfos.GetDateTime()
+ lat, long, _ := tagInfos.GetLatLong()
exifTags := make(map[string]any)
iptcTags := make(map[string]any)
files := `
-- hugo.toml --
[imaging.meta]
+fields = ['**']
sources = ['exif', 'iptc', 'xmp']
-- assets/sunset.jpg --
sourcefilename: ../../testdata/sunset.jpg
-- hugo.toml --
[imaging]
[imaging.meta]
-disableDate = true
-disableLatLong = true
fields = ['! *{Model,ColorSpace,Metering}*']
sources = ['exif', 'iptc']
-- assets/sunset.jpg --
b := hugolib.Test(t, files)
b.AssertFileContent("public/index.html",
- // disableLatLong = true
- "Lat: 0",
- "Long: 0",
- // disableDate = true
- "Date: 0001-01-01",
+ "Lat: 36.597441",
+ "Long: -4.50846",
+ "Date: 2017-10-27",
// Exif is included
"ExifMake: RICOH IMAGING COMPANY, LTD.",
// Model is excluded by fields pattern '! *Model*'
)
}
+func TestMetaFieldsFilterDateAndGPS(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+[imaging.meta]
+fields = ['! GPS*', '! *Date*', '! *Time*']
+-- assets/sunset.jpg --
+sourcefilename: ../../testdata/sunset.jpg
+-- layouts/home.html --
+{{ $img := resources.Get "sunset.jpg" }}
+{{ $meta := $img.Meta }}
+{{ with $meta }}
+Lat: {{ .Lat }}
+Long: {{ .Long }}
+Date: {{ .Date.Format "2006-01-02" }}
+ExifMake: {{ .Exif.Make }}
+{{ end }}
+`
+
+ b := hugolib.Test(t, files)
+
+ b.AssertFileContent("public/index.html",
+ "Lat: 0",
+ "Long: 0",
+ "Date: 0001-01-01",
+ "ExifMake: RICOH IMAGING COMPANY, LTD.",
+ )
+}
+
// TestMetaXMPOnly verifies behavior when only XMP is configured as a source.
// Note: Date and Lat/Long are extracted from whichever configured source contains them.
// The test image has GPS/Date in EXIF only, so with XMP-only sources they will be empty.