From 8fb19bb7969617821e9c2c1c64d113ff2c26abaf Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Wed, 30 Dec 2020 14:09:53 -0500 Subject: [PATCH] most parsing done --- headers/parser.h | 1 + parser.c | 80 +++++++++++++++++++++++++++++++++++++++++------- taskasaur.c | 28 ++++++++++++++++- 3 files changed, 97 insertions(+), 12 deletions(-) diff --git a/headers/parser.h b/headers/parser.h index f2a9632..db4bf8b 100644 --- a/headers/parser.h +++ b/headers/parser.h @@ -2,6 +2,7 @@ #include #include #include +#include #include typedef enum SubTaskState { diff --git a/parser.c b/parser.c index 0bc2d91..e0564c1 100644 --- a/parser.c +++ b/parser.c @@ -132,6 +132,8 @@ exit_todolist(State* state) return; } + exit_todoitem(state); + /* append new todolist to board */ board = state->board; todolist_list = board->todolist_list; @@ -149,19 +151,47 @@ exit_todolist(State* state) void enter_todoitem(State* state, char* item_name) { + TodoItem* new_todoitem; + + new_todoitem = malloc(sizeof(TodoItem)); + new_todoitem->item_name = item_name; + new_todoitem->description = NULL; + new_todoitem->due = NULL; + new_todoitem->subtask_list = malloc(0); + new_todoitem->subtask_count = 0; + state->cur_todoitem = new_todoitem; + } void exit_todoitem(State* state) { + TodoList* todolist; + TodoItem** item_list; + + if (state->cur_todoitem == NULL) { + return; + } + + /* append current item to todo list */ + todolist = state->cur_todolist; + item_list = todolist->item_list; + + todolist->item_count += 1; + item_list = realloc(item_list, todolist->item_count*sizeof(TodoItem*)); + item_list[todolist->item_count-1] = state->cur_todoitem; + state->cur_todoitem = NULL; + + /* save */ + todolist->item_list = item_list; } void set_description(State* state, char* description) { - + state->cur_todoitem->description = description; } void @@ -173,6 +203,22 @@ set_due(State* state, char* due) void add_subtask(State* state, char* subtask_name, SubTaskState subtask_state) { + SubTask* new_subtask; + SubTask** subtask_list; + + /* create new subtask */ + new_subtask = malloc(sizeof(SubTask)); + new_subtask->subtask_name = subtask_name; + new_subtask->done = subtask_state; + + /* add it */ + subtask_list = state->cur_todoitem->subtask_list; + + state->cur_todoitem->subtask_count += 1; + subtask_list = realloc(subtask_list, state->cur_todoitem->subtask_count*sizeof(SubTask*)); + subtask_list[state->cur_todoitem->subtask_count-1] = new_subtask; + + state->cur_todoitem->subtask_list = subtask_list; } @@ -190,32 +236,44 @@ leave_block(MD_BLOCKTYPE type, void* detail, void* userdata) switch (type) { - case MD_BLOCK_H: + case MD_BLOCK_H:; - switch(((MD_BLOCK_H_DETAIL*)detail)->level) { + MD_BLOCK_H_DETAIL* h_detail; + h_detail = ((MD_BLOCK_H_DETAIL*)detail); + + switch(h_detail->level) { case 1: - printf("leave h1, %s\n", state->last_block_text); - + break; + case 2: exit_todolist(state); enter_todolist(state, state->last_block_text); - printf("leave h2, %s\n", state->last_block_text); break; + case 3: - printf("leave h3, %s\n", state->last_block_text); + exit_todoitem(state); + enter_todoitem(state, state->last_block_text); break; - } break; case MD_BLOCK_QUOTE: - printf("blockquote, %s\n", state->last_block_text); + set_description(state, state->last_block_text); break; - case MD_BLOCK_LI: - printf("todo, %s\n", state->last_block_text); + case MD_BLOCK_LI:; + MD_BLOCK_LI_DETAIL* li_detail; + SubTaskState done; + + li_detail = ((MD_BLOCK_LI_DETAIL*)detail); + + assert(li_detail->is_task != 0); // all lists should be task lists + + done = li_detail->task_mark == ' ' ? SubTaskState_todo : SubTaskState_done; + + add_subtask(state, state->last_block_text, done); break; // no need for default case for now :> diff --git a/taskasaur.c b/taskasaur.c index 2097e63..50b72d4 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -7,7 +7,33 @@ main(int argc, char** argv) board = begin_parse("test_board.md"); - printf("%d\n", board->todolist_count); + for (int i = 0; i < board->todolist_count; i++) { + TodoList* todolist; + printf("List =-=-=-=-=-==-=-=-=-=-=-\n"); + todolist = board->todolist_list[i]; + printf("List name: %s\n", todolist->list_name); + printf("Num of items: %d\n", todolist->item_count); + + for (int j = 0; j < todolist->item_count; j++) { + TodoItem* todoitem; + printf("Item =-=-=-=-=-\n"); + todoitem = todolist->item_list[j]; + printf("Item name: %s\n", todoitem->item_name); + printf("Description: %s\n", todoitem->description); + printf("Num of subtasks: %d\n", todoitem->subtask_count); + + for (int k = 0; k < todoitem->subtask_count; k++) { + SubTask* subtask; + int done; + + subtask = todoitem->subtask_list[k]; + printf("Subtask: %s, %d\n", subtask->subtask_name, subtask->done); + } + } + + } + /* printf("%d\n", board->todolist_count); */ + /* printf("%s\n", board->todolist_list[1]->list_name); */ return 0; } -- 2.20.1