commit 959aa67bf21dbf54f0c4532dfbf3ec9daa202244 Author: nemo Date: Thu Sep 5 12:19:02 2024 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3b7b99 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +mmd +test.html diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..89219dd --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2024 Hamza NANAHA + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5b9c957 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: + clang -ggdb main.c -o mmd + +run: all + ./mmd test.md test.html diff --git a/main.c b/main.c new file mode 100644 index 0000000..7bb6787 --- /dev/null +++ b/main.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include + +#define CHECK_MD_BOUND(md) (md->in_cursor < md->in_cap && md->out_cursor < md->out_cap) + +typedef struct MegaData { + char *in; + char *out; + int in_cap; + int out_cap; + int in_cursor; + int out_cursor; +} MegaData; + +void skip_begin(MegaData *md) +{ + 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++; + } +} + +void skip_endline(MegaData *md) +{ + 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++; + } +} + +int mmdeeznut(MegaData *md) +{ + assert(md->in_cap < md->out_cap && "output buffer should be bigger than the input file size."); + while (CHECK_MD_BOUND(md)) { + skip_begin(md); + 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++; + 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] = '>'; + md->out_cursor++; + } + } + skip_endline(md); + } + + return md->out_cursor; +} + +int main(int argc, char **argv) +{ + if (argc != 3) { + printf("ERR: please input only 2 parameter.\nusage: %s \n", *argv); + exit(-69); + } + + FILE *input_file = fopen(argv[1], "r"); + FILE *output_file = fopen(argv[2], "w"); + + fseek(input_file , 0L, SEEK_END); + int file_size = ftell(input_file); + rewind(input_file); + + char *mmd_data = malloc(file_size); + assert(mmd_data && "Buy more RAM!"); + fread(mmd_data, file_size, 1, input_file); + + char *html_data = malloc(file_size*2); + + MegaData md = { + .in = mmd_data, + .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); + + fwrite(html_data, data_size, 1, output_file); + + fclose(output_file); + fclose(input_file); + return 0; +} diff --git a/test.md b/test.md new file mode 100644 index 0000000..fa49926 --- /dev/null +++ b/test.md @@ -0,0 +1,23 @@ + + + + + + Test Mega Markdown + + + # Mega Markdown + + ## Introduction + ##grotext + + # Title 1 + ## Title 2 + ### Title 3 + #### Title 4 + ##### Title 5 + ###### Title 6 + ####### Title 7 + ######## Title 8 + +