Go to file
2024-04-06 22:30:25 -07:00
devserver Cleanup 2024-03-03 21:39:08 -08:00
filters Revert "Change File to interface" 2024-03-03 18:09:21 -08:00
harness Move configuration to Goldsmith structure 2024-04-06 22:30:25 -07:00
plugins Revert "Change File to interface" 2024-03-03 18:09:21 -08:00
.gitignore Add components 2024-02-16 22:35:49 -08:00
cache.go Cleanup 2024-03-03 21:30:32 -08:00
chain_state.go Move configuration to Goldsmith structure 2024-04-06 22:30:25 -07:00
context.go Cleanup 2024-03-03 21:30:32 -08:00
extension.go Cleanup 2024-03-03 21:39:08 -08:00
file_exporter.go Revert "Change File to interface" 2024-03-03 18:09:21 -08:00
file_importer.go Cleanup 2024-03-03 21:30:32 -08:00
file.go Cleanup 2024-03-03 21:39:08 -08:00
filter_stack.go Cleanup 2024-03-03 21:39:08 -08:00
go.mod udpate plugins 2024-02-19 11:27:23 -08:00
go.sum udpate plugins 2024-02-19 11:27:23 -08:00
goldsmith.go Move configuration to Goldsmith structure 2024-04-06 22:30:25 -07:00
LICENSE Update copyright year 2023-10-02 19:42:23 -07:00
README.md Update README 2024-03-09 21:51:45 -05:00
test.sh Add test script 2024-02-19 10:06:00 -08:00

Goldsmith

Goldsmith is a fast and easily extensible static website generator written in Go. In contrast to many other generators, Goldsmith does not force any design paradigms or file organization rules on the user, making it possible to generate anything from blogs to image galleries using the same tool.

Tutorial

Goldsmith does not use any configuration files, and all behavior customization happens in code. Goldsmith uses the builder pattern to establish a chain, which modifies files as they pass through it. Although the Goldsmith API is short and (hopefully) easy to understand, it is often best to learn by example:

  1. Start by copying files from a source directory to a destination directory (the simplest possible use case):

    var gs goldsmith.Goldsmith
    gs.Begin(srcDir). // read files from srcDir
        End(dstDir)   // write files to dstDir
    
  2. Now let's convert any Markdown files to HTML fragments (while still copying the rest), using the Markdown plugin:

    var gs goldsmith.Goldsmith
    gs.Begin(srcDir).          // read files from srcDir
        Chain(markdown.New()). // convert *.md files to *.html files
        End(dstDir)            // write files to dstDir
    
  3. If we have any front matter in our Markdown files, we need to extract it using the, FrontMatter plugin:

    var gs goldsmith.Goldsmith
    gs.Begin(srcDir).             // read files from srcDir
        Chain(frontmatter.New()). // extract frontmatter and store it as metadata
        Chain(markdown.New()).    // convert *.md files to *.html files
        End(dstDir)               // write files to dstDir
    
  4. Next, we should run our barebones HTML through a template to add elements like a header, footer, or a menu; for this we can use the Layout plugin:

    var gs goldsmith.Goldsmith
    gs.Begin(srcDir).             // read files from srcDir
        Chain(frontmatter.New()). // extract frontmatter and store it as metadata
        Chain(markdown.New()).    // convert *.md files to *.html files
        Chain(layout.New()).      // apply *.gohtml templates to *.html files
        End(dstDir)               // write files to dstDir
    
  5. Now, let's minify our files to reduce data transfer and load times for our site's visitors using the Minify plugin:

    var gs goldsmith.Goldsmith
    gs.Begin(srcDir).             // read files from srcDir
        Chain(frontmatter.New()). // extract frontmatter and store it as metadata
        Chain(markdown.New()).    // convert *.md files to *.html files
        Chain(layout.New()).      // apply *.gohtml templates to *.html files
        Chain(minify.New()).      // minify *.html, *.css, *.js, etc. files
        End(dstDir)               // write files to dstDir
    
  6. Debugging problems in minified code can be tricky, so let's use the Condition filter to make minification occur only when we are ready for distribution.

    var gs goldsmith.Goldsmith
    gs.Begin(srcDir).                    // read files from srcDir
        Chain(frontmatter.New()).        // extract frontmatter and store it as metadata
        Chain(markdown.New()).           // convert *.md files to *.html files
        Chain(layout.New()).             // apply *.gohtml templates to *.html files
        FilterPush(condition.New(dist)). // push a dist-only conditional filter onto the stack
        Chain(minify.New()).             // minify *.html, *.css, *.js, etc. files
        FilterPop().                     // pop off the last filter pushed onto the stack
        End(dstDir)                      // write files to dstDir
    
  7. Now that we have all of our plugins chained up, let's look at a complete example which uses DevServer to bootstrap a complete development sever which automatically rebuilds the site whenever source files are updated.

    package main
    
    import (
        "flag"
        "log"
    
        "git.foosoft.net/alex/goldsmith"
        "git.foosoft.net/alex/goldsmith/devserver"
        "git.foosoft.net/alex/goldsmith/filters/condition"
        "git.foosoft.net/alex/goldsmith/plugins/frontmatter"
        "git.foosoft.net/alex/goldsmith/plugins/layout"
        "git.foosoft.net/alex/goldsmith/plugins/markdown"
        "git.foosoft.net/alex/goldsmith/plugins/minify"
    )
    
    type builder struct {
        dist bool
    }
    
    func (b *builder) Build(srcDir, dstDir, cacheDir string) {
        var gs goldsmith.Goldsmith
        errs := gs.Begin(srcDir).              // read files from srcDir
            Chain(frontmatter.New()).          // extract frontmatter and store it as metadata
            Chain(markdown.New()).             // convert *.md files to *.html files
            Chain(layout.New()).               // apply *.gohtml templates to *.html files
            FilterPush(condition.New(b.dist)). // push a dist-only conditional filter onto the stack
            Chain(minify.New()).               // minify *.html, *.css, *.js, etc. files
            FilterPop().                       // pop off the last filter pushed onto the stack
            End(dstDir)                        // write files to dstDir
    
        for _, err := range errs {
            log.Print(err)
        }
    }
    
    func main() {
        port := flag.Int("port", 8080, "server port")
        dist := flag.Bool("dist", false, "final dist mode")
        flag.Parse()
    
        devserver.DevServe(&builder{*dist}, *port, "content", "build", "cache")
    }
    

Samples

Below are some examples of Goldsmith usage which can used to base your site on:

Components

A growing set of plugins, filters, and other tools are provided to make it easier to get started with Goldsmith.

Plugins

  • Absolute: Convert relative HTML file references to absolute paths.
  • Breadcrumbs: Generate metadata required to build breadcrumb navigation.
  • Collection: Group related pages into named collections.
  • Document: Enable simple DOM modification via an API similar to jQuery.
  • Forward: Create simple redirections for pages that have moved to a new URL.
  • FrontMatter: Extract the JSON, YAML, or TOML metadata stored in your files.
  • Index: Create metadata for directory file listings and generate directory index pages.
  • Layout: Transform your HTML files with Go templates.
  • LiveJs: Inject JavaScript code to automatically reload pages when modified.
  • Markdown: Render Markdown documents as HTML fragments.
  • Minify: Remove superfluous data from a variety of web formats.
  • Pager: Split arrays of metadata into standalone pages.
  • Rule: Update metadata and filter files based on paths.
  • Summary: Extract summary and title metadata from HTML files.
  • Syndicate: Generate RSS, Atom, and JSON feeds from existing metadata.
  • Syntax: Enable syntax highlighting for pre-formatted code blocks.
  • Tags: Generate tag clouds and indices from file metadata.
  • Thumbnail: Build thumbnails for a variety of common image formats.

Filters

  • Condition: Filter files based on a single condition.
  • Operator: Join filters using logical AND, OR, and NOT operators.
  • Wildcard: Filter files using path wildcards (*, ?, etc.)

Other

  • DevServer: Simple framework for building, updating, and viewing your site.
  • Harness: Unit test harness for verifying Goldsmith plugins and filters.