shithub: hugo

Download patch

ref: 39df7724ad958cfaee631ea1cdc3c29343b63763
parent: b33bfd40bed7af36faba18bed09efb6b95c58192
author: Kishin Yagami <[email protected]>
date: Mon Aug 8 23:25:00 EDT 2016

source: Normalize file name to NFC

Fixes #2203

--- a/source/filesystem.go
+++ b/source/filesystem.go
@@ -18,9 +18,11 @@
 	"os"
 	"path/filepath"
 	"regexp"
+	"runtime"
 	"strings"
 
 	"github.com/spf13/hugo/hugofs"
+	"golang.org/x/text/unicode/norm"
 
 	"github.com/spf13/viper"
 
@@ -65,6 +67,11 @@
 // add populates a file in the Filesystem.files
 func (f *Filesystem) add(name string, reader io.Reader) (err error) {
 	var file *File
+
+	if runtime.GOOS == "darwin" {
+		// When a file system is HFS+, its filepath is in NFD form.
+		name = norm.NFC.String(name)
+	}
 
 	file, err = NewFileFromAbs(f.Base, name, reader)
 
--- a/source/filesystem_test.go
+++ b/source/filesystem_test.go
@@ -16,6 +16,8 @@
 import (
 	"bytes"
 	"path/filepath"
+	"runtime"
+	"strings"
 	"testing"
 )
 
@@ -81,4 +83,29 @@
 			}
 		}
 	}
+}
+
+func TestUnicodeNorm(t *testing.T) {
+	if runtime.GOOS != "darwin" {
+		// Normalization code is only for Mac OS, since it is not necessary for other OSes.
+		return
+	}
+
+	paths := []struct {
+		NFC string
+		NFD string
+	}{
+		{NFC: "å", NFD: "\x61\xcc\x8a"},
+		{NFC: "é", NFD: "\x65\xcc\x81"},
+	}
+
+	for _, path := range paths {
+		src := new(Filesystem)
+		_ = src.add(path.NFD, strings.NewReader(""))
+		f := src.Files()[0]
+		if f.BaseFileName() != path.NFC {
+			t.Fatalf("file name in NFD form should be normalized (%s)", path.NFC)
+		}
+	}
+
 }