soma.cell

CSV to Markdown table — render rows as a pipe-delimited table

Turns a CSV body into a GitHub-flavored Markdown table: a header row, a separator row of dashes, then one row per data line. Demonstrates splitting on newlines and commas, and assembling lines with join + interpolation. Run: soma run csv_to_markdown.cell --signal to_table "name,age\nAlice,30"

A Soma program from the text part of the verified corpus: 49 lines, cells CsvToMarkdown, CsvToMarkdownTest. It passes soma check with soma 3.2.4.

How it is built

The cell CsvToMarkdown declares 1 signal in its face: to_table(body: String) -> String. Handlers: to_table (line 21). Private helpers: _row.

The file also holds 1 test cell (CsvToMarkdownTest) with 4 assertions, run by soma test.

Generated from soma describe on this file.

The source

csv_to_markdown.cell

// ============================================================================
// CSV to Markdown table — render rows as a pipe-delimited table
// ============================================================================
//
// Turns a CSV body into a GitHub-flavored Markdown table: a header row, a
// separator row of dashes, then one row per data line. Demonstrates splitting
// on newlines and commas, and assembling lines with join + interpolation.
//
// Run: soma run csv_to_markdown.cell --signal to_table "name,age\nAlice,30"

cell CsvToMarkdown {
    face {
        signal to_table(body: String) -> String
    }

    // Wrap a list of cells as "| a | b | c |".
    on _row(cells: List<String>) {
        return "| " + join(cells, " | ") + " |"
    }

    on to_table(body: String) {
        let lines = split(body, "\n") |> filter(l => len(l) > 0)
        if len(lines) == 0 { return "" }
        let headers = split(lines[0], ",")
        let out = list(_row(headers))

        // separator row: one "---" per column
        let seps = list()
        for c in range(0, len(headers)) {
            seps = push(seps, "---")
        }
        out = push(out, _row(seps))

        for r in range(1, len(lines)) {
            out = push(out, _row(split(lines[r], ",")))
        }
        return join(out, "\n")
    }
}

cell test CsvToMarkdownTest {
    rules {
        assert to_table("a,b\n1,2") == "| a | b |\n| --- | --- |\n| 1 | 2 |"
        assert to_table("x\n9\n8") == "| x |\n| --- |\n| 9 |\n| 8 |"
        assert contains(to_table("name,age\nAlice,30"), "| Alice | 30 |")
        assert to_table("") == ""
    }
}

What the toolchain says

Run on a fresh copy of the program at build time; the outputs are pasted unedited.

$ soma check csv_to_markdown.cell
✓ All checks passed.
$ soma test csv_to_markdown.cell
test CsvToMarkdownTest ...
  ✓ assert to_table("a,b\n1,2") == "| a | b |\n| --- | --- |\n| 1 | 2 |"
  ✓ assert to_table("x\n9\n8") == "| x |\n| --- |\n| 9 |\n| 8 |"
  ✓ assert contains(to_table("name,age\nAlice,30"), "| Alice | 30 |")
  ✓ assert to_table("") == ""

4 tests: 4 passed, 0 failed
$ soma verify csv_to_markdown.cell
Verifying csv_to_markdown.cell...
No state machine in this program: nothing to prove beyond `soma check` (invariants and lifecycles are what verify proves).
VERIFY OK (vacuous — no state machine)

Builtins used

contains, filter, join, len, list, push, range, split

Source file: /corpus/text/csv_to_markdown.cell · examples/corpus/text/csv_to_markdown.cell