Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

source: Normalize file name to NFC #2259

Merged
merged 1 commit into from Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions source/filesystem.go
Expand Up @@ -18,9 +18,11 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"

"github.com/spf13/hugo/hugofs"
"golang.org/x/text/unicode/norm"

"github.com/spf13/viper"

Expand Down Expand Up @@ -66,6 +68,11 @@ func (f *Filesystem) Files() []*File {
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)

if err == nil {
Expand Down
27 changes: 27 additions & 0 deletions source/filesystem_test.go
Expand Up @@ -16,6 +16,8 @@ package source
import (
"bytes"
"path/filepath"
"runtime"
"strings"
"testing"
)

Expand Down Expand Up @@ -82,3 +84,28 @@ func TestAddFile(t *testing.T) {
}
}
}

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)
}
}

}