Skip to content
This repository has been archived by the owner on Mar 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request #39 from agonzalezro/bump
Browse files Browse the repository at this point in the history
Refactor + start using Glide
  • Loading branch information
agonzalezro committed May 2, 2016
2 parents 86e6036 + 666f490 commit 6ec222e
Show file tree
Hide file tree
Showing 205 changed files with 2,101 additions and 26,857 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
bin/
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ _testmain.go

.DS_Store

# getgb.io
bin/
pkg/
vendor/
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM golang:1.6.2-alpine

ENV APP $GOPATH/src/github.com/agonzalezro/polo
RUN mkdir -p $APP
WORKDIR $APP

ADD glide.yaml $APP/glide.yaml
ADD glide.lock $APP/glide.lock
RUN apk add --no-cache git \
&& go get -u github.com/Masterminds/glide/... \
&& go get -u github.com/jteeuwen/go-bindata/... \
&& glide install \
&& apk del git

ADD . $APP
RUN apk add --no-cache make \
&& make \
&& apk del make

ENTRYPOINT ["bin/polo"]
CMD ["--help"]
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all: polo

deps:
mkdir -p bin

polo: deps
cd cmd/polo&&go generate&&go build&&mv polo ../../bin
75 changes: 43 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ polo

[![circleci](https://circleci.com/gh/agonzalezro/polo.svg?style=shield)](https://circleci.com/gh/agonzalezro/polo)

polo is a static blog generator created with [Go](https://golang.org/). It's
polo is a static site generator created with [Go](https://golang.org/). It's
compatible with your current Jekyll and Pelican markdowns, so your migration
should be straightforward.

I'm happily using it on my blog: http://agonzalezro.github.io and you can use
it in yours!
it in yours! There are other places with completely different templates using
it as well: http://k8s.uk/

Yes, I know that there a lot of them out there but I just want mine to learn a
little bit of Go coding.
Expand All @@ -18,7 +19,7 @@ Here are some features:
- Jekyll and Pelican compatible.
- Can watch files for changes and in that case regenerate the site.
- Pretty quick! But new versions will be faster.
- Deploy it to `gh-pages` or create your own blog on github.
- Deploy it to `gh-pages` or create your own site on github.
- You can easily auto deploy it: [example for
CircleCI](http://agonzalezro.github.io/how-to-automagically-generate-your-polo-blo g-with-circleci.html).
- It supports templating, check [my personal blog
Expand All @@ -28,43 +29,59 @@ Here are some features:
Install
-------

### From binary

Find your version here: https://github.com/agonzalezro/polo/releases

If you want to build it yourself, I am using [gb](http://getgb.io):
### Docker

The latest master is always available as a Docker image:

docker run agonzalezro/polo

Remember that you will need to mount volumes and so on.

### DIY

$ gb build
If you want to build it yourself, I am using [glide](https://github.com/Masterminds/glide) for the dependencies. This means that you will need to use Go 1.5 at least:

$ glide install

How to use it?
--------------

If you call the binary without any argument you will get the help:

$ polo
Usage:
polo [OPTIONS] sourcedir outputdir
$ polo -h
usage: polo [<flags>] <source> <output>

Static site generator "compatible" with Jekyll & Pelican content.

Application Options:
-d, --daemon start a simple HTTP server watching for markdown changes.
-c, --config= the settings file. (config.json)
-p, --port= port where to run the server. (8080)
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
-d, --start-daemon Start a simple HTTP server watching for markdown changes.
-p, --port=8080 Port where to run the server.
-c, --config="config.json" The settings file.
--templates-base-path=. Where the 'templates/' folder resides (in case it exists).
-v, --verbose Verbose logging.

Help Options:
-h, --help Show this help message
Args:
<source> Folder where the content resides.
<output> Where to store the published files.

The basic usage mode is:

$ polo sourcedir outputdir
$ polo <source> <output>

If you want a server that watches for you changes, meaning that if you change
something in `sourcedir` the site will be regenerated:

$ polo -d sourcedir outputdir
...
2015/06/23 23:36:57 Static server running on http://localhost:8080
$ polo -d <source> <output>
INFO[0000] Static server running on :8080

There is an [example project
here](https://github.com/agonzalezro/polo/tree/master/example), you can use it
as `sourcedir`.
as `<source>`.

Configuration file
------------------
Expand All @@ -78,7 +95,7 @@ This is what you can configure:

- **author**: if it's not override with the Metadata it's the name that is
going to be shown on the articles.
- **title**: title of the blog, for the `<title>` element and the header.
- **title**: title of the site, for the `<title>` element and the header.
- **url**: sometimes the full url is needed.
- **show{Archive,Categories,Tags}**: if it's true the pages are going to be
created and the links are going to be added.
Expand All @@ -87,7 +104,7 @@ This is what you can configure:

### 3rd party

- **disqusSitename**: if you want comments on your blog.
- **disqusSitename**: if you want comments on your site.
- **googleAnalyticsId**: the Google Analytics ID.
- **shareThisPublisher**: the ShareThis publisher ID. If provided, there will
be some social buttons on the article view.
Expand Down Expand Up @@ -164,7 +181,7 @@ bootstrap theme:

1. Wherever you want (but it needs to be the same place where you run polo
from) you create the folder `templates/base`.
2. Then you edit `templates/base/header.html`, adding the following content:
2. Then you edit `templates/head/header.html`, adding the following content:

````html
{{define "header"}}
Expand All @@ -181,17 +198,11 @@ bootstrap theme:
### Modifying the one that is going to be included on the binary

If you want to do changes on the default theme, you need to remember that you
MUST recreate the binary data, you should do it this way:

$ cd src
$ go-bindata -o src/templates/bindata.go -pkg=templates -ignore=bindata.go src/templates/...
$ cd -

It's quite important that you `cd` to `src` before doing it, if not the paths
will not match.
MUST recreate the binary data. Use the `go:generate` provided on `cmd/polo` for
that purpose:

There is a [issue open](https://github.com/agonzalezro/polo/issues/35) to
automate this.
$ cd cmd/polo
$ go generate

Auto deploy
-----------
Expand Down
10 changes: 7 additions & 3 deletions build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
OSS=(darwin freebsd linux)
ARCHS=(386 amd64)

rm bin/polo*
mkdir -p bin
rm -f bin/polo*

for os in "${OSS[@]}"; do
for arch in "${ARCHS[@]}"; do
GOOS=$os GOARCH=$arch gb build
echo "Building for $os($arch)"
GOOS=$os GOARCH=$arch make
mv bin/polo bin/polo-$os-$arch
done
done

mv bin/polo bin/polo-darwin-amd64
# Link darwin amd64 to bin/polo
ln -s bin/polo-darwin-amd64 bin/polo
9 changes: 7 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
machine:
services:
- docker

dependencies:
override:
- go get github.com/constabulary/gb/...
- docker info
- docker build -t agonzalezro/polo .

test:
override:
- gb test
- docker run --entrypoint go agonzalezro/polo test $(docker run --entrypoint glide agonzalezro/polo novendor)
91 changes: 91 additions & 0 deletions cmd/polo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

//go:generate go-bindata -o ../../templates/assets.go -pkg=assets -ignore=.DS_Store -ignore=assets.go -prefix=../.. ../../templates/...

import (
"fmt"
"net/http"
"os"

log "github.com/Sirupsen/logrus"
config "github.com/agonzalezro/polo/config"
"github.com/agonzalezro/polo/site"
kingpin "gopkg.in/alecthomas/kingpin.v2"
fsnotify "gopkg.in/fsnotify.v1"
)

var (
app = kingpin.New("polo", `Static site generator "compatible" with Jekyll & Pelican content.`)

startDaemon = app.Flag("start-daemon", "Start a simple HTTP server watching for markdown changes.").Short('d').Bool()
port = app.Flag("port", "Port where to run the server.").Default("8080").Short('p').Int()
configPath = app.Flag("config", "The settings file.").Short('c').Default("config.json").String()

templatesBasePath = app.Flag("templates-base-path", fmt.Sprintf("Where the '%s/' folder resides (in case it exists).", site.TemplatesRelativePath)).Default(".").ExistingDir()

verbose = app.Flag("verbose", "Verbose logging.").Short('v').Bool()

source = app.Arg("source", "Folder where the content resides.").Required().ExistingDir()
output = app.Arg("output", "Where to store the published files.").Required().String()
)

func main() {
app.HelpFlag.Short('h')
kingpin.MustParse(app.Parse(os.Args[1:]))

if *verbose {
log.SetLevel(log.DebugLevel)
}

if !dirExists(*output) {
if err := os.Mkdir(*output, os.ModePerm); err != nil {
app.FatalUsage("The output folder couldn't be created!")
}
}

s, err := site.New(*source, *output, *configPath, *templatesBasePath)
if err != nil {
switch err.(type) {
case config.ErrorParsingConfigFile:
app.FatalUsage("Malformed JSON config file: ", err)
default:
log.Fatal(err)
}
}

if err := s.Write(); err != nil {
log.Fatal(err)
}

if *startDaemon {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()

go func() {
for {
select {
case event := <-watcher.Events:
if event.Op != fsnotify.Chmod {
log.Info("Rewriting the site")
if err := s.Write(); err != nil {
log.Fatal(err)
}
}
case err := <-watcher.Errors:
log.Fatal(err) // TODO: perhaps return err
}
}
}()

for path := range subdirectories(*source) {
watcher.Add(path)
}

addr := fmt.Sprintf(":%d", *port)
log.Info("Static server running on ", addr)
log.Fatal(http.ListenAndServe(addr, http.FileServer(http.Dir(*output))))
}
}
36 changes: 36 additions & 0 deletions cmd/polo/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"os"
"path/filepath"

log "github.com/Sirupsen/logrus"
)

func subdirectories(parentPath string) chan string {
paths := make(chan string, 1)

walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Warning(err)
}
if info.IsDir() {
paths <- path
}
return nil
}

go func() {
filepath.Walk(parentPath, walkFn)
close(paths)
}()

return paths
}

func dirExists(p string) bool {
if fi, err := os.Stat(p); err == nil {
return fi.IsDir()
}
return false
}
File renamed without changes.
Loading

0 comments on commit 6ec222e

Please sign in to comment.