aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremias Stotter <jeremias@stotter.eu>2022-02-20 22:38:35 +0100
committerJeremias Stotter <jeremias@stotter.eu>2022-02-20 22:38:35 +0100
commit7dc0b30a7eb628de1591e83b97a2cffbae763fbf (patch)
treeb3a0e989de52c06102623ba005a1e6b67cd40d15
parentd2a4ce480a38738f15dd00e2de164b5a6398a140 (diff)
downloadJBlog-7dc0b30a7eb628de1591e83b97a2cffbae763fbf.tar.gz
JBlog-7dc0b30a7eb628de1591e83b97a2cffbae763fbf.tar.bz2
JBlog-7dc0b30a7eb628de1591e83b97a2cffbae763fbf.zip
Images now work
Pretty much just a copy of the link code
-rw-r--r--md.c36
1 files changed, 30 insertions, 6 deletions
diff --git a/md.c b/md.c
index b8201da..9f2f0f5 100644
--- a/md.c
+++ b/md.c
@@ -34,8 +34,6 @@
// HTML escapes like &#42; are not handled by this although they might be handled by the client anyways
// No setext headings because why not just use the hash symbol?
-size_t append(char* in_dest, char* in_src);
-size_t prepend(char* in_dest, char* in_src);
char* get_link_components(char* start, char** out_text, char** out_loc, size_t* out_len);
enum html_type {
@@ -211,6 +209,12 @@ char* tree_to_html(struct tree_element* root) {
break;
}
break;
+ case(t_img):
+ html = realloc(html, realloc_len(img_html) + strlen(root->value));
+ if(!html)
+ exit(-1);
+ sprintf(html, img_html, root->value ? (char*)(root->value) : "", inner_html ? inner_html : "");
+ break;
case(t_a):
html = realloc(html, realloc_len(a_html) + strlen(root->value));
if(!html)
@@ -540,10 +544,27 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
active_element = active_element->parent->parent;
cur_char = new_position;
break;
- }
- else
+ } else
goto default2;
case('!'):
+ char* alt_text = NULL;
+ char* img_loc = NULL;
+ size_t img_len = 0;
+ new_position = get_link_components(cur_char + 1, &alt_text, &img_loc, &img_len);
+ if(new_position) {
+ while(!(active_element == root) && (!active_element->allow_inner && active_element))
+ active_element = active_element->parent;
+ // This contains the image link
+ new_active_child(active_element, active_element, -1, t_img, true);
+ active_element->value = img_loc;
+ // This contains the image alt text
+ new_active_child(active_element, active_element, -1, t_inner, false);
+ active_element->value = alt_text;
+ active_element = active_element->parent->parent;
+ cur_char = new_position;
+ break;
+ } else
+ goto default2;
break;
// Titles
case('='):
@@ -707,15 +728,18 @@ int trim_space(char* input, char** output) {
// out_text and out_loc are allocated by this function
// Don't forget to free
char* get_link_components(char* start, char** out_text, char** out_loc, size_t* out_len) {
+ // look how far the next newline is away
+ size_t line_length = strcspn(start, "\r\n");
+
// Search for the next closing bracket
- char* closing_sqr_bracket = strchr(start, ']');
+ char* closing_sqr_bracket = memchr(start, ']', line_length);
if(closing_sqr_bracket == NULL) {
return NULL;
}
if(*(closing_sqr_bracket + 1) != '(') {
return NULL;
}
- char* closing_rnd_bracket = strchr(closing_sqr_bracket + 1, ')');
+ char* closing_rnd_bracket = memchr(closing_sqr_bracket + 1, ')', line_length - (closing_sqr_bracket - start));
if(closing_rnd_bracket == NULL) {
return NULL;
}
Jeremias Stotters git repositories generated by CGIT