Add persitent URLs for year/month/day in the diary
This commit is contained in:
@@ -21,6 +21,67 @@ func init() {
|
||||
|
||||
type diaryHandler struct{}
|
||||
|
||||
// redirect resolves the persistent date links (today, this-month, this-year)
|
||||
// inside any diary root to a dated URL. Returns ok=false otherwise.
|
||||
func (d *diaryHandler) redirect(root, fsPath, urlPath string) (string, bool) {
|
||||
base := path.Base(strings.TrimSuffix(urlPath, "/"))
|
||||
switch base {
|
||||
case "today", "this-month", "this-year":
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
|
||||
parentFS := filepath.Dir(fsPath)
|
||||
parentURLPath := parentURL(urlPath)
|
||||
_, diaryRootFS, diaryRootURL, ok := findDiaryContext(root, parentFS, parentURLPath)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
year := fmt.Sprintf("%d", now.Year())
|
||||
month := fmt.Sprintf("%02d", int(now.Month()))
|
||||
day := fmt.Sprintf("%02d", now.Day())
|
||||
|
||||
switch base {
|
||||
case "today":
|
||||
target := path.Join(diaryRootURL, year, month, day) + "/"
|
||||
dayFS := filepath.Join(diaryRootFS, year, month, day)
|
||||
if _, err := os.Stat(dayFS); err != nil {
|
||||
target += "?edit"
|
||||
}
|
||||
return target, true
|
||||
case "this-month":
|
||||
return path.Join(diaryRootURL, year, month) + "/", true
|
||||
case "this-year":
|
||||
return path.Join(diaryRootURL, year) + "/", true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// defaultHeading returns the German long-form date as the editor pre-fill
|
||||
// heading for a diary day folder (depth 3 inside a diary root).
|
||||
func (d *diaryHandler) defaultHeading(root, fsPath, urlPath string) (string, bool) {
|
||||
depth, _, _, ok := findDiaryContext(root, fsPath, urlPath)
|
||||
if !ok || depth != 3 {
|
||||
return "", false
|
||||
}
|
||||
day, err := strconv.Atoi(filepath.Base(fsPath))
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
monthFS := filepath.Dir(fsPath)
|
||||
month, err := strconv.Atoi(filepath.Base(monthFS))
|
||||
if err != nil || month < 1 || month > 12 {
|
||||
return "", false
|
||||
}
|
||||
year, err := strconv.Atoi(filepath.Base(filepath.Dir(monthFS)))
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return formatGermanDate(time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)), true
|
||||
}
|
||||
|
||||
func (d *diaryHandler) handle(root, fsPath, urlPath string) *specialPage {
|
||||
depth, diaryRootFS, diaryRootURL, ok := findDiaryContext(root, fsPath, urlPath)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user