Skip to content

gohugoio/gotmplfmt

 
 

Repository files navigation

Note

Claude Code helped us get this code over the finish line, but all the tests are hand written (expcept for the fuzz tests, which are generated by Go's fuzzer), and all code is human inspected and understood.

This is a Go HTML template formatter1. It is based one the text/template/parse package in Go 1.20.4 (see license note below). The formatting is greatly inspired by prettier-plugin-go-template, but it will not be the same.

  • We focus on getting the overall structure right, and not on formatting details (which is often highly subjective).
  • We use tabs and not spaces for indentation.
  • We have no options.
  • We don't read .prettierignore.
  • But we do support {{/* gotmplfmt-ignore-all */}}, {{/* gotmplfmt-ignore-start */}} and {{/* gotmplfmt-ignore-end */}}.
  • define blocks don't trigger indentation. See this issue for some discussion.
  • We don't try to format script and style blocks.
  • <script> , <style> , </script> , </style> , {{/* gotmplfmt-ignore-start */}} , or {{/* gotmplfmt-ignore-end */}} have their leading whitespace preserved as written.
  • We don't auto-add trailing newlines to the document; see this issue for some context.
  • And we also care about idempotency, so if you run into a example where we format differently on subsequent runs, please report it as a bug.

Install

If you have Go installed, you can install from source:

go install github.com/gohugoio/gotmplfmt@latest

For installers, see releases.

On MacOs you can also install via Brew:

brew install --cask gohugoio/tap/gotmplfmt

Usage

To use this as a CLI tool, you can run:

The VSCode extension can be installed from marketplace.visualstudio.com.

usage: gotmplfmt [flags] [path ...]

  -d	display diffs instead of rewriting files
  -l	list files whose formatting differs from gotmplfmt's
  -w	write result to (source) file instead of stdout
  -version print version information and exit

Without flags, gotmplfmt prints the formatted output to stdout. When given a directory, it processes all template files (.html, .htm, .xml, .svg, .rss, .atom, .gotmpl, .txt) recursively. It also reads from stdin when no paths are given.

For the VS Code extension, see here

CI

To check that all files are formatted in CI, you can use the -l flag:

gotmplfmt -l . | grep . && exit 1

Or use -d to display the diffs:

gotmplfmt -d .

In a GitHub Actions, you may want to add something like these steps to your workflow:

steps:
  - name: Install gotmplfmt
    run: go install github.com/gohugoio/gotmplfmt@latest
  - name: Check go template formatting
    run: "diff <(gotmplfmt -d layouts) <(printf '')"

License

For the license for this code, please see the LICENSE file.

This code is based on code from the Go standard library. The BSD-ish license for that code is:

Copyright (c) 2009 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

   * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
   * Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Footnotes

  1. But it works pretty well for non-HTML Go templates, see the Golden tests.

About

Format Go templates.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Go 85.0%
  • HTML 12.2%
  • TypeScript 2.8%