Skip to content
Permalink
Browse files

Unlisting draft instead of not parsing them, RSS feed and more

  • Loading branch information...
Depado committed May 17, 2018
1 parent 53d9ec6 commit 170995714f71aa1189baee2782568ef392450c5a
Showing with 143 additions and 40 deletions.
  1. +21 −21 Gopkg.lock
  2. +2 −0 cmd/serve.go
  3. +3 −1 main.go
  4. +50 −0 models/feed.go
  5. +44 −4 models/pages.go
  6. +1 −0 router/router.go
  7. +1 −0 templates/index.tmpl
  8. +7 −2 templates/post.tmpl
  9. +1 −9 templates/share.tmpl
  10. +13 −3 views/views.go

Some generated files are not rendered by default. Learn more.

@@ -20,6 +20,8 @@ func init() {
serve.Flags().String("server.host", "127.0.0.1", "host on which the server should listen")
serve.Flags().Int("server.port", 8080, "port on which the server should listen")
serve.Flags().Bool("server.debug", false, "debug mode for the server")
serve.Flags().String("server.domain", "", "domain of the blog used for RSS feed and share functionalities")
serve.Flags().Bool("server.tls", false, "whether https is activated for the domain")

// Gitalk
serve.Flags().Bool("gitalk.enabled", false, "enable the gitalk feature")
@@ -1,6 +1,8 @@
package main

import "github.com/Depado/smallblog/cmd"
import (
"github.com/Depado/smallblog/cmd"
)

func main() {
cmd.Execute()
@@ -0,0 +1,50 @@
package models

import (
"fmt"
"time"

"github.com/gorilla/feeds"
"github.com/spf13/viper"
)

// RSS is the main RSS feed
var RSS *feeds.Feed

func init() {
now := time.Now()
RSS = &feeds.Feed{
Title: viper.GetString("blog.title"),
Description: viper.GetString("blog.description"),
Author: &feeds.Author{
Name: viper.GetString("blog.author"),
},
Link: &feeds.Link{Href: viper.GetString("server.domain")},
Created: now,
}
}

// UpdateRSSFeed will update the items in the RSS feed to reflect the changes
func UpdateRSSFeed() {
var items []*feeds.Item
root := viper.GetString("server.domain")

for _, v := range SPages {
if v.Draft {
continue
}
i := &feeds.Item{
Title: v.Title,
Link: &feeds.Link{Href: fmt.Sprintf("%s/post/%s", root, v.Slug)},
Created: v.Date,
Author: &feeds.Author{Name: v.Author.Name},
Description: v.Description,
Content: v.Raw,
}
if v.Banner != "" {
i.Enclosure = &feeds.Enclosure{Url: v.Banner, Type: "image"}
}
items = append(items, i)
}
RSS.Items = items
}
@@ -4,10 +4,12 @@ import (
"fmt"
"html/template"
"io/ioutil"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"

"github.com/spf13/viper"
@@ -41,6 +43,41 @@ type Page struct {
Draft bool
}

// GetURL returns the URL of the page
func (p Page) GetURL() *url.URL {
u := &url.URL{
Scheme: "http",
Host: viper.GetString("server.domain"),
Path: fmt.Sprintf("/post/%s", p.Slug),
}
if viper.GetBool("server.tls") {
u.Scheme = "https"
}
return u
}

// GetShare returns an URL for the Twitter share button
func (p Page) GetShare() *url.URL {
txt := fmt.Sprintf(`"%s"`, p.Title)
if p.Author != nil {
if p.Author.Twitter != "" {
txt = fmt.Sprintf("%s by @%s", txt, p.Author.Twitter)
} else {
txt = fmt.Sprintf("%s by %s", txt, p.Author.Name)
}
}
u := &url.URL{Scheme: "http", Host: "twitter.com", Path: "/share"}
if viper.GetBool("server.tls") {
u.Scheme = "https"
}
q := url.Values{}
q.Set("url", p.GetURL().String())
q.Set("text", txt)
q.Set("hashtags", strings.Join(p.Tags, ","))
u.RawQuery = q.Encode()
return u
}

// NewPageFromFile parses a file, inserts it in the map and slice, and returns a *Page instance
func NewPageFromFile(fn string) (*Page, error) {
var err error
@@ -121,6 +158,7 @@ func (p *Page) Pop() {
}
}
delete(MPages, p.Slug)
UpdateRSSFeed()
logrus.WithFields(logrus.Fields{
"file": filepath.Base(p.File),
"url": fmt.Sprintf("/post/%s", p.Slug),
@@ -172,18 +210,19 @@ func (p *Page) ParseMarkdown(b []byte) {
// it's not a batch insertion. (A batch insertion means after all the inserts,
// SPages will be sorted manually)
func (p *Page) Insert(batch bool) error {
if p.Draft && !viper.GetBool("blog.draft") {
logrus.WithFields(logrus.Fields{"file": p.File, "state": "ignored"}).Info("Draft")
return nil
}
if val, ok := MPages[p.Slug]; ok {
return fmt.Errorf("Two pages have the same slug : %s and %s both have %s", p.File, val.File, p.Slug)
}
MPages[p.Slug] = p
if p.Draft && !viper.GetBool("blog.draft") {
logrus.WithFields(logrus.Fields{"file": p.File, "state": "unlisted"}).Info("Draft")
return nil
}
SPages = append(SPages, p)
if !batch {
sort.Sort(SPages)
}
UpdateRSSFeed()
return nil
}

@@ -233,5 +272,6 @@ func ParseDir(dir string) error {
"files": len(files),
"took": time.Since(start),
}).Info("Generated files")
UpdateRSSFeed()
return nil
}
@@ -46,6 +46,7 @@ func Run() {

// Routes Definition
r.GET("/", views.Index)
r.GET("/rss", views.GetRSSFeed)
r.GET("/tag/:tag", views.PostsByTag)
r.GET("/post/:slug", views.Post)
r.GET("/post/:slug/raw", views.RawPost)
@@ -20,6 +20,7 @@
<div class="pure-u-1 pure-u-md-1-2">
<div class="head">
{{ if .title }}<h1 class="title">{{ .title }}</h1>{{ end }}
{{ if .feed }}<a href="/rss"><i class="fas fa-rss"></i></a>{{ end }}
{{ if .description }}{{ .description }}{{ end }}
{{ if .author }}{{ template "author" .author }}{{ end }}
</div>
@@ -45,9 +45,14 @@
{{ end }}
</div>
<div class="pure-u-1 pure-u-md-1-4">
{{ if .share }}
{{ if (and .share (not .post.Draft)) }}
<div style="float: right;">
{{ template "share" .post }}
{{ template "share" .share_url }}
</div>
{{ end }}
{{ if .post.Draft }}
<div class="btn tag" style="float: right; text-align: center;">
<i class="far fa-file-alt"></i> Draft Mode<br />This page is unlisted
</div>
{{ end }}
</div>
@@ -1,11 +1,3 @@
{{ define "share" }}
{{ if .Author }}
{{ if .Author.Twitter }}
<a target="_blank" href='//twitter.com/share?text="{{ .Title }}" by @{{ .Author.Twitter }}'><span class="btn twitter"><i class="fab fa-twitter"></i> Tweet</span></a>
{{ else }}
<a target="_blank" href='//twitter.com/share?text="{{ .Title }}" by {{ .Author.Name }}'><span class="btn twitter"><i class="fab fa-twitter"></i> Tweet</span></a>
{{ end }}
{{ else }}
<a target="_blank" href='//twitter.com/share?text="{{ .Title }}"'><span class="btn twitter"><i class="fab fa-twitter"></i> Tweet</span></a>
{{ end }}
<a target="_blank" href='{{ . }}'><span class="btn twitter"><i class="fab fa-twitter"></i> Tweet</span></a>
{{ end }}
@@ -11,6 +11,16 @@ import (
"github.com/Depado/smallblog/models"
)

// GetRSSFeed returns the RSS feed of the blog
func GetRSSFeed(c *gin.Context) {
rss, err := models.RSS.ToRss()
if err != nil {
c.String(http.StatusInternalServerError, "500 internal server error")
return
}
c.Data(200, "text/xml", []byte(rss))
}

// PostsByTag searches for posts containing tag
func PostsByTag(c *gin.Context) {
tag := c.Param("tag")
@@ -38,13 +48,13 @@ func PostsByTag(c *gin.Context) {

// Post is the views for a single post.
func Post(c *gin.Context) {
slug := c.Param("slug")
if val, ok := models.MPages[slug]; ok {
if page, ok := models.MPages[c.Param("slug")]; ok {
data := gin.H{
"post": val,
"post": page,
"gitalk": models.GetGitalk(),
"extra_style": models.GlobCSS,
"share": viper.GetBool("blog.share"),
"share_url": page.GetShare(),
}
c.HTML(http.StatusOK, "post.tmpl", data)
} else {

0 comments on commit 1709957

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