Skip to content

Commit

Permalink
preprocessor: add support for code directive
Browse files Browse the repository at this point in the history
A code directive is a txtar-based directive, that contains a single
file. It results in a ```-block, with the language of that block taken
from the extension of the single file in the txtar archive. The basename
of the file is irrelevant. The contents of the code directive are
formatted, unless the #nofmt tag is used.

Preprocessor-No-Write-Cache: true
Signed-off-by: Paul Jolly <[email protected]>
Change-Id: If68a9b0a10a2224e8841dfed73867fd41837b584
Dispatch-Trailer: {"type":"trybot","CL":1169683,"patchset":1,"ref":"refs/changes/83/1169683/1","targetBranch":"alpha"}
  • Loading branch information
myitcv authored and cueckoo committed Sep 19, 2023
1 parent b576b7f commit 408672e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
84 changes: 84 additions & 0 deletions internal/cmd/preprocessor/cmd/code_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2023 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"bytes"
"fmt"
)

const (
fnCode = "code"
)

// A codeNode is used to create a simple ```-based code block for a single file
// whose contents are formatted. This ensures good formatting but also valid
// code.
type codeNode struct {
txtarNode
}

func (c *codeNode) nodeType() string {
return fnCode
}

var _ runnableNode = (*codeNode)(nil)

type codeNodeRunContext struct {
*txtarRunContext
}

func (s *codeNode) validate() error {
if l := len(s.analysis.fileNames); l != 1 {
return fmt.Errorf("code nodes can only contain one file in the txtar archive")
}
return nil
}

func (s *codeNode) run() runnable {
return &codeNodeRunContext{
txtarRunContext: &txtarRunContext{
txtarNode: s.txtarNode,
executionContext: s.executionContext,
bufferedErrorContext: &errorContextBuffer{
executionContext: s.executionContext,
},
},
}
}

func (s *codeNodeRunContext) run() (err error) {
defer recoverFatalError(&err)

if err := s.formatFiles(); err != nil {
return errorIfInError(s)
}

return nil
}

func (s *codeNode) writeTransformTo(b *bytes.Buffer) error {
p := bufPrintf(b)

// There will only be one file
a := s.analysis.fileNames[0]
f := s.effectiveArchive.Files[0]

// TODO tidy up templating etc
p("```%s\n", a.Language)
p("%s", f.Data)
p("```")
return nil
}
6 changes: 6 additions & 0 deletions internal/cmd/preprocessor/cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ func (rf *rootFile) parse_WithNode(n *parse.WithNode) (node, error) {
return nil, err
}
return &sidebysideNode{txtarNode: t}, nil
case fnCode:
t, err := rf.parse_txtarNode(n, fn.Ident, c.Args[1:])
if err != nil {
return nil, err
}
return &codeNode{txtarNode: t}, nil
case fnStep:
// Increment first because we are numbering from 1
rf.stepNumber++
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 @@ -43,6 +43,7 @@ var (
// were function calls.
templateFunctions = map[string]any{
fnSidebyside: true,
fnCode: true,
fnStep: true,
fnUpload: true,
fnScript: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ package site
>
>{{{with step}}}
>some text
>{{{with code "en" "x.cue example"}}}
>-- x.cue --
>x: 5
>{{{end}}}
>{{{end}}}
>
>Some text after
Expand Down Expand Up @@ -98,6 +102,9 @@ map: {

{{< step stepNumber="2" >}}
some text
```text
x: 5
```
{{< /step >}}

Some text after
Expand Down

0 comments on commit 408672e

Please sign in to comment.