Markdown tables are drawn as columns now (src/md.c, test_md, and a case in test_ui.sh that checks the screen). The site counts lines, tools, tests, plugins and palettes from the tree instead of naming them; the binary-size figure is gone because it aged with every build. The delegate panel cycle has a test (host + tasks plugin, widget says delegate:panel exit 0), and the turn loop is driven with lcm at 8 KiB and 32 KiB budgets, so compaction is measured rather than claimed. swarm, provstate and planner get smoke tests. TODO.md is rewritten against the tree: the step limit was the old default (8 -> 150), skills and memory were already built, and the open points are claude exit 2, a real Telegram approval, A2A, the npm leftovers and the scheduler/job tests.
363 lines
7.4 KiB
C
363 lines
7.4 KiB
C
/*
|
|
* splinter -- markdown tables as aligned text.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* A chat is monospace, so a table can be a table: the pipe rows are rewritten
|
|
* with one width per column, taken from the longest cell. The block is the
|
|
* markdown shape a model actually emits -- a header line, a separator line
|
|
* ("|---|:---:|---|"), then body rows -- and it ends at the first line that is
|
|
* not a row. Text before and after the block is copied unchanged.
|
|
*
|
|
* This is deliberately not a full markdown parser. Escaped pipes are not
|
|
* supported (a table cell with a literal '|' is rare; a parser that guesses is
|
|
* worse than one that leaves the line alone), and alignment is in bytes, which
|
|
* is exact for ASCII. The screen transliterates UTF-8 before drawing, so a cell
|
|
* full of umlauts can still be a column short; that is a display detail, not a
|
|
* reason to turn the table back into pipes.
|
|
*/
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "splinter.h"
|
|
#include "md.h"
|
|
|
|
#define MD_MAX_COLS 32
|
|
#define MD_MAX_ROWS 512
|
|
|
|
struct md_row {
|
|
size_t n;
|
|
char **cell;
|
|
};
|
|
|
|
static const char *
|
|
eol(const char *s)
|
|
{
|
|
const char *p = strchr(s, '\n');
|
|
|
|
return p != NULL ? p : s + strlen(s);
|
|
}
|
|
|
|
/* A separator line: every cell has at least three dashes, optional colons. */
|
|
static int
|
|
is_separator(const char *s, size_t n)
|
|
{
|
|
size_t i = 0, pipes = 0, cells = 0;
|
|
|
|
while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\r'))
|
|
i++;
|
|
if (i < n && s[i] == '|') {
|
|
i++;
|
|
pipes++;
|
|
}
|
|
for (;;) {
|
|
size_t dashes = 0;
|
|
|
|
while (i < n && (s[i] == ' ' || s[i] == '\t'))
|
|
i++;
|
|
if (i < n && s[i] == ':')
|
|
i++;
|
|
while (i < n && s[i] == '-') {
|
|
i++;
|
|
dashes++;
|
|
}
|
|
if (dashes < 3)
|
|
return 0;
|
|
if (i < n && s[i] == ':')
|
|
i++;
|
|
while (i < n && (s[i] == ' ' || s[i] == '\t'))
|
|
i++;
|
|
cells++;
|
|
if (i < n && s[i] == '|') {
|
|
i++;
|
|
pipes++;
|
|
while (i < n && (s[i] == ' ' || s[i] == '\t'))
|
|
i++;
|
|
if (i >= n || s[i] == '\r')
|
|
break;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\r'))
|
|
i++;
|
|
return i == n && cells > 0 && pipes > 0;
|
|
}
|
|
|
|
static void
|
|
row_free(struct md_row *r)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < r->n; i++)
|
|
free(r->cell[i]);
|
|
free(r->cell);
|
|
r->cell = NULL;
|
|
r->n = 0;
|
|
}
|
|
|
|
/*
|
|
* Split one line into cells: trim the line, drop one leading and one trailing
|
|
* pipe, then split on '|'. A trailing pipe does not create an empty last cell.
|
|
*/
|
|
static int
|
|
row_parse(const char *s, size_t n, struct md_row *r)
|
|
{
|
|
size_t i = 0, cap = 0;
|
|
|
|
r->n = 0;
|
|
r->cell = NULL;
|
|
while (n > 0 && (s[n - 1] == ' ' || s[n - 1] == '\t' ||
|
|
s[n - 1] == '\r'))
|
|
n--;
|
|
while (i < n && (s[i] == ' ' || s[i] == '\t'))
|
|
i++;
|
|
if (i < n && s[i] == '|')
|
|
i++;
|
|
for (;;) {
|
|
size_t start = i, end;
|
|
|
|
while (i < n && s[i] != '|')
|
|
i++;
|
|
end = i;
|
|
while (end > start && (s[end - 1] == ' ' ||
|
|
s[end - 1] == '\t'))
|
|
end--;
|
|
while (start < end && (s[start] == ' ' || s[start] == '\t'))
|
|
start++;
|
|
if (r->n == MD_MAX_COLS)
|
|
goto fail;
|
|
if (r->n == cap) {
|
|
size_t ncap = cap == 0 ? 4 : cap * 2;
|
|
char **nc = realloc(r->cell, ncap * sizeof(*nc));
|
|
|
|
if (nc == NULL)
|
|
goto fail;
|
|
r->cell = nc;
|
|
cap = ncap;
|
|
}
|
|
r->cell[r->n] = malloc(end - start + 1);
|
|
if (r->cell[r->n] == NULL)
|
|
goto fail;
|
|
memcpy(r->cell[r->n], s + start, end - start);
|
|
r->cell[r->n][end - start] = '\0';
|
|
r->n++;
|
|
if (i < n && s[i] == '|') {
|
|
i++;
|
|
if (i >= n)
|
|
break;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
return 0;
|
|
|
|
fail:
|
|
row_free(r);
|
|
return -1;
|
|
}
|
|
|
|
static int
|
|
row_push(struct md_row **rows, size_t *n, size_t *cap, const char *s,
|
|
size_t len)
|
|
{
|
|
struct md_row *nr;
|
|
|
|
if (*n == MD_MAX_ROWS)
|
|
return -1;
|
|
if (*n == *cap) {
|
|
size_t ncap = *cap == 0 ? 4 : *cap * 2;
|
|
|
|
nr = realloc(*rows, ncap * sizeof(**rows));
|
|
if (nr == NULL)
|
|
return -1;
|
|
*rows = nr;
|
|
*cap = ncap;
|
|
}
|
|
memset(&(*rows)[*n], 0, sizeof(**rows));
|
|
if (row_parse(s, len, &(*rows)[*n]) != 0)
|
|
return -1;
|
|
(*n)++;
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
rows_free(struct md_row *rows, size_t n)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; i++)
|
|
row_free(&rows[i]);
|
|
free(rows);
|
|
}
|
|
|
|
/*
|
|
* Format the block that starts at 'p' (a header line followed by a separator)
|
|
* and return the first line after it. NULL means "leave it alone": too many
|
|
* rows or columns, or no memory.
|
|
*/
|
|
static const char *
|
|
format_table(const char *p, struct sp_buf *out)
|
|
{
|
|
struct md_row *rows = NULL;
|
|
size_t nrows = 0, cap = 0, ncols = 0, i, j;
|
|
size_t width[MD_MAX_COLS];
|
|
const char *e = eol(p);
|
|
const char *sep = (*e == '\n') ? e + 1 : e;
|
|
const char *se = eol(sep);
|
|
const char *cur = (*se == '\n') ? se + 1 : se;
|
|
|
|
memset(width, 0, sizeof(width));
|
|
if (row_push(&rows, &nrows, &cap, p, (size_t)(e - p)) != 0)
|
|
goto fail;
|
|
while (*cur != '\0') {
|
|
const char *ce = eol(cur);
|
|
size_t clen = (size_t)(ce - cur);
|
|
|
|
if (clen == 0 || memchr(cur, '|', clen) == NULL)
|
|
break;
|
|
if (row_push(&rows, &nrows, &cap, cur, clen) != 0)
|
|
goto fail;
|
|
if (*ce == '\0') {
|
|
cur = ce;
|
|
break;
|
|
}
|
|
cur = ce + 1;
|
|
}
|
|
if (nrows == 0)
|
|
goto fail;
|
|
for (i = 0; i < nrows; i++) {
|
|
if (rows[i].n > ncols)
|
|
ncols = rows[i].n;
|
|
for (j = 0; j < rows[i].n; j++) {
|
|
size_t l = strlen(rows[i].cell[j]);
|
|
|
|
if (l > width[j])
|
|
width[j] = l;
|
|
}
|
|
}
|
|
if (ncols == 0 || ncols > MD_MAX_COLS)
|
|
goto fail;
|
|
|
|
for (i = 0; i < nrows; i++) {
|
|
if (sp_buf_puts(out, "|") != 0)
|
|
goto fail;
|
|
for (j = 0; j < ncols; j++) {
|
|
const char *c = j < rows[i].n ? rows[i].cell[j] : "";
|
|
size_t l = strlen(c), k;
|
|
|
|
if (sp_buf_puts(out, " ") != 0 ||
|
|
sp_buf_puts(out, c) != 0)
|
|
goto fail;
|
|
for (k = l; k < width[j]; k++)
|
|
if (sp_buf_puts(out, " ") != 0)
|
|
goto fail;
|
|
if (sp_buf_puts(out, " |") != 0)
|
|
goto fail;
|
|
}
|
|
if (sp_buf_puts(out, "\n") != 0)
|
|
goto fail;
|
|
if (i == 0) {
|
|
if (sp_buf_puts(out, "|") != 0)
|
|
goto fail;
|
|
for (j = 0; j < ncols; j++) {
|
|
size_t k;
|
|
|
|
for (k = 0; k < width[j] + 2; k++)
|
|
if (sp_buf_puts(out, "-") != 0)
|
|
goto fail;
|
|
if (sp_buf_puts(out, "|") != 0)
|
|
goto fail;
|
|
}
|
|
if (sp_buf_puts(out, "\n") != 0)
|
|
goto fail;
|
|
}
|
|
}
|
|
rows_free(rows, nrows);
|
|
return cur;
|
|
|
|
fail:
|
|
rows_free(rows, nrows);
|
|
return NULL;
|
|
}
|
|
|
|
static const char *
|
|
next_table(const char *in)
|
|
{
|
|
const char *p = in;
|
|
|
|
while (*p != '\0') {
|
|
const char *e = eol(p);
|
|
|
|
if (*e == '\n' && memchr(p, '|', (size_t)(e - p)) != NULL) {
|
|
const char *q = e + 1;
|
|
const char *qe = eol(q);
|
|
|
|
if (is_separator(q, (size_t)(qe - q)))
|
|
return p;
|
|
}
|
|
if (*e == '\0')
|
|
break;
|
|
p = e + 1;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int
|
|
sp_md_table_starts(const char *in)
|
|
{
|
|
const char *e, *q, *qe;
|
|
|
|
if (in == NULL)
|
|
return 0;
|
|
e = eol(in);
|
|
if (*e != '\n')
|
|
return 0;
|
|
q = e + 1;
|
|
qe = eol(q);
|
|
return memchr(in, '|', (size_t)(e - in)) != NULL &&
|
|
is_separator(q, (size_t)(qe - q));
|
|
}
|
|
|
|
int
|
|
sp_md_table_format(const char *in, struct sp_buf *out)
|
|
{
|
|
const char *p;
|
|
int changed = 0;
|
|
|
|
if (in == NULL || out == NULL)
|
|
return -1;
|
|
if (next_table(in) == NULL)
|
|
return 0;
|
|
p = in;
|
|
while (*p != '\0') {
|
|
const char *e = eol(p);
|
|
|
|
if (*e == '\n' && memchr(p, '|', (size_t)(e - p)) != NULL) {
|
|
const char *q = e + 1;
|
|
const char *qe = eol(q);
|
|
|
|
if (is_separator(q, (size_t)(qe - q))) {
|
|
const char *after = format_table(p, out);
|
|
|
|
if (after == NULL)
|
|
return -1;
|
|
changed = 1;
|
|
p = after;
|
|
continue;
|
|
}
|
|
}
|
|
if (sp_buf_add(out, p, (size_t)(e - p)) != 0)
|
|
return -1;
|
|
if (*e == '\0') {
|
|
p = e;
|
|
break;
|
|
}
|
|
if (sp_buf_add(out, "\n", 1) != 0)
|
|
return -1;
|
|
p = e + 1;
|
|
}
|
|
return changed;
|
|
}
|