This commit is contained in:
radhitya 2025-01-18 13:03:08 +07:00
parent 52d54e50e7
commit be9b9c0ae6

50
main.c
View File

@ -1,9 +1,3 @@
/* daisy
* alif@radhitya.org
*
* thank you stackoverflow, geeksforgeeks, grok
*/
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include <stdio.h> #include <stdio.h>
@ -285,11 +279,11 @@ void generate(void) {
fprintf(fpto, "<link rel=\"stylesheet\" href=\"style.css\">\n"); fprintf(fpto, "<link rel=\"stylesheet\" href=\"style.css\">\n");
fprintf(fpto, "</head>\n<body>\n"); fprintf(fpto, "</head>\n<body>\n");
int judul = 0;
while (fgets(txt, MAX_LENGTH, fptr) != NULL) { while (fgets(txt, MAX_LENGTH, fptr) != NULL) {
len = strlen(txt); len = strlen(txt);
int judul = 0;
if (len > 0 && txt[len - 1] == '\n') { if (len > 0 && txt[len - 1] == '\n') {
txt[--len] = '\0'; txt[--len] = '\0';
} }
@ -302,26 +296,32 @@ void generate(void) {
i = 0; i = 0;
if (txt[0] == '#') { if (txt[0] == '#') {
int heading_count = 0; int heading_count = 0;
while (i < len && txt[i] == '#') { heading_count++; i++; }
if (i < len && txt[i] == ' ') { while (i < len && txt[i] == '#') {
i++; // Skip space after # heading_count++;
const char *title_start = txt + i; i++;
if (heading_count == 1 && !judul) { }
judul = 1;
fseek(fpto, 0, SEEK_SET); if (i < len && txt[i] == ' ') {
fprintf(fpto, "<html>\n<head>\n<meta charset=\"utf-8\"/>\n" i++;
"<title>%s</title>\n<link rel=\"stylesheet\" href=\"style.css\">\n"
"</head>\n<body>\n", title_start); char *title_start = txt + i;
fseek(fpto, 0, SEEK_END);
fprintf(fpto, "<h1>%s</h1>", title_start); if (heading_count == 1) {
} else { judul = 1;
fprintf(fpto, "<h%d>%s</h%d>\n", heading_count, title_start, heading_count); fseek(fpto, 0, SEEK_SET);
fprintf(fpto, "<html>\n<head>\n<meta charset=\"utf-8\"/>\n"
"<title>%s</title>\n<link rel=\"stylesheet\" href=\"style.css\">\n"
"</head>\n<body>\n", title_start);
fseek(fpto, 0, SEEK_END);
fprintf(fpto, "<h1>%s</h1>", title_start);
} else {
fprintf(fpto, "<h%d>%s</h%d>\n", heading_count, title_start, heading_count);
}
continue;
} }
continue;
} }
}
else if (len > 1 && txt[0] == '>' && txt[1] != '\\') { else if (len > 1 && txt[0] == '>' && txt[1] != '\\') {