Skip to content

Commit

Permalink
[skip ci][wip] Author handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Lhussiez committed Mar 28, 2018
1 parent 89d3b25 commit f86ce73
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 18 deletions.
22 changes: 11 additions & 11 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions assets/style.css
Expand Up @@ -4,6 +4,13 @@ html, button, input, select, textarea,
.pure-g [class *= "pure-u"] {
font-family: 'Roboto', serif;
}
.pure-g {
display: flex;
}
.avatar {
border-radius: 50%;
margin: 0px;
}
.draft {
float: right;
color: white;
Expand Down
23 changes: 21 additions & 2 deletions cmd/new.go
Expand Up @@ -47,11 +47,25 @@ var newCmd = &cobra.Command{
o := models.MetaData{
Date: time.Now().Format("2006-01-02 15:04:05"),
Tags: viper.GetStringSlice("tags"),
Author: viper.GetString("author"),
Title: viper.GetString("title"),
Description: viper.GetString("description"),
Draft: viper.GetBool("draft"),
}
// Author related stuff
a := &models.Author{
Name: viper.GetString("author.name"),
Site: viper.GetString("author.site"),
Twitter: viper.GetString("author.twitter"),
Github: viper.GetString("author.github"),
}

// If no author information has been given and the global author is
// not empty, then set that to nil, it will fallback to the default
// author
if a.IsEmpty() && !models.GetGlobalAuthor().IsEmpty() {
a = nil
}
o.Author = a
o.Slug = o.GenerateSlug()

if err = yaml.NewEncoder(fd).Encode(o); err != nil {
Expand All @@ -66,8 +80,13 @@ func init() {
newCmd.Flags().String("title", "", "title of the article")
newCmd.Flags().String("description", "", "description of the article")
newCmd.Flags().String("slug", "", "slug of the article")
newCmd.Flags().String("author", "", "author of the article")
newCmd.Flags().String("banner", "", "banner URL of the article")
newCmd.Flags().Bool("draft", false, "set the status of the article to draft")

newCmd.Flags().String("author.twitter", "", "twitter handle of the author (overrides global conf)")
newCmd.Flags().String("author.name", "", "name (or nickname) of the author (overrides global conf)")
newCmd.Flags().String("author.github", "", "github username of the author (overrides global conf)")
newCmd.Flags().String("author.site", "", "website of the author (overrides global conf)")
newCmd.Flags().String("author.avatar", "", "URL to the author's avatar (overrides global conf)")
viper.BindPFlags(newCmd.Flags())
}
7 changes: 7 additions & 0 deletions cmd/root.go
Expand Up @@ -34,8 +34,15 @@ func init() {
rootCmd.PersistentFlags().String("blog.title", "", "your blog's title")
rootCmd.PersistentFlags().String("blog.description", "", "your blog's description")
rootCmd.PersistentFlags().Bool("blog.draft", false, "show drafts")

// Blog - Author
rootCmd.PersistentFlags().String("blog.pages", "pages/", "directory in which articles are stored")
rootCmd.PersistentFlags().String("blog.code.style", "monokai", "style of the code sections")
rootCmd.PersistentFlags().String("blog.author.twitter", "", "Twitter handle of the author")
rootCmd.PersistentFlags().String("blog.author.name", "", "name (or nickname) of the author")
rootCmd.PersistentFlags().String("blog.author.github", "", "github username of the author")
rootCmd.PersistentFlags().String("blog.author.site", "", "website of the author")
rootCmd.PersistentFlags().String("blog.author.avatar", "", "URL to the author's avatar")

// Flag binding
viper.BindPFlags(rootCmd.PersistentFlags())
Expand Down
16 changes: 15 additions & 1 deletion models/metadata.go
Expand Up @@ -11,13 +11,27 @@ type MetaData struct {
Title string `yaml:"title"` // Mandatory
Description string `yaml:"description"`
Banner string `yaml:"banner"`
Author string `yaml:"author"`
Author *Author `yaml:"author,omitempty"`
Slug string `yaml:"slug"`
Tags []string `yaml:"tags"`
Date string `yaml:"date"` // Mandatory
Draft bool `yaml:"draft"`
}

// Author represents the author of a single article
type Author struct {
Name string `yaml:"name"`
Twitter string `yaml:"twitter"`
Site string `yaml:"site"`
Github string `yaml:"github"`
Avatar string `yaml:"avatar"`
}

// IsEmpty checks if all the fields of an Author are blank
func (a Author) IsEmpty() bool {
return a.Name == "" && a.Twitter == "" && a.Site == "" && a.Github == "" && a.Avatar == ""
}

// Validate validates that the metada is valid
func (m *MetaData) Validate() error {
if m.Date == "" {
Expand Down
24 changes: 22 additions & 2 deletions models/pages.go
Expand Up @@ -31,7 +31,7 @@ type Page struct {
Markdown template.HTML
Title string
Description string
Author string
Author *Author
Banner string
Date time.Time
DateFmt string
Expand All @@ -41,6 +41,22 @@ type Page struct {
Draft bool
}

var globalAuthor *Author

// GetGlobalAuthor retrieves the author configured in the configuration file
func GetGlobalAuthor() *Author {
if globalAuthor == nil {
globalAuthor = &Author{
Name: viper.GetString("blog.author.name"),
Github: viper.GetString("blog.author.github"),
Site: viper.GetString("blog.author.site"),
Twitter: viper.GetString("blog.author.twitter"),
Avatar: viper.GetString("blog.author.avatar"),
}
}
return globalAuthor
}

// NewPageFromFile parses a file, inserts it in the map and slice, and returns a *Page instance
func NewPageFromFile(fn string) (*Page, error) {
var err error
Expand Down Expand Up @@ -149,7 +165,11 @@ func (p *Page) ParseMetadata(h []byte) error {
p.DateFmt = t.Format("2006/01/02 15:04")
p.Banner = m.Banner
p.Tags = m.Tags
p.Author = m.Author
if m.Author != nil {
p.Author = m.Author
} else {
p.Author = GetGlobalAuthor()
}
p.Title = m.Title
p.Draft = m.Draft
return nil
Expand Down
10 changes: 8 additions & 2 deletions templates/post.tmpl
Expand Up @@ -18,7 +18,13 @@
<body>
<div class="pure-g">
<div class="pure-u-1 pure-u-md-1-4">
<a href="/">← Back</a>
<a href="/">← Back</a><br />
{{ if .post.Author }}
<div class="pure-g">
<div class="pure-u-1 pure-u-md-1-5"><img src="{{ .post.Author.Avatar }}" class="avatar"></div>
<div class="pure-u-1 pure-u-md-4-5">hihihi</div>
</div>
{{ end }}
</div>
<div class="pure-u-1 pure-u-md-1-2">
<div class="head">
Expand All @@ -31,7 +37,7 @@
{{ .post.Markdown }}
</div>
<div class="footerinfo">
{{ .post.DateFmt }}{{ if .post.Author }} - {{ .post.Author }}{{ end }} - <a href="/post/{{ .post.Slug }}/raw">Raw Markdown</a>
{{ .post.DateFmt }} - <a href="/post/{{ .post.Slug }}/raw">Raw Markdown</a>
</div>
{{ if .gitalk.Enabled }}
<div id="gitalk-container"></div>
Expand Down

0 comments on commit f86ce73

Please sign in to comment.