# Ord CSV mapping Version 0.8.0-draft, 2026-12-01. A companion spec to [Ord specification](Ord_Specification.md), of the same version. The Spec's conventions (S1 to S6) and versioning (S10) apply here, and rule IDs that start with `C`, `L`, `D` or `T` refer to the Spec and the [Test format](Test_format.md), and those that start with `M` to [Semantics](Semantics.md). ## 1. Scope - **V1.** This spec maps CSV files to the Core data model (Spec, section 1) and back, directly, without JSON. A **CSV file** is a file whose name ends in `.csv` or `.tsv`, in any letter case. A row is a node and a column is a field. Reading (section 3) is syntactic: columns such as `_types`, `_id` and `_parent` are fields, which the index reads as system fields (M11) as it reads them in text files. Writing (section 4) turns a model into rows, its hierarchy into `_parent` cells and its field children into path columns. Converting (section 5) writes a CSV file as a text file, with the hierarchy and label forms back in place. - **V2.** A **CSV mapper** reads, writes and converts CSV as this spec says, and passes the tests with the `from-csv`, `to-csv` and `csv-to-ord` options (T4). - **V25.** A `.tsv` file is **TSV**: CSV as this spec reads and writes it, with a tab in place of `,` as its delimiter (V5) and nothing else changed. A cell that holds a tab, `"` or a line ending is written in `"` quotes, with each `"` doubled, and reads back as one cell (V15), as the macOS clipboard and spreadsheets write tab-separated text. TSV as IANA registers it (`text/tab-separated-values`), which has no quotes and no tabs or line endings in cells, reads the same, except a cell that starts with `"`, which is quoted here. So cells copied from a spreadsheet, as the macOS clipboard holds them, read as the table they were when saved as a `.tsv` file. ## 2. Settings - **V16.** A CSV file's **settings** are in the workspace file (Spec F5): a `csv::` field line, at any depth, has a child line for each file, its path relative to the workspace folder with `/` between folders, and the file's settings as field lines under it: - `label column::` the name of the column whose cells are the labels (V3) - `delimiter::` `comma`, `semicolon` or `tab` (V5) A file without a setting uses the guess that V3 or V5 makes. An editor that opens a file of the workspace without a setting writes its guesses there, so that they can be seen and changed, and its CSV view has a command that sets the label column. A setting for a column the header doesn't have, or with another delimiter, is the warning `V16.1` and is ignored. Outside a workspace, as in tests, the guesses are used. In any other file, `csv` is an ordinary field. ``` csv:: contacts.csv label column:: Name delimiter:: semicolon ``` ## 3. Reading CSV - **V4.** A CSV file is UTF-8 (C49), with an optional BOM (C2). A file that isn't valid UTF-8 is read as Windows-1252, as old spreadsheet exports are written, with the warning `V4.1`. It is a list of records. Outside quoted cells, a record ends at `\n` or `\r\n`, and the last one may have no ending. A blank line is skipped. - **V5.** Cells are separated by the file's **delimiter**: its setting (V16), or else a tab for a file whose name ends in `.tsv`, in any letter case, or else whichever of `,`, `;` and tab is found most often in the first record outside quoted cells, with `,` on a tie and when there is none. A cell that starts with `"` is quoted: it runs to the next `"` that isn't doubled, `""` in it stands for one `"`, and it can hold the delimiter and line endings. Mistakes are kept as text, with a warning: - a `"` in a cell that isn't quoted is kept - text after a quoted cell's closing `"`, up to the next delimiter, is added to the cell - a quoted cell that is never closed runs to the end of the file - **V6.** The first record is the **header**. Its cells are the column names, without spaces and tabs at their start and end, which give the warning `V6.1`. Names are compared as written, so `_ID` and `_Label` are ordinary columns. A name with a `.` is a path (V18). - **V7.** The file is the root node (C14). Each record after the header is a node, a child of the root, in file order. - **V3.** The **label column** is the first column named `_label`, or the column that the file's setting names (V16). Without either, it is the first column whose name doesn't start with `_`, so a file that starts with `_id` doesn't take its IDs as labels; when every name starts with `_`, there is none. Every other column is a field. - **V8.** The label column's cell is the node's label, as written, and Part 2 reads it as it reads a label in a text file: `Send draft +task` has the type `task`, `[[Anna]]` is a link and `@ Anna` a mirror. The label is quoted (Spec, section 1) only where a writer would quote it (C26) or drop spaces or tabs at its end (C34), so it means in the model what it would mean written as a line; a label that is a block (C32) isn't quoted. An empty cell, or no label column, gives an empty label that is quoted, so a writer writes it `""` (C42). - **V9.** Every other column gives each node a field, in column order, whose key is the column name, unless the node's cell is empty: an empty cell, quoted or not, gives no field. The field's value is the cell, read as a field line's value in a text file (Spec, section 9, and Part 2): split into items at `;`, with spans, quotes, value triggers, mirror values and value forms as there. So `Anna; Erik` is two items, `design;` a list, `"x; y"` one quoted item, `[[Acme Corp]]` a link, `@anna` a mirror value, `https://example.com` a URL, `=(hours * rate)` a formula and `2026-10-10` a date. - **V18.** A column name with a `.` is a **path**: the text between the dots are keys, and the column gives the fields they name, each under the one before it as its field children (C13). So `address.city` gives the field `address` with `city` under it, holding the cell's value. Columns whose paths start alike share their fields: `address.street` and `address.city` give one `address` with both under it, at the place of the first. A part after the first that is digits only, without a leading zero, is a number n, and picks the nth field line with the key before it, counting from 1: `lines.1.name` and `lines.2.name` give two `lines` field lines, each with a `name`, and `lines.1` gives the first one's own items. `\.` in a name is a `.` in a key, and `\` before a part that is digits only makes it a key: `v1\.2` is the key `v1.2`, and `year.\2026` gives `2026` under `year`. A field whose cells are all empty is left out, with the fields above it that are left with nothing under them. - **V11.** A record with fewer cells than the header gets empty cells for the rest. A cell after the last column gives a field whose key is its position, counting from 1, in digits: the fourth cell of a record under a header of three columns gives `4`. A second `_label` column is an ordinary field, a column whose name is empty is left out, and a column with the same name as a column before it gives fields of its own, so a node can have that key twice. Each of these gives a warning. ```example from-csv V3 V7 V8 V9 _id,Name,Email,tags a1,Anna,anna@x.se,design; review ,Erik,,design; . root node "Anna" field "_id" "a1" field "Email" "anna@x.se" field "tags" "design" "review" node "Erik" field "tags" list "design" ``` ```example from-csv labels V8 V9 _label,client,site,host,total,due Send draft +task,[[Acme]],https://acme.com,@anna,=(hours * rate),2026-10-10 . root node "Send draft +task" label "Send draft" field "_types" inline "task" field "client" "[[Acme]]" _link field "site" "https://acme.com" _url field "host" "@anna" _mirror field "total" "=(hours * rate)" _result field "due" "2026-10-10" date ``` ```example from-csv V8 _label Plain Send draft +task \#hashtag "Trailing␣" Ends with colons:: "" . root node "Plain" node "Send draft +task" node "\\#hashtag" node quoted "Trailing " node quoted "Ends with colons::" node quoted "" ``` ```example from-csv V5 V6 V11 V18 _label; tag ;tag;address.city;lines.1.name;lines.2.name Kickoff;a;b;Lund;Workshop;Review Retro;;;;;;extra . root node "Kickoff" field "tag" "a" field "tag" "b" field "address" node field "city" "Lund" field "lines" node field "name" "Workshop" field "lines" node field "name" "Review" node "Retro" field "7" "extra" ! V6.1 1:8 ! V11.5 1:14 ! V11.2 3:11 ``` ```example from-csv V5 V9 V11 _label,tags "Say ""hi""","a; b" Short row x,y,extra . root node "Say \"hi\"" field "tags" "a" "b" node "Short row" node "x" field "tags" "y" field "3" "extra" ! V11.1 3:1 ! V11.2 4:4 ``` ```example from-csv V5 V25 _label→note→tags Kickoff→"two→cells, ""quoted"""→a; b "Multi line"→x→ . root node "Kickoff" field "note" "two\tcells, \"quoted\"" field "tags" "a" "b" node quoted "Multi\nline" field "note" "x" ``` ## 4. Writing CSV - **V12.** Any model can be written as CSV, except one where a field has a node item with child lines, such as `attendees::` with `Anna` under it and `role:: host` under that. A writer rejects that model. - **V19.** Each node is a row, in file order, a node's row before the rows of the nodes under it, and the root has no row (V21). A node under another node gets a `_parent` cell with that node's ID. A node with nodes under it and no ID gets one, written in its `_id` cell: `r` and its row's number, counting the rows after the header from 1, such as `r2`, or, when that is taken in the model, that and `-` and the smallest number from 2 that makes it unique. A mirror (L30) is a row like any other, with its label, so `@ Anna` reads back as a mirror (V8). - **V20.** A node's label is written without the forms that Part 2 reads from it into these fields (L19), which go in its cells as those fields' items: its ID (`^s1`) in `_id`, its types (`+task`) in `_types`, its state (`[ ]`, `[-]`, `[?]`, `[x]` or a custom marker) in `_state`, as the state it reads as (L11): `new`, `in-progress`, `waiting`, `done`, or the marker, the tags at the end of its name (M11) in `_tags`, and its inline fields (L5), `[#Ord notes]` included (L29), in columns of their keys. Runs of spaces and tabs left where a form was are made one space, and none are left at the label's start and end. The other forms stay in the label: links, URLs, mirrors, formulas, and tags elsewhere in it, such as `#budget` in `Call #budget soon`. - **V13.** The header has a `_label` column first, when any node has a label that isn't empty after V20, and then each column the rows have, in the order it first appears, going through the rows in order and each row's columns in this order: `_id`, the root's fields (V21), the fields from the label in the order written, the node's fields in order, and `_parent`. A writer that rewrites a file of the workspace whose label column is set (V16) writes that column's name in place of `_label`. - **V14.** A node's label is written as it is after V20, without Ord quotes. A field's cell is its items, written as in a field line (C43), separated by `;` and a space: its text items, quoted with `"` where C26 says, and its node items, which have no child lines (V12), each as a text item of its label. A list with one item gets `;` after it (C16), so `tags:: design;` gives `design;`, and a list without items gives `;`. Fields with the same key on one node share a cell, their items in order, unless they have field children (V24). A key the node doesn't have, and a field without items that isn't a list, give an empty cell, which reads back as no field (V9). A formula is written as written, so it reads back as a formula; a tool may offer an export with each formula's result in its place, as text (M39), for spreadsheets, which read a cell that starts with `=` as a formula of their own. - **V24.** A field's field children (C13) are written as path columns (V18): each field under it is a column whose name is the field's key, `.` and its key, at any depth, so `address::` with `city:: Lund` under it gives the column `address.city`. Where a node has more than one field line with a key and one of them has field children, the field lines are numbered after the key, counting from 1: `lines.1.name`, `lines.2.name`, and `lines.1` for the first one's own items. In a column name, a `.` in a key is written `\.`, and a key after the first that is digits only gets `\` before it. - **V21.** The root's fields are written in every row, as if each node had them, after its `_id` (V13). A system field (M11) or reserved key (M92) of the root isn't, since every row would share an ID or a type: it is left out, with the warning `V21.1`. A root that has fields and no nodes is one row of its fields, without a `_label` column. - **V22.** A field `_label`, in any form other than the label itself, is left out, with the warning `V22.1`, since the label column holds the label. - **V15.** A cell that holds the delimiter, `"`, `\r` or `\n` is written in `"` quotes, with each `"` doubled; so a label that is a block is one cell over several lines. Every record ends with a line ending. A new file has `,` between cells, a tab in a `.tsv` file, `\n` line endings and no BOM, except that an export for spreadsheets starts with a BOM, which they need to read UTF-8. An edit keeps the file's delimiter, line ending and BOM, as C45 says for text. ```example to-csv V13 V19 V20 V21 client:: Acme Acme ^acme →[x] Planning +task #urgent →→Book room [due:: 2026-12-01] →Call [[Anna]] about #budget soon . _label,_id,client,_state,_types,_tags,_parent,due Acme,acme,Acme,,,,, Planning,r2,Acme,done,task,urgent,acme, Book room,,Acme,,,,r2,2026-12-01 Call [[Anna]] about #budget soon,,Acme,,,,acme, ``` ```example to-csv V12 V13 V14 V15 V24 Kickoff →attendees:: →→Anna →→Erik →tags:: design; →note:: "x; y" →tag:: a →tag:: b →address:: →→city:: Lund →v1.2:: x →total:: =(hours * rate) →empty:: Invoice 1 →lines:: →→name:: Workshop →→hours:: 6 →lines:: →→name:: Review . _label,attendees,tags,note,tag,address.city,v1\.2,total,empty,lines.1.name,lines.1.hours,lines.2.name Kickoff,Anna; Erik,design;,"""x; y""",a; b,Lund,x,=(hours * rate),,,, Invoice 1,,,,,,,,,Workshop,6,Review ``` ```example to-csv V21 name:: Anna email:: anna@x.se . name,email Anna,anna@x.se ``` ## 5. Converting CSV to Ord - **V23.** A tool that converts a CSV file to a text file writes the model that section 3 reads (Spec, section 10), with these changes: - A row whose `_parent` cell has one item, the `_id` of one other row of the file, is written under that row, after its field lines and the rows written under it before, and its `_parent` field is left out. Rows in a loop of such cells (M15), and rows whose `_parent` names anything else, stay where they are and keep the field. - The fields that V20 writes from the label are written back in it, and left out as fields: a `_state` that is a built-in state (M26) as its marker at its start, after any list marker (L16): `new` as `[ ]`, `in-progress` as `[-]`, `waiting` as `[?]` and `done` as `[x]`; `_types` items that are names (L3) as `+type`; `_tags` items as `#tag`, or `[#tag]` when they aren't names (L29); and an `_id` that is a name as `^id`, in that order, after the label, each with a space before it. Inline fields come back as field lines. ```example csv-to-ord V23 _id,_label,_state,_types,_tags,_parent,due acme,Acme,,,,, r2,Planning,done,task,urgent,acme, ,Book room,,,,r2,2026-12-01 ,Draft,in-progress,,,, ,Reply,waiting,,,, ,Lone,,,,elsewhere, . Acme ^acme →[x] Planning +task #urgent ^r2 →→Book room →→→due:: 2026-12-01 [-] Draft [?] Reply Lone →_parent:: elsewhere ``` ## 6. Shapes compared | Ord | CSV | |---|---| | a node with fields | a row, a column per key | | `tags:: a; b`, or `a` and `b` as lines under `tags::` | the cell `a; b` | | `tags:: a;` | the cell `a;` | | `Send draft +task #urgent [x] ^s1` | `_label` `Send draft`, and `_types`, `_tags`, `_state` and `_id` cells | | `[ ]`, `[-]`, `[?]`, `[x]` | `_state` `new`, `in-progress`, `waiting`, `done` | | a node under another node | a row with a `_parent` cell holding the other's `_id` | | `address::` with `city:: Lund` under it | the column `address.city` | | a field line per record, `lines::` with fields under it, twice | the columns `lines.1.name`, `lines.2.name` | | fields on the root, beside nodes | the same cells in every row | | fields on the root, alone | one row without `_label` | ## 7. Known gaps What Ord → CSV → Ord loses: - A field without items that isn't a list comes back as no field (V9, V14). - Fields with the same key on one node come back as one field, unless they have field children (V24). - A node's fields come back in column order, and a field's node items as inline items. - Inline fields come back as field lines, and a label's state, types, end tags and ID at fixed places in it (V23). - IDs a writer gives to nodes with nodes under them come back (V19). - The root's fields come back on every node, without its system fields, and a root with only fields comes back as one node (V21). - A field `_label` doesn't round-trip (V22). - A node item with child lines can't be written (V12). - A formula exported as its result comes back as text (V14). What CSV → Ord → CSV loses: - Cells are read as field values, so `;` splits them into items, and `a;b` comes back as `a; b`. - A column whose name repeats an earlier one is merged with it, and cells after the last column get a column named by their position (V11). - A file read as Windows-1252 comes back as UTF-8 (V4). ## 8. Diagnostics Codes and severities work as in the Spec (C35 to C37). Positions are in the input: the CSV text when reading CSV, the Ord file when writing CSV, and the workspace file for settings. Lines and columns are counted as in the Spec (D9). | Code | Severity | Given when | Position | |---|---|---|---| | `V4.1` | warning | The file isn't valid UTF-8, and is read as Windows-1252 | 1:1 | | `V5.1` | warning | A cell that isn't quoted holds a `"` | the `"` | | `V5.2` | warning | Text follows a quoted cell's closing `"` | the first character after it | | `V5.3` | warning | A quoted cell is never closed | its opening `"` | | `V6.1` | warning | A column's name starts or ends with spaces or tabs | the cell's first character | | `V11.1` | warning | A record has fewer cells than the header | the record's first character | | `V11.2` | warning | A record has more cells than the header | the delimiter before the first extra cell | | `V11.3` | warning | The header has a second `_label` column | that column's name | | `V11.4` | warning | A column's name is empty | the delimiter before it, or 1:1 for the first column | | `V11.5` | warning | A column has the same name as a column before it | the later name | | `V16.1` | warning | A setting names a column the header doesn't have, or another delimiter | the setting's value | | `V21.1` | warning | A system field or reserved key on the root is left out of the rows | the field's key | | `V22.1` | warning | A field `_label` is left out | the field's key |