#define g_object_get_data g_object_get_data
static CxMap *markdown_tags;
+static MDTag *code_block_tag;
static void editor_set_cursor_cb(
GtkTextBuffer *buffer,
cxMapPut(markdown_tags, EDITOR_STYLE_EMPHASIS, &((MDTag){"*", "*"}));
cxMapPut(markdown_tags, EDITOR_STYLE_UNDERLINE, &((MDTag){"_", "_"}));
cxMapPut(markdown_tags, EDITOR_STYLE_CODE, &((MDTag){"`", "`"}));
+
+ code_block_tag = cxMapGet(markdown_tags, EDITOR_STYLE_CODE_BLOCK);
}
void editor_init_textview(UiObject *obj, UIWIDGET textview) {
}
gchar *text = gtk_text_buffer_get_text(buffer, &start, &iter, TRUE);
+ gboolean is_code_block = false;
GSList *tags = gtk_text_iter_get_tags(&start);
CxMap *begin_tags = tags2map(tags);
cx_foreach(CxMapEntry *, entry, i) {
const char *name = entry->key->data;
MDTag *t = cxMapGet(markdown_tags, *entry->key);
+ if(t == code_block_tag) {
+ is_code_block = true;
+ break;
+ }
if(t && t->begin) {
cxBufferPutString(&out, t->begin);
}
// add content
printf("range: {%s}\n\n", text);
- cxBufferPutString(&out, text);
+ if(!is_code_block) {
+ cxBufferPutString(&out, text);
+ } else {
+ int code_block_start = 0;
+ size_t text_len = strlen(text);
+ for(int i=0;i<text_len;i++) {
+ if(text[i] == '\n') {
+ cxBufferPutString(&out, " ");
+ cxBufferPutString(&out, cx_strn(text + code_block_start, i - code_block_start + 1));
+ code_block_start = i + 1;
+ }
+ }
+ }
// get all tags that ended here
tags = gtk_text_iter_get_tags(&iter);
}
}
+CX_TEST(test_editor_load_markdown_code2) {
+ CX_TEST_DO {
+ CX_TEST_CALL_SUBROUTINE(test_editor_markdown_load_get, cx_mutstr(
+ "Test Multiline code\n\n"
+ " #include <stdio.h>\n"
+ " \n"
+ " int main() {"
+ " }\n"
+ "\nend\n"));
+ }
+}
+
+
CX_TEST(test_editor_load_markdown_link) {
CX_TEST_DO {
CX_TEST_CALL_SUBROUTINE(test_editor_markdown_load_get, cx_mutstr("Link [link1](https://example.com/link1)\n[link2][1]\n\n[1]: https://example.com/link2"));
cx_test_register(suite, test_editor_load_markdown_list2);
cx_test_register(suite, test_editor_load_markdown_span1);
cx_test_register(suite, test_editor_load_markdown_code1);
+ cx_test_register(suite, test_editor_load_markdown_code2);
cx_test_register(suite, test_editor_load_markdown_link);
cx_test_register(suite, test_text_search_strcasestr);