Skip to content

Commit

Permalink
frontmatter custom delimiters, doc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fileformat committed Jul 11, 2022
1 parent ddf4b0d commit adc37ff
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 44 deletions.
9 changes: 9 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
- [ ] SignedRange (i.e. handle negative numbers: -10-10, -20--10, --10, -10-)
- [ ] Range: take a comma-separated list of acceptable values

## Online

- [ ] logging (not per-page, since that is done by CloudRun)
- [ ] disclaimer in all responses
- [ ] verbose flag
- [ ] pass flags to badger
- [ ] forms (in docs)
- [ ] api.html (in docs)
- [ ] links to forms from /index.html

## Docs

Expand Down
13 changes: 1 addition & 12 deletions cmd/badger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,8 @@ func main() {

shared.AddCommon(rootCmd)

command.AddExtCommand(rootCmd)
command.AddFrontmatterCommand(rootCmd)
command.AddHtmlCommand(rootCmd)
command.AddIcoCommand(rootCmd)
command.AddJpegCommand(rootCmd)
command.AddJsonCommand(rootCmd)
command.AddMimeTypeCommand(rootCmd)
command.AddPngCommand(rootCmd)
command.AddSvgCommand(rootCmd)
command.AddTextCommand(rootCmd)
command.AddAllCommands(rootCmd)
command.AddVersionCommand(rootCmd, command.VersionInfo{Commit: commit, Version: version, LastMod: date, Builder: builtBy})
command.AddXmlCommand(rootCmd)
command.AddYamlCommand(rootCmd)

if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err.Error())
Expand Down
19 changes: 19 additions & 0 deletions internal/command/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package command

import "github.com/spf13/cobra"

func AddAllCommands(rootCmd *cobra.Command) {

AddExtCommand(rootCmd)
AddFrontmatterCommand(rootCmd)
AddHtmlCommand(rootCmd)
AddIcoCommand(rootCmd)
AddJpegCommand(rootCmd)
AddJsonCommand(rootCmd)
AddMimeTypeCommand(rootCmd)
AddPngCommand(rootCmd)
AddSvgCommand(rootCmd)
AddTextCommand(rootCmd)
AddXmlCommand(rootCmd)
AddYamlCommand(rootCmd)
}
2 changes: 1 addition & 1 deletion internal/command/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
var extCmd = &cobra.Command{
Aliases: []string{"extension", "extensions"},
Args: cobra.MinimumNArgs(1),
Use: "ext [flags] filespec [filespec...]",
Use: "ext [options] files...",
Short: "Validate (or report) file extensions",
Long: `Check and report on the file extensions in use`,
PreRunE: extensionReportInit,
Expand Down
47 changes: 34 additions & 13 deletions internal/command/frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"bytes"
"fmt"
"os"

"github.com/adrg/frontmatter"
"github.com/fileformat/badger/internal/shared"
Expand All @@ -11,17 +12,18 @@ import (
)

var (
fmReport bool
fmStrict bool
fmStrictSet map[string]bool
fmSorted bool
fmRequired []string
fmOptional []string
fmForbidden []string
fmReport bool
fmStrict bool
fmStrictSet map[string]bool
fmSorted bool
fmRequired []string
fmOptional []string
fmForbidden []string
fmDelimiters []string
)
var frontmatterCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Use: "frontmatter",
Use: "frontmatter [options] files...",
Short: "Validate frontmatter",
Long: `Checks that the frontmatter in your files is valid`,
PreRunE: frontmatterPrepare,
Expand All @@ -36,7 +38,8 @@ func AddFrontmatterCommand(rootCmd *cobra.Command) {
frontmatterCmd.Flags().StringSliceVar(&fmForbidden, "forbidden", []string{}, "Forbidden keys")
frontmatterCmd.Flags().BoolVar(&fmStrict, "strict", false, "Strict (keys must be in `--required` or `--optional`)")
frontmatterCmd.Flags().BoolVar(&fmSorted, "sorted", false, "Keys need to be in alphabetical order")
//LATER: content: required/optional/forbidden
frontmatterCmd.Flags().StringSliceVar(&fmDelimiters, "delimiters", []string{}, "Custom delimiters (if other than `---`, `+++` and `;;;`)")

//LATER: report
//LATER: schema
}
Expand All @@ -53,12 +56,25 @@ func frontmatterCheck(f *shared.FileContext) {

yamlData := make(map[interface{}]interface{})

_, parseErr := frontmatter.Parse(bytes.NewReader(data), &yamlData)
var formats []*frontmatter.Format

if len(fmDelimiters) > 0 {
var end = fmDelimiters[0]
if len(fmDelimiters) > 1 {
end = fmDelimiters[1]
}
formats = append(formats, frontmatter.NewFormat(fmDelimiters[0], end, yaml.Unmarshal))
if shared.Debug {
fmt.Fprintf(os.Stderr, "DEBUG: using custom delimiters '%s' and '%s'\n", fmDelimiters[0], end)
}
}

_, parseErr := frontmatter.MustParse(bytes.NewReader(data), &yamlData, formats...)

f.RecordResult("frontmatterParse", parseErr == nil, map[string]interface{}{
"error": shared.ErrString(parseErr),
})
if parseErr != nil {
f.RecordResult("frontmatterParse", false, map[string]interface{}{
"error": parseErr,
})
return
}

Expand Down Expand Up @@ -130,5 +146,10 @@ func frontmatterPrepare(cmd *cobra.Command, args []string) error {
fmStrictSet[key] = true
}
}

if len(fmDelimiters) > 2 {
fmt.Fprintf(os.Stderr, "ERROR: delimiter count must be <=2 (passed %d)", len(fmDelimiters))
os.Exit(7)
}
return nil
}
2 changes: 1 addition & 1 deletion internal/command/jpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
// jpegCmd represents the jpeg command
var jpegCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Use: "jpeg",
Use: "jpeg [options] files...",
Short: "Validate JPEG images",
Long: `Check that your JPEG files are valid`,
RunE: shared.MakeFileCommand(jpegCheck),
Expand Down
2 changes: 1 addition & 1 deletion internal/command/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// jsonCmd represents the json command
var jsonCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Use: "json",
Use: "json [options] files...",
Short: "Validate JSON files",
Long: `Check that your JSON files are valid`,
RunE: shared.MakeFileCommand(jsonCheck),
Expand Down
2 changes: 1 addition & 1 deletion internal/command/mimetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
var mimetypeCmd = &cobra.Command{
Aliases: []string{"mt", "filetype"},
Args: cobra.MinimumNArgs(1),
Use: "mimetype [flags] files...",
Use: "mimetype [options] files...",
Short: "Validate (or report) MIME content types",
Long: `Check and report on the MIME content types in use`,
Example: `Content type detection uses the Go standard library [http.DetectContentType](https://golang.org/pkg/net/http/#DetectContentType) function.`,
Expand Down
2 changes: 1 addition & 1 deletion internal/command/png.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
// pngCmd represents the png command
var pngCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Use: "png",
Use: "png [options] files...",
Short: "Validate png images",
Long: `Check that your PNG files are valid`,
RunE: shared.MakeFileCommand(pngCheck),
Expand Down
20 changes: 9 additions & 11 deletions internal/command/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ var (
svgImage = argtype.NewStringSet("Images", "none", []string{"any", "embedded", "external", "none"})
)

// svgCmd represents the svg command
var svgCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Use: "svg",
Short: "Validate SVG images",
Long: `Check that SVG files are error free and (optionally) don't have any undesirable things in them.`,
PreRunE: svgPrepare,
RunE: shared.MakeFileCommand(svgCheck),
}

func AddSvgCommand(rootCmd *cobra.Command) {
rootCmd.AddCommand(svgCmd)
var svgCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Use: "svg [options] files...",
Short: "Validate SVG images",
Long: `Check that SVG files are error free and (optionally) don't have any undesirable things in them.`,
PreRunE: svgPrepare,
RunE: shared.MakeFileCommand(svgCheck),
}

svgCmd.Flags().Var(&svgHeight, "height", "Range of allowed SVG heights")
svgCmd.Flags().Var(&svgViewBox, "viewbox", "Ranges of allowed SVG viewBox values")
Expand All @@ -57,6 +54,7 @@ func AddSvgCommand(rootCmd *cobra.Command) {
//LATER: meta
//LATER: optimized (and/or pretty?)

rootCmd.AddCommand(svgCmd)
}

func svgCheck(f *shared.FileContext) {
Expand Down
2 changes: 1 addition & 1 deletion internal/command/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (

// textCmd represents the text command
var textCmd = &cobra.Command{
Use: "text",
Use: "text [options] files...",
Short: "Validate plain text files",
Long: `Checks that plain text files really are plain text, have the correct line endings and more`,
RunE: shared.MakeFileCommand(textCheck),
Expand Down
2 changes: 1 addition & 1 deletion internal/command/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
// xmlCmd represents the xml command
var xmlCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Use: "xml",
Use: "xml [options] files...",
Short: "Validate XML files",
Long: `Checks that your XML files are valid`,
PreRunE: xmlInit,
Expand Down
2 changes: 1 addition & 1 deletion internal/command/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
)
var yamlCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Use: "yaml",
Use: "yaml [options] files...",
Short: "Validate YAML files",
Long: `Check that your YAML files are valid`,
PreRunE: yamlPrepare,
Expand Down
24 changes: 24 additions & 0 deletions testdata/frontmatter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

! exec badger frontmatter --show-tests=all missing.md

exec badger frontmatter --show-tests=all test.md

! exec badger frontmatter --show-tests=all test.sql

exec badger frontmatter --delimiters=/***,***/ --debug --show-tests=all test.sql

-- test.md --
---
title: Markdown with frontmatter
---
Content

-- missing.md --
Content

-- test.sql --
/***
title: SQL with custom delimiters
***/
SELECT * FROM examples

0 comments on commit adc37ff

Please sign in to comment.