mirror of https://github.com/jlelse/GoBlog
Add archives for months and months + days
This commit is contained in:
parent
93cebc32ae
commit
a34dc887b0
40
http.go
40
http.go
|
@ -247,23 +247,41 @@ func buildHandler() (http.Handler, error) {
|
|||
}
|
||||
|
||||
// Year / month archives
|
||||
months, err := allMonths(blog)
|
||||
dates, err := allPublishedDates(blog)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for y, ms := range months {
|
||||
yearPath := blogPath + "/" + fmt.Sprintf("%0004d", y)
|
||||
yearHandler := serveYearMonth(blog, yearPath, y, 0)
|
||||
for _, d := range dates {
|
||||
// Year
|
||||
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+feedPath, yearHandler)
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(yearPath+paginationPath, yearHandler)
|
||||
for _, m := range ms {
|
||||
monthPath := yearPath + "/" + fmt.Sprintf("%02d", m)
|
||||
monthHandler := serveYearMonth(blog, monthPath, y, m)
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(monthPath, monthHandler)
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(monthPath+feedPath, monthHandler)
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(monthPath+paginationPath, monthHandler)
|
||||
}
|
||||
// Specific month
|
||||
monthPath := yearPath + "/" + fmt.Sprintf("%02d", d.month)
|
||||
monthHandler := serveDate(blog, monthPath, d.year, d.month, 0)
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(monthPath, monthHandler)
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(monthPath+feedPath, 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
|
||||
|
|
46
posts.go
46
posts.go
|
@ -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
|
||||
if month == 0 {
|
||||
// Specific
|
||||
if year != 0 && month == 0 && day == 0 {
|
||||
title = fmt.Sprintf("%0004d", year)
|
||||
} else {
|
||||
} else if year != 0 && month != 0 && day == 0 {
|
||||
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)
|
||||
} 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{
|
||||
blog: blog,
|
||||
path: path,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
title: title,
|
||||
})
|
||||
}
|
||||
|
||||
type indexConfig struct {
|
||||
blog string
|
||||
path string
|
||||
section *section
|
||||
tax *taxonomy
|
||||
taxValue string
|
||||
parameter string
|
||||
year int
|
||||
month int
|
||||
title string
|
||||
description string
|
||||
summaryTemplate string
|
||||
blog string
|
||||
path string
|
||||
section *section
|
||||
tax *taxonomy
|
||||
taxValue string
|
||||
parameter string
|
||||
year, month, day int
|
||||
title string
|
||||
description string
|
||||
summaryTemplate string
|
||||
}
|
||||
|
||||
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,
|
||||
publishedYear: ic.year,
|
||||
publishedMonth: ic.month,
|
||||
publishedDay: ic.day,
|
||||
}}, appConfig.Blogs[ic.blog].Pagination)
|
||||
p.SetPage(pageNo)
|
||||
var posts []*post
|
||||
|
|
42
postsDb.go
42
postsDb.go
|
@ -220,18 +220,17 @@ func getPost(path string) (*post, error) {
|
|||
}
|
||||
|
||||
type postsRequestConfig struct {
|
||||
search string
|
||||
blog string
|
||||
path string
|
||||
limit int
|
||||
offset int
|
||||
sections []string
|
||||
taxonomy *taxonomy
|
||||
taxonomyValue string
|
||||
parameter string
|
||||
parameterValue string
|
||||
publishedYear int
|
||||
publishedMonth int
|
||||
search string
|
||||
blog string
|
||||
path string
|
||||
limit int
|
||||
offset int
|
||||
sections []string
|
||||
taxonomy *taxonomy
|
||||
taxonomyValue string
|
||||
parameter string
|
||||
parameterValue string
|
||||
publishedYear, publishedMonth, publishedDay int
|
||||
}
|
||||
|
||||
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)"
|
||||
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 "
|
||||
defaultSorting := " order by p.published desc "
|
||||
if config.path != "" {
|
||||
|
@ -367,19 +370,22 @@ func allTaxonomyValues(blog string, taxonomy string) ([]string, error) {
|
|||
return values, nil
|
||||
}
|
||||
|
||||
func allMonths(blog string) (months map[int][]int, err error) {
|
||||
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))
|
||||
type publishedDate struct {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
months = map[int][]int{}
|
||||
for rows.Next() {
|
||||
var year, month int
|
||||
err = rows.Scan(&year, &month)
|
||||
var year, month, day int
|
||||
err = rows.Scan(&year, &month, &day)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
months[year] = append(months[year], month)
|
||||
dates = append(dates, publishedDate{year, month, day})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue