]> uap-core.de Git - note.git/commitdiff
convert textview links to markdown
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 18 Mar 2025 20:50:48 +0000 (21:50 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 18 Mar 2025 20:50:48 +0000 (21:50 +0100)
application/gtk-text.c

index 1393e78e7e5e63101e3fdc26312b122f8f59f8e2..f1cd7c1202a1758bf63ee33e73ad95ccc34060de 100644 (file)
@@ -439,6 +439,8 @@ void editor_apply_styles(UiText *text, CxList /* MDDocStyleSection */ *styles) {
  */
 static CxMap* tags2map(GSList *tags) {
     CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS);
+    int anon_tag = 0;
+    char buf[32];
     while(tags) {
         GtkTextTag *t = tags->data;
         GValue name_value = G_VALUE_INIT;
@@ -446,6 +448,9 @@ static CxMap* tags2map(GSList *tags) {
         const char *name = g_value_get_string(&name_value);
         if(name) {
             cxMapPut(map, name, t);
+        } else {
+            snprintf(buf, 32, "_%d", anon_tag++);
+            cxMapPut(map, buf, t);
         }
         tags = tags->next;
     }
@@ -504,13 +509,22 @@ cxmutstr editor_get_markdown(UiText *text, const CxAllocator *a) {
         cxMapFree(prev_tags);
         prev_tags = begin_tags;
         
+        TextLink *link = NULL;
         // check all new tags and add tag prefix code
         CxMapIterator i = cxMapIterator(new_tags);
         cx_foreach(CxMapEntry *, entry, i) {
+            const char *name = entry->key->data;
             MDTag *t = cxMapGet(markdown_tags, *entry->key);
             if(t && t->begin) {
                 cxBufferPutString(&out, t->begin);
             }
+            if(name[0] == '_') {
+                GtkTextTag *tag = entry->value;
+                link = g_object_get_data(G_OBJECT(tag), "link");
+                if(link) {
+                    cxBufferPut(&out, '[');
+                }
+            }
         }
         cxMapFree(new_tags);
         
@@ -532,6 +546,11 @@ cxmutstr editor_get_markdown(UiText *text, const CxAllocator *a) {
                 cxBufferPutString(&out, t->end);
             }
         }
+        if(link) {
+            cxBufferPutString(&out, "](");
+            cxBufferPutString(&out, link->link);
+            cxBufferPut(&out, ')');
+        }
         cxMapFree(begin_tags2);
         cxMapFree(end_tags);