Skip to content

Commit

Permalink
preprocessor: support hidden uploads
Browse files Browse the repository at this point in the history
This allows guides which only choose to present select files (as opposed
to a full tutorial from start to end) to still be validated as working,
by uploading files which the user does not see. (The files themselves
are not hidden, just the upload block itself is not rendered in the Hugo
output).

Preprocessor-No-Write-Cache: true
Signed-off-by: Paul Jolly <[email protected]>
Change-Id: Ia7289d0fc730c744375c23a4136e57493baa1fcb
Dispatch-Trailer: {"type":"trybot","CL":1177013,"patchset":4,"ref":"refs/changes/13/1177013/4","targetBranch":"alpha"}
  • Loading branch information
myitcv authored and cueckoo committed Feb 20, 2024
1 parent 77dfeb5 commit acc71a8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
8 changes: 6 additions & 2 deletions internal/cmd/preprocessor/cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,16 @@ func (rf *rootFile) parse_WithNode(n *parse.WithNode) (node, error) {
// Increment first because we are numbering from 1
rf.stepNumber++
return rf.parse_stepNode(n, rf.stepNumber)
case fnUpload:
case fnUpload, fnHiddenUpload:
hidden := fn.Ident == fnHiddenUpload
t, err := rf.parse_txtarNode(n, fn.Ident, c.Args[1:])
if err != nil {
return nil, err
}
return &uploadNode{txtarNode: t}, nil
return &uploadNode{
txtarNode: t,
hidden: hidden,
}, nil
case fnScript, fnHiddenScript:
hidden := fn.Ident == fnHiddenScript
t, err := rf.parse_txtarNode(n, fn.Ident, c.Args[1:])
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/preprocessor/cmd/rootfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var (
fnCode: true,
fnStep: true,
fnUpload: true,
fnHiddenUpload: true,
fnScript: true,
fnHiddenScript: true,
"reference": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ content: dir: page: {
>title: JSON Superset
>---
>{{{with step}}}
>
>{{{with _upload "en" "hidden file"}}}
>-- hidden.txt --
>Secret message
>{{{end}}}
>
>{{{with upload "en" "upload-some-cue"}}}
>#codetab(in.cue) linenos="table"
>-- in.cue --
Expand Down Expand Up @@ -121,6 +127,12 @@ content: dir: page: {
>title: JSON Superset
>---
>{{{with step}}}
>
>{{{with _upload "en" "hidden file"}}}
>-- hidden.txt --
>Secret message
>{{{end}}}
>
>{{{with upload "en" "upload-some-cue"}}}
>#codetab(in.cue) linenos="table"
>-- in.cue --
Expand Down Expand Up @@ -233,12 +245,13 @@ package site
page: {
cache: {
upload: {
"hidden file": "9v5xA21u0+ZiHt6/cqBWgR29ErERw+0r7ZdN9YrBMDA="
"upload-some-cue": "tP3KoyKTIn79Gk8U9FATNuou3WfinCe3OvMMJIGDL+Y="
"upload-some-json": "xZav4QaIGOVUoGgl6FLH1FyMGWS9+cOFaTTEoD5mc1U="
"in-subdir": "WFUtw4fPeq+fBaYmRr1v6Va3OJiB3Pht2bJZgs+whQU="
}
multi_step: {
"J523PNTTH7RBNDJU4R39J6VCBERKCGAQASGSROP9NVDJ7EEPFC4G====": [{
"7HR2L5CP9OEINJ1A2AGDLFO5DR2378A0TEDTCLBMHBMVATC7BR40====": [{
doc: """
# script doc comment
#scripttag
Expand Down
13 changes: 12 additions & 1 deletion internal/cmd/preprocessor/cmd/upload_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ import (
"bytes"
)

const fnUpload = "upload"
const (
fnUpload = "upload"
fnHiddenUpload = "_upload"
)

type uploadNode struct {
txtarNode

// hidden is set to indicate the script exists for side effects
// only and will not be rendered
hidden bool
}

var _ validatingNode = (*uploadNode)(nil)
Expand All @@ -30,6 +37,10 @@ func (u *uploadNode) nodeType() string {
return "upload"
}

func (u *uploadNode) isHidden() bool {
return u.hidden
}

func (u *uploadNode) validate() {
if l := len(u.analysis.fileNames); l != 1 {
u.errorf("%v: upload nodes can only contain a single file; saw %d", u, l)
Expand Down

0 comments on commit acc71a8

Please sign in to comment.