/* * splinter -- model markdown as Telegram HTML. * * The chat used to show "**fett**", "|---|" and "" as characters. These * cases pin what has to become a tag, what has to stay text (snake_case, a bare * '<'), and how a long answer is split so that every piece parses on its own. */ #include #include #include "splinter.h" #include "tgmd.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static void check_render(const char *md, const char *want) { struct sp_buf b; if (sp_buf_init(&b) != 0) { fprintf(stderr, "FAIL init\n"); failures++; return; } if (sp_tgmd_render(md, &b) != 0) { fprintf(stderr, "FAIL render of:\n%s\n", md); failures++; } else if (b.p == NULL || strcmp(b.p, want) != 0) { fprintf(stderr, "FAIL %s:%d:\n--- in ---\n%s--- want ---\n%s" "--- got ---\n%s\n", __FILE__, __LINE__, md, want, b.p != NULL ? b.p : "(null)"); failures++; } sp_buf_free(&b); } /* tag names opened and closed in 's', in order: 1 when every tag balances */ static int balanced(const char *s) { char stack[32][16]; size_t depth = 0; size_t i; for (i = 0; s[i] != '\0'; i++) { char name[16]; size_t n = 0, j; if (s[i] != '<') continue; j = i + 1; if (s[j] == '/') { j++; while (s[j] != '\0' && s[j] != '>') name[n++] = s[j++]; name[n] = '\0'; if (depth == 0 || strcmp(stack[depth - 1], name) != 0) return 0; depth--; i = j; continue; } while (s[j] != '\0' && s[j] != '>' && s[j] != ' ') name[n++] = s[j++]; name[n] = '\0'; if (n == 0) continue; if (depth == sizeof(stack) / sizeof(stack[0])) return 0; snprintf(stack[depth++], sizeof(stack[0]), "%s", name); i = j; } return depth == 0; } /* the text between tags, with the entities the renderer writes undone */ static int plain_into(struct sp_buf *out, const char *s) { size_t i; for (i = 0; s[i] != '\0'; i++) { if (s[i] == '<') { const char *gt = strchr(s + i, '>'); if (gt == NULL) return -1; i = (size_t)(gt - s); continue; } if (strncmp(s + i, "&", 5) == 0) { if (sp_buf_puts(out, "&") != 0) return -1; i += 4; } else if (strncmp(s + i, "<", 4) == 0) { if (sp_buf_puts(out, "<") != 0) return -1; i += 3; } else if (strncmp(s + i, ">", 4) == 0) { if (sp_buf_puts(out, ">") != 0) return -1; i += 3; } else if (sp_buf_add(out, s + i, 1) != 0) { return -1; } } return 0; } static void check_chunks(void) { struct sp_buf md, html, piece, all, back; struct sp_tgmd_cursor cur; size_t i; int n, pieces = 0; CHECK(sp_buf_init(&md) == 0); CHECK(sp_buf_init(&html) == 0); CHECK(sp_buf_init(&piece) == 0); CHECK(sp_buf_init(&all) == 0); CHECK(sp_buf_init(&back) == 0); for (i = 0; i < 120; i++) CHECK(sp_buf_printf(&md, "**Zeile %zu** mit `code` und einem & mehr Text, " "damit die Antwort lang genug fuer mehrere Nachrichten ist.\n", i) == 0); CHECK(sp_tgmd_render(md.p, &html) == 0); CHECK(sp_tgmd_cursor_init(&cur, html.p) == 0); while ((n = sp_tgmd_next(&cur, 1000, &piece)) > 0) { pieces++; CHECK(piece.len > 0); /* a piece is what Telegram gets: it has to fit and to parse */ CHECK(piece.len < 4096); CHECK(balanced(piece.p)); CHECK(plain_into(&all, piece.p) == 0); } CHECK(n == 0); CHECK(pieces > 1); CHECK(plain_into(&back, html.p) == 0); CHECK(all.p != NULL && back.p != NULL && strcmp(all.p, back.p) == 0); sp_tgmd_cursor_free(&cur); sp_buf_free(&md); sp_buf_free(&html); sp_buf_free(&piece); sp_buf_free(&all); sp_buf_free(&back); } /* A link whose tag alone is longer than a piece once looped forever. */ static void check_huge_link(void) { struct sp_buf md, html, piece; struct sp_tgmd_cursor cur; size_t i; int n, pieces = 0; CHECK(sp_buf_init(&md) == 0); CHECK(sp_buf_init(&html) == 0); CHECK(sp_buf_init(&piece) == 0); CHECK(sp_buf_puts(&md, "vorher **[x](https://example.org/") == 0); for (i = 0; i < 4000; i++) CHECK(sp_buf_puts(&md, "a") == 0); CHECK(sp_buf_puts(&md, ")** nachher") == 0); CHECK(sp_tgmd_render(md.p, &html) == 0); CHECK(sp_tgmd_cursor_init(&cur, html.p) == 0); while ((n = sp_tgmd_next(&cur, 1000, &piece)) > 0 && pieces < 100) { pieces++; CHECK(piece.len < 4096); CHECK(balanced(piece.p)); } CHECK(n == 0); CHECK(pieces < 100); sp_tgmd_cursor_free(&cur); sp_buf_free(&md); sp_buf_free(&html); sp_buf_free(&piece); } /* * The contract: a piece is either text or the end. Handing back 1 with * nothing in it and the cursor where it was makes the sender's * `while (sp_tgmd_next(...) > 0)` spin for ever - an entity that starts at a * piece boundary ("&" with no ";" behind it) did exactly that. */ static void check_no_stall(void) { static const char *const in[] = { "&", "&a", "&aaaa", "&", "&", "x&", "&", "&;", "&x", "&#", "&#x", "text &", "& text" }; size_t i; for (i = 0; i < sizeof(in) / sizeof(in[0]); i++) { struct sp_tgmd_cursor cur; struct sp_buf piece; size_t limit; int guard; for (limit = 256; limit <= 300; limit += 44) { CHECK(sp_buf_init(&piece) == 0); CHECK(sp_tgmd_cursor_init(&cur, in[i]) == 0); for (guard = 0; guard < 200; guard++) { size_t before = cur.pos; int r; sp_buf_clear(&piece); r = sp_tgmd_next(&cur, limit, &piece); if (r != 1) break; CHECK(cur.pos > before || piece.len > 0); } CHECK(guard < 200); CHECK(cur.pos == cur.len); sp_tgmd_cursor_free(&cur); sp_buf_free(&piece); } } } int main(void) { /* inline: the shapes a model actually writes */ check_render("**fett**", "fett"); check_render("__fett__", "fett"); check_render("*kursiv*", "kursiv"); check_render("_kursiv_", "kursiv"); check_render("~~weg~~", "weg"); check_render("ein `code` hier", "ein code hier"); check_render("**fett mit *kursiv***", "fett mit kursiv"); check_render("[Titel](https://example.org/a?x=1&y=2)", "Titel"); check_render("", "https://example.org/x"); check_render("![Bild](https://example.org/b.png)", "Bild"); /* what must stay text */ check_render("snake_case_name", "snake_case_name"); check_render("2 * 3 * 4 = 24", "2 * 3 * 4 = 24"); check_render("a < b & c > d", "a < b & c > d"); check_render("", "<script>x</script>"); check_render("[kein link](javascript:alert(1))", "[kein link](javascript:alert(1))"); check_render("* nicht fett", "\xe2\x80\xa2 nicht fett"); /* blocks */ check_render("## Titel\nText\n", "Titel\nText\n"); check_render("> zitat\n> mehr\n", "
zitat\nmehr\n
\n"); check_render("---\n", "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\n"); check_render("- eins\n- zwei\n", "\xe2\x80\xa2 eins\n\xe2\x80\xa2 zwei\n"); check_render("```c\nif (a < b)\n\treturn **x**;\n```\n", "
if (a < b)\n\treturn **x**;"
	    "
\n"); check_render("```\nplain & simple\n```\n", "
plain & simple
\n"); /* a table: columns in
, not a row of pipes in a proportional font */
	check_render("| a | b |\n|---|---|\n| 1 | 22 |\n",
	    "
| a | b  |\n|---|----|\n| 1 | 22 |\n
\n"); check_chunks(); check_huge_link(); check_no_stall(); if (failures == 0) printf("test_tgmd: ok\n"); return failures == 0 ? 0 : 1; }