This commit is contained in:
nemo 2024-09-05 12:19:02 +02:00
commit 959aa67bf2
5 changed files with 156 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
mmd
test.html

20
LICENSE Normal file
View File

@ -0,0 +1,20 @@
Copyright (c) 2024 Hamza NANAHA <hamza.nanaha@hotmail.com>
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.

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
all:
clang -ggdb main.c -o mmd
run: all
./mmd test.md test.html

106
main.c Normal file
View File

@ -0,0 +1,106 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#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 <input file> <output file>\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;
}

23
test.md Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>Test Mega Markdown</title>
</head>
<body>
# Mega Markdown
## Introduction
##grotext
# Title 1
## Title 2
### Title 3
#### Title 4
##### Title 5
###### Title 6
####### Title 7
######## Title 8
</body>
</html>