Skip to content

Commit

Permalink
Adds ink new command (Resolves #67)
Browse files Browse the repository at this point in the history
  • Loading branch information
dignissimus committed Jul 27, 2018
1 parent 9d8f31e commit cc47f0e
Showing 1 changed file with 206 additions and 2 deletions.
208 changes: 206 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,33 @@ import (
"github.com/codegangsta/cli"
"github.com/facebookgo/symwalk"
"gopkg.in/yaml.v2"
"html/template"
"time"
)

const (
VERSION = "RELEASE 2017-09-24"
DEFAULT_ROOT = "blog"
VERSION = "RELEASE 2017-09-24"
DEFAULT_ROOT = "blog"
DATE_FORMAT_STRING = "2006-01-02 15:04:05"
INDENT = " " // 2 spaces
POST_TEMPLATE = `title: {{.Title}}
date: {{.DateString}}
author: {{.Author}}
{{- if .Cover}}
cover: {{.Cover}}
{{- end}}
draft: {{.Draft}}
top: {{.Top}}
{{- if .Preview}}
preview: {{.Preview}}
{{- end}}
{{- if .Tags}}
{{.Tags}}
{{- end}}
type: {{.Type}}
hide: {{.Hide}}
---
`
)

var globalConfig *GlobalConfig
Expand Down Expand Up @@ -79,6 +101,62 @@ func main() {
return nil
},
},
{
Name: "new",
Usage: "Creates a new article",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "hide",
Usage: "Hides the article",
},
cli.BoolFlag{
Name: "top",
Usage: "Places the article at the top",
},
cli.BoolFlag{
Name: "post",
Usage: "The article is a post",
},
cli.BoolFlag{
Name: "page",
Usage: "The article is a page",
},
cli.BoolFlag{
Name: "draft",
Usage: "The article is a draft",
},

cli.StringFlag{
Name: "title",
Usage: "Article title",
},
cli.StringFlag{
Name: "author",
Usage: "Article author",
},
cli.StringFlag{
Name: "cover",
Usage: "Article cover path",
},
cli.StringFlag{
Name: "date",
Usage: "The date and time on which the article was created (2006-01-02 15:04:05)",
},
cli.StringFlag{
Name: "file",
Usage: "The path of where the article will be stored",
},

cli.StringSliceFlag{
Name: "tag",
Usage: "Adds a tag to the article",
},
},
Action: func(c *cli.Context) error {
New(c)
return nil
},
},
}
app.Run(os.Args)
os.Exit(exitCode)
Expand Down Expand Up @@ -107,6 +185,132 @@ func ParseGlobalConfigWrap(root string, develop bool) {
}
}

func New(c *cli.Context) {
// If source folder does not exist, create
if _, err := os.Stat("source/"); os.IsNotExist(err) {
os.Mkdir("source", os.ModePerm)
}

var author, blogTitle, fileName string
var tags []string

// Default values
draft := "false"
top := "false"
postType := "post"
hide := "false"
date := time.Now()

// Empty string values
preview := ""
cover := ""

// Parse args
args := c.Args()
if len(args) > 0 {
blogTitle = args[0]
}
if blogTitle == "" {
if c.String("title") != "" {
blogTitle = c.String("title")
} else {
Fatal("Please specify the name of the blog post")
}
}

fileName = blogTitle + ".md"
if c.String("file") != "" {
fileName = c.String("file")
}

if len(args) > 1 {
author = args[1]
}
if author == "" {
author = c.String("author")
}

if c.Bool("post") && c.Bool("page") {
Fatal("The post and page arguments are mutually exclusive and cannot appear together")
}
if c.Bool("post") {
postType = "post"
}
if c.Bool("page") {
postType = "page"
}
if c.Bool("hide") {
hide = "true"
}
if c.Bool("draft") {
draft = "true"
}
if c.Bool("top") {
top = "true"
}

if c.String("preview") != "" {
preview = c.String("preview")
}
if c.String("cover") != "" {
cover = c.String("cover")
}

var filePath = "source/" + fileName
file, err := os.Create(filePath)
if err != nil {
Fatal(err)
}
postTemplate, err := template.New("post").Parse(POST_TEMPLATE)
if err != nil {
Fatal(err)
}

if c.StringSlice("tag") != nil {
tags = c.StringSlice("tag")
}

var tagString string
if len(tags) > 0 {
tagString = "tags:"
for _, tag := range tags {
tagString += "\n" + INDENT + "- " + tag
}
}

var dateString string
if c.String("date") != "" {
dateString = c.String("date")
_, err = time.Parse(DATE_FORMAT_STRING, dateString)
if err != nil {
Fatal("Illegal date string")
}
} else {
dateString = date.Format(DATE_FORMAT_STRING)
}
data := map[string]string{
"Title": blogTitle,
"DateString": dateString,
"Author": author,
"Draft": draft,
"Top": top,
"Type": postType,
"Hide": hide,
"Preview": preview,
"Cover": cover,
"Tags": tagString,
}
fileWriter := bufio.NewWriter(file)
err = postTemplate.Execute(fileWriter, data)
if err != nil {
Fatal(err)
}
err = fileWriter.Flush()
if err != nil {
Fatal(err)
}
}

func Publish() {
command := globalConfig.Build.Publish
// Prepare exec command
Expand Down

0 comments on commit cc47f0e

Please sign in to comment.