Adiciona tinyblocks-syntax, tinyblocks-regex e tinyblocks-markdown (com testes)

This commit is contained in:
mguinhos committed 2026-10-20 22:04:09 -03:00
1 parent c75d4be1c2
commit a921f6574d
16 files changed
+1846 -4

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
# Makefile da library markdown (categoria "Markdown")
CC := gcc
BUILD := build
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude
OBJ := $(BUILD)/tb_markdown.o
LIB_A := $(BUILD)/libtinyblocks-markdown.a
LIB_SO := $(BUILD)/libtinyblocks-markdown.so
.PHONY: all static shared clean
all: static shared
static: $(LIB_A)
shared: $(LIB_SO)
$(OBJ): src/tb_markdown.c include/tb_markdown.h
@mkdir -p $(BUILD)
$(CC) $(CFLAGS) -fPIC -c $< -o $@
$(LIB_A): $(OBJ)
ar rcs $@ $^
$(LIB_SO): $(OBJ)
$(CC) -shared -o $@ $^
clean:
$(RM) -r $(BUILD)
@@ -0,0 +1,23 @@
/*
* TinyBlocks - tb_markdown.h
* Library tinyblocks-markdown: converte Markdown para HTML, XML ou SVG.
* Independente do editor.
*/
#ifndef TINYBLOCKS_MARKDOWN_H
#define TINYBLOCKS_MARKDOWN_H
#include <tinyblocks/tinyblocks.h>
TB_API const TinyBlocks_LibraryDescriptor* tb_markdown_get_library(void);
/* -- API C (resultados com malloc; libere com free) ---------------------- */
TB_API char* tb_markdown_to_html(const char* md);
TB_API char* tb_markdown_to_xml(const char* md);
TB_API char* tb_markdown_to_svg(const char* md);
/* Variante do SVG com largura/fonte configuraveis. */
TB_API char* tb_markdown_to_svg_opts(const char* md, int width, int font_size);
TB_API const char* tb_markdown_last_error(void);
#endif /* TINYBLOCKS_MARKDOWN_H */
@@ -0,0 +1,15 @@
# Metadados da library markdown. Lido pela UI/build sem recompilar o .so.
[library]
name = "tinyblocks-markdown"
version = "1.0.0"
category = "Markdown"
description = "Converte Markdown para HTML, XML ou SVG"
color = [96, 125, 139, 255]
dark = [55, 71, 79, 255]
[build]
sources = ["src/tb_markdown.c"]
includes = ["include"]
shared = "build/libtinyblocks-markdown.so"
static = "build/libtinyblocks-markdown.a"
depends = []
@@ -0,0 +1,517 @@
/*
* TinyBlocks - tb_markdown.c
* Categoria "Markdown": Markdown.ToHtml / ToXml / ToSvg.
*
* Parser por linhas -> lista de blocos (H1..H6, paragrafo, item de lista,
* citacao, bloco de codigo, regra) -> serializacao para HTML, XML ou SVG.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <tinyblocks/tinyblocks.h>
#include "tb_markdown.h"
static char s_err[160] = "";
const char* tb_markdown_last_error(void) { return s_err; }
/* ---------------------------------------------------------------- Buffer */
typedef struct { char* p; size_t n, cap; } Buf;
static bool b_res(Buf* b, size_t extra) {
if (b->n + extra + 1 <= b->cap) return true;
size_t cap = b->cap ? b->cap : 128;
while (cap < b->n + extra + 1) cap *= 2;
char* p = (char*)realloc(b->p, cap);
if (!p) return false;
b->p = p; b->cap = cap; return true;
}
static bool b_put(Buf* b, const char* s, size_t n) {
if (!b_res(b, n)) return false;
memcpy(b->p + b->n, s, n); b->n += n; b->p[b->n] = 0; return true;
}
static bool b_str(Buf* b, const char* s) { return b_put(b, s, strlen(s)); }
static bool b_ch(Buf* b, char c) { return b_put(b, &c, 1); }
static bool b_fmt(Buf* b, const char* f, int v) {
char tmp[48]; int n = snprintf(tmp, sizeof(tmp), f, v);
return b_put(b, tmp, (size_t)(n > 0 ? n : 0));
}
static void b_esc(Buf* b, const char* s, size_t n) {
for (size_t i = 0; i < n; i++) {
unsigned char c = (unsigned char)s[i];
if (c == '&') b_str(b, "&amp;");
else if (c == '<') b_str(b, "&lt;");
else if (c == '>') b_str(b, "&gt;");
else if (c == '"') b_str(b, "&quot;");
else b_ch(b, (char)c);
}
}
/* ----------------------------------------------------------------- Blocos */
typedef enum { BLK_H, BLK_P, BLK_LI, BLK_CODE, BLK_QUOTE, BLK_HR } BlkKind;
typedef struct {
BlkKind kind;
int level; /* heading */
bool ordered; /* item de lista ordenada */
int index; /* numero do item ordenado */
char* text; /* conteudo (inline markdown) */
char* lang; /* linguagem do bloco de codigo */
} Blk;
typedef struct { Blk* v; int n, cap; } Doc;
static Blk* doc_push(Doc* d, BlkKind k) {
if (d->n == d->cap) {
int cap = d->cap ? d->cap * 2 : 16;
Blk* v = (Blk*)realloc(d->v, (size_t)cap * sizeof(Blk));
if (!v) return NULL;
d->v = v; d->cap = cap;
}
Blk* b = &d->v[d->n++];
memset(b, 0, sizeof(*b));
b->kind = k;
return b;
}
static void doc_free(Doc* d) {
for (int i = 0; i < d->n; i++) { free(d->v[i].text); free(d->v[i].lang); }
free(d->v);
d->v = NULL; d->n = d->cap = 0;
}
static char* dupn(const char* s, size_t n) {
char* p = (char*)malloc(n + 1);
if (!p) return NULL;
memcpy(p, s, n); p[n] = 0; return p;
}
static void rtrim(char* s) { size_t n = strlen(s); while (n && (s[n-1]=='\n'||s[n-1]=='\r'||s[n-1]==' '||s[n-1]=='\t')) s[--n]=0; }
static void ltrim_ptr(const char** p) { while (**p==' '||**p=='\t') (*p)++; }
static bool hr_line(const char* s) {
int cnt = 0; char c0 = 0;
for (; *s; s++) {
if (*s == ' ' || *s == '\t') continue;
if (!c0) c0 = *s;
if (*s != c0) return false;
cnt++;
}
return cnt >= 3 && (c0 == '-' || c0 == '*' || c0 == '_');
}
static Doc* parse(const char* md) {
Doc* d = (Doc*)calloc(1, sizeof(Doc));
if (!d) return NULL;
if (!md) md = "";
const char* p = md;
bool in_code = false;
char code_lang[32] = "";
Buf code = {0};
Buf para = {0};
bool para_active = false;
#define FLUSH_PARA() do { if (para_active) { Blk* b = doc_push(d, BLK_P); if (b) { b->text = dupn(para.p ? para.p : "", para.p ? strlen(para.p) : 0); } \
free(para.p); para.p = NULL; para.n = para.cap = 0; para_active = false; } } while (0)
while (*p) {
const char* nl = strchr(p, '\n');
size_t len = nl ? (size_t)(nl - p) : strlen(p);
char* line = dupn(p, len);
if (!line) break;
rtrim(line);
const char* t = line;
ltrim_ptr(&t);
if (!in_code && !strncmp(t, "```", 3)) {
FLUSH_PARA();
in_code = true;
const char* lang = t + 3;
ltrim_ptr(&lang);
snprintf(code_lang, sizeof(code_lang), "%s", lang);
free(line);
if (!nl) break;
p = nl + 1;
continue;
}
if (in_code) {
if (!strncmp(t, "```", 3)) {
Blk* b = doc_push(d, BLK_CODE);
if (b) { b->text = dupn(code.p ? code.p : "", code.p ? strlen(code.p) : 0);
b->lang = dupn(code_lang, strlen(code_lang)); }
free(code.p); code.p = NULL; code.n = code.cap = 0;
in_code = false;
} else {
b_put(&code, line, strlen(line));
b_ch(&code, '\n');
}
free(line);
if (!nl) break;
p = nl + 1;
continue;
}
if (!*t) { FLUSH_PARA(); free(line); if (!nl) break; p = nl + 1; continue; }
int h = 0;
while (t[h] == '#' && h < 6) h++;
if (h > 0 && (t[h] == ' ' || t[h] == 0)) {
FLUSH_PARA();
const char* body = t + h; ltrim_ptr(&body);
Blk* b = doc_push(d, BLK_H);
if (b) { b->level = h; b->text = dupn(body, strlen(body)); }
free(line); if (!nl) break; p = nl + 1; continue;
}
if (hr_line(t)) {
FLUSH_PARA();
doc_push(d, BLK_HR);
free(line); if (!nl) break; p = nl + 1; continue;
}
if (*t == '>') {
FLUSH_PARA();
const char* body = t + 1; ltrim_ptr(&body);
Blk* b = doc_push(d, BLK_QUOTE);
if (b) b->text = dupn(body, strlen(body));
free(line); if (!nl) break; p = nl + 1; continue;
}
if ((t[0] == '-' || t[0] == '*' || t[0] == '+') && (t[1] == ' ' || t[1] == 0)) {
FLUSH_PARA();
const char* body = t + 1; ltrim_ptr(&body);
Blk* b = doc_push(d, BLK_LI);
if (b) { b->ordered = false; b->text = dupn(body, strlen(body)); }
free(line); if (!nl) break; p = nl + 1; continue;
}
if (isdigit((unsigned char)t[0])) {
const char* q = t;
while (isdigit((unsigned char)*q)) q++;
if ((*q == '.' || *q == ')') && (q[1] == ' ' || q[1] == 0)) {
FLUSH_PARA();
const char* body = q + 1; ltrim_ptr(&body);
Blk* b = doc_push(d, BLK_LI);
if (b) { b->ordered = true; b->index = atoi(t); b->text = dupn(body, strlen(body)); }
free(line); if (!nl) break; p = nl + 1; continue;
}
}
/* paragrafo (linhas consecutivas unidas por espaco) */
if (para_active) b_ch(&para, ' ');
b_str(&para, t);
para_active = true;
free(line);
if (!nl) break;
p = nl + 1;
}
if (in_code && code.p) {
Blk* b = doc_push(d, BLK_CODE);
if (b) b->text = dupn(code.p, strlen(code.p));
free(code.p); code.p = NULL;
}
FLUSH_PARA();
free(para.p);
#undef FLUSH_PARA
return d;
}
/* ---------------------------------------------------------------- Inline */
/* Serializa inline markdown para HTML (xml=false) ou XML (xml=true). */
static void inline_out(Buf* b, const char* s, bool xml) {
const char* bold0 = xml ? "<bold>" : "<strong>";
const char* bold1 = xml ? "</bold>" : "</strong>";
const char* it0 = xml ? "<italic>" : "<em>";
const char* it1 = xml ? "</italic>" : "</em>";
while (*s) {
if (s[0] == '*' && s[1] == '*') {
const char* e = strstr(s + 2, "**");
if (e) {
char* mid = dupn(s + 2, (size_t)(e - (s + 2)));
b_str(b, bold0); inline_out(b, mid ? mid : "", xml); b_str(b, bold1);
free(mid); s = e + 2; continue;
}
}
if (s[0] == '`') {
const char* e = strchr(s + 1, '`');
if (e) { b_str(b, "<code>"); b_esc(b, s + 1, (size_t)(e - (s + 1))); b_str(b, "</code>"); s = e + 1; continue; }
}
if (s[0] == '!' && s[1] == '[') {
const char* close = strchr(s + 2, ']');
if (close && close[1] == '(') {
const char* rp = strchr(close + 2, ')');
if (rp) {
b_str(b, xml ? "<image src=\"" : "<img alt=\"");
if (!xml) { b_esc(b, s + 2, (size_t)(close - (s + 2))); b_str(b, "\" src=\""); }
b_esc(b, close + 2, (size_t)(rp - (close + 2)));
if (xml) b_str(b, "\" alt=\"");
if (xml) b_esc(b, s + 2, (size_t)(close - (s + 2)));
b_str(b, xml ? "\"/>" : "\">");
s = rp + 1; continue;
}
}
}
if (s[0] == '[') {
const char* close = strchr(s + 1, ']');
if (close && close[1] == '(') {
const char* rp = strchr(close + 2, ')');
if (rp) {
b_str(b, xml ? "<link href=\"" : "<a href=\"");
b_esc(b, close + 2, (size_t)(rp - (close + 2)));
b_str(b, "\">");
{ char* mid = dupn(s + 1, (size_t)(close - (s + 1)));
inline_out(b, mid, xml); free(mid); }
b_str(b, xml ? "</link>" : "</a>");
s = rp + 1; continue;
}
}
}
if ((s[0] == '*' || s[0] == '_')) {
char q = s[0];
const char* e = strchr(s + 1, q);
if (e && e > s + 1) {
b_str(b, it0);
{ char* mid = dupn(s + 1, (size_t)(e - (s + 1)));
inline_out(b, mid, xml); free(mid); }
b_str(b, it1);
s = e + 1; continue;
}
}
if (s[0] == '&') { b_str(b, "&amp;"); s++; continue; }
if (s[0] == '<') { b_str(b, "&lt;"); s++; continue; }
if (s[0] == '>') { b_str(b, "&gt;"); s++; continue; }
b_ch(b, s[0]); s++;
}
}
/* ------------------------------------------------------------------ HTML */
char* tb_markdown_to_html(const char* md) {
s_err[0] = 0;
Doc* d = parse(md);
if (!d) { snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
Buf b = {0};
bool in_ul = false, in_ol = false;
for (int i = 0; i < d->n; i++) {
Blk* k = &d->v[i];
bool is_li = k->kind == BLK_LI;
if (!is_li && (in_ul || in_ol)) {
b_str(&b, in_ul ? "</ul>\n" : "</ol>\n"); in_ul = in_ol = false;
}
switch (k->kind) {
case BLK_H:
b_fmt(&b, "<h%d>", k->level); inline_out(&b, k->text ? k->text : "", false); b_fmt(&b, "</h%d>\n", k->level);
break;
case BLK_P:
b_str(&b, "<p>"); inline_out(&b, k->text ? k->text : "", false); b_str(&b, "</p>\n");
break;
case BLK_QUOTE:
b_str(&b, "<blockquote>"); inline_out(&b, k->text ? k->text : "", false); b_str(&b, "</blockquote>\n");
break;
case BLK_CODE:
b_str(&b, "<pre><code");
if (k->lang && k->lang[0]) { b_str(&b, " class=\"language-"); b_esc(&b, k->lang, strlen(k->lang)); b_str(&b, "\""); }
b_str(&b, ">"); b_esc(&b, k->text ? k->text : "", k->text ? strlen(k->text) : 0); b_str(&b, "</code></pre>\n");
break;
case BLK_HR:
b_str(&b, "<hr>\n");
break;
case BLK_LI:
if (k->ordered) { if (!in_ol) { b_str(&b, "<ol>\n"); in_ol = true; } }
else { if (!in_ul) { b_str(&b, "<ul>\n"); in_ul = true; } }
b_str(&b, "<li>"); inline_out(&b, k->text ? k->text : "", false); b_str(&b, "</li>\n");
break;
}
}
if (in_ul) b_str(&b, "</ul>\n");
if (in_ol) b_str(&b, "</ol>\n");
doc_free(d); free(d);
return b.p;
}
/* ------------------------------------------------------------------- XML */
char* tb_markdown_to_xml(const char* md) {
s_err[0] = 0;
Doc* d = parse(md);
if (!d) { snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
Buf b = {0};
b_str(&b, "<markdown>\n");
bool in_list = false; bool list_ordered = false;
for (int i = 0; i < d->n; i++) {
Blk* k = &d->v[i];
bool is_li = k->kind == BLK_LI;
if (!is_li && in_list) { b_str(&b, "</list>\n"); in_list = false; }
switch (k->kind) {
case BLK_H:
b_str(&b, "<heading level=\""); b_fmt(&b, "%d", k->level); b_str(&b, "\">");
inline_out(&b, k->text ? k->text : "", true); b_str(&b, "</heading>\n");
break;
case BLK_P:
b_str(&b, "<paragraph>"); inline_out(&b, k->text ? k->text : "", true); b_str(&b, "</paragraph>\n");
break;
case BLK_QUOTE:
b_str(&b, "<quote>"); inline_out(&b, k->text ? k->text : "", true); b_str(&b, "</quote>\n");
break;
case BLK_CODE:
b_str(&b, "<code");
if (k->lang && k->lang[0]) { b_str(&b, " lang=\""); b_esc(&b, k->lang, strlen(k->lang)); b_str(&b, "\""); }
b_str(&b, ">"); b_esc(&b, k->text ? k->text : "", k->text ? strlen(k->text) : 0); b_str(&b, "</code>\n");
break;
case BLK_HR:
b_str(&b, "<rule/>\n");
break;
case BLK_LI:
if (!in_list) {
b_str(&b, "<list ordered=\""); b_str(&b, k->ordered ? "true" : "false"); b_str(&b, "\">\n");
in_list = true; list_ordered = k->ordered;
}
(void)list_ordered;
b_str(&b, "<item>"); inline_out(&b, k->text ? k->text : "", true); b_str(&b, "</item>\n");
break;
}
}
if (in_list) b_str(&b, "</list>\n");
b_str(&b, "</markdown>");
doc_free(d); free(d);
return b.p;
}
/* ------------------------------------------------------------------- SVG */
char* tb_markdown_to_svg_opts(const char* md, int width, int font_size) {
s_err[0] = 0;
if (width < 80) width = 640;
if (font_size < 6) font_size = 16;
Doc* d = parse(md);
if (!d) { snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
int pad = 16;
int line_h = (int)(font_size * 1.5);
int y = pad + font_size;
Buf body = {0};
char num[24];
for (int i = 0; i < d->n; i++) {
Blk* k = &d->v[i];
int fs = font_size;
bool bold = false, mono = false, italic = false;
int indent = 0;
const char* prefix = "";
int bullet_r = 0; /* raio do marcador de lista */
switch (k->kind) {
case BLK_H:
fs = font_size + (7 - k->level) * 4;
if (fs < font_size) fs = font_size;
bold = true;
y += (fs - font_size) / 2;
break;
case BLK_P: break;
case BLK_QUOTE: italic = true; indent = 14; break;
case BLK_CODE: mono = true; indent = 14; break;
case BLK_HR: break;
case BLK_LI: indent = 12; bullet_r = (fs / 5) + 1; break;
}
if (k->kind == BLK_HR) {
b_fmt(&body, " <line x1=\"%d\" y1=\"", pad);
b_fmt(&body, "%d", y - fs / 2);
b_str(&body, "\" x2=\"");
b_fmt(&body, "%d", width - pad);
b_str(&body, "\" y2=\"");
b_fmt(&body, "%d", y - fs / 2);
b_str(&body, "\" stroke=\"#cccccc\" stroke-width=\"1\"/>\n");
y += line_h / 2;
continue;
}
if (bullet_r) {
b_str(&body, " <circle cx=\"");
b_fmt(&body, "%d", pad + indent);
b_str(&body, "\" cy=\"");
b_fmt(&body, "%d", y - fs / 3);
b_str(&body, "\" r=\"");
b_fmt(&body, "%d", bullet_r);
b_str(&body, "\" fill=\"#555555\"/>\n");
}
const char* txt = k->text ? k->text : "";
if (k->kind == BLK_LI && k->ordered) {
snprintf(num, sizeof(num), "%d. ", k->index);
prefix = num;
}
b_str(&body, " <text x=\"");
b_fmt(&body, "%d", pad + indent + (k->kind == BLK_LI ? 14 : 0));
b_str(&body, "\" y=\"");
b_fmt(&body, "%d", y);
b_str(&body, "\" font-family=\"");
b_str(&body, mono ? "monospace" : "sans-serif");
b_str(&body, "\" font-size=\"");
b_fmt(&body, "%d", fs);
b_str(&body, "\"");
if (bold) b_str(&body, " font-weight=\"bold\"");
if (italic) b_str(&body, " font-style=\"italic\"");
b_str(&body, " fill=\"#");
b_str(&body, k->kind == BLK_QUOTE ? "666666" : (mono ? "333333" : "111111"));
b_str(&body, "\">");
b_esc(&body, prefix, strlen(prefix));
/* texto simples (sem marcadores inline) */
for (const char* p = txt; *p; p++) {
if (*p == '*' || *p == '`') continue;
b_esc(&body, p, 1);
}
b_str(&body, "</text>\n");
y += (fs > font_size ? line_h + (fs - font_size) : line_h);
}
int height = y + pad;
Buf out = {0};
b_str(&out, "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"");
b_fmt(&out, "%d", width);
b_str(&out, "\" height=\"");
b_fmt(&out, "%d", height);
b_str(&out, "\" viewBox=\"0 0 ");
b_fmt(&out, "%d", width);
b_ch(&out, ' ');
b_fmt(&out, "%d", height);
b_str(&out, "\">\n");
b_str(&out, " <rect width=\"100%\" height=\"100%\" fill=\"#ffffff\"/>\n");
b_str(&out, body.p ? body.p : "");
b_str(&out, "</svg>");
free(body.p);
doc_free(d); free(d);
return out.p;
}
char* tb_markdown_to_svg(const char* md) { return tb_markdown_to_svg_opts(md, 640, 16); }
/* ---------------------------------------------------------------- Blocos */
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
}
static void eval_html(TinyBlocks_Block* self, TinyBlocks* rt) {
char* s = tb_markdown_to_html(in_text(self, rt, 0));
tb_block_set_output(self, 0, tb_value_text(s ? s : "")); free(s); self->dirty = false;
}
static void eval_xml(TinyBlocks_Block* self, TinyBlocks* rt) {
char* s = tb_markdown_to_xml(in_text(self, rt, 0));
tb_block_set_output(self, 0, tb_value_text(s ? s : "")); free(s); self->dirty = false;
}
static void eval_svg(TinyBlocks_Block* self, TinyBlocks* rt) {
char* s = tb_markdown_to_svg(in_text(self, rt, 0));
tb_block_set_output(self, 0, tb_value_text(s ? s : "")); free(s); self->dirty = false;
}
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
const TinyBlocks_SlotDesc* in, uint32_t ni,
const TinyBlocks_SlotDesc* out, uint32_t no,
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
return t && tb_library_add(lib, &t->base);
}
static bool markdown_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_Category cat = {
"Markdown",
{ 96/255.f, 125/255.f, 139/255.f, 1.f},
{ 55/255.f, 71/255.f, 79/255.f, 1.f},
};
tb_library_set_category(lib, &cat);
static const TinyBlocks_SlotDesc in_md[] = {{"markdown", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc out_text[] = {{"texto", TB_VALUE_TEXT}};
return reg(rt, lib, "Markdown.ToHtml", in_md, 1, out_text, 1, eval_html)
&& reg(rt, lib, "Markdown.ToXml", in_md, 1, out_text, 1, eval_xml)
&& reg(rt, lib, "Markdown.ToSvg", in_md, 1, out_text, 1, eval_svg);
}
TINYBLOCKS_DEFINE_LIBRARY(markdown, "tinyblocks-markdown", "1.0.0", markdown_init, NULL)
const TinyBlocks_LibraryDescriptor* tb_markdown_get_library(void) {
return &_tb_lib_desc_markdown;
}