Add archives for months and months + days

This commit is contained in:
Jan-Lukas Else 2020-12-26 20:40:22 +01:00
parent 93cebc32ae
commit a34dc887b0
3 changed files with 84 additions and 44 deletions

40
http.go
View File

@ -247,23 +247,41 @@ func buildHandler() (http.Handler, error) {
} }
// Year / month archives // Year / month archives
months, err := allMonths(blog) dates, err := allPublishedDates(blog)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for y, ms := range months { for _, d := range dates {
yearPath := blogPath + "/" + fmt.Sprintf("%0004d", y) // Year
yearHandler := serveYearMonth(blog, yearPath, y, 0) yearPath := blogPath + "/" + fmt.Sprintf("%0004d", d.year)
yearHandler := serveDate(blog, yearPath, d.year, 0, 0)
r.With(cacheMiddleware, minifier.Middleware).Get(yearPath, yearHandler) r.With(cacheMiddleware, minifier.Middleware).Get(yearPath, yearHandler)
r.With(cacheMiddleware, minifier.Middleware).Get(yearPath+feedPath, yearHandler) r.With(cacheMiddleware, minifier.Middleware).Get(yearPath+feedPath, yearHandler)
r.With(cacheMiddleware, minifier.Middleware).Get(yearPath+paginationPath, yearHandler) r.With(cacheMiddleware, minifier.Middleware).Get(yearPath+paginationPath, yearHandler)
for _, m := range ms { // Specific month
monthPath := yearPath + "/" + fmt.Sprintf("%02d", m) monthPath := yearPath + "/" + fmt.Sprintf("%02d", d.month)
monthHandler := serveYearMonth(blog, monthPath, y, m) monthHandler := serveDate(blog, monthPath, d.year, d.month, 0)
r.With(cacheMiddleware, minifier.Middleware).Get(monthPath, monthHandler) r.With(cacheMiddleware, minifier.Middleware).Get(monthPath, monthHandler)
r.With(cacheMiddleware, minifier.Middleware).Get(monthPath+feedPath, monthHandler) r.With(cacheMiddleware, minifier.Middleware).Get(monthPath+feedPath, monthHandler)
r.With(cacheMiddleware, minifier.Middleware).Get(monthPath+paginationPath, monthHandler) r.With(cacheMiddleware, minifier.Middleware).Get(monthPath+paginationPath, monthHandler)
} // Specific day
dayPath := monthPath + "/" + fmt.Sprintf("%02d", d.day)
dayHandler := serveDate(blog, monthPath, d.year, d.month, d.day)
r.With(cacheMiddleware, minifier.Middleware).Get(dayPath, dayHandler)
r.With(cacheMiddleware, minifier.Middleware).Get(dayPath+feedPath, dayHandler)
r.With(cacheMiddleware, minifier.Middleware).Get(dayPath+paginationPath, dayHandler)
// Generic month
genericMonthPath := blogPath + "/x/" + fmt.Sprintf("%02d", d.month)
genericMonthHandler := serveDate(blog, genericMonthPath, 0, d.month, 0)
r.With(cacheMiddleware, minifier.Middleware).Get(genericMonthPath, genericMonthHandler)
r.With(cacheMiddleware, minifier.Middleware).Get(genericMonthPath+feedPath, genericMonthHandler)
r.With(cacheMiddleware, minifier.Middleware).Get(genericMonthPath+paginationPath, genericMonthHandler)
// Specific day
genericMonthDayPath := genericMonthPath + "/" + fmt.Sprintf("%02d", d.day)
genericMonthDayHandler := serveDate(blog, genericMonthDayPath, 0, d.month, d.day)
r.With(cacheMiddleware, minifier.Middleware).Get(genericMonthDayPath, genericMonthDayHandler)
r.With(cacheMiddleware, minifier.Middleware).Get(genericMonthDayPath+feedPath, genericMonthDayHandler)
r.With(cacheMiddleware, minifier.Middleware).Get(genericMonthDayPath+paginationPath, genericMonthDayHandler)
} }
// Blog // Blog

View File

@ -140,36 +140,51 @@ func serveSearchResults(blog string, path string) func(w http.ResponseWriter, r
}) })
} }
func serveYearMonth(blog string, path string, year, month int) func(w http.ResponseWriter, r *http.Request) { func serveDate(blog string, path string, year, month, day int) func(w http.ResponseWriter, r *http.Request) {
var title string var title string
if month == 0 { // Specific
if year != 0 && month == 0 && day == 0 {
title = fmt.Sprintf("%0004d", year) title = fmt.Sprintf("%0004d", year)
} else { } else if year != 0 && month != 0 && day == 0 {
ml := monday.Locale(appConfig.Blogs[blog].TimeLang) ml := monday.Locale(appConfig.Blogs[blog].TimeLang)
date := time.Date(0, 0, 10, 0, 0, 0, 0, time.Local).AddDate(year, month, 0) date := time.Date(year, time.Month(month), 1, 1, 0, 0, 0, time.Local)
title = monday.Format(date, "January 2006", ml) title = monday.Format(date, "January 2006", ml)
} else if year != 0 && month != 0 && day != 0 {
ml := monday.Locale(appConfig.Blogs[blog].TimeLang)
date := time.Date(year, time.Month(month), day, 1, 0, 0, 0, time.Local)
title = monday.Format(date, "January 2, 2006", ml)
} else
// Generic
if year == 0 && month != 0 && day == 0 {
ml := monday.Locale(appConfig.Blogs[blog].TimeLang)
date := time.Date(0, time.Month(month), 1, 1, 0, 0, 0, time.Local)
title = monday.Format(date, "January", ml)
} else if year == 0 && month != 0 && day != 0 {
ml := monday.Locale(appConfig.Blogs[blog].TimeLang)
date := time.Date(0, time.Month(month), day, 1, 0, 0, 0, time.Local)
title = monday.Format(date, "January 2", ml)
} }
return serveIndex(&indexConfig{ return serveIndex(&indexConfig{
blog: blog, blog: blog,
path: path, path: path,
year: year, year: year,
month: month, month: month,
day: day,
title: title, title: title,
}) })
} }
type indexConfig struct { type indexConfig struct {
blog string blog string
path string path string
section *section section *section
tax *taxonomy tax *taxonomy
taxValue string taxValue string
parameter string parameter string
year int year, month, day int
month int title string
title string description string
description string summaryTemplate string
summaryTemplate string
} }
func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) { func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) {
@ -197,6 +212,7 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) {
search: search, search: search,
publishedYear: ic.year, publishedYear: ic.year,
publishedMonth: ic.month, publishedMonth: ic.month,
publishedDay: ic.day,
}}, appConfig.Blogs[ic.blog].Pagination) }}, appConfig.Blogs[ic.blog].Pagination)
p.SetPage(pageNo) p.SetPage(pageNo)
var posts []*post var posts []*post

View File

@ -220,18 +220,17 @@ func getPost(path string) (*post, error) {
} }
type postsRequestConfig struct { type postsRequestConfig struct {
search string search string
blog string blog string
path string path string
limit int limit int
offset int offset int
sections []string sections []string
taxonomy *taxonomy taxonomy *taxonomy
taxonomyValue string taxonomyValue string
parameter string parameter string
parameterValue string parameterValue string
publishedYear int publishedYear, publishedMonth, publishedDay int
publishedMonth int
} }
func buildQuery(config *postsRequestConfig) (query string, args []interface{}) { func buildQuery(config *postsRequestConfig) (query string, args []interface{}) {
@ -280,6 +279,10 @@ func buildQuery(config *postsRequestConfig) (query string, args []interface{}) {
postsTable = "(select * from " + postsTable + " p where substr(p.published, 6, 2) = @publishedmonth)" postsTable = "(select * from " + postsTable + " p where substr(p.published, 6, 2) = @publishedmonth)"
args = append(args, sql.Named("publishedmonth", fmt.Sprintf("%02d", config.publishedMonth))) args = append(args, sql.Named("publishedmonth", fmt.Sprintf("%02d", config.publishedMonth)))
} }
if config.publishedDay != 0 {
postsTable = "(select * from " + postsTable + " p where substr(p.published, 9, 2) = @publishedday)"
args = append(args, sql.Named("publishedday", fmt.Sprintf("%02d", config.publishedDay)))
}
defaultTables := " from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path " defaultTables := " from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path "
defaultSorting := " order by p.published desc " defaultSorting := " order by p.published desc "
if config.path != "" { if config.path != "" {
@ -367,19 +370,22 @@ func allTaxonomyValues(blog string, taxonomy string) ([]string, error) {
return values, nil return values, nil
} }
func allMonths(blog string) (months map[int][]int, err error) { type publishedDate struct {
rows, err := appDbQuery("select distinct substr(published, 1, 4) as year, substr(published, 6, 2) as month from posts where blog = @blog and year != '' and month != ''", sql.Named("blog", blog)) year, month, day int
}
func allPublishedDates(blog string) (dates []publishedDate, err error) {
rows, err := appDbQuery("select distinct substr(published, 1, 4) as year, substr(published, 6, 2) as month, substr(published, 9, 2) as day from posts where blog = @blog and year != '' and month != '' and day != ''", sql.Named("blog", blog))
if err != nil { if err != nil {
return nil, err return nil, err
} }
months = map[int][]int{}
for rows.Next() { for rows.Next() {
var year, month int var year, month, day int
err = rows.Scan(&year, &month) err = rows.Scan(&year, &month, &day)
if err != nil { if err != nil {
return nil, err return nil, err
} }
months[year] = append(months[year], month) dates = append(dates, publishedDate{year, month, day})
} }
return return
} }