2022-05-23 01:20:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrPageNotFound = fmt.Errorf("page not found")
|
|
|
|
|
|
|
|
// A Page is a single unit of content on the site.
|
|
|
|
type Page struct {
|
|
|
|
Title string
|
|
|
|
Contents []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index is a set of pages that exist at this level,
|
|
|
|
// as well as a set of "folders" that contain sub-indexes.
|
|
|
|
// The 'index' key in Pages is special, and will be returned
|
|
|
|
// if no key is provided.
|
|
|
|
type Index struct {
|
|
|
|
Children map[string]Index
|
|
|
|
Pages map[string]Page
|
|
|
|
}
|
|
|
|
|
|
|
|
// Page returns the requested page from the index, recursively
|
|
|
|
// key is assumed to be a `/`-separated string; Page will split on the slashes,
|
|
|
|
// descending into an index to find the page, if possible. If no match is found,
|
|
|
|
// return an empty page and `ErrPageNotFound`.
|
|
|
|
// `foo/` will return a page named `foo` in the current index, if it exists.
|
|
|
|
// Otherwise, if a child index named "foo" exists, page will attempt to return its index page.
|
|
|
|
// Page strips leading / from keys.
|
|
|
|
func (i *Index) Page(key string) (*Page, error) {
|
|
|
|
if key == "" || key == "/" {
|
|
|
|
key = "index"
|
|
|
|
}
|
|
|
|
if key[0] == '/' { // strip leading slash
|
|
|
|
key = key[1:]
|
|
|
|
}
|
|
|
|
curr, rest, found := strings.Cut(key, "/")
|
|
|
|
page, pageok := i.Pages[curr]
|
|
|
|
child, childok := i.Children[curr]
|
2022-05-23 01:31:23 +00:00
|
|
|
// no trailing slash and doesn't exist
|
2022-05-23 01:20:46 +00:00
|
|
|
if !found && !pageok {
|
|
|
|
return &Page{}, ErrPageNotFound
|
|
|
|
}
|
2022-05-23 01:31:23 +00:00
|
|
|
// trailing slash, exists as page
|
2022-05-23 01:20:46 +00:00
|
|
|
if rest == "" && pageok {
|
|
|
|
return &page, nil
|
|
|
|
}
|
2022-05-23 01:31:23 +00:00
|
|
|
// neither page nor child
|
2022-05-23 01:20:46 +00:00
|
|
|
if !childok {
|
|
|
|
return &Page{}, ErrPageNotFound
|
|
|
|
}
|
2022-05-23 01:31:23 +00:00
|
|
|
// recurse
|
2022-05-23 01:20:46 +00:00
|
|
|
return (&child).Page(rest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save stores a page in the index, recursively,
|
|
|
|
// overwriting any that may have existed before.
|
|
|
|
// `foo/` is stored as a page named 'foo' in the current index;
|
|
|
|
// default 'index' files should be explicitly passed as such.
|
2022-05-27 23:34:48 +00:00
|
|
|
// The empty key or `/` are turned into the "index" key.
|
2022-05-23 01:20:46 +00:00
|
|
|
// Leading slashes are stripped.
|
|
|
|
func (i *Index) Save(key string, page *Page) error {
|
|
|
|
if key == "" || key == "/" {
|
2022-05-27 23:34:48 +00:00
|
|
|
key = "index"
|
2022-05-23 01:20:46 +00:00
|
|
|
}
|
|
|
|
if key[0] == '/' { // strip leading slash
|
|
|
|
key = key[1:]
|
|
|
|
}
|
2022-05-23 01:31:23 +00:00
|
|
|
// init maps if not yet created
|
2022-05-23 01:20:46 +00:00
|
|
|
if i.Pages == nil {
|
|
|
|
i.Pages = map[string]Page{}
|
|
|
|
}
|
|
|
|
if i.Children == nil {
|
|
|
|
i.Children = map[string]Index{}
|
|
|
|
}
|
2022-05-23 01:31:23 +00:00
|
|
|
|
|
|
|
// save as page
|
2022-05-23 01:20:46 +00:00
|
|
|
curr, rest, _ := strings.Cut(key, "/")
|
|
|
|
if rest == "" {
|
|
|
|
i.Pages[curr] = *page
|
2022-05-23 01:31:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// recurse and save in child
|
|
|
|
children := i.Children[curr]
|
|
|
|
err := (&children).Save(rest, page)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-05-23 01:20:46 +00:00
|
|
|
}
|
2022-05-23 01:31:23 +00:00
|
|
|
i.Children[curr] = children
|
2022-05-23 01:20:46 +00:00
|
|
|
return nil
|
|
|
|
}
|