#include <cx/buffer.h>
#include <cx/tree.h>
+#include <libidav/utils.h> // url encoding/decoding
void editor_load_markdown(Resource *note, UIWIDGET textview, cxmutstr markdown) {
} else if(type == MD_SPAN_IMG) {
MD_SPAN_IMG_DETAIL *img = detail;
if(img) {
- node->link = cx_strdup_a(data->a, cx_strn(img->src.text, img->src.size));
+ cxstring imgsrc = cx_strn(img->src.text, img->src.size);
+ if(cx_strprefix(imgsrc, CX_STR("http://")) || cx_strprefix(imgsrc, CX_STR("https://"))) {
+ // copy url in case it is http/https
+ node->link = cx_strdup_a(data->a, imgsrc);
+ } else {
+ // in case it is a path, urldecode the link
+ node->link = util_url_decode_s(data->a, imgsrc);
+ }
}
}
void md_serialize_image(EmbeddedWidget *em, CxBuffer *out) {
Attachment *attachment = em->data1;
- cx_bprintf(out, "", attachment->name);
+ // TODO: relative path to parent resource
+ char *path = util_url_encode(NULL, attachment->name);
+ cx_bprintf(out, "", path);
+ free(path);
}
static void editor_insert_image(NoteEditor *editor, Attachment *attachment, GtkTextIter *iter) {
return path;
}
+char* util_url_encode(DavSession *sn, const char *url) {
+ CURL *handle = sn ? sn->handle : NULL;
+#if LIBCURL_VERSION_NUM < 0x075200
+ int cleanup_handle = 0;
+ if(!handle) {
+ handle = curl_easy_init();
+ cleanup_handle = 1;
+ }
+#endif
+
+ char *esc = curl_easy_escape(handle, url, strlen(url));
+ char *ret = esc ? strdup(esc) : NULL;
+ curl_free(esc);
+
+#if LIBCURL_VERSION_NUM < 0x075200
+ if(cleanup_handle) {
+ curl_easy_cleanup(handle);
+ }
+#endif
+
+ return ret;
+}
+
char* util_url_decode(DavSession *sn, const char *url) {
- char *unesc = curl_easy_unescape(sn->handle, url, strlen(url), NULL);
- char *ret = strdup(unesc);
+ CURL *handle = sn ? sn->handle : NULL;
+#if LIBCURL_VERSION_NUM < 0x075200
+ int cleanup_handle = 0;
+ if(!handle) {
+ handle = curl_easy_init();
+ cleanup_handle = 1;
+ }
+#endif
+
+ char *unesc = curl_easy_unescape(handle, url, strlen(url), NULL);
+ char *ret = unesc ? strdup(unesc) : NULL;
+ curl_free(unesc);
+
+#if LIBCURL_VERSION_NUM < 0x075200
+ if(cleanup_handle) {
+ curl_easy_cleanup(handle);
+ }
+#endif
+
+ return ret;
+}
+
+cxmutstr util_url_encode_s(const CxAllocator *a, cxstring url) {
+ CURL *handle = NULL;
+#if LIBCURL_VERSION_NUM < 0x075200
+ handle = curl_easy_init();
+#endif
+
+ char *esc = curl_easy_escape(handle, url.ptr, url.length);
+ cxmutstr ret = esc ? cx_strdup_a(a, cx_str(esc)) : (cxmutstr){NULL, 0};
+ curl_free(esc);
+
+#if LIBCURL_VERSION_NUM < 0x075200
+ curl_easy_cleanup(handle);
+#endif
+ return ret;
+}
+
+cxmutstr util_url_decode_s(const CxAllocator *a, cxstring url) {
+ CURL *handle = NULL;
+#if LIBCURL_VERSION_NUM < 0x075200
+ handle = curl_easy_init();
+#endif
+
+ char *unesc = curl_easy_unescape(handle, url.ptr, url.length, NULL);
+ cxmutstr ret = unesc ? cx_strdup_a(a, cx_str(unesc)) : (cxmutstr){NULL, 0};
curl_free(unesc);
+
+#if LIBCURL_VERSION_NUM < 0x075200
+ curl_easy_cleanup(handle);
+#endif
return ret;
}
cxstring util_url_base_s(cxstring url);
const char* util_url_path(const char *url);
cxstring util_url_path_s(cxstring url);
+char* util_url_encode(DavSession *sn, const char *url);
char* util_url_decode(DavSession *sn, const char *url);
+cxmutstr util_url_encode_s(const CxAllocator *a, cxstring url);
+cxmutstr util_url_decode_s(const CxAllocator *a, cxstring url);
const char* util_resource_name(const char *url);
const char* util_resource_name_c(const char *url, char pathseparator);
const char* util_path_file_name(const char *url);