From f65c0bfe0ef31762a2685dfaef5e43b40af3da79 Mon Sep 17 00:00:00 2001 From: Joseph Kato Date: Thu, 9 Jan 2025 13:25:49 -0800 Subject: [PATCH] refactor: support assigning markup type per-scope --- internal/core/blueprint.go | 2 ++ internal/core/file.go | 5 ++++ internal/lint/data.go | 23 ++++++++++--------- internal/lint/lint.go | 4 ++-- testdata/fixtures/blueprints/.vale.ini | 3 --- testdata/styles/config/blueprints/OpenAPI.yml | 4 ++++ 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/internal/core/blueprint.go b/internal/core/blueprint.go index 5d82bb12..ce63dec3 100644 --- a/internal/core/blueprint.go +++ b/internal/core/blueprint.go @@ -37,6 +37,7 @@ type Blueprint struct { // A ScopedValues is a value that has been assigned a scope. type ScopedValues struct { Scope string + Format string Values []string } @@ -89,6 +90,7 @@ func (b *Blueprint) Apply(f *File) ([]ScopedValues, error) { found = append(found, ScopedValues{ Scope: s.Name, Values: values, + Format: s.Type, }) } diff --git a/internal/core/file.go b/internal/core/file.go index ae181c10..cf572dbb 100755 --- a/internal/core/file.go +++ b/internal/core/file.go @@ -271,6 +271,11 @@ func (f *File) SetText(s string) { f.history = map[string]int{} } +// SetNormedExt sets the normalized extension of a File. +func (f *File) SetNormedExt(ext string) { + f.NormedExt = "." + ext +} + // AddAlert calculates the in-text location of an Alert and adds it to a File. func (f *File) AddAlert(a Alert, blk nlp.Block, lines, pad int, lookup bool) { ctx := blk.Context diff --git a/internal/lint/data.go b/internal/lint/data.go index 1ee0b3fc..6340a16f 100644 --- a/internal/lint/data.go +++ b/internal/lint/data.go @@ -38,28 +38,29 @@ func (l *Linter) lintScopedValues(f *core.File, values []core.ScopedValues) erro last := 0 index := 0 - for _, matches := range values { - l.SetMetaScope(matches.Scope) - for _, v := range matches.Values { + for _, match := range values { + l.SetMetaScope(match.Scope) + for _, v := range match.Values { i, line := findLineBySubstring(wholeFile, v, index) if i == 0 { return core.NewE100(f.Path, fmt.Errorf("'%s' not found", v)) } - index = i + f.SetText(v) + f.SetNormedExt(match.Format) - switch f.NormedExt { - case ".md": + switch match.Format { + case "md": err = l.lintMarkdown(f) - case ".rst": + case "rst": err = l.lintRST(f) - case ".xml": - err = l.lintADoc(f) - case ".html": + case "html": err = l.lintHTML(f) - case ".org": + case "org": err = l.lintOrg(f) + case "adoc": + err = l.lintADoc(f) default: err = l.lintLines(f) } diff --git a/internal/lint/lint.go b/internal/lint/lint.go index 52a7b430..01cb02ac 100755 --- a/internal/lint/lint.go +++ b/internal/lint/lint.go @@ -274,12 +274,12 @@ func (l *Linter) lintProse(f *core.File, blk nlp.Block, lines int) error { } func (l *Linter) lintTxt(f *core.File) error { - block := nlp.NewBlock("", f.Content, "text"+f.RealExt) + block := nlp.NewBlock("", f.Content, "text"+l.metaScope+f.RealExt) return l.lintProse(f, block, len(f.Lines)) } func (l *Linter) lintLines(f *core.File) error { - block := nlp.NewBlock("", f.Content, "text"+f.RealExt) + block := nlp.NewBlock("", f.Content, "text"+l.metaScope+f.RealExt) return l.lintBlock(f, block, len(f.Lines), 0, true) } diff --git a/testdata/fixtures/blueprints/.vale.ini b/testdata/fixtures/blueprints/.vale.ini index a18968a0..e943c220 100644 --- a/testdata/fixtures/blueprints/.vale.ini +++ b/testdata/fixtures/blueprints/.vale.ini @@ -1,9 +1,6 @@ StylesPath = ../../styles MinAlertLevel = suggestion -[formats] -yml = md - [*.py] vale.Annotations = YES diff --git a/testdata/styles/config/blueprints/OpenAPI.yml b/testdata/styles/config/blueprints/OpenAPI.yml index 6fef35de..608142b2 100644 --- a/testdata/styles/config/blueprints/OpenAPI.yml +++ b/testdata/styles/config/blueprints/OpenAPI.yml @@ -2,5 +2,9 @@ engine: dasel scopes: - name: title expr: info.title + - expr: info.description + type: md + - expr: servers.all().description + type: md