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.
  • define blocks don't trigger indentation. See this issue for some discussion.
  • We don't try to format script and style blocks.
  • We don't auto-add trailing newlines to the document; see this issue for some context.
  • We don't read .prettierignore.
  • But we do support {{/* gotmplfmt-ignore-all */}}, {{/* gotmplfmt-ignore-start */}} and {{/* gotmplfmt-ignore-end */}}.
  • 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.

Usage

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

go install github.com/gohugoio/gotmplfmt@latest
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

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

Work in progress.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

No contributors

Languages

  • Go 87.1%
  • HTML 9.8%
  • TypeScript 3.1%