aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremias Stotter <jeremias@stotter.eu>2022-01-23 14:39:00 +0100
committerJeremias Stotter <jeremias@stotter.eu>2022-01-23 14:39:00 +0100
commit2f1aa02d5578bd1bc33a16e6501b13113d66d0cb (patch)
tree476d02910bcc4a44dfd7dcc9d2f8bd664ec50bc7
parentb9327be7aaae902a2e89deb9f118d0331281608f (diff)
downloadJBlog-2f1aa02d5578bd1bc33a16e6501b13113d66d0cb.tar.gz
JBlog-2f1aa02d5578bd1bc33a16e6501b13113d66d0cb.tar.bz2
JBlog-2f1aa02d5578bd1bc33a16e6501b13113d66d0cb.zip
Strength now works with underscores _ !
-rw-r--r--md.c144
1 files changed, 79 insertions, 65 deletions
diff --git a/md.c b/md.c
index 44fd7cb..6cc992b 100644
--- a/md.c
+++ b/md.c
@@ -175,7 +175,7 @@ char* tree_to_html(struct tree_element* root) {
break;
default:
html = realloc_for_html("");
- strcpy(html, inner_html);
+ strcpy(html, inner_html ? inner_html : "");
break;
}
break;
@@ -255,11 +255,29 @@ struct tree_element* find_parent_type(struct tree_element* root, struct tree_ele
return NULL;
}
+// These store temporary strength values
+unsigned int temp_str_ast = 0;
+unsigned int temp_str_und = 0;
+
+// These are true if a strength element is waiting for closing
+bool str_cl_wait_ast = false;
+bool str_cl_wait_und = false;
+
+// These are true if a strength element waits for a different character
+bool str_chr_wait_ast = false;
+bool str_chr_wait_und = false;
+
+bool str_fin_wait_ast = false;
+bool str_fin_wait_und = false;
+
+#define ZERO_STR_AST temp_str_ast = 0; str_cl_wait_ast = false; str_chr_wait_ast = false; str_fin_wait_ast = 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);
- *active_element = parent_strength->parent;
+ 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++)
@@ -280,6 +298,56 @@ void end_strength(struct tree_element* root, struct tree_element** active_elemen
}
+// 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) {
+ if(*str_cl_wait) {
+ if(*str_fin_wait) {
+ end_strength(root, active_element, str_chr, *temp_str);
+ *str_fin_wait = false;
+ *temp_str = 0;
+ }
+ *str_cl_wait = false;
+ } else
+ *str_cl_wait = true;
+
+ *str_chr_wait = false;
+}
+
+void str_chr_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, int desired_type) {
+ struct tree_element* parent_strength = find_parent_type(root, *active_element, desired_type);
+ if(parent_strength) {
+ // We are already in a strength element
+ // Check if we are waiting to close
+ if(*str_cl_wait) {
+ (*(unsigned int*)(parent_strength->value))++;
+ if((*(unsigned int*)(parent_strength->value)) >= *temp_str) {
+ *active_element = parent_strength->parent;
+ ZERO_STR_AST
+ return;
+ } else {
+ *str_fin_wait = true;
+ }
+ } else {
+ (*temp_str)++;
+ }
+ } else {
+ // Enter a new strength element as we are currently not in one
+ if((*active_element)->type == t_inner)
+ *active_element = (*active_element)->parent;
+ if(*active_element == root) {
+ *active_element = new_child(root, -1);
+ (*active_element)->type = t_p;
+ (*active_element)->allow_inner = true;
+ }
+ *active_element = new_child(*active_element, -1);
+ (*active_element)->type = desired_type;
+ (*active_element)->allow_inner = true;
+ (*active_element)->value = calloc(sizeof(unsigned int), 1);
+ *temp_str = 1;
+ }
+ *str_chr_wait = true;
+}
+
// 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
int parse_markdown(char* input, char* buffer, size_t buffer_size) {
clock_t before = clock();
@@ -294,24 +362,7 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
int spaces_trimmed = 0;
int root_on_newline = false;
- // These store temporary strength values
- unsigned int temp_str_ast = 0;
- unsigned int temp_str_und = 0;
-
- // These are true if a strength element is waiting for closing
- bool str_cl_wait_ast = false;
- bool str_cl_wait_und = false;
-
- // These are true if a strength element waits for a different character
- bool str_chr_wait_ast = false;
- bool str_chr_wait_und = false;
-
- bool str_fin_wait_ast = false;
- bool str_fin_wait_und = false;
-
-#define ZERO_STR_AST temp_str_ast = 0; str_cl_wait_ast = false; str_chr_wait_ast = false; str_fin_wait_ast = false;
-#define ZERO_STR_UND temp_str_und = 0; str_cl_wait_und = false; str_chr_wait_und = false; str_fin_wait_und = false;
-
+ // @todo: These two variables should be globals! Why did I even define them here at all?
struct tree_element* root = new_element();
struct tree_element* active_element = root;
for(char* cur_char = input; *cur_char != '\0'; cur_char++) {
@@ -330,17 +381,11 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
}
if(str_chr_wait_ast && *cur_char != '*') {
- if(str_cl_wait_ast) {
- if(str_fin_wait_ast) {
- end_strength(root, &active_element, '*', temp_str_ast);
- str_fin_wait_ast = false;
- temp_str_ast = 0;
- }
- str_cl_wait_ast = false;
- } else
- str_cl_wait_ast = true;
-
- str_chr_wait_ast = false;
+ str_wait_hit(root, &active_element, &str_cl_wait_ast, &str_fin_wait_ast, &temp_str_ast, &str_chr_wait_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, '_');
}
#define LAST_TO_TITLE(n) {\
@@ -423,42 +468,11 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
if((active_element == root || newline) && *(cur_char+1) == ' ' ) {
list_waiting = true;
} else {
- struct tree_element* parent_strength = find_parent_type(root, active_element, t_str_ast);
- if(parent_strength) {
- // We are already in a strength element
- // Check if we are waiting to close
- if(str_cl_wait_ast) {
- (*(unsigned int*)(parent_strength->value))++;
- if((*(unsigned int*)(parent_strength->value)) >= temp_str_ast) {
- active_element = parent_strength->parent;
- ZERO_STR_AST
- break;
- } else {
- str_fin_wait_ast = true;
- }
- } else {
- temp_str_ast++;
- }
- } else {
- // Enter a new strength element as we are currently not in one
- if(active_element->type == t_inner)
- active_element = active_element->parent;
- if(active_element == root) {
- active_element = new_child(root, -1);
- active_element->type = t_p;
- active_element->allow_inner = true;
- }
- active_element = new_child(active_element, -1);
- active_element->type = t_str_ast;
- active_element->allow_inner = true;
- active_element->value = calloc(sizeof(unsigned int), 1);
- temp_str_ast = 1;
- }
- str_chr_wait_ast = true;
+ str_chr_hit(root, &active_element, &str_cl_wait_ast, &str_fin_wait_ast, &temp_str_ast, &str_chr_wait_ast, t_str_ast);
}
break;
- case('_'):
-
+ case('_'):
+ str_chr_hit(root, &active_element, &str_cl_wait_und, &str_fin_wait_und, &temp_str_und, &str_chr_wait_und, t_str_und);
break;
case('`'):
break;
Jeremias Stotters git repositories generated by CGIT