diff --git a/include/articles_op/article.h b/include/articles_op/article.h new file mode 100644 index 0000000..c6bc63e --- /dev/null +++ b/include/articles_op/article.h @@ -0,0 +1,29 @@ +#ifndef _ARTICLE_H +#define _ARTICLE_H + +#include +#include +#include +#include +#include + +#include "../../include/file_op/file.h" +#include "../../include/utils_op/utils.h" +#include "../../include/articles_op/process_md.h" + +#define LINE_LENGTH 512 + +#ifndef _ARTICLE_INFO +#define _ARTICLE_INFO + +typedef struct +{ + char *title; + long time; + char *content; + unsigned long length; +} article_info; + +#endif + +#endif \ No newline at end of file diff --git a/include/articles_op/process_md.h b/include/articles_op/process_md.h new file mode 100644 index 0000000..be1b83f --- /dev/null +++ b/include/articles_op/process_md.h @@ -0,0 +1,23 @@ +#ifndef _PROCESS_MD_H +#define _PROCESS_MD_H + +#include +#include +#include + +#ifndef _ARTICLE_INFO +#define _ARTICLE_INFO + +typedef struct +{ + char *title; + long time; + char *content; + unsigned long length; +} article_info; + +#endif + +int process_md(article_info article, char **out); + +#endif \ No newline at end of file diff --git a/include/file_op/file.h b/include/file_op/file.h index ebe8b99..fdd97cc 100644 --- a/include/file_op/file.h +++ b/include/file_op/file.h @@ -7,6 +7,7 @@ #include #include #include +#include struct file_s { @@ -18,5 +19,6 @@ struct file_s char *gen_file_path(char *path); int send_file(int cli_fd, struct file_s *file); struct file_s *get_file_info(char *file_path); +size_t get_file_size(FILE *file); #endif \ No newline at end of file diff --git a/include/utils_op/utils.h b/include/utils_op/utils.h index 9dffb7c..08fca2e 100644 --- a/include/utils_op/utils.h +++ b/include/utils_op/utils.h @@ -10,5 +10,6 @@ void err_msg(char *msg); char *concat_to_front(char **str1, char *str2); char *get_status_message(int status_code); char *to_lower(char *str); +char *trim(char *str); #endif \ No newline at end of file diff --git a/src/articles_op/article.c b/src/articles_op/article.c index 5c773d3..3a8cd4e 100644 --- a/src/articles_op/article.c +++ b/src/articles_op/article.c @@ -1,43 +1,4 @@ -#include -#include -#include -#include -#include - -#include "../../include/file_op/file.h" - -#define LINE_LENGTH 512; - -typedef struct -{ - char *title; - long time; - char *content; - unsigned long length; -} article_info; - -char *trim(char *str) -{ - while (str[0] == ' ' || str[0] == '\n' || str[0] == '\t') - { - memmove(str, str + 1, strlen(str)); - } - while (str[strlen(str) - 1] == ' ' || str[strlen(str) - 1] == '\n' || str[strlen(str) - 1] == '\t') - { - str[strlen(str) - 1] = '\0'; - } - - return str; -} - -size_t get_file_size(FILE *file) -{ - fseek(file, 0L, SEEK_END); - size_t size = ftell(file); - fseek(file, 0L, SEEK_SET); - - return size; -} +#include "../../include/articles_op/article.h" int list_articles(article_info **articles) { @@ -141,194 +102,6 @@ long get_article_contents(article_info *article) return article->length; } -int process_md(article_info article, char **out) -{ - *out = malloc(0); - char *tmp = strdup(article.content); - char *buff = strtok(tmp, "\n"); - - printf("%s\n", *out); - - int is_in_list = 0; - - while (buff != NULL) - { - if (strcmp(buff, "***") == 0) - { - char *tmp_out = realloc(*out, strlen(*out) + strlen("
\n") + 1); - if (tmp_out == NULL) - { - perror("Couldn't allocate memory for new element"); - continue; - } - *out = tmp_out; - strcat(*out, "
\n"); - continue; - } - if ((buff[0] == '*' && buff[1] == ' ') || (buff[0] == '-' && buff[1] == ' ') || (buff[0] == '+' && buff[1] == ' ')) - { - char *begining = ""; - if (!is_in_list) - { - char *begining = "
    \n"; - is_in_list = 1; - } - - memmove(buff, buff + 2, strlen(buff) - 1); - - size_t append_line_length = snprintf(NULL, 0, "%s
  • %s
  • \n", begining, buff) + 1; - char *append = malloc(append_line_length); - char *tmp_out = realloc(*out, strlen(*out) + append_line_length); - if (append == NULL || tmp_out == NULL) - { - perror("Couldn't allocate memory for new element"); - continue; - } - *out = tmp_out; - - snprintf(append, append_line_length, "%s
  • %s
  • \n", begining, buff); - strcat(*out, append); - - free(append); - } - else - { - if (is_in_list) - { - char *tmp_out = realloc(*out, strlen(*out) + strlen("
\n") + 1); - if (tmp_out == NULL) - { - perror("Couldn't allocate memory for new element"); - continue; - } - *out = tmp_out; - strcat(*out, "\n"); - } - - if (buff[0] == '#') - { - int n = 1; - while (buff[n] == '#') - n++; - - memmove(buff, buff + n + 1, strlen(buff) - (n + 1) + 1); // n + 1 is to remove space after # - - size_t append_line_length = snprintf(NULL, 0, "%s\n", n, buff, n) + 1; - char *append = malloc(append_line_length); - char *tmp_out = realloc(*out, strlen(*out) + append_line_length); - if (append == NULL || tmp_out == NULL) - { - perror("Couldn't allocate memory for new element"); - continue; - } - *out = tmp_out; - - snprintf(append, append_line_length, "%s\n", n, buff, n); - strcat(*out, append); - - free(append); - } - else if (buff[0] == '>') - { - memmove(buff, buff + 2, strlen(buff) - 1); - - size_t append_line_length = snprintf(NULL, 0, "
%s
\n", buff) + 1; - char *append = malloc(append_line_length); - char *tmp_out = realloc(*out, strlen(*out) + append_line_length); - if (append == NULL || tmp_out == NULL) - { - perror("Couldn't allocate memory for new element"); - continue; - } - *out = tmp_out; - - snprintf(append, append_line_length, "
%s
\n", buff); - strcat(*out, append); - - free(append); - } - else - { - for (int i = 0; i < strlen(buff); i++) - { - if (buff[i] == '*' && buff[i - 1] != '\\' && buff[i + 1] != ' ') - { - int n = 1; - while (buff[n] == '*') - n++; - - char *tag = ""; - if (n == 1) - tag = "i"; - else if (n == 2) - tag = "b"; - else if (n == 2) - tag = "mark"; - - int j = 0; - while (buff[i + j + n] != '*' && buff[i + j + n - 1] != '\\') - j++; - - char *internal = malloc(j + 1); - memcpy(internal, buff + i + n, j); - internal[j] = '\0'; - - size_t append_line_length = snprintf(NULL, 0, "<%s>%s", tag, internal, tag) + 1; - char *append = malloc(append_line_length); - char *tmp_out = realloc(*out, strlen(*out) + append_line_length); - if (append == NULL || tmp_out == NULL) - { - perror("Couldn't allocate memory for new element"); - continue; - } - *out = tmp_out; - - snprintf(append, append_line_length, "<%s>%s", tag, internal, tag); - strcat(*out, append); - - i += n * 2 + j; - - free(append); - free(internal); - } - else - { - size_t len = strlen(*out); - char *tmp_out = realloc(*out, len + 2); - if (tmp_out == NULL) - { - perror("Couldn't allocate more memory to porcess Markdown"); - return -1; - } - *out = tmp_out; - (*out)[len] = buff[i]; - (*out)[len + 1] = '\0'; - } - } - } - } - - size_t len = strlen(*out); - if ((*out)[len - 1] != '\n') - { - char *tmp_out = realloc(*out, len + 2); - if (tmp_out == NULL) - { - perror("Couldn't allocate more memory to porcess Markdown"); - return -1; - } - *out = tmp_out; - (*out)[len] = '\n'; - (*out)[len + 1] = '\0'; - } - - buff = strtok(NULL, "\n"); - } - - // printf("--%s--\n", *out); - return strlen(*out); -} - int gen_html_article(article_info article, char **out) { // FILE *template = fopen("./static/article.html", "r"); diff --git a/src/articles_op/process_md.c b/src/articles_op/process_md.c new file mode 100644 index 0000000..57eb0aa --- /dev/null +++ b/src/articles_op/process_md.c @@ -0,0 +1,297 @@ +#include "../../include/articles_op/process_md.h" + +int process_md(article_info article, char **out) +{ + *out = malloc(1); + (*out)[0] = '\0'; + char *rest = strdup(article.content); + char *buff; + + int is_in_list = 0; + + while ((buff = strtok_r(rest, "\n", &rest)) != NULL) + { + + if (strcmp(buff, "***") == 0) + { + char *tmp_out = realloc(*out, strlen(*out) + strlen("
\n") + 1); + if (tmp_out == NULL) + { + perror("Couldn't allocate memory for new element"); + continue; + } + *out = tmp_out; + strcat(*out, "
\n"); + continue; + } + + if ((buff[0] == '*' && buff[1] == ' ') || (buff[0] == '-' && buff[1] == ' ') || (buff[0] == '+' && buff[1] == ' ')) + { + char *begining = ""; + if (!is_in_list) + { + begining = "
    \n"; + is_in_list = 1; + } + + memmove(buff, buff + 2, strlen(buff) - 1); + + size_t append_line_length = snprintf(NULL, 0, "%s
  • %s
  • \n", begining, buff) + 1; + char *append = malloc(append_line_length); + char *tmp_out = realloc(*out, strlen(*out) + append_line_length); + if (append == NULL || tmp_out == NULL) + { + perror("Couldn't allocate memory for new element"); + continue; + } + *out = tmp_out; + + snprintf(append, append_line_length, "%s
  • %s
  • \n", begining, buff); + strcat(*out, append); + + free(append); + continue; + } + + if (is_in_list) + { + char *tmp_out = realloc(*out, strlen(*out) + strlen("
\n") + 1); + if (tmp_out == NULL) + { + perror("Couldn't allocate memory for new element"); + continue; + } + *out = tmp_out; + strcat(*out, "\n"); + is_in_list = 0; + } + + if (buff[0] == '#') + { + int n = 1; + while (buff[n] == '#') + n++; + int k = (n > 6) ? 6 : n; + + memmove(buff, buff + n + 1, strlen(buff) - (n + 1) + 1); // n + 1 is to remove space after # + + size_t append_line_length = snprintf(NULL, 0, "%s\n", k, buff, k) + 1; + char *append = malloc(append_line_length); + char *tmp_out = realloc(*out, strlen(*out) + append_line_length); + if (append == NULL || tmp_out == NULL) + { + perror("Couldn't allocate memory for new element"); + continue; + } + *out = tmp_out; + + snprintf(append, append_line_length, "%s\n", k, buff, k); + strcat(*out, append); + + free(append); + continue; + } + + if (buff[0] == '>') + { + memmove(buff, buff + 2, strlen(buff) - 1); + + size_t append_line_length = snprintf(NULL, 0, "
%s
\n", buff) + 1; + char *append = malloc(append_line_length); + char *tmp_out = realloc(*out, strlen(*out) + append_line_length); + if (append == NULL || tmp_out == NULL) + { + perror("Couldn't allocate memory for new element"); + continue; + } + *out = tmp_out; + + snprintf(append, append_line_length, "
%s
\n", buff); + strcat(*out, append); + + free(append); + continue; + } + + char *tmp_out = realloc(*out, strlen(*out) + strlen("

") + 1); + if (tmp_out == NULL) + { + perror("Couldn't allocate memory for new element"); + continue; + } + *out = tmp_out; + + strcat(*out, "

"); + + for (int i = 0; i < strlen(buff); i++) + { + if (buff[i] == '*' && buff[i - 1] != '\\' && buff[i + 1] != ' ') + { + int n = 1; + while (buff[i + n] == '*') + n++; + + char *tag = ""; + if (n == 1) + tag = "i"; + else if (n == 2) + tag = "b"; + else if (n == 3) + tag = "mark"; + + int j = 0; + while (buff[i + j + n] != '*' && buff[i + j + n - 1] != '\\') + j++; + + char *internal = malloc(j + 1); + memcpy(internal, buff + i + n, j); + internal[j] = '\0'; + + size_t append_line_length = snprintf(NULL, 0, "<%s>%s ", tag, internal, tag) + 1; + char *append = malloc(append_line_length); + char *tmp_out = realloc(*out, strlen(*out) + append_line_length); + if (append == NULL || tmp_out == NULL) + { + perror("Couldn't allocate memory for new element"); + continue; + } + *out = tmp_out; + + snprintf(append, append_line_length, "<%s>%s ", tag, internal, tag); + strcat(*out, append); + + i += n * 2 + j; + + free(append); + free(internal); + + continue; + } + + if (buff[i] == '!' && buff[i + 1] == '[') + { + int n = 0; + while (buff[i + 2 + n] != ']') + n++; + + if (buff[i + 2 + n + 1] != '(') + continue; + + int k = 0; + while (buff[i + 2 + n + 2 + k] != ')') + k++; + + char *internal_text = malloc(n + 1); + memcpy(internal_text, buff + i + 2, n); + internal_text[n] = '\0'; + + char *src = malloc(k + 1); + memcpy(src, buff + i + 2 + n + 2, k); + src[k] = '\0'; + + size_t append_line_length = snprintf(NULL, 0, "\"%s\" ", src, internal_text) + 1; + char *append = malloc(append_line_length); + char *tmp_out = realloc(*out, strlen(*out) + append_line_length); + if (append == NULL || tmp_out == NULL) + { + perror("Couldn't allocate memory for new element"); + continue; + } + *out = tmp_out; + + snprintf(append, append_line_length, "\"%s\" ", src, internal_text); + strcat(*out, append); + + i += i + 2 + n + 2 + k + 1; + + free(append); + free(src); + free(internal_text); + + continue; + } + + if (buff[i] == '[') + { + int n = 0; + while (buff[i + 1 + n] != ']') + n++; + + if (buff[i + 1 + n + 1] != '(') + continue; + + int k = 0; + while (buff[i + 1 + n + 2 + k] != ')') + k++; + + char *internal_text = malloc(n + 1); + memcpy(internal_text, buff + i + 1, n); + internal_text[n] = '\0'; + + char *href = malloc(k + 1); + memcpy(href, buff + i + 1 + n + 2, k); + href[k] = '\0'; + + size_t append_line_length = snprintf(NULL, 0, "%s ", href, internal_text) + 1; + char *append = malloc(append_line_length); + char *tmp_out = realloc(*out, strlen(*out) + append_line_length); + if (append == NULL || tmp_out == NULL) + { + perror("Couldn't allocate memory for new element"); + continue; + } + *out = tmp_out; + + snprintf(append, append_line_length, "%s ", href, internal_text); + strcat(*out, append); + + i += i + 1 + n + 2 + k + 1; + + free(append); + free(href); + free(internal_text); + + continue; + } + + size_t len = strlen(*out); + char *tmp_out = realloc(*out, len + 2); + if (tmp_out == NULL) + { + perror("Couldn't allocate more memory to porcess Markdown"); + return -1; + } + *out = tmp_out; + (*out)[len] = buff[i]; + (*out)[len + 1] = '\0'; + } + + tmp_out = realloc(*out, strlen(*out) + strlen("

") + 1); + if (tmp_out == NULL) + { + perror("Couldn't allocate memory for new element"); + continue; + } + *out = tmp_out; + + strcat(*out, "

"); + + size_t len = strlen(*out); + if ((*out)[len - 1] != '\n') + { + char *tmp_out = realloc(*out, len + 2); + if (tmp_out == NULL) + { + perror("Couldn't allocate more memory to porcess Markdown"); + return -1; + } + *out = tmp_out; + (*out)[len] = '\n'; + (*out)[len + 1] = '\0'; + } + } + + printf("%s\n\n***\n", *out); + + return strlen(*out); +} diff --git a/src/file_op/file.c b/src/file_op/file.c index 326b9c0..f06aee1 100644 --- a/src/file_op/file.c +++ b/src/file_op/file.c @@ -78,4 +78,19 @@ struct file_s *get_file_info(char *file_path) file->size = stat_buf.st_size; return file; +} + +/** + * @brief Get the file size + * + * @param file + * @return size_t + */ +size_t get_file_size(FILE *file) +{ + fseek(file, 0L, SEEK_END); + size_t size = ftell(file); + fseek(file, 0L, SEEK_SET); + + return size; } \ No newline at end of file diff --git a/src/utils_op/utils.c b/src/utils_op/utils.c index 12369fd..426e11b 100644 --- a/src/utils_op/utils.c +++ b/src/utils_op/utils.c @@ -64,5 +64,25 @@ char *to_lower(char *str) for (char *p = str; *p != '\0'; p++) *p = tolower(*p); + return str; +} + +/** + * @brief Remove unneded spaces at the begining and ending of string + * + * @param str + * @return char* + */ +char *trim(char *str) +{ + while (str[0] == ' ' || str[0] == '\n' || str[0] == '\t') + { + memmove(str, str + 1, strlen(str)); + } + while (str[strlen(str) - 1] == ' ' || str[strlen(str) - 1] == '\n' || str[strlen(str) - 1] == '\t') + { + str[strlen(str) - 1] = '\0'; + } + return str; } \ No newline at end of file