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

Progress on #14 #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ install: all

build-docker:
docker build -t freetonik/underblog .

fmt:
go fmt ./app/...

lint:
golint ./app/... && go vet ./app/...
15 changes: 8 additions & 7 deletions app/cmd/blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"errors"
"fmt"
"html/template"
"io"
Expand All @@ -17,9 +16,10 @@ import (
"github.com/freetonik/underblog/app/internal"
)

// DefaultMarkdownPath The folder where the markdown posts are stored
const DefaultMarkdownPath = "./markdown/"

// Create and initialize Blog
// NewBlog Create and initialize Blog
func NewBlog(opts internal.Opts) *Blog {
b := new(Blog)

Expand Down Expand Up @@ -49,13 +49,13 @@ type Blog struct {
// Render md-files->HTML, generate root index.html
func (b *Blog) Render() error {
if err := b.verifyMarkdownPresent(); err != nil {
log.Fatal(errors.New(fmt.Sprintf("Markdown directory is not found: %v", err)))
log.Fatal(fmt.Errorf("Markdown directory is not found: %v", err))
}

b.indexPage = b.getIndexPage(b.opts.Path)
b.createPosts()
err := b.renderMd()
b.copyCssToPublicDir()
b.copyCSSToPublicDir()

return err
}
Expand All @@ -75,13 +75,13 @@ func (b *Blog) getIndexPage(currentPath string) io.Writer {
p := filepath.Join(rootPath, "public")
err := os.MkdirAll(p, os.ModePerm)
if err != nil {
log.Fatal(errors.New(fmt.Sprintf("Can't create public dir: %v", err)))
log.Fatal(fmt.Errorf("Can't create public dir: %v", err))
}

f, err := os.Create("public/index.html")

if err != nil {
log.Fatal(errors.New(fmt.Sprintf("Can't create public/index.html: %v", err)))
log.Fatal(fmt.Errorf("Can't create public/index.html: %v", err))
}

return f
Expand Down Expand Up @@ -134,7 +134,7 @@ func (b *Blog) createPosts() {
close(filesChan)
}

func (b *Blog) copyCssToPublicDir() {
func (b *Blog) copyCSSToPublicDir() {
from, err := os.Open("./css/styles.css")
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -184,6 +184,7 @@ func (b *Blog) getTemplateFuncs() template.FuncMap {
}
}

// SortPosts Sort posts
func (b *Blog) SortPosts() {
sort.Slice(b.Posts, func(i, j int) bool {
return b.Posts[i].Date.Unix() > b.Posts[j].Date.Unix()
Expand Down
3 changes: 3 additions & 0 deletions app/cmd/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"gopkg.in/russross/blackfriday.v2"
)

// NewPost Converts a Markdown post to HTML
func NewPost(filename string) Post {
post, err := ExtractMetaFromFilename(filename)
if err != nil {
Expand Down Expand Up @@ -43,6 +44,7 @@ func NewPost(filename string) Post {
return post
}

// Post Represents a post
type Post struct {
Title string
Body template.HTML
Expand Down Expand Up @@ -78,6 +80,7 @@ func fNameWithoutExtension(fn string) string {
return strings.TrimSuffix(fn, path.Ext(fn))
}

// ExtractMetaFromFilename Extracts slug and date from filename
func ExtractMetaFromFilename(filename string) (Post, error) {
errorMessage := fmt.Sprintf("can't parse filename '%s', it should be in format 'YYYY-MM-DD-slug.md'", filename)
dateFormat := "2006-01-02"
Expand Down
1 change: 1 addition & 0 deletions app/internal/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
)

// Opts options of the command line
type Opts struct {
Version bool
WatchMode bool
Expand Down
2 changes: 2 additions & 0 deletions app/internal/watch_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"
)

// WatchForChangedFiles Rebuild the blog when files are changed
func WatchForChangedFiles(rebuildBlog func()) {
w := watcher.New()

Expand Down Expand Up @@ -44,6 +45,7 @@ func WatchForChangedFiles(rebuildBlog func()) {
}
}

// RunDevelopmentWebserver Run the development server
func RunDevelopmentWebserver() {
// todo: extract ./public to constant
http.Handle("/", http.FileServer(http.Dir("./public")))
Expand Down