URI: 
       rewriting guessSection to accurately reflect intent and usage. Update tests. - hugo - [fork] hugo port for 9front
  HTML git clone git@git.drkhsh.at/hugo.git
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
   DIR README
   DIR LICENSE
       ---
   DIR commit a40bd3caf16ecabc189dd6e676792555d175540b
   DIR parent e2634752ce7dcc2f6cf1902b0409206e0ed8a571
  HTML Author: spf13 <steve.francia@gmail.com>
       Date:   Tue,  4 Nov 2014 20:19:45 -0500
       
       rewriting guessSection to accurately reflect intent and usage. Update tests.
       
       Diffstat:
         M helpers/path.go                     |      35 ++++++++++++++++---------------
         M helpers/path_test.go                |       9 ++++++---
       
       2 files changed, 24 insertions(+), 20 deletions(-)
       ---
   DIR diff --git a/helpers/path.go b/helpers/path.go
       @@ -209,36 +209,37 @@ func GetRelativePath(path, base string) (final string, err error) {
        }
        
        // Given a source path, determine the section
       +// A section is the part between the root slash and the second slash or before the first slash
        func GuessSection(in string) string {
                parts := strings.Split(in, "/")
       +        // This will include an empty entry before and after paths with leading and trailing slashes
       +        // eg... /sect/one/ -> ["", "sect", "one", ""]
        
       -        if len(parts) == 0 {
       +        // Needs to have at least a value and a slash
       +        if len(parts) < 2 {
                        return ""
                }
        
       -        // trim filename
       -        if !strings.HasSuffix(in, "/") {
       -                parts = parts[:len(parts)-1]
       -        }
       -
       -        if len(parts) == 0 {
       +        // If it doesn't have a leading slash and value and file or trailing slash, then return ""
       +        if parts[0] == "" && len(parts) < 3 {
                        return ""
                }
        
       -        // if first directory is "content", return second directory
       -        section := ""
       -
       -        if parts[0] == "content" && len(parts) > 1 {
       -                section = parts[1]
       -        } else {
       -                section = parts[0]
       +        // strip leading slash
       +        if parts[0] == "" {
       +                parts = parts[1:]
                }
        
       -        if section == "." {
       -                return ""
       +        // if first directory is "content", return second directory
       +        if parts[0] == "content" {
       +                if len(parts) > 2 {
       +                        return parts[1]
       +                } else {
       +                        return ""
       +                }
                }
        
       -        return section
       +        return parts[0]
        }
        
        func PathPrep(ugly bool, in string) string {
   DIR diff --git a/helpers/path_test.go b/helpers/path_test.go
       @@ -423,17 +423,20 @@ func TestGuessSection(t *testing.T) {
                        {"", ""},
                        {"/content", ""},
                        {"content/", ""},
       -                {"/content/", "content"},
       +                {"/content/", ""}, // /content/ is a special case. It will never be the section
                        {"/blog", ""},
                        {"/blog/", "blog"},
                        {"blog", ""},
                        {"content/blog", ""},
                        {"/content/blog/", "blog"},
       -                {"/content/blog", "blog"},
       -                {"content/blog/", ""},
       +                {"/content/blog", ""}, // Lack of trailing slash indicates 'blog' is not a directory.
       +                {"content/blog/", "blog"},
                        {"/contents/myblog/", "contents"},
                        {"/contents/yourblog", "contents"},
                        {"/contents/ourblog/", "contents"},
       +                {"/content/myblog/", "myblog"},
       +                {"/content/yourblog", ""},
       +                {"/content/ourblog/", "ourblog"},
                }
        
                for i, d := range data {