Пример #1
0
// GET handles the GET method on browse page
func GET(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
	functions := template.FuncMap{
		"CanBeEdited": utils.CanBeEdited,
		"Defined":     utils.Defined,
	}

	tpl, err := utils.GetTemplate(r, functions, "browse")

	if err != nil {
		return http.StatusInternalServerError, err
	}

	b := browse.Browse{
		Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
			return 404, nil
		}),
		Root: c.Path,
		Configs: []browse.Config{
			{
				PathScope: "/",
				Variables: c,
				Template:  tpl,
			},
		},
		IgnoreIndexes: true,
	}

	return b.ServeHTTP(w, r)
}
Пример #2
0
// GET handles the GET method on editor page
func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
	// Check if the file format is supported. If not, send a "Not Acceptable"
	// header and an error
	if !utils.CanBeEdited(filename) {
		return http.StatusNotAcceptable, errors.New("File format not supported.")
	}

	// Check if the file exists. If it doesn't, send a "Not Found" message
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		return http.StatusNotFound, nil
	}

	// Open the file and check if there was some error while opening
	file, err := ioutil.ReadFile(filename)
	if err != nil {
		return http.StatusInternalServerError, err
	}

	// Create a new editor variable and set the extension
	page := new(editor)
	page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".")
	page.Name = filename
	page.Config = c
	page.IsPost = false

	// Sanitize the extension
	page.Mode = sanitizeMode(page.Mode)

	// Handle the content depending on the file extension
	switch page.Mode {
	case "markdown":
		if hasFrontMatterRune(file) {
			// Starts a new buffer and parses the file using Hugo's functions
			buffer := bytes.NewBuffer(file)
			file, err := parser.ReadFrom(buffer)
			if err != nil {
				return http.StatusInternalServerError, err
			}

			if strings.Contains(string(file.FrontMatter()), "date") {
				page.IsPost = true
			}

			// Parses the page content and the frontmatter
			page.Content = strings.TrimSpace(string(file.Content()))
			page.FrontMatter, err = frontmatter.Pretty(file.FrontMatter())
			page.Class = "complete"
		} else {
			// The editor will handle only content
			page.Class = "content-only"
			page.Content = string(file)
		}
	case "json", "toml", "yaml":
		// Defines the class and declares an error
		page.Class = "frontmatter-only"
		var err error

		// Checks if the file already has the frontmatter rune and parses it
		if hasFrontMatterRune(file) {
			page.FrontMatter, err = frontmatter.Pretty(file)
		} else {
			page.FrontMatter, err = frontmatter.Pretty(appendFrontMatterRune(file, page.Mode))
		}

		// Check if there were any errors
		if err != nil {
			return http.StatusInternalServerError, err
		}
	default:
		// The editor will handle only content
		page.Class = "content-only"
		page.Content = string(file)
	}

	// Create the functions map, then the template, check for erros and
	// execute the template if there aren't errors
	functions := template.FuncMap{
		"SplitCapitalize": utils.SplitCapitalize,
		"Defined":         utils.Defined,
	}

	tpl, err := utils.GetTemplate(r, functions, "editor", "frontmatter")

	if err != nil {
		return http.StatusInternalServerError, err
	}

	return http.StatusOK, tpl.Execute(w, page)
}