Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support category in article url #49

Merged
merged 2 commits into from
Jun 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"fmt"
"github.com/gorilla/feeds"
"github.com/facebookgo/symwalk"
"github.com/gorilla/feeds"
"html/template"
"io/ioutil"
"os"
Expand Down Expand Up @@ -119,16 +119,23 @@ func Build() {
if article == nil || article.Draft {
return nil
}
var (
datePrefix = article.Time.Format("2006-01-02-")
)
// Generate page name
fileName := strings.TrimSuffix(strings.ToLower(filepath.Base(path)), ".md")
if strings.HasPrefix(fileName, datePrefix) {
fileName = fileName[len(datePrefix):]
}

Log("Building " + fileName)
// Genetate custom link
unixTime := time.Unix(article.Date, 0)
linkMap := map[string]string{
"{year}": unixTime.Format("2006"),
"{month}": unixTime.Format("01"),
"{day}": unixTime.Format("02"),
"{title}": fileName,
"{year}": article.Time.Format("2006"),
"{month}": article.Time.Format("01"),
"{day}": article.Time.Format("02"),
"{category}": article.Category,
"{title}": fileName,
}
var link string
if globalConfig.Site.Link == "" {
Expand Down Expand Up @@ -156,13 +163,13 @@ func Build() {
tagMap[tag] = append(tagMap[tag], *article)
}
// Get archive info
dateYear := unixTime.Format("2006")
dateYear := article.Time.Format("2006")
if _, ok := archiveMap[dateYear]; !ok {
archiveMap[dateYear] = make(Collections, 0)
}
articleInfo := ArticleInfo{
DetailDate: article.Date,
Date: unixTime.Format("2006-01-02"),
Date: article.Time.Format("2006-01-02"),
Title: article.Title,
Link: article.Link,
Top: article.Top,
Expand Down Expand Up @@ -217,7 +224,7 @@ func Build() {
articleValue := article.(Article)
articleInfos = append(articleInfos, ArticleInfo{
DetailDate: articleValue.Date,
Date: time.Unix(articleValue.Date, 0).Format("2006-01-02"),
Date: articleValue.Time.Format("2006-01-02"),
Title: articleValue.Title,
Link: articleValue.Link,
Top: articleValue.Top,
Expand Down Expand Up @@ -285,8 +292,8 @@ func GenerateRSS(articles Collections) {
Link: &feeds.Link{Href: globalConfig.Site.Url + article.Link},
Description: string(article.Content),
Author: &feeds.Author{article.Author.Name, ""},
Created: time.Unix(article.Date, 0),
Updated: time.Unix(article.Update, 0),
Created: article.Time,
Updated: article.MTime,
})
}
if atom, err := feed.ToAtom(); err == nil {
Expand Down
51 changes: 36 additions & 15 deletions parse.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"github.com/InkProject/blackfriday"
"gopkg.in/yaml.v2"
"html/template"
"io/ioutil"
"path/filepath"
"strings"
"time"

"github.com/InkProject/blackfriday"
"gopkg.in/yaml.v2"
)

type SiteConfig struct {
Expand Down Expand Up @@ -63,14 +65,17 @@ type ArticleConfig struct {
type Article struct {
GlobalConfig
ArticleConfig
Date int64
Update int64
Author AuthorConfig
Tags []string
Preview template.HTML
Content template.HTML
Link string
Config interface{}
Time time.Time
MTime time.Time
Date int64
Update int64
Author AuthorConfig
Category string
Tags []string
Preview template.HTML
Content template.HTML
Link string
Config interface{}
}

type ThemeConfig struct {
Expand Down Expand Up @@ -185,20 +190,36 @@ func ParseArticle(markdownPath string) *Article {
article.Preview = config.Preview
article.Config = config.Config
article.Content = Parse(content)
article.Date = ParseDate(config.Date).Unix()
article.Time = ParseDate(config.Date)
article.Date = article.Time.Unix()
if config.Update != "" {
article.MTime = ParseDate(config.Update)
article.Update = article.MTime.Unix()
}
article.Title = config.Title
article.Tags = config.Tags
article.Topic = config.Topic
article.Draft = config.Draft
article.Top = config.Top
if config.Update != "" {
article.Update = ParseDate(config.Update).Unix()
}
if author, ok := globalConfig.Authors[config.Author]; ok {
author.Id = config.Author
author.Avatar = ReplaceRootFlag(author.Avatar)
article.Author = author
}
if len(config.Categories) > 0 {
article.Category = config.Categories[0]
} else {
article.Category = "misc"
}
tags := map[string]bool{}
article.Tags = config.Tags
for _, tag := range config.Tags {
tags[tag] = true
}
for _, cat := range config.Categories {
if _, ok := tags[cat]; !ok {
article.Tags = append(article.Tags, cat)
}
}
// Support topic and cover field
if config.Cover != "" {
article.Cover = config.Cover
Expand Down
2 changes: 1 addition & 1 deletion template/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ site:
url: "http://www.inkpaper.io/blog"
comment: username
# logo: "-/images/avatar.jpg"
# link: "{title}.html"
# link: "{category}/{year}/{month}/{day}/{title}.html"
# root: "/blog"

authors:
Expand Down