|
| 1 | +The CaT module is available via `require "pluto:cat"` and provides encoding and decoding for [the Colons and Tabs format](https://docs.soup.do/user/cat). |
| 2 | + |
| 3 | +### `cat.encode` |
| 4 | +Returns a string of CaT. |
| 5 | +#### Parameters |
| 6 | +1. `data` — The table to encode in CaT. |
| 7 | +```pluto |
| 8 | +local cat = require "pluto:cat" |
| 9 | +
|
| 10 | +cat.encode { |
| 11 | + List = { |
| 12 | + __value = "With Value", |
| 13 | + Child = "Also With Value" |
| 14 | + } |
| 15 | +} |> io.write |
| 16 | +--> List: With Value |
| 17 | +--> Child: Also With Value |
| 18 | +``` |
| 19 | + |
| 20 | +### `cat.decode` |
| 21 | +Returns a table representing the CaT data. |
| 22 | +#### Parameters |
| 23 | +1. `data` — The CaT data to decode. |
| 24 | +2. `presentation` — How to represent the data as a table. Can be "flat", "flatwithorder", or "full" (default). |
| 25 | + |
| 26 | +```pluto |
| 27 | +local cat = require "pluto:cat" |
| 28 | +
|
| 29 | +local data = cat.decode [[ |
| 30 | +List: With Value |
| 31 | + Child: Also With Value |
| 32 | +]] |
| 33 | +
|
| 34 | +print(dumpvar(data)) |
| 35 | +--> { |
| 36 | +--> [1] = { |
| 37 | +--> ["value"] = string(10) "With Value", |
| 38 | +--> ["name"] = string(4) "List", |
| 39 | +--> ["children"] = { |
| 40 | +--> [1] = { |
| 41 | +--> ["name"] = string(5) "Child", |
| 42 | +--> ["value"] = string(15) "Also With Value", |
| 43 | +--> }, |
| 44 | +--> }, |
| 45 | +--> }, |
| 46 | +--> } |
| 47 | +``` |
| 48 | + |
| 49 | +With the default "full" representation, the retuned table has an `__index` metamethod for ease of use: |
| 50 | + |
| 51 | +```pluto |
| 52 | +print(data.List.value) --> With Value |
| 53 | +print(data.List.Child.value) --> Also With Value |
| 54 | +``` |
| 55 | + |
| 56 | +Alternatively, the other representations are available: |
| 57 | +```pluto |
| 58 | +local cat = require "pluto:cat" |
| 59 | +
|
| 60 | +print(dumpvar(cat.decode([[ |
| 61 | +home |
| 62 | + john |
| 63 | + hello.txt: Hello, world! |
| 64 | +]], "flat"))) |
| 65 | +--> { |
| 66 | +--> ["home"] = { |
| 67 | +--> ["john"] = { |
| 68 | +--> ["hello.txt"] = string(13) "Hello, world!", |
| 69 | +--> }, |
| 70 | +--> }, |
| 71 | +--> } |
| 72 | +``` |
| 73 | +```pluto |
| 74 | +local cat = require "pluto:cat" |
| 75 | +
|
| 76 | +print(dumpvar(cat.decode([[ |
| 77 | +Primary Color: Purple |
| 78 | +Secondary Color: White |
| 79 | +]], "flatwithorder"))) |
| 80 | +--> { |
| 81 | +--> ["__order"] = { |
| 82 | +--> [1] = string(13) "Primary Color", |
| 83 | +--> [2] = string(15) "Secondary Color", |
| 84 | +--> }, |
| 85 | +--> ["Primary Color"] = string(6) "Purple", |
| 86 | +--> ["Secondary Color"] = string(5) "White", |
| 87 | +--> } |
| 88 | +``` |
0 commit comments