aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremias Stotter <jeremias@stotter.eu>2022-01-02 22:37:20 +0100
committerJeremias Stotter <jeremias@stotter.eu>2022-01-02 22:37:20 +0100
commita4a5a6050a18a034633306a4e031f2f6d3cd02cc (patch)
treed8304fd686e4ff6aac29a4ae9699e509c3646f49
parent098d68661c6be286b41245927e8ab2f7806419ad (diff)
downloadJBlog-a4a5a6050a18a034633306a4e031f2f6d3cd02cc.tar.gz
JBlog-a4a5a6050a18a034633306a4e031f2f6d3cd02cc.tar.bz2
JBlog-a4a5a6050a18a034633306a4e031f2f6d3cd02cc.zip
Fixed a nasty bug with lists
-rw-r--r--md.c60
1 files changed, 35 insertions, 25 deletions
diff --git a/md.c b/md.c
index b38c35d..c5ac072 100644
--- a/md.c
+++ b/md.c
@@ -186,7 +186,7 @@ char* tree_to_html(struct tree_element* root) {
// This appends the cur_char to active element / it creates a new active element if the active element can not have text
void append_char_to_active(struct tree_element* root, struct tree_element** active_element, char cur_char) {
// We are not allowed to add inner to this element so we'll start a new paragraph
- struct tree_element* new_active_element = *active_element;
+ struct tree_element* new_active_element = active_element ? *active_element : root;
if(!(new_active_element->allow_inner) && new_active_element->type != t_inner) {
new_active_element = new_child(root, -1);
new_active_element->type = t_p;
@@ -209,7 +209,20 @@ void append_char_to_active(struct tree_element* root, struct tree_element** acti
char append[2] = {cur_char, '\0'};
new_active_element->value = realloc_append(new_active_element->value, append);
}
- *active_element = new_active_element;
+ if(active_element)
+ *active_element = new_active_element;
+}
+
+// This adds a new unordered list and returns a pointer to it
+// indent is the spaces that the list indicator ( * or - ) was intendend
+struct tree_element* new_ul(struct tree_element* parent, int indent) {
+ struct tree_element *new_ul;
+ new_ul = new_child(parent, -1);
+ new_ul->type = t_ul;
+ new_ul->allow_inner = false;
+ new_ul->value = malloc(sizeof(int));
+ *(int*)new_ul->value = indent;
+ return new_ul;
}
// The program needs to loop through the loop again to cose all the open things at the end, THIS NEEDS TO BE IMPLEMENTED for xhtml
@@ -217,8 +230,8 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
clock_t before = clock();
memset(buffer, 0, buffer_size);
bool escaped = false;
- bool newline = true;
- bool list_waiting = true;
+ bool newline = false;
+ bool list_waiting = false;
int hash_chain = 0;
int dash_chain = 0;
@@ -244,12 +257,15 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
}
#define LAST_TO_TITLE(n) {\
+ if(root->children_n >= 1) {\
struct tree_element* last_element = root->children[root->children_n-1];\
- last_element->type = t_h;\
- if(last_element->value)\
- free(last_element->value);\
- last_element->value = malloc(sizeof(int));\
- *(int*)last_element->value = n;\
+ fprintf(stderr, "last_element: %ld\n", (long)last_element);\
+ last_element->type = t_h;\
+ if(last_element->value)\
+ free(last_element->value);\
+ last_element->value = malloc(sizeof(int));\
+ *(int*)last_element->value = n;\
+ }\
}
switch(*cur_char) {
@@ -266,6 +282,7 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
// Ignoring \r goes against the commonmark spec, but who cares
break;
case('\n'):
+ list_waiting = false;
if(dash_chain >= 1) {
active_element = root;
// Make the last element a title unless
@@ -276,24 +293,24 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
hr->allow_inner = false;
} else {
for(int i = 0; i < dash_chain; i++)
- append_char_to_active(root, &active_element, '-');
+ append_char_to_active(root, NULL, '-');
}
} else {
LAST_TO_TITLE(2);
}
+ dash_chain = 0;
}
if(eq_chain >= 1) {
+ active_element = root;
if(newline) {
LAST_TO_TITLE(1);
} else {
for(int i = 0; i < eq_chain; i++)
- append_char_to_active(root, &active_element, '=');
+ append_char_to_active(root, NULL, '=');
}
+ eq_chain = 0;
}
- eq_chain = 0;
- dash_chain = 0;
-
if(hash_chain > 0)
active_element = root;
if(newline) {
@@ -364,15 +381,6 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
}
// Here lists are created
if(list_waiting) {
- // This adds a new unordered list
- void new_ul(struct tree_element* parent) {
- active_element = new_child(parent, -1);
- active_element->type = t_ul;
- active_element->allow_inner = false;
- active_element->value = malloc(sizeof(int));
- *(int*)active_element->value = spaces_trimmed;
- }
-
newline = false;
/* Look if we have an ancestor somewhere that has spaces fewer or equal to the spaces we skipped.
* On fewer spaces we enter a new list below the one we found
@@ -382,11 +390,13 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
bool found_ul = false;
while(look_element && look_element != root){
if(look_element->type == t_ul) {
+ // Enter new child ul
if(*(int*)look_element->value < spaces_trimmed) {
- new_ul(look_element);
+ active_element = new_ul(look_element, spaces_trimmed);
found_ul = true;
break;
}
+ // We found a list of the exact indentation level
if(*(int*)look_element->value == spaces_trimmed) {
active_element = look_element;
found_ul = true;
@@ -397,7 +407,7 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
}
// Enter a new list
if(!found_ul) {
- new_ul(root);
+ active_element = new_ul(root, spaces_trimmed);
}
active_element = new_child(active_element, -1);
Jeremias Stotters git repositories generated by CGIT