Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
124 lines (108 sloc) 3.62 KB
package main
import (
"flag"
"log"
"path/filepath"
"github.com/FooSoft/goldsmith"
"github.com/FooSoft/goldsmith-components/devserver"
"github.com/FooSoft/goldsmith-components/filters/condition"
"github.com/FooSoft/goldsmith-components/filters/operator"
"github.com/FooSoft/goldsmith-components/filters/wildcard"
"github.com/FooSoft/goldsmith-components/plugins/absolute"
"github.com/FooSoft/goldsmith-components/plugins/breadcrumbs"
"github.com/FooSoft/goldsmith-components/plugins/collection"
"github.com/FooSoft/goldsmith-components/plugins/document"
"github.com/FooSoft/goldsmith-components/plugins/frontmatter"
"github.com/FooSoft/goldsmith-components/plugins/index"
"github.com/FooSoft/goldsmith-components/plugins/layout"
"github.com/FooSoft/goldsmith-components/plugins/livejs"
"github.com/FooSoft/goldsmith-components/plugins/markdown"
"github.com/FooSoft/goldsmith-components/plugins/minify"
"github.com/FooSoft/goldsmith-components/plugins/summary"
"github.com/FooSoft/goldsmith-components/plugins/syntax"
"github.com/FooSoft/goldsmith-components/plugins/tags"
"github.com/FooSoft/goldsmith-components/plugins/thumbnail"
"github.com/PuerkitoBio/goquery"
"github.com/russross/blackfriday"
)
func fixup(doc *goquery.Document) error {
doc.Find("table").AddClass("table").Find("thead").AddClass("thead-light")
doc.Find("img[src*='thumb']").Each(func(i int, s *goquery.Selection) {
thumbLink := s.ParentFiltered("a")
thumbLink.AddClass("img-thumbnail", "img-thumbnail-inline")
thumbLink.SetAttr("data-title", s.AttrOr("alt", ""))
thumbLink.SetAttr("data-toggle", "lightbox")
thumbLink.SetAttr("data-gallery", "gallery")
})
return nil
}
type builder struct {
dev bool
}
func (b *builder) Build(contentDir, buildDir, cacheDir string) {
log.Print("building...")
tagMeta := map[string]interface{}{
"Area": "tags",
"CrumbParent": "tags",
"Layout": "tag",
}
indexMeta := map[string]interface{}{
"Layout": "index",
}
markdownFlags := blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_AUTOLINK |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS |
blackfriday.EXTENSION_HEADER_IDS |
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
blackfriday.EXTENSION_DEFINITION_LISTS |
blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK |
blackfriday.EXTENSION_AUTO_HEADER_IDS
errs := goldsmith.Begin(contentDir).
Cache(cacheDir).
FilterPush(operator.Not(wildcard.New("**/samples/**"))).
Chain(frontmatter.New()).
Chain(markdown.New().MarkdownFlags(markdownFlags)).
Chain(summary.New()).
Chain(collection.New()).
Chain(index.New(indexMeta)).
Chain(tags.New().IndexMeta(tagMeta)).
Chain(breadcrumbs.New()).
Chain(layout.New()).
Chain(absolute.New()).
Chain(syntax.New().Placement(syntax.PlaceInline)).
Chain(document.New(fixup)).
Chain(thumbnail.New()).
FilterPush(condition.New(b.dev)).
Chain(livejs.New()).
FilterPop().
FilterPush(condition.New(!b.dev)).
Chain(minify.New()).
FilterPop().
FilterPop().
End(buildDir)
for _, err := range errs {
log.Print(err)
}
}
func main() {
update := flag.Bool("watch", false, "watch for file changes and update")
port := flag.Int("port", 8080, "server port")
flag.Parse()
if flag.NArg() != 1 {
log.Fatal("unexpected number of arguments")
}
var (
baseDir = flag.Arg(0)
contentDir = filepath.Join(baseDir, "content")
buildDir = filepath.Join(baseDir, "build")
cacheDir = filepath.Join(baseDir, "cache")
)
b := &builder{*update}
if *update {
devserver.DevServe(b, *port, contentDir, buildDir, cacheDir)
} else {
b.Build(contentDir, buildDir, cacheDir)
}
}