rewrite some frontmatter and editor-related stuff
parent
e8eeb9260e
commit
15e46f99c7
|
@ -2,6 +2,7 @@ package frontmatter
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -28,28 +29,30 @@ func AppendRune(frontmatter []byte, mark rune) []byte {
|
|||
return frontmatter
|
||||
}
|
||||
|
||||
func RuneToStringFormat(mark rune) string {
|
||||
// RuneToStringFormat converts the rune to a string with the format
|
||||
func RuneToStringFormat(mark rune) (string, error) {
|
||||
switch mark {
|
||||
case '-':
|
||||
return "yaml"
|
||||
return "yaml", nil
|
||||
case '+':
|
||||
return "toml"
|
||||
return "toml", nil
|
||||
case '{':
|
||||
return "json"
|
||||
return "json", nil
|
||||
default:
|
||||
return ""
|
||||
return "", errors.New("Unsupported format type.")
|
||||
}
|
||||
}
|
||||
|
||||
func StringFormatToRune(format string) rune {
|
||||
// StringFormatToRune converts the format name to its rune
|
||||
func StringFormatToRune(format string) (rune, error) {
|
||||
switch format {
|
||||
case "yaml":
|
||||
return '-'
|
||||
return '-', nil
|
||||
case "toml":
|
||||
return '+'
|
||||
return '+', nil
|
||||
case "json":
|
||||
return '{'
|
||||
return '{', nil
|
||||
default:
|
||||
return '0'
|
||||
return '0', errors.New("Unsupported format type.")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
"github.com/mholt/archiver"
|
||||
)
|
||||
|
||||
// Download creates an archieve in one of the supported formats (zip, tar,
|
||||
// Download creates an archive in one of the supported formats (zip, tar,
|
||||
// tar.gz or tar.bz2) and sends it to be downloaded.
|
||||
func Download(w http.ResponseWriter, r *http.Request, c *config.Config, i *file.Info) (int, error) {
|
||||
query := r.URL.Query().Get("download")
|
||||
|
|
|
@ -29,52 +29,62 @@ func GetEditor(r *http.Request, i *file.Info) (*Editor, error) {
|
|||
var err error
|
||||
|
||||
// Create a new editor variable and set the mode
|
||||
editor := new(Editor)
|
||||
editor.Mode = editorMode(i.Name)
|
||||
editor.Class = editorClass(editor.Mode)
|
||||
e := new(Editor)
|
||||
e.Mode = editorMode(i.Name)
|
||||
e.Class = editorClass(e.Mode)
|
||||
|
||||
if editor.Class == "frontmatter-only" || editor.Class == "complete" {
|
||||
editor.Visual = true
|
||||
if e.Class == "frontmatter-only" || e.Class == "complete" {
|
||||
e.Visual = true
|
||||
}
|
||||
|
||||
if r.URL.Query().Get("visual") == "false" {
|
||||
editor.Class = "content-only"
|
||||
e.Class = "content-only"
|
||||
}
|
||||
|
||||
if editor.Class == "frontmatter-only" {
|
||||
// Checks if the file already has the frontmatter rune and parses it
|
||||
if frontmatter.HasRune(i.Content) {
|
||||
editor.FrontMatter.Content, _, err = frontmatter.Pretty(i.Content)
|
||||
} else {
|
||||
editor.FrontMatter.Rune = frontmatter.StringFormatToRune(editor.Mode)
|
||||
editor.FrontMatter.Content, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.FrontMatter.Rune))
|
||||
hasRune := frontmatter.HasRune(i.Content)
|
||||
|
||||
if e.Class == "frontmatter-only" && !hasRune {
|
||||
e.FrontMatter.Rune, err = frontmatter.StringFormatToRune(e.Mode)
|
||||
if err != nil {
|
||||
goto Error
|
||||
}
|
||||
i.Content = frontmatter.AppendRune(i.Content, e.FrontMatter.Rune)
|
||||
}
|
||||
|
||||
if e.Class == "frontmatter-only" && hasRune {
|
||||
e.FrontMatter.Content, _, err = frontmatter.Pretty(i.Content)
|
||||
if err != nil {
|
||||
goto Error
|
||||
}
|
||||
}
|
||||
|
||||
if editor.Class == "complete" && frontmatter.HasRune(i.Content) {
|
||||
if e.Class == "complete" && hasRune {
|
||||
var page parser.Page
|
||||
// Starts a new buffer and parses the file using Hugo's functions
|
||||
buffer := bytes.NewBuffer(i.Content)
|
||||
page, err = parser.ReadFrom(buffer)
|
||||
|
||||
if err == nil {
|
||||
// Parses the page content and the frontmatter
|
||||
editor.Content = strings.TrimSpace(string(page.Content()))
|
||||
editor.FrontMatter.Rune = rune(i.Content[0])
|
||||
editor.FrontMatter.Content, _, err = frontmatter.Pretty(page.FrontMatter())
|
||||
}
|
||||
if err != nil {
|
||||
goto Error
|
||||
}
|
||||
|
||||
if editor.Class == "complete" && !frontmatter.HasRune(i.Content) {
|
||||
// Parses the page content and the frontmatter
|
||||
e.Content = strings.TrimSpace(string(page.Content()))
|
||||
e.FrontMatter.Rune = rune(i.Content[0])
|
||||
e.FrontMatter.Content, _, err = frontmatter.Pretty(page.FrontMatter())
|
||||
}
|
||||
|
||||
if e.Class == "complete" && !hasRune {
|
||||
err = errors.New("Complete but without rune")
|
||||
}
|
||||
|
||||
if editor.Class == "content-only" || err != nil {
|
||||
editor.Class = "content-only"
|
||||
editor.Content = i.StringifyContent()
|
||||
Error:
|
||||
if e.Class == "content-only" || err != nil {
|
||||
e.Class = "content-only"
|
||||
e.Content = i.StringifyContent()
|
||||
}
|
||||
|
||||
return editor, nil
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func editorClass(mode string) string {
|
||||
|
|
|
@ -52,7 +52,8 @@ func PreProccessPUT(
|
|||
var mark rune
|
||||
|
||||
if v := r.Header.Get("Rune"); v != "" {
|
||||
n, err := strconv.Atoi(v)
|
||||
var n int
|
||||
n, err = strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -160,7 +160,7 @@ func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro
|
|||
return http.StatusOK, err
|
||||
}
|
||||
|
||||
// PrintAsJSON prints the current Page infromation in JSON
|
||||
// PrintAsJSON prints the current Page information in JSON
|
||||
func (p Page) PrintAsJSON(w http.ResponseWriter) (int, error) {
|
||||
marsh, err := json.MarshalIndent(p.Info.Data, "", " ")
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue