Skip to content

Commit

Permalink
docs/*: replace manual fenced code blocks
Browse files Browse the repository at this point in the history
This removes several uses of "```" to format code on the page, and
replaces them with "{{{with code ...}}}". This benefits us by increasing
automated "cue fmt" coverage of the examples on the site, and by
engaging syntax highlighting without manual language markers.

Some examples of "```" remain, such as where an environment variable is
being demonstrated; or inside the language guide and spec, which aren't
being updated on the master branch currently.

One example in the modules reference triggers cue-lang/cue#722, and ends
up with a leading comment being incorrectly indented; this error is
allowed to exist as the issue is fixed, and the page will soon fix
itself when the site is updated to use CUE v0.10.0 or later.

One page (list-no-duplicates) loses the highlighting of specific lines
in some examples. This is acceptable in light of the changes anticipated
by cue-lang/docs-and-content#171, which are likely to result in this
page being completely rewritten or subsumed into another howto guide.

Preview-Path: /docs/howto/list-no-duplicates/
Preview-Path: /docs/introduction/
Preview-Path: /docs/reference/modules/
Signed-off-by: Jonathan Matthews <[email protected]>
Change-Id: I1a15a52ddf979ad6ee44bc8950d5e67892bfeb2f
Dispatch-Trailer: {"type":"trybot","CL":1198345,"patchset":1,"ref":"refs/changes/45/1198345/1","targetBranch":"master"}
  • Loading branch information
jpluscplusm authored and cueckoo committed Jul 24, 2024
1 parent b5df5d4 commit c1ab908
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 54 deletions.
54 changes: 29 additions & 25 deletions content/docs/howto/list-no-duplicates/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ has no duplicate items, use

## Strings

```{title="CUE"}
{{{with code "en" "strings"}}}
-- strings.cue --
import "list"

items: ["a", "b", "c", "a"]
items: list.UniqueItems
```
{{{end}}}

## Integers

```{title="CUE"}
{{{with code "en" "ints"}}}
-- ints.cue --
import "list"

items: [1, 2, 3, 1]
items: list.UniqueItems
```
{{{end}}}

{{< warning >}}
Note that this approach does not work correctly on non-integer values.
Expand All @@ -44,44 +45,46 @@ To ensure a list of structs has no duplicate keys, one common approach is to
guarantee that the list has no duplicate items by constructing the list from a
map.

```txt {title="CUE", hl_lines=["5"]}
{{{with code "en" "struct from map"}}}
-- structs.cue --
_items: {
joe: age: 30
joe: age: 30
alice: age: 35
}
_items: [name=string]: "name": name

items: [
for item in _items {
item
}
},
]
```
{{{end}}}

If the list must be constrained directly, you can write an auxiliary field that
creates a mapping from the keys

```txt {title="CUE", hl_lines=["15"]}
{{{with code "en" "auxiliary field"}}}
-- auxiliary-field.cue --
items: [
{
name: "joe"
age: 30
age: 30
},
{
name: "alice"
age: 35
age: 35
},
{
name: "joe"
age: 31
}
age: 31
},
]
_itemsCheck: {
for i, item in items {
(item.name): i
}
}
```
{{{end}}}

If the key is specified twice, there will be a conflict in `_itemsCheck`.

Expand All @@ -93,34 +96,35 @@ be used, using
composite string key from the keys. Here, for example, the combination of `name`
and `dateOfBirth` must be unique:

```txt {title=CUE, hl_lines=["27"]}
{{{with code "en" "multiple keys"}}}
-- multiple-keys.cue --
import "encoding/json"

items: [
{
name: "joe"
name: "joe"
dateOfBirth: "1983-10-21"
country: "US"
country: "US"
},
{
name: "alice"
name: "alice"
dateOfBirth: "1987-10-15"
country: "DE"
country: "DE"
},
{
name: "joe"
name: "joe"
dateOfBirth: "2010-02-05"
country: "UK"
country: "UK"
},
{
name: "alice"
name: "alice"
dateOfBirth: "1987-10-15"
country: "BE"
country: "BE"
},
]
_itemsCheck: {
for i, item in items {
(json.Marshal([item.name, item.dateOfBirth])): i
}
}
```
{{{end}}}
22 changes: 22 additions & 0 deletions content/docs/howto/list-no-duplicates/gen_cache.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package site
{
content: {
docs: {
howto: {
"list-no-duplicates": {
page: {
cache: {
code: {
strings: "UDA4btkP1sMyqQ8F13Wf3lG9SQtZHIurT5ExFvL8mBw="
ints: "skB2q9wfYgZN7V5nX8BrLUV7sGTlhpPMIOaO1B04/wU="
"struct from map": "rGbK+3zQxkWtKpupxPWg/pUuWHr+GVzBKymWxz8QC5Q="
"auxiliary field": "U0E9h7vg2vakrA0MhHGrP2Tbkg/YJw0tJsjxwFuNK4U="
"multiple keys": "yvbqt7Lgfsaz2odQIxgssDRhuylctioFj4C9v6fflKs="
}
}
}
}
}
}
}
}
7 changes: 5 additions & 2 deletions content/docs/introduction/_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,12 @@ All the information of the original JSON file is retained in this
representation.

CUE generalizes this notion to the following pattern:
```
{{{with code "en" "nodes"}}}
#nofmt
-- nodes.cue --
<set of nodes>: <constraints>
```
{{{end}}}

Each field declaration in CUE defines a set of nodes to which to apply
a specific constraint.
Because order doesn't matter, multiple constraints can be applied to the
Expand Down
1 change: 1 addition & 0 deletions content/docs/introduction/gen_cache.cue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package site
CUE: "L27YUfUhlDILxqBuHpE6Mv2FV0cPpLfols+VgNvhcns="
json: "JyuREI1gId2S3q7JyYKhXTThUyIpMV0UKnVugjaqask="
"cue form of json": "Q4UWI0aL60lnJRpuefQ2J5EfOP8JtHLuvSSPRqhkClc="
nodes: "05C+DBtOHLmiF1a1eIFaSy7tgk0vS0EWoVNl83laRS4="
"non-dry": "fDfRG8xVX6aHGwCwAGcSql+uLonm//P0GzdP35JR3xc="
dry: "JpX7f3/30o/xjR/LLaEf0Kbf6/A4kZpntNdQEUxkeoY="
}
Expand Down
12 changes: 7 additions & 5 deletions content/docs/reference/modules/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ in the host `registry.acme.example` at port 6000.
A module is defined by a `cue.mod` directory in its root containing
a `module.cue` CUE file.

```cue
// module indicates the module's path.
{{{with code "en" "module.cue schema"}}}
-- schema.cue --
// module indicates the module's path.
module!: #Module

// version indicates the language version used by the code in this module
Expand Down Expand Up @@ -323,11 +324,12 @@ custom?: [#Module | "legacy"]: [_]: _
// to determining the list of files in a module.
kind!: "self" | "git"
}
```
{{{end}}}

For example:

```cue
{{{with code "en" "module.cue example"}}}
-- example.cue --
language: version: "v0.4.3"

module: "foo.example/my/thing@v1"
Expand All @@ -336,7 +338,7 @@ deps: {
"foo.example/other/thing@v1": v: "v1.0.2"
"foo.example/new/thing@v2": v: "v2.3.4"
}
```
{{{end}}}

The `module.cue` file is designed to be human readable and machine writable. The
`cue` command will provide several subcommands that manipulate `cue.mod/module.cue` files.
Expand Down
19 changes: 19 additions & 0 deletions content/docs/reference/modules/gen_cache.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package site
{
content: {
docs: {
reference: {
modules: {
page: {
cache: {
code: {
"module.cue schema": "oM0yOYti8hU3XV3wcI5d2+UQ+bYY1TKEpsL1u/tT9sk="
"module.cue example": "1kYCBXX1oLFNO8U5Ny/F6w4MEQF6p+kjPfodcFeEcIU="
}
}
}
}
}
}
}
}
39 changes: 19 additions & 20 deletions hugo/content/en/docs/howto/list-no-duplicates/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ has no duplicate items, use

## Strings

```{title="CUE"}
```cue
import "list"
items: ["a", "b", "c", "a"]
Expand All @@ -22,8 +22,7 @@ items: list.UniqueItems

## Integers

```{title="CUE"}
```cue
import "list"
items: [1, 2, 3, 1]
Expand All @@ -44,37 +43,37 @@ To ensure a list of structs has no duplicate keys, one common approach is to
guarantee that the list has no duplicate items by constructing the list from a
map.

```txt {title="CUE", hl_lines=["5"]}
```cue
_items: {
joe: age: 30
joe: age: 30
alice: age: 35
}
_items: [name=string]: "name": name
items: [
for item in _items {
item
}
},
]
```

If the list must be constrained directly, you can write an auxiliary field that
creates a mapping from the keys

```txt {title="CUE", hl_lines=["15"]}
```cue
items: [
{
name: "joe"
age: 30
age: 30
},
{
name: "alice"
age: 35
age: 35
},
{
name: "joe"
age: 31
}
age: 31
},
]
_itemsCheck: {
for i, item in items {
Expand All @@ -93,29 +92,29 @@ be used, using
composite string key from the keys. Here, for example, the combination of `name`
and `dateOfBirth` must be unique:

```txt {title=CUE, hl_lines=["27"]}
```cue
import "encoding/json"
items: [
{
name: "joe"
name: "joe"
dateOfBirth: "1983-10-21"
country: "US"
country: "US"
},
{
name: "alice"
name: "alice"
dateOfBirth: "1987-10-15"
country: "DE"
country: "DE"
},
{
name: "joe"
name: "joe"
dateOfBirth: "2010-02-05"
country: "UK"
country: "UK"
},
{
name: "alice"
name: "alice"
dateOfBirth: "1987-10-15"
country: "BE"
country: "BE"
},
]
_itemsCheck: {
Expand Down
3 changes: 2 additions & 1 deletion hugo/content/en/docs/introduction/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,10 @@ All the information of the original JSON file is retained in this
representation.

CUE generalizes this notion to the following pattern:
```
```cue
<set of nodes>: <constraints>
```

Each field declaration in CUE defines a set of nodes to which to apply
a specific constraint.
Because order doesn't matter, multiple constraints can be applied to the
Expand Down
2 changes: 1 addition & 1 deletion hugo/content/en/docs/reference/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ A module is defined by a `cue.mod` directory in its root containing
a `module.cue` CUE file.

```cue
// module indicates the module's path.
// module indicates the module's path.
module!: #Module
// version indicates the language version used by the code in this module
Expand Down

0 comments on commit c1ab908

Please sign in to comment.