From: Olaf Wintermann Date: Tue, 11 Mar 2025 21:23:45 +0000 (+0100) Subject: store markdown links in MDDoc X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fmain;p=note.git store markdown links in MDDoc --- diff --git a/application/editor.c b/application/editor.c index 690cb59..045fb5d 100644 --- a/application/editor.c +++ b/application/editor.c @@ -87,6 +87,11 @@ static int md_enter_block(MD_BLOCKTYPE type, void* detail, void* userdata) { p->next = NULL; p->type = type; + if(type == MD_BLOCK_H) { + MD_BLOCK_H_DETAIL *heading = detail; + p->heading = heading->level; + } + if(!data->doc->content) { data->doc->content = p; } else { @@ -101,7 +106,6 @@ static int md_enter_block(MD_BLOCKTYPE type, void* detail, void* userdata) { } static int md_leave_block(MD_BLOCKTYPE type, void* detail, void* userdata) { - return 0; } @@ -147,6 +151,13 @@ static int md_enter_span(MD_SPANTYPE type, void* detail, void* userdata) { MDNode *node = md_node_create(data); node->type = type; + if(type == MD_SPAN_A) { + MD_SPAN_A_DETAIL *link = detail; + if(link && link->href.size > 0) { + node->link = cx_strdup_a(data->a, cx_strn(link->href.text, link->href.size)); + } + } + return 0; } @@ -261,6 +272,7 @@ static const char* node_style(MDNode *n) { switch(n->type) { case MD_SPAN_EM: return EDITOR_STYLE_EMPHASIS; case MD_SPAN_STRONG: return EDITOR_STYLE_STRONG; + case MD_SPAN_A: return EDITOR_STYLE_LINK; } return NULL; } @@ -286,6 +298,7 @@ static void linearize_mdnodes(CxBuffer *buf, CxList *sections, MDNode *n, int de sec.pos = start_pos; sec.length = buf->pos - start_pos; sec.style = node_style(n); + sec.link = n->link.ptr; cxListAdd(sections, &sec); } } @@ -293,7 +306,16 @@ static void linearize_mdnodes(CxBuffer *buf, CxList *sections, MDNode *n, int de static const char* paragraph_style(MDPara *p) { // TODO: implement all styles switch(p->type) { - case MD_BLOCK_H: return EDITOR_STYLE_HEADING1; + case MD_BLOCK_H: { + switch(p->heading) { + default: return EDITOR_STYLE_HEADING1; + case 2: return EDITOR_STYLE_HEADING2; + case 3: return EDITOR_STYLE_HEADING3; + case 4: return EDITOR_STYLE_HEADING4; + case 5: return EDITOR_STYLE_HEADING5; + case 6: return EDITOR_STYLE_HEADING6; + } + } default: return EDITOR_STYLE_PARAGRAPH; } } diff --git a/application/editor.h b/application/editor.h index a026be7..3c1499b 100644 --- a/application/editor.h +++ b/application/editor.h @@ -49,6 +49,7 @@ extern "C" { #define EDITOR_STYLE_CODE "code" #define EDITOR_STYLE_EMPHASIS "emphasis" #define EDITOR_STYLE_STRONG "strong" +#define EDITOR_STYLE_LINK "link" #define MD_MAX_DEPTH 50 @@ -63,6 +64,7 @@ struct MDPara { MD_BLOCKTYPE type; MDNode *content; MDPara *next; + int heading; }; struct MDNode { @@ -91,6 +93,7 @@ struct MDDocStyleSection { int pos; int length; const char *style; + const char *link; }; void editor_init(UiText *text); diff --git a/application/gtk-text.c b/application/gtk-text.c index 688f14a..ab7a4fb 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -123,10 +123,15 @@ void init_tagtable(GtkTextTagTable *table) { tag = gtk_text_tag_new(EDITOR_STYLE_STRONG); g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL); gtk_text_tag_table_add(table, tag); + + tag = gtk_text_tag_new(EDITOR_STYLE_LINK); + g_object_set(tag, "foreground", "blue", NULL); + gtk_text_tag_table_add(table, tag); } void editor_apply_styles(UiText *text, CxList /* MDDocStyleSection */ *styles) { GtkTextBuffer *buffer = text->data1; + GtkTextTagTable *tagtable = gtk_text_buffer_get_tag_table(buffer); CxIterator i = cxListIterator(styles); cx_foreach(MDDocStyleSection*, sec, i) { @@ -136,6 +141,14 @@ void editor_apply_styles(UiText *text, CxList /* MDDocStyleSection */ *styles) { if(sec->style) { gtk_text_buffer_apply_tag_by_name(buffer, sec->style, &begin, &end); } + if(sec->link) { + GtkTextTag *linktag = gtk_text_tag_new(NULL); + char *link = strdup(sec->link); + g_object_set_data(G_OBJECT(linktag), "link", link); + // TODO: destroy handler + gtk_text_tag_table_add(tagtable, linktag); + gtk_text_buffer_apply_tag(buffer, linktag, &begin, &end); + } } }