forked from go-xiaohei/pugo
/
author.go
66 lines (59 loc) · 1.46 KB
/
author.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package model
import (
"errors"
"github.com/kdf5000/pugo/app/helper"
"gopkg.in/ini.v1"
)
type (
// Author is author item in meta file
Author struct {
Name string `toml:"name" ini:"name"`
Nick string `toml:"nick" ini:"nick"`
Email string `toml:"email" ini:"email"`
URL string `toml:"url" ini:"url"`
Avatar string `toml:"avatar" ini:"avatar"`
Bio string `toml:"bio" ini:"bio"`
Repo string `toml:"repo" ini:"repo"` // github repository
IsOwner bool // must be the first author
}
// AuthorGroup is collection of Authors
AuthorGroup []*Author
)
var (
errAuthorInvalid = errors.New("author must have name")
errAuthorGroupEmpty = errors.New("must add an author")
)
func (a *Author) normalize() error {
if a.Name == "" {
return errAuthorInvalid
}
if a.Nick == "" {
a.Nick = a.Name
}
if a.Avatar == "" && a.Email != "" {
a.Avatar = helper.Gravatar(a.Email, 0)
}
return nil
}
func (ag AuthorGroup) normalize() error {
if len(ag) == 0 {
return errAuthorGroupEmpty
}
ag[0].IsOwner = true
for _, a := range ag {
if err := a.normalize(); err != nil {
return err
}
}
return nil
}
func newAuthorFromIniSection(section *ini.Section) (*Author, error) {
a := &Author{
Name: section.Key("author").Value(),
Email: section.Key("author_email").Value(),
URL: section.Key("author_url").Value(),
Avatar: section.Key("author_avatar").Value(),
Bio: section.Key("author_bio").Value(),
}
return a, a.normalize()
}