Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 31, 2017
0 parents commit 60ec92b
Show file tree
Hide file tree
Showing 16 changed files with 1,424 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include github.com/tj/make/golang

# Generate themes.
generate:
@go generate ./...
.PHONY: generate
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<img src="http://tjholowaychuk.com:6000/svg/title/STATIC/ANTI FRAMEWORK">

The goal of this project is to build a common toolkit for building static sites for a variety of domains. Each program is tailored specifically to one domain, such as a blog, documentation site, or others, the programs are named to reflect this, for example `static-docs` or `static-blog`. Focusing the UX on each domain as necessary, making for a smoother experience, but still sharing a common library, making it easy to create your own.

I don't have much time for this project right now, so it only has what I need, but hopefully long-term it'll turn into something real :D.

---

[![GoDoc](https://godoc.org/github.com/apex/static?status.svg)](https://godoc.org/github.com/apex/static)
![](https://img.shields.io/badge/license-MIT-blue.svg)
![](https://img.shields.io/badge/status-stable-green.svg)

<a href="https://apex.sh"><img src="http://tjholowaychuk.com:6000/svg/sponsor"></a>
57 changes: 57 additions & 0 deletions cmd/static-docs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"flag"
"time"

"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/tj/go/flag/usage"

"github.com/apex/static/docs"
)

func init() {
log.SetHandler(cli.Default)
}

func main() {
flag.Usage = usage.Output(&usage.Config{
Examples: []usage.Example{
{
Help: "Generate site in ./build from ./docs/*.md.",
Command: "static-docs -title Up -in ./docs",
},
{
Help: "Generate a site in ./ from ../up/docs/*.md",
Command: "static-docs -title Up -in ../up/docs -out .",
},
},
})

title := flag.String("title", "", "Site title.")
subtitle := flag.String("subtitle", "", "Site subtitle or slogan.")
theme := flag.String("theme", "apex", "Theme name.")
src := flag.String("in", ".", "Source directory for markdown files.")
dst := flag.String("out", "build", "Output directory for the static site.")
flag.Parse()

println()
defer println()

start := time.Now()

c := &docs.Config{
Src: *src,
Dst: *dst,
Title: *title,
Subtitle: *subtitle,
Theme: *theme,
}

if err := docs.Compile(c); err != nil {
log.Fatalf("error: %s", err)
}

log.Infof("compiled in %s\n", time.Since(start).Round(time.Millisecond))
}
182 changes: 182 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package docs

import (
"html/template"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/apex/log"
"github.com/pkg/errors"
"github.com/segmentio/go-snakecase"

"github.com/apex/static"
"github.com/apex/static/docs/themes/apex"
)

// Config options.
type Config struct {
// Src dir of markdown documentation files.
Src string

// Dst dir of the static site.
Dst string

// Title of the site.
Title string

// Subtitle of the site.
Subtitle string

// Theme name.
Theme string
}

// Page model.
type Page struct {
// Title of the page.
Title string `yml:"title"`

// Slug of the page.
Slug string `yml:"slug"`

// Skip the page.
Skip bool `yml:"skip"`

// Content of the page.
Content template.HTML
}

// Compile docs site.
func Compile(c *Config) error {
log.Infof("compiling %s to %s", c.Src, c.Dst)

if err := os.MkdirAll(c.Dst, 0755); err != nil {
return errors.Wrap(err, "mkdir")
}

if err := initTheme(c); err != nil {
return errors.Wrap(err, "initializing theme")
}

files, err := ioutil.ReadDir(c.Src)
if err != nil {
return errors.Wrap(err, "reading dir")
}

var pages []*Page

for _, f := range files {
path := filepath.Join(c.Src, f.Name())

log.Infof("compiling %q", path)
p, err := compile(c, path)
if err != nil {
return errors.Wrapf(err, "compiling %q", path)
}

if p == nil {
log.Infof("skipping %q", path)
continue
}

pages = append(pages, p)
}

out := filepath.Join(c.Dst, "index.html")
f, err := os.Create(out)
if err != nil {
return errors.Wrap(err, "opening")
}

if err := render(f, c, pages); err != nil {
return errors.Wrap(err, "rendering")
}

if err := f.Close(); err != nil {
return errors.Wrap(err, "closing")
}

return nil
}

// render to writer w.
func render(w io.Writer, c *Config, pages []*Page) error {
path := filepath.Join(c.Dst, "theme", c.Theme, "views", "*.html")

views, err := template.ParseGlob(path)
if err != nil {
return errors.Wrap(err, "parsing templates")
}

err = views.ExecuteTemplate(w, "index.html", struct {
*Config
Pages []*Page
}{
Config: c,
Pages: pages,
})

if err != nil {
return errors.Wrap(err, "rendering")
}

return nil
}

// compile file.
func compile(c *Config, path string) (*Page, error) {
// open
f, err := os.Open(path)
if err != nil {
return nil, errors.Wrap(err, "open")
}
defer f.Close()

// meta-data
var page Page
rc := static.Markdown(static.Frontmatter(f, &page))

// contents
b, err := ioutil.ReadAll(rc)
if err != nil {
rc.Close()
return nil, errors.Wrap(err, "reading")
}

if err := rc.Close(); err != nil {
return nil, errors.Wrap(err, "closing")
}

// populate
if page.Slug == "" {
page.Slug = snakecase.Snakecase(page.Title)
}
page.Content = template.HTML(b)

// skip
if page.Skip {
return nil, nil
}

return &page, nil
}

// initTheme populates the theme directory unless present.
func initTheme(c *Config) error {
dir := filepath.Join(c.Dst, "theme", c.Theme)
_, err := os.Stat(dir)

if os.IsNotExist(err) {
return apex.RestoreAssets(dir, "")
}

return nil
}

// stripExt returns the path sans-extname.
func stripExt(s string) string {
return strings.Replace(s, filepath.Ext(s), "", 1)
}
3 changes: 3 additions & 0 deletions docs/themes/apex/apex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//go:generate go-bindata -modtime 0 -pkg apex -prefix files files/...

package apex
310 changes: 310 additions & 0 deletions docs/themes/apex/bindata.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/themes/apex/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//go:generate go-bindata -modtime 0 -pkg apex -prefix files files/...

package apex
Loading

0 comments on commit 60ec92b

Please sign in to comment.