aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremias Stotter <jeremias@stotter.eu>2022-02-27 20:44:28 +0100
committerJeremias Stotter <jeremias@stotter.eu>2022-02-27 20:44:28 +0100
commit3cbe1cd90ba3419c2d6e11b79754fa1d775d8f77 (patch)
tree817d0b3740f1b03d848963c660dcfe1811186559
parent5c4e17aaa1bd54be51b380d76581fb107d657be6 (diff)
downloadJBlog-3cbe1cd90ba3419c2d6e11b79754fa1d775d8f77.tar.gz
JBlog-3cbe1cd90ba3419c2d6e11b79754fa1d775d8f77.tar.bz2
JBlog-3cbe1cd90ba3419c2d6e11b79754fa1d775d8f77.zip
Fixed a bug with the strength markers
-rw-r--r--md.c37
1 files changed, 23 insertions, 14 deletions
diff --git a/md.c b/md.c
index 3277413..8555a34 100644
--- a/md.c
+++ b/md.c
@@ -159,9 +159,11 @@ char* realloc_append(char* dest, char* src) {
#define hr_html "<hr/>\n"
#define code_html "<pre><code><p>%s</p></code></pre>\n"
// Resolve a tree to html
+int depth = 0;
char* tree_to_html(struct tree_element* root) {
char* html=NULL;
char* inner_html = NULL;
+ printf("%d: %d\n", depth, root->type);
for(int i = 0; i < root->children_n; i++) {
struct tree_element* child = root->children[i];
switch(child->type) {
@@ -259,6 +261,9 @@ void append_char_to_active(struct tree_element* root, struct tree_element** acti
// 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 ? *active_element : root;
NEXT_ALLOW_INNER((*active_element), root)
+ if(*active_element == root) {
+ NEW_ACTIVE_CHILD(new_active_element, root, -1, t_p, true);
+ }
if(new_active_element->type != t_inner) {
NEW_ACTIVE_CHILD(new_active_element, new_active_element, -1, t_inner, false);
}
@@ -319,14 +324,14 @@ bool str_fin_wait_und = false;
#define ZERO_STR_UND temp_str_und = 0; str_cl_wait_und = false; str_chr_wait_und = false; str_fin_wait_und = false;
-void end_strength(struct tree_element* root, struct tree_element** active_element, char marker, int tmp_str) {
- struct tree_element* parent_strength = find_parent_type(root, *active_element, t_str_ast);
+void end_strength(struct tree_element* root, struct tree_element** active_element, char marker, int tmp_str, int type) {
+ struct tree_element* parent_strength = find_parent_type(root, *active_element, type);
if(!parent_strength)
return;
if(tmp_str < *(unsigned int*)parent_strength->value) {
struct tree_element* tmp_active = parent_strength->parent;
for(int i = tmp_str; i < *(unsigned int*)parent_strength->value; i++)
- append_char_to_active(root, &tmp_active, '*');
+ append_char_to_active(root, &tmp_active, marker);
*(unsigned int*)parent_strength->value = tmp_str;
} else if(tmp_str > *(unsigned int*)parent_strength->value) {
struct tree_element* tmp_active;
@@ -335,17 +340,18 @@ void end_strength(struct tree_element* root, struct tree_element** active_elemen
else {
NEW_ACTIVE_CHILD(tmp_active, parent_strength->parent, 0, t_inner, false);
}
+ //printf("\naa:%d\n", tmp_str);
for(int i = *(unsigned int*)parent_strength->value; i < tmp_str; i++)
- append_char_to_active(root, &tmp_active, '*');
+ append_char_to_active(root, &tmp_active, marker);
}
}
// Call this function if a strength character is waiting to hit a different character
-void str_wait_hit(struct tree_element* root, struct tree_element** active_element, bool* str_cl_wait, bool* str_fin_wait, unsigned int* temp_str, bool* str_chr_wait, char str_chr) {
+void str_wait_hit(struct tree_element* root, struct tree_element** active_element, bool* str_cl_wait, bool* str_fin_wait, unsigned int* temp_str, bool* str_chr_wait, char str_chr, int type) {
if(*str_cl_wait) {
if(*str_fin_wait) {
- end_strength(root, active_element, str_chr, *temp_str);
+ end_strength(root, active_element, str_chr, *temp_str, type);
*str_fin_wait = false;
*temp_str = 0;
}
@@ -408,6 +414,7 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
// @todo: These two variables should be globals! Why did I even define them here at all?
struct tree_element* root = new_element();
+ root->allow_inner = false;
struct tree_element* active_element = root;
for(char* cur_char = input; *cur_char != '\0'; cur_char++) {
printf("%c", *cur_char);
@@ -424,11 +431,11 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
}
if(str_chr_wait_ast && *cur_char != '*') {
- str_wait_hit(root, &active_element, &str_cl_wait_ast, &str_fin_wait_ast, &temp_str_ast, &str_chr_wait_ast, '*');
+ str_wait_hit(root, &active_element, &str_cl_wait_ast, &str_fin_wait_ast, &temp_str_ast, &str_chr_wait_ast, '*', t_str_ast);
}
if(str_chr_wait_und && *cur_char != '_') {
- str_wait_hit(root, &active_element, &str_cl_wait_und, &str_fin_wait_und, &temp_str_und, &str_chr_wait_und, '_');
+ str_wait_hit(root, &active_element, &str_cl_wait_und, &str_fin_wait_und, &temp_str_und, &str_chr_wait_und, '_', t_str_und);
}
#define LAST_TO_TITLE(n) {\
@@ -456,7 +463,6 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
// Ignoring \r goes against the commonmark spec, but who cares
break;
case('\n'):
- printf("%d\n", soft_newline_count);
list_waiting = false;
if(dash_chain >= 1) {
active_element = root;
@@ -505,11 +511,14 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
}
}
+ //printf("%d", temp_str_ast);
if(temp_str_ast > 0)
- end_strength(root, &active_element, '*', temp_str_ast);
-
+ end_strength(root, &active_element, '*', temp_str_ast, t_str_ast);
+ ZERO_STR_AST
+ //printf("%d\n", temp_str_und);
if(temp_str_und > 0)
- end_strength(root, &active_element, '_', temp_str_und);
+ end_strength(root, &active_element, '_', temp_str_und, t_str_und);
+ ZERO_STR_UND
if(code_element)
append_char_to_active(root, &active_element, '\n');
@@ -517,8 +526,6 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
hash_chain = 0;
spaces_trimmed = 0;
soft_newline_count = 0;
- ZERO_STR_AST
- ZERO_STR_UND
break;
case('*'):
if((active_element == root || newline) && *(cur_char+1) == ' ' ) {
@@ -712,6 +719,7 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
}
}
// Convert the tree to valid html
+ depth++;
char* html = tree_to_html(root);
//printf("%s\n", html ? html : "" );
// Tear down the tree
@@ -721,6 +729,7 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
}
free(html);
printf("Time to process in ns: %ld\n", (clock() - before) / (CLOCKS_PER_SEC / 1000000));
+ depth--;
return 0;
}
Jeremias Stotters git repositories generated by CGIT