Added markdown code tag support
This commit is contained in:
parent
cbdfcae15b
commit
d57a642d69
@ -7,10 +7,54 @@ int process_md(article_info article, char **out)
|
|||||||
char *rest = strdup(article.content), *free_rest = rest;
|
char *rest = strdup(article.content), *free_rest = rest;
|
||||||
char *buff;
|
char *buff;
|
||||||
|
|
||||||
|
int is_in_code = 0;
|
||||||
int is_in_list = 0;
|
int is_in_list = 0;
|
||||||
|
|
||||||
while ((buff = strtok_r(rest, "\n", &rest)) != NULL)
|
while ((buff = strtok_r(rest, "\n", &rest)) != NULL)
|
||||||
{
|
{
|
||||||
|
if (strncmp(buff, "```", 3) == 0)
|
||||||
|
{
|
||||||
|
char *snippet;
|
||||||
|
|
||||||
|
if (is_in_code)
|
||||||
|
{
|
||||||
|
snippet = "</pre></code>\n";
|
||||||
|
is_in_code = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snippet = "<pre><code>\n";
|
||||||
|
is_in_code = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *tmp_out = realloc(*out, strlen(*out) + strlen(snippet) + 1);
|
||||||
|
if (tmp_out == NULL)
|
||||||
|
{
|
||||||
|
perror("Couldn't allocate memory for new element");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
*out = tmp_out;
|
||||||
|
strcat(*out, snippet);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_in_code)
|
||||||
|
{
|
||||||
|
char *tmp_out = realloc(*out, strlen(*out) + strlen(buff) + strlen("<br/>") + 1);
|
||||||
|
if (tmp_out == NULL)
|
||||||
|
{
|
||||||
|
perror("Couldn't allocate memory for new element");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
*out = tmp_out;
|
||||||
|
|
||||||
|
strcat(*out, buff);
|
||||||
|
strcat(*out, "<br/>");
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ((buff[0] == '*' && buff[1] == ' ') || (buff[0] == '-' && buff[1] == ' ') || (buff[0] == '+' && buff[1] == ' '))
|
if ((buff[0] == '*' && buff[1] == ' ') || (buff[0] == '-' && buff[1] == ' ') || (buff[0] == '+' && buff[1] == ' '))
|
||||||
{
|
{
|
||||||
char *begining = "";
|
char *begining = "";
|
||||||
@ -38,7 +82,7 @@ int process_md(article_info article, char **out)
|
|||||||
free(append);
|
free(append);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_in_list)
|
if (is_in_list)
|
||||||
{
|
{
|
||||||
char *tmp_out = realloc(*out, strlen(*out) + strlen("</ul>\n") + 1);
|
char *tmp_out = realloc(*out, strlen(*out) + strlen("</ul>\n") + 1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user