rewrite some frontmatter and editor-related stuff

Former-commit-id: 15e46f99c7
This commit is contained in:
Henrique Dias 2017-01-18 18:40:20 +00:00
parent 34ce7afb88
commit 0ad540249e
5 changed files with 51 additions and 37 deletions

View File

@ -2,6 +2,7 @@ package frontmatter
import ( import (
"bytes" "bytes"
"errors"
"strings" "strings"
) )
@ -28,28 +29,30 @@ func AppendRune(frontmatter []byte, mark rune) []byte {
return frontmatter 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 { switch mark {
case '-': case '-':
return "yaml" return "yaml", nil
case '+': case '+':
return "toml" return "toml", nil
case '{': case '{':
return "json" return "json", nil
default: 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 { switch format {
case "yaml": case "yaml":
return '-' return '-', nil
case "toml": case "toml":
return '+' return '+', nil
case "json": case "json":
return '{' return '{', nil
default: default:
return '0' return '0', errors.New("Unsupported format type.")
} }
} }

View File

@ -14,7 +14,7 @@ import (
"github.com/mholt/archiver" "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. // 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) { func Download(w http.ResponseWriter, r *http.Request, c *config.Config, i *file.Info) (int, error) {
query := r.URL.Query().Get("download") query := r.URL.Query().Get("download")

View File

@ -29,52 +29,62 @@ func GetEditor(r *http.Request, i *file.Info) (*Editor, error) {
var err error var err error
// Create a new editor variable and set the mode // Create a new editor variable and set the mode
editor := new(Editor) e := new(Editor)
editor.Mode = editorMode(i.Name) e.Mode = editorMode(i.Name)
editor.Class = editorClass(editor.Mode) e.Class = editorClass(e.Mode)
if editor.Class == "frontmatter-only" || editor.Class == "complete" { if e.Class == "frontmatter-only" || e.Class == "complete" {
editor.Visual = true e.Visual = true
} }
if r.URL.Query().Get("visual") == "false" { if r.URL.Query().Get("visual") == "false" {
editor.Class = "content-only" e.Class = "content-only"
} }
if editor.Class == "frontmatter-only" { hasRune := frontmatter.HasRune(i.Content)
// Checks if the file already has the frontmatter rune and parses it
if frontmatter.HasRune(i.Content) { if e.Class == "frontmatter-only" && !hasRune {
editor.FrontMatter.Content, _, err = frontmatter.Pretty(i.Content) e.FrontMatter.Rune, err = frontmatter.StringFormatToRune(e.Mode)
} else { if err != nil {
editor.FrontMatter.Rune = frontmatter.StringFormatToRune(editor.Mode) goto Error
editor.FrontMatter.Content, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.FrontMatter.Rune)) }
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 var page parser.Page
// Starts a new buffer and parses the file using Hugo's functions // Starts a new buffer and parses the file using Hugo's functions
buffer := bytes.NewBuffer(i.Content) buffer := bytes.NewBuffer(i.Content)
page, err = parser.ReadFrom(buffer) page, err = parser.ReadFrom(buffer)
if err == nil { if err != nil {
// Parses the page content and the frontmatter goto Error
editor.Content = strings.TrimSpace(string(page.Content()))
editor.FrontMatter.Rune = rune(i.Content[0])
editor.FrontMatter.Content, _, err = frontmatter.Pretty(page.FrontMatter())
} }
// 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 editor.Class == "complete" && !frontmatter.HasRune(i.Content) { if e.Class == "complete" && !hasRune {
err = errors.New("Complete but without rune") err = errors.New("Complete but without rune")
} }
if editor.Class == "content-only" || err != nil { Error:
editor.Class = "content-only" if e.Class == "content-only" || err != nil {
editor.Content = i.StringifyContent() e.Class = "content-only"
e.Content = i.StringifyContent()
} }
return editor, nil return e, nil
} }
func editorClass(mode string) string { func editorClass(mode string) string {

View File

@ -52,7 +52,8 @@ func PreProccessPUT(
var mark rune var mark rune
if v := r.Header.Get("Rune"); v != "" { if v := r.Header.Get("Rune"); v != "" {
n, err := strconv.Atoi(v) var n int
n, err = strconv.Atoi(v)
if err != nil { if err != nil {
return err return err
} }

View File

@ -160,7 +160,7 @@ func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro
return http.StatusOK, err 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) { func (p Page) PrintAsJSON(w http.ResponseWriter) (int, error) {
marsh, err := json.MarshalIndent(p.Info.Data, "", " ") marsh, err := json.MarshalIndent(p.Info.Data, "", " ")
if err != nil { if err != nil {