Skip to content

Commit

Permalink
preprocessor: add ellipsis sanitiser
Browse files Browse the repository at this point in the history
Some commands have long output that is largely uninteresting to the
user. In such situations, it is typical to trim that output and
"summarise" with '...'.

The ellipsis sanitiser does just that. It takes an optional Start field
which indicates from which line number the ellipsis should start.

Preprocessor-No-Write-Cache: true
Signed-off-by: Paul Jolly <[email protected]>
Change-Id: Ia285f810eb880b2e9b09833d82ab3b91bd0710a9
Dispatch-Trailer: {"type":"trybot","CL":1176701,"patchset":7,"ref":"refs/changes/01/1176701/7","targetBranch":"alpha"}
  • Loading branch information
myitcv authored and cueckoo committed Feb 12, 2024
1 parent 831b09a commit 1675f53
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
17 changes: 13 additions & 4 deletions internal/cmd/preprocessor/cmd/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
24 changes: 24 additions & 0 deletions internal/cmd/preprocessor/cmd/sanitisers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
14 changes: 13 additions & 1 deletion internal/cmd/preprocessor/cmd/schema.cue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ package preprocessor
}
}

#sanitiser: _kind & #patternSanitiser // build up a disjunction of all sanitisers
#sanitiser: _kind & {
#patternSanitiser |
#ellipsisSanitiser
}
#matchingSanitiser: {
_matcher
#sanitiser
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
...
```

0 comments on commit 1675f53

Please sign in to comment.