-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <md4c.h>
-
-typedef struct TodoItem {
- char* name;
- char* description;
- char* due;
- char** items;
-} TodoItem;
+
+#include "headers/parser.h"
+
+typedef struct State {
+ Board* board;
+ TodoList* cur_todolist;
+ TodoItem* cur_todoitem;
+ /* for use during processing */
+ char* last_block_text;
+} State;
+
+char* read_file(char* file_name, long* size);
+
+/* processing */
+void enter_todolist();
+void exit_todolist();
+
+void enter_todoitem();
+void exit_todoitem();
/* callbacks to parser */
int enter_block(MD_BLOCKTYPE type, void* detail, void* userdata);
void debug_log(const char* msg, void* userdata);
void syntax(void);
-static MD_PARSER parser = {
+const MD_PARSER parser = {
0,
MD_DIALECT_COMMONMARK,
&enter_block,
&syntax
};
-static char task_md[] = "###";
-int
-main(int argc, char** argv)
-{
- FILE* input_file;
- int todo_length;
+char*
+read_file(char* file_name, long* size)
+{ // apparently using seek isnt the greatest, may change to chunk reading later
+ FILE* file;
+ char* output;
+ long fsize;
+
+ file = fopen(file_name, "r");
+ if (file == NULL) {
+ printf("Something went wrong opening file\n");
+ return NULL;
+ }
+
+ fseek(file, 0, SEEK_END);
+ fsize = ftell(file);
+ fseek(file, 0, SEEK_SET);
- input_file = fopen("test_board.md", "r");
- if (!input_file) {
- printf("Something went wrong opening file");
- return 1;
+ output = malloc(fsize+1); // maybe error check malloc
+ fread(output, 1, fsize, file);
+ if (ferror(file)) {
+ printf("Something went wrong reading file\n");
+ fclose(file);
+ free(output);
+ return NULL;
}
+ output[fsize] = 0; // add null terminator
+
+ /* clean up */
+ fclose(file);
- return 0;
+ /* return */
+ *size = fsize;
+ return output;
+}
+
+Board*
+begin_parse(char* board_path)
+{
+ const char* input_buffer;
+ long input_size;
+ State* state;
+
+ /* read entire file */
+ input_buffer = read_file(board_path, &input_size);
+
+ /* setup state */
+ state = malloc(sizeof(State));
+ state->cur_todolist = NULL;
+ state->cur_todoitem = NULL;
+
+ state->board = malloc(sizeof(Board));
+ state->board->todolist_list = malloc(0);
+ state->board->todolist_count = 0;
+
+ md_parse(input_buffer, input_size, &parser, state);
+
+ free((char*)input_buffer);
+
+ return state->board;
}
int
int
leave_block(MD_BLOCKTYPE type, void* detail, void* userdata)
{
+ State* state;
+ state = (State*)userdata;
+
+ switch (type) {
+
+ case MD_BLOCK_H:
+
+ switch(((MD_BLOCK_H_DETAIL*)detail)->level) {
+
+ case 1:
+ printf("leave h1, %s\n", state->last_block_text);
+
+ case 2:
+ printf("leave h2, %s\n", state->last_block_text);
+ break;
+ case 3:
+ printf("leave h3, %s\n", state->last_block_text);
+ break;
+
+ }
+
+ break;
+
+ // no need for default case for now :>
+ }
return 0;
}
int
text(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* userdata){
+
+ State* state;
+ char* content;
+
+ state = (State*)userdata;
+
+ content = malloc(size*sizeof(char)+1);
+ memcpy(content, text, size);
+ content[size] = 0;
+
+ /* printf("%s\n", content); */
+ state->last_block_text = content;
+
return 0;
}
void
debug_log(const char* msg, void* userdata)
{
+ printf(msg);
return;
}