]> uap-core.de Git - note.git/commitdiff
store markdown links in MDDoc main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 11 Mar 2025 21:23:45 +0000 (22:23 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 11 Mar 2025 21:23:45 +0000 (22:23 +0100)
application/editor.c
application/editor.h
application/gtk-text.c

index 690cb59b5edb82a10c3b4a8a031debd4cd54125e..045fb5d6538dff91dff9e37dc4ef8da0bb47d799 100644 (file)
@@ -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;
     }
 }
index a026be76d0f74e8071b338e5a002bb7d88d6854a..3c1499bbe04b2ad74d59e48d56fb1d4deea779b4 100644 (file)
@@ -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);
index 688f14a6e9d02bb5626cd92baae469064b0ae144..ab7a4fb9ae2f8445ae6ff3135936a3fb6b1adc2a 100644 (file)
@@ -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);
+        }
     }
 }