Skip to content

Commit

Permalink
tag links support
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxiaohei committed Feb 18, 2014
1 parent 6bca46b commit 274dbc9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/handler/func.go
Expand Up @@ -80,5 +80,6 @@ func SidebarHtml(context *GoInk.Context) string {
return Theme(context).Tpl("sidebar", map[string]interface{}{
"Popular": model.GetPopularArticleList(4),
"RecentComment": model.GetCommentRecentList(3),
"Tags":model.GetContentTags(),
})
}
32 changes: 32 additions & 0 deletions app/model/content.go
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/fuxiaohei/GoBlog/app/utils"
"net/url"
"os"
"path/filepath"
"sort"
Expand All @@ -15,6 +16,7 @@ var (
contents map[int]*Content
contentMaxId int
contentsIndex map[string][]int
tags []*Tag
)

// Content instance, defines content data items.
Expand Down Expand Up @@ -56,6 +58,15 @@ func (cnt *Content) TagString() string {
return strings.Join(cnt.Tags, ",")
}

// GetTags returns tags struct in this content.
func (cnt *Content) GetTags() []*Tag {
tgs := make([]*Tag, len(cnt.Tags))
for i, t := range cnt.Tags {
tgs[i] = &Tag{Name: t}
}
return tgs
}

// Link returns content link as {type}/{id}/{slug}.html.
// If content isn't published, return "#".
// If content is page and top linked, return {slug}.html as top level link.
Expand Down Expand Up @@ -122,6 +133,17 @@ func (cnt *Content) User() *User {
return GetUserById(cnt.AuthorId)
}

// Content Tag struct. It convert tag string to proper struct or link.
type Tag struct {
Name string
Cid []int
}

// Link returns tag name url-encoded link.
func (t *Tag) Link() string {
return "/tag/" + url.QueryEscape(t.Name)
}

// GetContentById gets a content by given id.
func GetContentById(id int) *Content {
return contents[id]
Expand Down Expand Up @@ -332,16 +354,26 @@ func generateContentTmpIndexes() {

// assemble indexes map
data["pop-index"] = popIndex
tags = make([]*Tag, 0)
for tag, index := range tagIndexes {
sort.Sort(sort.Reverse(sort.IntSlice(index)))
data["t-"+tag] = index
contentsIndex["t-"+tag] = index
t := new(Tag)
t.Name = tag
t.Cid = index
tags = append(tags, t)
}

// write to tmp data
TmpStorage.Set("contents", data)
}

// GetContentTags returns all tags.
func GetContentTags() []*Tag {
return tags
}

// GetPopularArticleList returns popular articles list.
// Popular articles are ordered by comment number.
func GetPopularArticleList(size int) []*Content {
Expand Down
2 changes: 1 addition & 1 deletion view/saber/article.html
Expand Up @@ -8,7 +8,7 @@ <h3 class="title"><a href="{{.Link}}" title="{{.Title}}">{{.Title}}</a></h3>
<p class="time-info">{{if .User}}<strong>{{.User.Nick}}</strong>&nbsp;&nbsp;&nbsp;{{end}}发表于&nbsp;&nbsp;&nbsp;{{DateInt64 .CreateTime "YYYY 年 MM 月 DD 日"}}</p>
<section class="content markdown">{{Html .Content}}</section>
<p class="info clear">
<span class="tag inline-block">标签:{{.TagString}}</span>
<span class="tag inline-block">标签:{{range .GetTags}}<a href="{{.Link}}">{{.Name}}</a>{{end}}</span>
</p>
</div>{{end}}
</article>
Expand Down
2 changes: 1 addition & 1 deletion view/saber/index.html
Expand Up @@ -9,7 +9,7 @@ <h3 class="title"><a href="{{.Link}}" title="{{.Title}}">{{.Title}}</a></h3>
<section class="content markdown">{{Html .Summary}}</section>
<p class="info clear">
<span class="time-info">{{if .User}}<strong>{{.User.Nick}}</strong>&nbsp;&nbsp;&nbsp;{{end}}发表于&nbsp;&nbsp;&nbsp;{{DateInt64 .CreateTime "YYYY 年 MM 月 DD 日"}}</span>
<span class="tag">标签:{{.TagString}}</span>
<span class="tag">标签:{{range .GetTags}}<a href="{{.Link}}">{{.Name}}</a>{{end}}</span>
<a class="inline-block read right" href="{{.Link}}">继续阅读</a>
</p>
</div>
Expand Down
8 changes: 8 additions & 0 deletions view/saber/sidebar.html
Expand Up @@ -17,4 +17,12 @@ <h5 class="title">最新评论</h5>
</li>{{end}}
</ul>
</div>
<div class="sidebar" id="tags-sidebar">
<h5 class="title">文章标签</h5>
<ul class="list">{{range .Tags}}
<li class="inline-block">
<a class="author lbl lbl-green" href="{{.Link}}">{{.Name}}</a>
</li>{{end}}
</ul>
</div>
</div>

0 comments on commit 274dbc9

Please sign in to comment.