refacto + liste 1 profondeur non num

This commit is contained in:
nemo 2024-09-09 21:07:11 +02:00
parent 959aa67bf2
commit 03090cf86e
2 changed files with 97 additions and 16 deletions

103
main.c
View File

@ -3,6 +3,8 @@
#include <assert.h>
#include <string.h>
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define CHECK_MD_BOUND(md) (md->in_cursor < md->in_cap && md->out_cursor < md->out_cap)
typedef struct MegaData {
@ -12,53 +14,123 @@ typedef struct MegaData {
int out_cap;
int in_cursor;
int out_cursor;
int list_level;
int list_item_added;
} MegaData;
void skip_begin(MegaData *md)
#define md_append(md, str) md_append_imp(md, str, sizeof(str)-1)
void md_append_imp(MegaData *md, const char *str, const int len)
{
for (int i=0; i<len; i++) {
md->out[++md->out_cursor] = str[i];
}
}
int skip_begin(MegaData *md)
{
int before = md->in_cursor;
while (CHECK_MD_BOUND(md) && (md->in[md->in_cursor] == ' ' || md->in[md->in_cursor] == '\t' || md->in[md->in_cursor] == '\n')) {
md->out[md->out_cursor] = md->in[md->in_cursor];
md->in_cursor++;
md->out_cursor++;
// NOTE: c'est tres moche, la balise ne devrait pas etre ajouter ici
// j'ai mis ça la parce que skip_begin() saute meme les lignes vides et j'ai pas le temps de tout refacto avant ce soir
// donc on peut pas se positioner juste apres une list et en general sa ajoute la balise apres un autre element
// TODO: refacto cet fonction
// deplacer l'ajout de cet balise au bon endroit
// gerer le probleme de cursor ++ ou --
// liste dans list
// peut etre lire le livre sur ecrire un interpreteur avant d'essayer de lexer mes couilles comme ça
if (md->list_item_added && md->in[md->in_cursor] == '\n') {
md->out_cursor--;
md_append(md, "</ul>\n");
}
}
md->in_cursor--;
return md->in_cursor - before;
}
void skip_endline(MegaData *md)
int skip_line(MegaData *md)
{
int before = md->in_cursor;
while (CHECK_MD_BOUND(md) && md->in[md->in_cursor] != '\n') {
md->out[md->out_cursor] = md->in[md->in_cursor];
md->in_cursor++;
md->out_cursor++;
}
return md->in_cursor - before;
}
int skip_endline(MegaData *md)
{
md->out_cursor++;
int before = md->in_cursor;
while (CHECK_MD_BOUND(md) && md->in[md->in_cursor] != '\n') {
md->out[md->out_cursor] = md->in[md->in_cursor];
md->in_cursor++;
md->out_cursor++;
}
md->out_cursor--;
return md->in_cursor - before;
}
void repeat(MegaData *md, const char c, const int nb)
{
for (int i=0; i<nb; i++) {
md->out[++md->out_cursor] = c;
}
}
int mmdeeznut(MegaData *md)
{
assert(md->in_cap < md->out_cap && "output buffer should be bigger than the input file size.");
md->list_item_added = 0;
while (CHECK_MD_BOUND(md)) {
skip_begin(md);
int begin_skipped = skip_begin(md);
md->in_cursor++;
if ((md->in[md->in_cursor] == '-' || md->in[md->in_cursor] == '+') && md->in[md->in_cursor+1] == ' ') {
md->list_item_added = 1;
if (md->list_level < begin_skipped) {
md->out_cursor--;
md_append(md, "<ul>\n");
repeat(md, ' ', begin_skipped);
}
md->list_level = begin_skipped;
md->in_cursor += 2;
md->out_cursor--;
md_append(md, "<li>");
skip_endline(md);
md_append(md, "</li>");
md->out_cursor++;
} else if (md->list_item_added) {
md->list_item_added = 0;
// NOTE: voir note dans skip_begin()
// md->out_cursor--;
// md_append(md, "</ul>\n");
// repeat(md, ' ', begin_skipped);
// md->out_cursor++;
}
if (md->in[md->in_cursor] == '#') {
int level = 1;
while (md->in[md->in_cursor+level] == '#' && level < 6)
level++;
if (md->in[md->in_cursor+level] == ' ') {
md->in_cursor += level+1;
md->out[md->out_cursor] = '<';
md->out[++md->out_cursor] = 'h';
md->out[++md->out_cursor] = '0' + level;
md->out[++md->out_cursor] = '>';
md->out_cursor++;
char open[] = "<h0>";
open[2] += level;
md->out_cursor--;
md_append(md, open);
skip_endline(md);
md->out[md->out_cursor] = '<';
md->out[++md->out_cursor] = '/';
md->out[++md->out_cursor] = 'h';
md->out[++md->out_cursor] = '0' + level;
md->out[++md->out_cursor] = '>';
char close[] = "</h0>";
close[3] += level;
md_append(md, close);
md->out_cursor++;
}
}
skip_endline(md);
skip_line(md);
}
return md->out_cursor;
@ -89,14 +161,13 @@ int main(int argc, char **argv)
.in_cap = file_size,
.out = html_data,
.out_cap = file_size*2,
.in_cursor = 0,
.out_cursor = 0,
};
int data_size = mmdeeznut(&md);
printf("%s", mmd_data);
printf("--------------------\n");
printf("%.*s", data_size, html_data);
printf("%d -> %d bytes\n", file_size, data_size);
fwrite(html_data, data_size, 1, output_file);

10
test.md
View File

@ -19,5 +19,15 @@
###### Title 6
####### Title 7
######## Title 8
- gros
- caca
- de la magnagna
- hihiha
- grrrrr
### gros caca
</body>
</html>