aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremias Stotter <jeremias@stotter.eu>2022-01-30 21:19:36 +0100
committerJeremias Stotter <jeremias@stotter.eu>2022-01-30 21:19:36 +0100
commitfdef5848e48a1a6892b025f58596fcacf6b999f8 (patch)
treefc2dfedd76daef1ff6511ff45e5012e36185c6f7
parent947f2dd1e4cac6ae3b0d960b4a43145bc10cb0b1 (diff)
downloadJBlog-fdef5848e48a1a6892b025f58596fcacf6b999f8.tar.gz
JBlog-fdef5848e48a1a6892b025f58596fcacf6b999f8.tar.bz2
JBlog-fdef5848e48a1a6892b025f58596fcacf6b999f8.zip
Added full-line code support
Inline is next
-rw-r--r--md.c50
1 files changed, 41 insertions, 9 deletions
diff --git a/md.c b/md.c
index 11a2df2..3a25924 100644
--- a/md.c
+++ b/md.c
@@ -51,7 +51,8 @@ enum html_type {
t_ol,
t_ul, // Value contains the ammount of spaces used to indent at that level
t_li,
- t_hr
+ t_hr,
+ t_code
};
// The file will be structured as a tree that, in the end will be converted to valid html
@@ -158,6 +159,7 @@ char* realloc_append(char* dest, char* src) {
#define ul_html "<ul>%s</ul>\n"
#define li_html "<li>%s</li>\n"
#define hr_html "<hr/>\n"
+#define code_html "<pre><code><p>%s</p></code></pre>\n"
// Resolve a tree to html
char* tree_to_html(struct tree_element* root) {
char* html=NULL;
@@ -224,6 +226,10 @@ char* tree_to_html(struct tree_element* root) {
html = realloc_for_html(hr_html);
strcpy(html, hr_html);
break;
+ case(t_code):
+ html = realloc_for_html(code_html);
+ sprintf(html, code_html, inner_html ? inner_html : "");
+ break;
default:
html=inner_html;
break;
@@ -386,6 +392,8 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
bool escaped = false;
bool newline = false;
bool list_waiting = false;
+ // This will be set to some non-null value when there is a code element to return to
+ struct tree_element* code_element = NULL;
int hash_chain = 0;
int dash_chain = 0;
@@ -398,9 +406,9 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
struct tree_element* active_element = root;
for(char* cur_char = input; *cur_char != '\0'; cur_char++) {
printf("%c", *cur_char);
- if(escaped) {
+ if(escaped || (code_element && *cur_char != '\n' && *cur_char != '\r' && !newline)) {
escaped = false;
- if(*cur_char == '\n' || *cur_char == '\r') {
+ if((*cur_char == '\n' || *cur_char == '\r')) {
if(active_element->parent)
active_element = active_element->parent;
struct tree_element* br_child = new_child(active_element, -1);
@@ -436,9 +444,9 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
escaped = true;
break;
// Tabs
- case('\t'):
+ /*case('\t'):
- break;
+ break;*/
// Newline
case('\r'):
// Ignoring \r goes against the commonmark spec, but who cares
@@ -480,6 +488,8 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
active_element = root;
newline = false;
root_on_newline = false;
+ code_element = NULL;
+ active_element = root;
} else if(active_element != root) {
newline = true;
}
@@ -490,6 +500,9 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
if(temp_str_und > 0)
end_strength(root, &active_element, '_', temp_str_und);
+ if(code_element)
+ append_char_to_active(root, &active_element, '\n');
+
hash_chain = 0;
spaces_trimmed = 0;
ZERO_STR_AST
@@ -597,6 +610,23 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
}
// Default character handling
default:
+ if(spaces_trimmed >= 4 && (newline || active_element == root)) {
+ // if code_element is set the new active element is just returned to the code element
+ if(code_element) {
+ active_element = code_element;
+ append_char_to_active(root, &active_element, '\n');
+ } else {
+ active_element = new_child(root, -1);
+ active_element->type = t_code;
+ active_element->allow_inner = true;
+ code_element = active_element;
+ }
+ for(int i = 0; i < spaces_trimmed - 4; i++)
+ append_char_to_active(root, &active_element, ' ');
+ } else if(code_element) {
+ active_element = root;
+ code_element = false;
+ }
if(hash_chain > 0) {
for(int i = 0; i < hash_chain; i++)
append_char_to_active(root, &active_element, '#');
@@ -613,11 +643,13 @@ int parse_markdown(char* input, char* buffer, size_t buffer_size) {
eq_chain = 0;
}
- if(newline && root_on_newline) {
- active_element = root;
- root_on_newline = false;
+ if(newline) {
+ if(root_on_newline) {
+ active_element = root;
+ root_on_newline = false;
+ } else
+ append_char_to_active(root, &active_element, ' ');
}
-
newline = false;
list_waiting = false;
append_char_to_active(root, &active_element, *cur_char);
Jeremias Stotters git repositories generated by CGIT