README

vugufmt

gofmt, but for vugu and html files.

Goals

  • Match args and output as closely as possible to the original gofmt's general usecase.
  • Wrap around the actual gofmt; don't copy its code here.
  • Format HTML5 in addition to go source.
  • Report correct absolute line numbers and file names in the output.
  • Pure go implementation.

Non-Goals

  • Support versions of HTML other than HTML5.

Stretch-Goals

  • Pretty fmt'ing CSS. A lot of CSS these days is generated by a preprocessor like SASS or LESS.

Implementation

  1. Use golang.org/x/net/html to parse the vugu file. This will probably need a tweak to handle the lack of a single root element, missing body, etc. This package has a Render function which re-prints the HTML parse tree, which we will use as the formatted output.
  2. Pass in text data for <script type="application/x-go"> tags into gofmt. Capture its output.

Sources

This project took a lot of its code from gofmt and Go's Token file. As such, I'm using the same license as gofmt.

Expand ▾ Collapse ▴

Documentation

Overview

    Package vugufmt provides gofmt-like functionality for vugu files.

    Index

    Constants

    This section is empty.

    Variables

    This section is empty.

    Functions

    func UseGoFmt

    func UseGoFmt(simplifyAST bool) func(*Formatter)

      UseGoFmt sets the formatter to use gofmt on x-go blocks. Set simplifyAST to true to simplify the AST. This is false by default for gofmt, and is the same as passing in -s for it.

      func UseGoImports

      func UseGoImports(f *Formatter)

        UseGoImports sets the formatter to use goimports on x-go blocks.

        Types

        type FmtError

        type FmtError struct {
        	Msg      string
        	FileName string
        	Line     int
        	Column   int
        }

          FmtError is a formatting error.

          func (FmtError) Error

          func (e FmtError) Error() string

          type Formatter

          type Formatter struct {
          	// ScriptFormatters maps script blocks to formatting
          	// functions.
          	// For each type of script block,
          	// we can run it through the supplied function.
          	// If the function returns error, we should
          	// not accept the output written to the writer.
          	// You can add your own custom one for JS, for
          	// example. If you want to use gofmt or goimports,
          	// see how to apply options in NewFormatter.
          	ScriptFormatters map[string]func([]byte) ([]byte, *FmtError)
          	// StyleFormatter handles CSS blocks.
          	StyleFormatter func([]byte) ([]byte, *FmtError)
          }

            Formatter allows you to format vugu files.

            func NewFormatter

            func NewFormatter(opts ...func(*Formatter)) *Formatter

              NewFormatter creates a new formatter. Pass in vugufmt.UseGoFmt to use gofmt. Pass in vugufmt.UseGoImports to use goimports.

              func (*Formatter) Diff

              func (f *Formatter) Diff(filename string, input io.Reader, output io.Writer) (bool, error)

                Diff will show differences between input and what Format() would do. It will return (true, nil) if there is a difference, (false, nil) if there is no difference, and (*, notnil) when the difference can't be determined. filename is optional, but helps with generating useful output.

                func (*Formatter) FormatHTML

                func (f *Formatter) FormatHTML(filename string, in io.Reader, out io.Writer) error

                  FormatHTML formats script and css nodes.

                  func (*Formatter) FormatScript

                  func (f *Formatter) FormatScript(scriptType string, scriptContent []byte) ([]byte, *FmtError)

                    FormatScript formats script text nodes.

                    func (*Formatter) FormatStyle

                    func (f *Formatter) FormatStyle(styleContent []byte) ([]byte, *FmtError)

                      FormatStyle formats script text nodes.