hmm:
This commit is contained in:
parent
289b4d8d66
commit
5dae148967
104
main.c
104
main.c
|
|
@ -1,65 +1,81 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#define MAX_LENGTH 1000
|
||||||
|
|
||||||
|
typedef struct markdown {
|
||||||
|
int bold;
|
||||||
|
int quote;
|
||||||
|
int italic;
|
||||||
|
int headingone;
|
||||||
|
int headingtwo;
|
||||||
|
int headingthree;
|
||||||
|
} markdown;
|
||||||
|
|
||||||
|
markdown mdt;
|
||||||
|
|
||||||
FILE *fptr;
|
FILE *fptr;
|
||||||
|
|
||||||
void checker() {
|
void checker() {
|
||||||
fptr = fopen("./source/data.txt", "rb+");
|
fptr = fopen("./source/data.txt", "rb+");
|
||||||
|
|
||||||
if (fptr == NULL) {
|
if (fptr == NULL) {
|
||||||
perror("Galat checker buat ./source/data.txt");
|
perror("Galat checker buat ./source/data.txt");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
printf("Penggunaan: %s <berkas_input>\natau %s -h untuk info lebih lanjut\n", argv[0], argv[0]);
|
printf("penggunaan: %s <berkas_input>\natau %s -h untuk info lebih lanjut\n", argv[0], argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
if (strcmp("-v", argv[i]) == 0) {
|
if (strcmp("-v", argv[i]) == 0) {
|
||||||
printf("daisy © radhitya.org\n");
|
printf("daisy © radhitya.org\n");
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
else if(strcmp("-h", argv[i]) == 0) {
|
else if(strcmp("-h", argv[i]) == 0) {
|
||||||
printf("ini -h\n");
|
printf("ini -h\n");
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
else if(strcmp("new", argv[i]) == 0) {
|
else if(strcmp("new", argv[i]) == 0) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
if(stat("./source", &st) == 0) {
|
||||||
if(stat("./source", &st) == 0 || stat("./target", &st) == 0) {
|
if(stat("./target", &st) == 0) {
|
||||||
printf("./source dan ./target telah dibuat\n");
|
printf("./source dan ./target telah dibuat\n");
|
||||||
|
} else {
|
||||||
|
mkdir("./target", 0700);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
if(stat("./source", &st) == -1) {
|
|
||||||
mkdir("./source", 0700);
|
mkdir("./source", 0700);
|
||||||
}
|
|
||||||
|
|
||||||
if(stat("./target", &st) == -1) {
|
|
||||||
mkdir("./target", 0700);
|
mkdir("./target", 0700);
|
||||||
}
|
}
|
||||||
|
|
||||||
fptr = fopen("./source/data.txt", "rb+");
|
fptr = fopen("./source/data.txt", "rb+");
|
||||||
if (fptr == NULL) {
|
if (fptr == NULL) {
|
||||||
printf("Sukses buat ./source/data.txt\n");
|
|
||||||
fptr = fopen("./source/data.txt", "wb");
|
fptr = fopen("./source/data.txt", "wb");
|
||||||
if (fptr == NULL) {
|
if (fptr == NULL) {
|
||||||
perror("Galat buat ./source/data.txt");
|
perror("galat buat ./source/data.txt (fptr)");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
} else {
|
||||||
|
printf("sukses buat ./source/data.txt\n");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
printf("./source/data.txt sudah ada\n");
|
||||||
|
fclose(fptr);
|
||||||
}
|
}
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char txt[50], of[50];
|
char txt[MAX_LENGTH], of[MAX_LENGTH];
|
||||||
int i, len, in_italic = 0;
|
|
||||||
|
int i, len;
|
||||||
|
mdt.italic = 0, mdt.bold = 0, mdt.quote = 0;
|
||||||
|
|
||||||
const char *file_name = strrchr(argv[1], '/');
|
const char *file_name = strrchr(argv[1], '/');
|
||||||
if (file_name == NULL) {
|
if (file_name == NULL) {
|
||||||
file_name = argv[1];
|
file_name = argv[1];
|
||||||
|
|
@ -69,7 +85,6 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
snprintf(of, sizeof(of), "target/%s.html", file_name);
|
snprintf(of, sizeof(of), "target/%s.html", file_name);
|
||||||
|
|
||||||
|
|
||||||
fptr = fopen(argv[1], "rt");
|
fptr = fopen(argv[1], "rt");
|
||||||
if (fptr == NULL) {
|
if (fptr == NULL) {
|
||||||
perror("Galat membuka masukan berkas fptr\n");
|
perror("Galat membuka masukan berkas fptr\n");
|
||||||
|
|
@ -84,32 +99,71 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(fpto, "<html>\n<body>\n");
|
fprintf(fpto, "<html>\n<body>\n");
|
||||||
while (fgets(txt, 50, fptr) != NULL) {
|
while (fgets(txt, MAX_LENGTH, fptr) != NULL) {
|
||||||
len = strlen(txt);
|
len = strlen(txt);
|
||||||
|
|
||||||
if (len > 0 && txt[len-1] == '\n') {
|
if (len > 0 && txt[len-1] == '\n') {
|
||||||
txt[--len] = '\0';
|
txt[--len] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mdt.headingone = 0;
|
||||||
|
mdt.quote = 0;
|
||||||
|
if(len > 0 && txt[0] == '#' && )
|
||||||
fprintf(fpto, "<p>");
|
fprintf(fpto, "<p>");
|
||||||
|
|
||||||
for(i = 0; i < len; i++) {
|
for(i = 0; i < len; i++) {
|
||||||
|
|
||||||
if (txt[i] == '_' && (i == 0 || txt[i-1] != '\\')) {
|
if (txt[i] == '_' && (i == 0 || txt[i-1] != '\\')) {
|
||||||
if(!in_italic) {
|
if(!mdt.italic) {
|
||||||
fprintf(fpto, "<i>");
|
fprintf(fpto, "<i>");
|
||||||
in_italic = 1;
|
mdt.italic = 1;
|
||||||
} else {
|
} else {
|
||||||
fprintf(fpto, "</i>");
|
fprintf(fpto, "</i>");
|
||||||
in_italic = 0;
|
mdt.italic = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (i + 1 < len && txt[i] == '*' && txt[i+1] == '*' && (i == 0 || txt[i-1] != '\\')) {
|
||||||
|
if(mdt.bold) {
|
||||||
|
fprintf(fpto, "<b>");
|
||||||
|
mdt.bold = 1;
|
||||||
|
i++;
|
||||||
} else {
|
} else {
|
||||||
|
fprintf(fpto, "</b>");
|
||||||
|
mdt.bold = 0;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (txt[i] == '>' && (i == 0 || txt[i-1] != '\\')) {
|
||||||
|
if(!mdt.quote) {
|
||||||
|
fprintf(fpto, "<blockquote>");
|
||||||
|
mdt.quote = 1;
|
||||||
|
} else {
|
||||||
|
fprintf(fpto, "</blockquote>");
|
||||||
|
mdt.quote = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
fputc(txt[i], fpto);
|
fputc(txt[i], fpto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(in_italic) {
|
if(mdt.italic) {
|
||||||
fprintf(fpto, "</i>");
|
fprintf(fpto, "</i>");
|
||||||
};
|
};
|
||||||
|
if(mdt.bold) {
|
||||||
|
fprintf(fpto, "</b>");
|
||||||
|
}
|
||||||
|
if(mdt.quote) {
|
||||||
|
fprintf(fpto, "</blockquote>");
|
||||||
|
}
|
||||||
|
if(mdt.headingone) {
|
||||||
|
fprintf(fpto, "</h1>");
|
||||||
|
}
|
||||||
fprintf(fpto, "</p>\n");
|
fprintf(fpto, "</p>\n");
|
||||||
in_italic = 0;
|
mdt.italic = mdt.bold = mdt.quote = mdt.headingone = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(fpto, "</body>\n</html>");
|
fprintf(fpto, "</body>\n</html>");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user