get rid of html inside generator once and for all
This commit is contained in:
parent
adbf2cad0e
commit
6cc17fd4e8
88
blog/blog.go
88
blog/blog.go
|
@ -23,26 +23,7 @@ var folder string
|
|||
var style string
|
||||
var templateString string
|
||||
var t *template.Template
|
||||
var categories map[string][]post
|
||||
|
||||
type metadata struct {
|
||||
Title string
|
||||
Date time.Time
|
||||
Categories []string
|
||||
}
|
||||
|
||||
type post struct {
|
||||
Content string
|
||||
Meta metadata
|
||||
Filename string
|
||||
Link string
|
||||
}
|
||||
|
||||
type categoryListing struct {
|
||||
Name string
|
||||
ASCIIName string
|
||||
Posts []post
|
||||
}
|
||||
var categories map[string][]Post
|
||||
|
||||
func linkToPost(filename string) string {
|
||||
filename = filepath.Base(filename)
|
||||
|
@ -62,14 +43,14 @@ func linkToCategory(name string) string {
|
|||
return fmt.Sprintf("/%s/categories.html#%s", folder, ConvASCII(strings.ToLower(name)))
|
||||
}
|
||||
|
||||
func addToCategory(category string, post post) {
|
||||
func addToCategory(category string, post Post) {
|
||||
c := strings.ToLower(category)
|
||||
if len(c) > 0 {
|
||||
categories[c] = append(categories[c], post)
|
||||
}
|
||||
}
|
||||
|
||||
func keys(m map[string][]post) []string {
|
||||
func keys(m map[string][]Post) []string {
|
||||
keys := make([]string, 0)
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
|
@ -77,7 +58,7 @@ func keys(m map[string][]post) []string {
|
|||
return keys
|
||||
}
|
||||
|
||||
func emitPostPage(post post) string {
|
||||
func emitPostPage(post Post) string {
|
||||
filename := strings.TrimPrefix(strings.TrimPrefix(post.Filename, "stage1/"), "stage1\\")
|
||||
link := post.Link
|
||||
os.Remove(filepath.Join("stage2", filename))
|
||||
|
@ -110,13 +91,13 @@ func indexPageExists(i int, total int) bool {
|
|||
return (i >= 0) && (i <= max)
|
||||
}
|
||||
|
||||
type byDateDesc []post
|
||||
type byDateDesc []Post
|
||||
|
||||
func (p byDateDesc) Len() int { return len(p) }
|
||||
func (p byDateDesc) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
func (p byDateDesc) Less(i, j int) bool { return p[i].Meta.Date.After(p[j].Meta.Date) }
|
||||
|
||||
func emitIndexPage(i int, posts []post, total int) {
|
||||
func emitIndexPage(i int, posts []Post, total int) {
|
||||
file := indexFilename(i)
|
||||
postCollection := make([]Blogpost, 0)
|
||||
sort.Sort(byDateDesc(posts))
|
||||
|
@ -145,9 +126,9 @@ func emitIndexPage(i int, posts []post, total int) {
|
|||
goutil.WriteFile(file, buf.String())
|
||||
}
|
||||
|
||||
func getMetadata(file string) (metadata, error) {
|
||||
func getMetadata(file string) (Metadata, error) {
|
||||
metaAll, err := goutil.ReadFile(goutil.TrimExt(file) + ".desc")
|
||||
ret := metadata{}
|
||||
ret := Metadata{}
|
||||
if err == nil {
|
||||
lines := strings.Split(metaAll, "\n")
|
||||
for _, line := range lines {
|
||||
|
@ -177,12 +158,12 @@ func getMetadata(file string) (metadata, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func getPost(file string) (post, error) {
|
||||
func getPost(file string) (Post, error) {
|
||||
tmp, err := goutil.ReadFile(file)
|
||||
if err == nil {
|
||||
meta, err2 := getMetadata(file)
|
||||
if err2 == nil {
|
||||
p := post{}
|
||||
p := Post{}
|
||||
p.Content = tmp
|
||||
p.Meta = meta
|
||||
p.Filename = file
|
||||
|
@ -190,71 +171,52 @@ func getPost(file string) (post, error) {
|
|||
p.Link = linkToPost(filename)
|
||||
return p, nil
|
||||
} else {
|
||||
return post{}, err2
|
||||
return Post{}, err2
|
||||
}
|
||||
} else {
|
||||
return post{}, err
|
||||
return Post{}, err
|
||||
}
|
||||
}
|
||||
|
||||
func makeCategories() {
|
||||
ct := template.Must(template.New("Categories").Parse(categoryTemplate))
|
||||
page := Page{}
|
||||
page.Title = blogname + ": Categories"
|
||||
page.Style = style
|
||||
keySet := keys(categories)
|
||||
sort.Strings(keySet)
|
||||
page.Content += "<dl style=\"padding-bottom:150em\">"
|
||||
for _, k := range keySet {
|
||||
listing := categoryListing{}
|
||||
listing := CategoryListing{}
|
||||
listing.Name = k
|
||||
listing.ASCIIName = ConvASCII(k)
|
||||
sort.Sort(byDate(categories[k]))
|
||||
listing.Posts = categories[k]
|
||||
buf := new(bytes.Buffer)
|
||||
ct.Execute(buf, listing)
|
||||
page.Content += buf.String()
|
||||
page.CategoryListings = append(page.CategoryListings, listing)
|
||||
}
|
||||
page.Content += "</dl>"
|
||||
buf := new(bytes.Buffer)
|
||||
t.Execute(buf, page)
|
||||
goutil.WriteFile(filepath.Join("stage2", folder, "categories.html"), buf.String())
|
||||
}
|
||||
|
||||
func makeTimeline(posts []post) {
|
||||
func makeTimeline(posts []Post) {
|
||||
sort.Sort(byDateDesc(posts))
|
||||
page := Page{}
|
||||
page.Title = blogname + ": Timeline"
|
||||
page.Style = style
|
||||
page.Content += "<dl>"
|
||||
year := -1
|
||||
month := -1
|
||||
for _, p := range posts {
|
||||
if year != p.Meta.Date.Year() {
|
||||
year = p.Meta.Date.Year()
|
||||
page.Content += "</dl>\n\n" + "<h3>" + strconv.Itoa(year) + "</h3>\n<dl>\n"
|
||||
}
|
||||
if month != int(p.Meta.Date.Month()) {
|
||||
month = int(p.Meta.Date.Month())
|
||||
page.Content += fmt.Sprintf("<dt>%02d</dt>", month)
|
||||
}
|
||||
page.Content += fmt.Sprintf("<dd><a href=\"%s\">%s</a></dd>\n", p.Link, p.Meta.Title)
|
||||
}
|
||||
page.Content += "</dl>"
|
||||
page.TimelineListing = posts
|
||||
buf := new(bytes.Buffer)
|
||||
t.Execute(buf, page)
|
||||
goutil.WriteFile(filepath.Join("stage2", folder, "timeline.html"), buf.String())
|
||||
}
|
||||
|
||||
type byDate []post
|
||||
type byDate []Post
|
||||
|
||||
func (p byDate) Len() int { return len(p) }
|
||||
func (p byDate) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
func (p byDate) Less(i, j int) bool { return p[i].Meta.Date.Before(p[j].Meta.Date) }
|
||||
|
||||
func getPosts() []post {
|
||||
func getPosts() []Post {
|
||||
files := goutil.ListFilesExt(filepath.Join("stage1", folder), ".html")
|
||||
posts := make([]post, 0)
|
||||
posts := make([]Post, 0)
|
||||
for _, file := range files {
|
||||
tmp, err := getPost(file)
|
||||
if err == nil {
|
||||
|
@ -268,13 +230,13 @@ func makeBlog() {
|
|||
posts := getPosts()
|
||||
sort.Sort(byDate(posts))
|
||||
total := len(posts)
|
||||
s := make([]post, 0)
|
||||
s := make([]Post, 0)
|
||||
n := 0
|
||||
for i, tmp := range posts {
|
||||
if (i / postsPerPage) != n {
|
||||
emitIndexPage(n, s, total)
|
||||
n = i / postsPerPage
|
||||
s = make([]post, 0)
|
||||
s = make([]Post, 0)
|
||||
}
|
||||
s = append(s, tmp)
|
||||
}
|
||||
|
@ -293,7 +255,7 @@ func Generate(dir string) {
|
|||
blogname = "blog"
|
||||
}
|
||||
blogname = strings.TrimSpace(blogname)
|
||||
categories = make(map[string][]post)
|
||||
categories = make(map[string][]Post)
|
||||
var err error
|
||||
var err2 error
|
||||
style, err = goutil.ReadFile("style.css")
|
||||
|
@ -306,9 +268,3 @@ func Generate(dir string) {
|
|||
fmt.Println(err2)
|
||||
}
|
||||
}
|
||||
|
||||
const categoryTemplate string = `
|
||||
<dt><a name="{{.ASCIIName}}" class="categoryListing">{{.Name}}</a></dt>
|
||||
{{range $index, $post := .Posts}}<dd><a href="{{$post.Link}}">{{$post.Meta.Title}}</a></dd>
|
||||
{{end}}
|
||||
`
|
||||
|
|
|
@ -77,6 +77,10 @@ func initialize(style Style) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.Mkdir("stage0/blog", 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = dumpInitFiles(style)
|
||||
}
|
||||
return err
|
||||
|
|
|
@ -26,9 +26,19 @@ a.linktopost {
|
|||
a.categoryListing {
|
||||
color: #E8E8E8;
|
||||
}
|
||||
a.timelineListing {
|
||||
color: #E8E8E8;
|
||||
text-decoration : none;
|
||||
}
|
||||
a.timelineListing:hover {
|
||||
color : red;
|
||||
}
|
||||
div.timelineListing {
|
||||
font-family: Monospace;
|
||||
}
|
||||
img {
|
||||
display:block;
|
||||
max-width: 80%;
|
||||
max-width: 100%;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ var defaultTemplate string = `<!DOCTYPE html>
|
|||
<h1>{{if .Blogpost}}{{.Blogname}}: {{end}}{{.Title}}</h1>
|
||||
<div class="navigation">
|
||||
<a href="/">home</a>
|
||||
<a href="/blog">blog</a>
|
||||
<a href="/blog/">blog</a>
|
||||
<a href="/photos/pages/">photos</a>
|
||||
</div>
|
||||
|
||||
|
@ -44,9 +44,35 @@ var defaultTemplate string = `<!DOCTYPE html>
|
|||
{{end}}
|
||||
{{end}}
|
||||
|
||||
|
||||
|
||||
<div class="content">
|
||||
{{.Content}}
|
||||
|
||||
{{if .CategoryListings}}
|
||||
<div class="categoryListing">
|
||||
<dl style="padding-bottom:150em;">
|
||||
{{range $i, $listing := .CategoryListings -}}
|
||||
<dt><a name="{{$listing.ASCIIName}}" class="categoryListing">{{$listing.Name}}</a></dt>
|
||||
{{range $j, $post := $listing.Posts -}}
|
||||
<dd><a href="{{$post.Link}}">{{$post.Meta.Title}}</a></dd>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</dl>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{if .TimelineListing}}
|
||||
<div class="timelineListing">
|
||||
{{range $i, $post := .TimelineListing -}}
|
||||
<a href="{{$post.Link}}" class="timelineListing">{{$post.Meta.FormattedDate}} {{$post.Meta.Title}}</a><br>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
{{if .Categories}}
|
||||
<div class="categories">
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package templatestructures
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Page struct {
|
||||
Title string
|
||||
Style string
|
||||
|
@ -11,6 +15,8 @@ type Page struct {
|
|||
Blogpost bool
|
||||
PostCollection []Blogpost
|
||||
Categories []Category
|
||||
CategoryListings []CategoryListing
|
||||
TimelineListing []Post
|
||||
Previous string
|
||||
Later string
|
||||
Time string
|
||||
|
@ -32,3 +38,26 @@ type Category struct {
|
|||
Name string
|
||||
Path string
|
||||
}
|
||||
|
||||
type CategoryListing struct {
|
||||
Name string
|
||||
ASCIIName string
|
||||
Posts []Post
|
||||
}
|
||||
|
||||
type Post struct {
|
||||
Content string
|
||||
Meta Metadata
|
||||
Filename string
|
||||
Link string
|
||||
}
|
||||
|
||||
type Metadata struct {
|
||||
Title string
|
||||
Date time.Time
|
||||
Categories []string
|
||||
}
|
||||
|
||||
func (m Metadata) FormattedDate() string {
|
||||
return m.Date.Format("2006-01-02")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user