diff --git a/internal/cmd/preprocessor/cmd/page.go b/internal/cmd/preprocessor/cmd/page.go index a479c9c47..666e67f55 100644 --- a/internal/cmd/preprocessor/cmd/page.go +++ b/internal/cmd/preprocessor/cmd/page.go @@ -177,14 +177,23 @@ func (p *page) parseSanitiser(m json.RawMessage) (sanitiserMatcher, error) { switch kind { case "patternSanitiser": - var c patternSanitiserMatcher - if err := json.Unmarshal(m, &c); err != nil { + var ps patternSanitiserMatcher + if err := json.Unmarshal(m, &ps); err != nil { return nil, fmt.Errorf("failed to unmarshal %s: %v", kind, err) } - if err := c.init(); err != nil { + if err := ps.init(); err != nil { return nil, fmt.Errorf("failed to init %s: %v", kind, err) } - return &c, nil + return &ps, nil + case "ellipsisSanitiser": + var es ellipsisSanitiserMatcher + if err := json.Unmarshal(m, &es); err != nil { + return nil, fmt.Errorf("failed to unmarshal %s: %v", kind, err) + } + if err := es.init(); err != nil { + return nil, fmt.Errorf("failed to init %s: %v", kind, err) + } + return &es, nil default: return nil, fmt.Errorf("unknown sanitiser: %q", kind) } diff --git a/internal/cmd/preprocessor/cmd/sanitisers.go b/internal/cmd/preprocessor/cmd/sanitisers.go index 7138dda65..ca1e3a4b8 100644 --- a/internal/cmd/preprocessor/cmd/sanitisers.go +++ b/internal/cmd/preprocessor/cmd/sanitisers.go @@ -77,6 +77,7 @@ func (m *matchSpec) init() error { if m.CommandPrefix != "" { cmd = m.CommandPrefix } + // k.Step is only != "" when we are matching based on step file, err := syntax.NewParser(syntax.KeepComments(true)).Parse(strings.NewReader(cmd), " ") if err != nil { return fmt.Errorf("failed to parse %q: %v", cmd, err) @@ -187,3 +188,26 @@ func (p *patternSanitiserMatcher) init() error { } return nil } + +// An ellipsisSanitiser allows very long output to be removed and replaced with +// the canonical '...' which is intended to indicate "and there is is more not +// shown here". +type ellipsisSanitiser struct { + Start int `json:"start"` +} + +func (e *ellipsisSanitiser) sanitise(cmd *commandStmt) error { + if strings.Count(cmd.Output, "\n") <= e.Start { + return nil + } + lines := strings.Split(cmd.Output, "\n") + lines = append(lines[:e.Start], "...") + cmd.Output = strings.Join(lines, "\n") + "\n" // re-add trailing newline + return nil +} + +type ellipsisSanitiserMatcher struct { + kind + ellipsisSanitiser + matchSpec +} diff --git a/internal/cmd/preprocessor/cmd/schema.cue b/internal/cmd/preprocessor/cmd/schema.cue index 0eb7d40b4..1b8952767 100644 --- a/internal/cmd/preprocessor/cmd/schema.cue +++ b/internal/cmd/preprocessor/cmd/schema.cue @@ -20,7 +20,10 @@ package preprocessor } } - #sanitiser: _kind & #patternSanitiser // build up a disjunction of all sanitisers + #sanitiser: _kind & { + #patternSanitiser | + #ellipsisSanitiser + } #matchingSanitiser: { _matcher #sanitiser @@ -50,6 +53,15 @@ package preprocessor #patternSanitiser } + // Instances of #ellipsisSanitiser summarise long output with '...' + #ellipsisSanitiser: { + _kind + kind: "ellipsisSanitiser" + + // start defines the line from which to consider an ellipsis + start?: int + } + #comparator: _kind & (#patternComparator | #unstableLineOrderComparator) #matchingComparator: { _matcher diff --git a/internal/cmd/preprocessor/cmd/testdata/execute_ellipsis_sanitiser.txtar b/internal/cmd/preprocessor/cmd/testdata/execute_ellipsis_sanitiser.txtar new file mode 100644 index 000000000..0d60c42b2 --- /dev/null +++ b/internal/cmd/preprocessor/cmd/testdata/execute_ellipsis_sanitiser.txtar @@ -0,0 +1,61 @@ +# Ensure that the ellipsis sanitiser works. + +unquote content/dir/en.md + +# Run the preprocessor +exec preprocessor execute + +# Verify that the target files matche the source contents +cmp hugo/content/en/dir/index.md golden/hugo/content/en/dir/index.md + +-- hugo/.keep -- +-- content/dir/site.cue -- +package site + +content: dir: page: { + leftDelim: "{{{" + rightDelim: "}}}" + + sanitisers: [ + { + kind: "ellipsisSanitiser" + command: "seq 1 5" + }, + { + kind: "ellipsisSanitiser" + start: 5 + command: "seq 1 10" + }, + ] +} +-- content/dir/en.md -- +>--- +>title: JSON Superset +>--- +> +>{{{with script "en" "zero"}}} +>seq 1 5 +>{{{end}}} +> +>{{{with script "en" "five"}}} +>seq 1 10 +>{{{end}}} +-- golden/hugo/content/en/dir/index.md -- +--- +title: JSON Superset +--- + +```text { title="TERMINAL" codeToCopy="c2VxIDEgNQo=" } +$ seq 1 5 +... +``` + +```text { title="TERMINAL" codeToCopy="c2VxIDEgMTAK" } +$ seq 1 10 +1 +2 +3 +4 +5 +... +```