forked from filebrowser/filebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package frontmatter | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/hacdias/caddy-hugo/utils" | ||
"github.com/spf13/hugo/parser" | ||
) | ||
|
||
// Pretty creates a new FrontMatter object | ||
func Pretty(content []byte, language string) (interface{}, error) { | ||
var err error | ||
var c interface{} | ||
|
||
if language == "yaml" { | ||
c, err = parser.HandleYAMLMetaData(content) | ||
} else if language == "json" { | ||
c, err = parser.HandleJSONMetaData(content) | ||
} else if language == "toml" { | ||
c, err = parser.HandleTOMLMetaData(content) | ||
} | ||
|
||
if err != nil { | ||
return []string{}, err | ||
} | ||
|
||
return rawToPretty(c, ""), nil | ||
} | ||
|
||
type frontmatter struct { | ||
Name string | ||
Tag string | ||
Content interface{} | ||
SubContent bool | ||
} | ||
|
||
func rawToPretty(config interface{}, master string) interface{} { | ||
var mapsNames []string | ||
var stringsNames []string | ||
|
||
for index, element := range config.(map[string]interface{}) { | ||
if utils.IsMap(element) { | ||
mapsNames = append(mapsNames, index) | ||
} else { | ||
stringsNames = append(stringsNames, index) | ||
} | ||
} | ||
|
||
sort.Strings(mapsNames) | ||
sort.Strings(stringsNames) | ||
names := append(stringsNames, mapsNames...) | ||
|
||
settings := make([]interface{}, len(names)) | ||
|
||
for index := range names { | ||
c := new(frontmatter) | ||
c.Name = names[index] | ||
c.Tag = master + "_" + names[index] | ||
c.SubContent = false | ||
|
||
i := config.(map[string]interface{})[names[index]] | ||
|
||
if utils.IsMap(i) { | ||
c.Content = rawToPretty(i, c.Name) | ||
c.SubContent = true | ||
} else { | ||
c.Content = i | ||
} | ||
|
||
settings[index] = c | ||
} | ||
|
||
return settings | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters