<commit_before>package views

import (
	"fmt"
	"html/template"
	"net/http"
	"sort"

	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"

	"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")
	res := []*models.Page{}
	for _, v := range models.SPages {
		for _, b := range v.Tags {
			if b == tag {
				res = append(res, v)
			}
		}
	}
	if len(res) > 0 {
		data := gin.H{
			"posts":       res,
			"title":       viper.GetString("blog.title"),
			"description": viper.GetString("blog.description"),
			"extra":       template.HTML(fmt.Sprintf(`Posts tagged with <span class="home-sm-tag">%s</span>`, tag)),
			"analytics":   gin.H{"tag": viper.GetString("analytics.tag"), "enabled": viper.GetBool("analytics.enabled")},
			"author":      models.GetGlobalAuthor(),
		}
		c.HTML(http.StatusOK, "index.tmpl", data)
	} else {
		c.String(http.StatusNotFound, "404 no posts found with this tag")
	}
}

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

// RawPost is used to view the raw markdown file
func RawPost(c *gin.Context) {
	slug := c.Param("slug")
	if val, ok := models.MPages[slug]; ok {
		c.Writer.Write([]byte(val.Raw))
	} else {
		c.String(http.StatusNotFound, "404 not found")
	}
}

// Index is the view to list all posts.
func Index(c *gin.Context) {
	data := gin.H{
		"posts":       models.SPages,
		"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)
}

// 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)
}<commit_msg>Adding explanation text on draft page<commit_after>package views

import (
	"fmt"
	"html/template"
	"net/http"
	"sort"

	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"

	"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")
	res := []*models.Page{}
	for _, v := range models.SPages {
		for _, b := range v.Tags {
			if b == tag {
				res = append(res, v)
			}
		}
	}
	if len(res) > 0 {
		data := gin.H{
			"posts":       res,
			"title":       viper.GetString("blog.title"),
			"description": viper.GetString("blog.description"),
			"extra":       template.HTML(fmt.Sprintf(`Posts tagged with <span class="btn tag">%s</span>`, tag)),
			"analytics":   gin.H{"tag": viper.GetString("analytics.tag"), "enabled": viper.GetBool("analytics.enabled")},
			"author":      models.GetGlobalAuthor(),
		}
		c.HTML(http.StatusOK, "index.tmpl", data)
	} else {
		c.String(http.StatusNotFound, "404 no posts found with this tag")
	}
}

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

// RawPost is used to view the raw markdown file
func RawPost(c *gin.Context) {
	slug := c.Param("slug")
	if val, ok := models.MPages[slug]; ok {
		c.Writer.Write([]byte(val.Raw))
	} else {
		c.String(http.StatusNotFound, "404 not found")
	}
}

// Index is the view to list all posts.
func Index(c *gin.Context) {
	data := gin.H{
		"posts":       models.SPages,
		"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)
}

// 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"),
		"extra":       template.HTML(`These articles are drafts and may be incomplete`),
		"analytics":   gin.H{"tag": viper.GetString("analytics.tag"), "enabled": viper.GetBool("analytics.enabled")},
		"author":      models.GetGlobalAuthor(),
	}
	c.HTML(http.StatusOK, "index.tmpl", data)
}
{'fraction_non_alphanumeric': 0.1819227608874281, 'fraction_numerical': 0.005916187345932621, 'mean_word_length': 3.572501878287002, 'pattern_counts': {'":': 61, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1595396', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2247, 'n_words': 481}