Skip to content
Permalink
Browse files

Adding view to list drafts

  • Loading branch information...
Depado committed Sep 13, 2018
1 parent f4cb8b8 commit bea4ea83ac7b26cbea67e26e9822f59e9146a4b6
Showing with 31 additions and 7 deletions.
  1. +7 −6 models/pages.go
  2. +4 −1 router/router.go
  3. +20 −0 views/views.go
@@ -25,7 +25,7 @@ var MPages map[string]*Page

// SPages is a sorted slice of pages. Sorted by date, it is used to render all
// the pages on the index page.
var SPages pageSlice
var SPages PageSlice

// Page is the main struct. It contains everything needed to render the article.
type Page struct {
@@ -98,20 +98,21 @@ func NewPageFromFile(fn string) (*Page, error) {
return p, nil
}

type pageSlice []*Page
// PageSlice is a slice of pointer to pages
type PageSlice []*Page

// Len is part of sort.Interface.
func (p pageSlice) Len() int {
func (p PageSlice) Len() int {
return len(p)
}

// Swap is part of sort.Interface.
func (p pageSlice) Swap(i, j int) {
func (p PageSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}

// Less is part of sort.Interface. We use count as the value to sort by
func (p pageSlice) Less(i, j int) bool {
func (p PageSlice) Less(i, j int) bool {
return p[i].Date.After(p[j].Date)
}

@@ -237,7 +238,7 @@ func ParseDir(dir string) error {
}

MPages = make(map[string]*Page, len(files))
SPages = make(pageSlice, 0, len(files))
SPages = make(PageSlice, 0, len(files))

for _, f := range files {
if f.IsDir() {
@@ -46,11 +46,14 @@ func Run() {

// Routes Definition
r.GET("/", views.Index)
r.GET("/drafts", views.GetDrafts)
r.GET("/rss", views.GetRSSFeed)
r.GET("/tag/:tag", views.PostsByTag)
r.GET("/post/:slug", views.Post)
r.GET("/post/:slug/raw", views.RawPost)
r.GET("/robots.txt", func(c *gin.Context) { c.String(http.StatusOK, "User-Agent: *\nDisallow: /post/*/raw") })
r.GET("/robots.txt", func(c *gin.Context) {
c.String(http.StatusOK, "User-Agent: *\nDisallow: /post/*/raw\nDisallow: /tag/*")
})

// Run
logrus.WithFields(logrus.Fields{
@@ -4,6 +4,7 @@ import (
"fmt"
"html/template"
"net/http"
"sort"

"github.com/gin-gonic/gin"
"github.com/spf13/viper"
@@ -85,3 +86,22 @@ func Index(c *gin.Context) {
}
c.HTML(http.StatusOK, "index.tmpl", data)
}

// GetDrafts gets the unsorted drafts
func GetDrafts(c *gin.Context) {
o := models.PageSlice{}
for _, v := range models.MPages {
if v.Draft {
o = append(o, v)
}
}
sort.Sort(o)
data := gin.H{
"posts": o,
"title": viper.GetString("blog.title"),
"description": viper.GetString("blog.description"),
"analytics": gin.H{"tag": viper.GetString("analytics.tag"), "enabled": viper.GetBool("analytics.enabled")},
"author": models.GetGlobalAuthor(),
}
c.HTML(http.StatusOK, "index.tmpl", data)
}

0 comments on commit bea4ea8

Please sign in to comment.
You can’t perform that action at this time.