Added paragraph, image and link detection to porcess_md function. Refactored code a bit
This commit is contained in:
parent
06ff0761fc
commit
4c61d24352
29
include/articles_op/article.h
Normal file
29
include/articles_op/article.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#ifndef _ARTICLE_H
|
||||||
|
#define _ARTICLE_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
#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
|
23
include/articles_op/process_md.h
Normal file
23
include/articles_op/process_md.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef _PROCESS_MD_H
|
||||||
|
#define _PROCESS_MD_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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
|
@ -7,6 +7,7 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
struct file_s
|
struct file_s
|
||||||
{
|
{
|
||||||
@ -18,5 +19,6 @@ struct file_s
|
|||||||
char *gen_file_path(char *path);
|
char *gen_file_path(char *path);
|
||||||
int send_file(int cli_fd, struct file_s *file);
|
int send_file(int cli_fd, struct file_s *file);
|
||||||
struct file_s *get_file_info(char *file_path);
|
struct file_s *get_file_info(char *file_path);
|
||||||
|
size_t get_file_size(FILE *file);
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -10,5 +10,6 @@ void err_msg(char *msg);
|
|||||||
char *concat_to_front(char **str1, char *str2);
|
char *concat_to_front(char **str1, char *str2);
|
||||||
char *get_status_message(int status_code);
|
char *get_status_message(int status_code);
|
||||||
char *to_lower(char *str);
|
char *to_lower(char *str);
|
||||||
|
char *trim(char *str);
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,43 +1,4 @@
|
|||||||
#include <stdio.h>
|
#include "../../include/articles_op/article.h"
|
||||||
#include <time.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int list_articles(article_info **articles)
|
int list_articles(article_info **articles)
|
||||||
{
|
{
|
||||||
@ -141,194 +102,6 @@ long get_article_contents(article_info *article)
|
|||||||
return article->length;
|
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("<hr>\n") + 1);
|
|
||||||
if (tmp_out == NULL)
|
|
||||||
{
|
|
||||||
perror("Couldn't allocate memory for new element");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
*out = tmp_out;
|
|
||||||
strcat(*out, "<hr>\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if ((buff[0] == '*' && buff[1] == ' ') || (buff[0] == '-' && buff[1] == ' ') || (buff[0] == '+' && buff[1] == ' '))
|
|
||||||
{
|
|
||||||
char *begining = "";
|
|
||||||
if (!is_in_list)
|
|
||||||
{
|
|
||||||
char *begining = "<ul>\n";
|
|
||||||
is_in_list = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
memmove(buff, buff + 2, strlen(buff) - 1);
|
|
||||||
|
|
||||||
size_t append_line_length = snprintf(NULL, 0, "%s<li>%s</li>\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<li>%s</li>\n", begining, buff);
|
|
||||||
strcat(*out, append);
|
|
||||||
|
|
||||||
free(append);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (is_in_list)
|
|
||||||
{
|
|
||||||
char *tmp_out = realloc(*out, strlen(*out) + strlen("</ul>\n") + 1);
|
|
||||||
if (tmp_out == NULL)
|
|
||||||
{
|
|
||||||
perror("Couldn't allocate memory for new element");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
*out = tmp_out;
|
|
||||||
strcat(*out, "</ul>\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, "<h%d>%s</h%d>\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, "<h%d>%s</h%d>\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, "<blockquote>%s</blockquote>\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, "<blockquote>%s</blockquote>\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</%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</%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)
|
int gen_html_article(article_info article, char **out)
|
||||||
{
|
{
|
||||||
// FILE *template = fopen("./static/article.html", "r");
|
// FILE *template = fopen("./static/article.html", "r");
|
||||||
|
297
src/articles_op/process_md.c
Normal file
297
src/articles_op/process_md.c
Normal file
@ -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("<hr>\n") + 1);
|
||||||
|
if (tmp_out == NULL)
|
||||||
|
{
|
||||||
|
perror("Couldn't allocate memory for new element");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
*out = tmp_out;
|
||||||
|
strcat(*out, "<hr>\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((buff[0] == '*' && buff[1] == ' ') || (buff[0] == '-' && buff[1] == ' ') || (buff[0] == '+' && buff[1] == ' '))
|
||||||
|
{
|
||||||
|
char *begining = "";
|
||||||
|
if (!is_in_list)
|
||||||
|
{
|
||||||
|
begining = "<ul>\n";
|
||||||
|
is_in_list = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
memmove(buff, buff + 2, strlen(buff) - 1);
|
||||||
|
|
||||||
|
size_t append_line_length = snprintf(NULL, 0, "%s<li>%s</li>\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<li>%s</li>\n", begining, buff);
|
||||||
|
strcat(*out, append);
|
||||||
|
|
||||||
|
free(append);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_in_list)
|
||||||
|
{
|
||||||
|
char *tmp_out = realloc(*out, strlen(*out) + strlen("</ul>\n") + 1);
|
||||||
|
if (tmp_out == NULL)
|
||||||
|
{
|
||||||
|
perror("Couldn't allocate memory for new element");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
*out = tmp_out;
|
||||||
|
strcat(*out, "</ul>\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, "<h%d>%s</h%d>\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, "<h%d>%s</h%d>\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, "<blockquote>%s</blockquote>\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, "<blockquote>%s</blockquote>\n", buff);
|
||||||
|
strcat(*out, append);
|
||||||
|
|
||||||
|
free(append);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *tmp_out = realloc(*out, strlen(*out) + strlen("<p>") + 1);
|
||||||
|
if (tmp_out == NULL)
|
||||||
|
{
|
||||||
|
perror("Couldn't allocate memory for new element");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
*out = tmp_out;
|
||||||
|
|
||||||
|
strcat(*out, "<p>");
|
||||||
|
|
||||||
|
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</%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</%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, "<img src=\"%s\" alt=\"%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, "<img src=\"%s\" alt=\"%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, "<a href=\"%s\">%s</a> ", 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, "<a href=\"%s\">%s</a> ", 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("</p>") + 1);
|
||||||
|
if (tmp_out == NULL)
|
||||||
|
{
|
||||||
|
perror("Couldn't allocate memory for new element");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
*out = tmp_out;
|
||||||
|
|
||||||
|
strcat(*out, "</p>");
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
@ -79,3 +79,18 @@ struct file_s *get_file_info(char *file_path)
|
|||||||
|
|
||||||
return file;
|
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;
|
||||||
|
}
|
@ -66,3 +66,23 @@ char *to_lower(char *str)
|
|||||||
|
|
||||||
return str;
|
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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user