kittendust
This commit is contained in:
parent
f0598ebce4
commit
1fd687a091
324
main.c
324
main.c
|
|
@ -4,15 +4,17 @@
|
||||||
* thank you stackoverflow, geeksforgeeks, grok
|
* thank you stackoverflow, geeksforgeeks, grok
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define _DEFAULT_SOURCE
|
||||||
|
|
||||||
#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>
|
||||||
|
#include <dirent.h>
|
||||||
|
|
||||||
#define MAX_LENGTH 1000
|
#define MAX_LENGTH 1000
|
||||||
|
|
||||||
typedef struct
|
typedef struct {
|
||||||
markdown {
|
|
||||||
int bold;
|
int bold;
|
||||||
int quote;
|
int quote;
|
||||||
int italic;
|
int italic;
|
||||||
|
|
@ -21,15 +23,15 @@ markdown {
|
||||||
int headingone;
|
int headingone;
|
||||||
int headingtwo;
|
int headingtwo;
|
||||||
int headingthree;
|
int headingthree;
|
||||||
} markdown;
|
} Markdown;
|
||||||
|
|
||||||
markdown mdt;
|
Markdown mdt;
|
||||||
|
|
||||||
FILE *fptr;
|
FILE *fptr, *fpto;
|
||||||
|
char txt[MAX_LENGTH], of[MAX_LENGTH];
|
||||||
|
int i, len;
|
||||||
|
|
||||||
char
|
char *gantihuruf(const char *kata) {
|
||||||
*gantihuruf(const char *kata)
|
|
||||||
{
|
|
||||||
static char output[100];
|
static char output[100];
|
||||||
char *dot = strrchr(kata, '.');
|
char *dot = strrchr(kata, '.');
|
||||||
|
|
||||||
|
|
@ -45,9 +47,7 @@ char
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void checker(void) {
|
||||||
checker(void)
|
|
||||||
{
|
|
||||||
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");
|
||||||
|
|
@ -55,115 +55,28 @@ checker(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
void build(void) {
|
||||||
main(int argc, char *argv[])
|
mdt.italic = mdt.bold = mdt.quote = mdt.headingone = mdt.headingtwo = mdt.headingthree = mdt.code = 0;
|
||||||
{
|
|
||||||
if (argc < 2) {
|
|
||||||
printf("penggunaan: %s <berkas_input>\natau %s -h untuk info lebih lanjut\n", argv[0], argv[0]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
fprintf(fpto, "<html>\n<body>\n<head>\n<meta charset=\"utf-8\"/>\n<title>daisy homepage</title>\n");
|
||||||
if (strcmp("-v", argv[i]) == 0) {
|
fprintf(fpto, "<style>body{background: #4caf50}</style>\n</head>");
|
||||||
printf("daisy © radhitya.org\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else if(strcmp("-h", argv[i]) == 0) {
|
|
||||||
printf("-h : help");
|
|
||||||
printf("new : new site");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else if(strcmp("new", argv[i]) == 0) {
|
|
||||||
struct stat st;
|
|
||||||
if(stat("./source", &st) == 0) {
|
|
||||||
if(stat("./target", &st) == 0) {
|
|
||||||
if(stat("./media", &st) == 0) {
|
|
||||||
printf("./source dan ./target telah dibuat\n");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
mkdir("./media", 0700);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
mkdir("./target", 0700);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
mkdir("./source", 0700);
|
|
||||||
mkdir("./target", 0700);
|
|
||||||
mkdir("./media", 0700);
|
|
||||||
}
|
|
||||||
|
|
||||||
fptr = fopen("./source/data.txt", "r");
|
|
||||||
if (fptr == NULL) {
|
|
||||||
fptr = fopen("./source/data.txt", "w");
|
|
||||||
if (fptr == NULL) {
|
|
||||||
perror("galat buat ./source/data.txt (fptr)");
|
|
||||||
exit(1);
|
|
||||||
} else {
|
|
||||||
printf("sukses buat ./source/data.txt\n");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
printf("./source/data.txt sudah ada\n");
|
|
||||||
fclose(fptr);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
printf("salah perintah\ngunakan -h untuk info lebih lanjut\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char txt[MAX_LENGTH], of[MAX_LENGTH];
|
|
||||||
|
|
||||||
int i, len;
|
|
||||||
|
|
||||||
mdt.italic = 0, mdt.bold = 0, mdt.quote = 0, mdt.headingone = 0, mdt.headingtwo = 0;
|
|
||||||
mdt.headingthree = 0, mdt.code = 0;
|
|
||||||
|
|
||||||
const char *file_name = strrchr(argv[1], '/');
|
|
||||||
if (file_name == NULL) {
|
|
||||||
file_name = argv[1];
|
|
||||||
} else {
|
|
||||||
file_name++;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *new_name = gantihuruf(file_name);
|
|
||||||
snprintf(of, sizeof(of), "target/%s", new_name);
|
|
||||||
|
|
||||||
fptr = fopen(argv[1], "rt");
|
|
||||||
if (fptr == NULL) {
|
|
||||||
perror("galat membuka masukan berkas fptr\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE *fpto = fopen(of, "w");
|
|
||||||
if (fpto == NULL) {
|
|
||||||
perror("galat membuat keluaran berkas fpto\n");
|
|
||||||
fclose(fptr);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(fpto, "<html>\n<body>\n");
|
|
||||||
|
|
||||||
while (fgets(txt, MAX_LENGTH, fptr) != NULL) {
|
while (fgets(txt, MAX_LENGTH, fptr) != NULL) {
|
||||||
len = strlen(txt);
|
len = strlen(txt);
|
||||||
|
|
||||||
// hapus newline
|
if (len > 0 && txt[len-1] == '\n') {
|
||||||
if (len > 0 && txt[len-1] == '\n') {
|
|
||||||
txt[--len] = '\0';
|
txt[--len] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
mdt.headingone = 0;
|
mdt.headingone = mdt.headingtwo = mdt.quote = 0;
|
||||||
mdt.headingtwo = 0;
|
if (len == 0) {
|
||||||
mdt.quote = 0;
|
|
||||||
if(len == 0) {
|
|
||||||
fprintf(fpto, "\n");
|
fprintf(fpto, "\n");
|
||||||
} else {
|
} else {
|
||||||
i = 0;
|
i = 0;
|
||||||
if(len > 1 && txt[0] == '#' && txt[1] != '\\') {
|
|
||||||
if(len > 2 && txt[1] == '#' && txt[2] != '\\') {
|
if (len > 1 && txt[0] == '#' && txt[1] != '\\') {
|
||||||
if(len > 3 && txt[2] == '#' && txt[3] != '\\') {
|
if (len > 2 && txt[1] == '#' && txt[2] != '\\') {
|
||||||
|
if (len > 3 && txt[2] == '#' && txt[3] != '\\') {
|
||||||
fprintf(fpto, "<h3>");
|
fprintf(fpto, "<h3>");
|
||||||
mdt.headingthree = 1;
|
mdt.headingthree = 1;
|
||||||
while (i < len && (txt[i] == '#' || txt[i] == ' ')) i++;
|
while (i < len && (txt[i] == '#' || txt[i] == ' ')) i++;
|
||||||
|
|
@ -183,78 +96,29 @@ main(int argc, char *argv[])
|
||||||
while (i < len && (txt[i] == '>' || txt[i] == ' ')) i++;
|
while (i < len && (txt[i] == '>' || txt[i] == ' ')) i++;
|
||||||
} else {
|
} else {
|
||||||
fprintf(fpto, "<p>");
|
fprintf(fpto, "<p>");
|
||||||
i = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(; i < len; i++) {
|
for (; i < len; i++) {
|
||||||
if (txt[i] == '_' && (i == 0 || txt[i-1] != '\\')) {
|
if (txt[i] == '_' && (i == 0 || txt[i-1] != '\\')) {
|
||||||
if(!mdt.italic) {
|
if (!mdt.italic) {
|
||||||
fprintf(fpto, "<i>");
|
fprintf(fpto, "<i>");
|
||||||
mdt.italic = 1;
|
mdt.italic = 1;
|
||||||
} else {
|
} else {
|
||||||
fprintf(fpto, "</i>");
|
fprintf(fpto, "</i>");
|
||||||
mdt.italic = 0;
|
mdt.italic = 0;
|
||||||
}
|
}
|
||||||
}
|
} else if (txt[i] == '[' && (i == 0 || txt[i-1] != '\\')) {
|
||||||
else if (txt[i] == '[' && (i == 0 || txt[i-1] != '\\')) {
|
fprintf(fpto, "[%c]", txt[i]);
|
||||||
int start_linkname = i + 1;
|
} else if (txt[i] == '`' && (i == 0 || txt[i-1] != '\\')) {
|
||||||
int end_linkname = -1;
|
if (!mdt.code) {
|
||||||
int start_linkurl = -1;
|
fprintf(fpto, "<code>");
|
||||||
int end_linkurl = -1;
|
mdt.code = 1;
|
||||||
int j;
|
} else {
|
||||||
|
fprintf(fpto, "</code>");
|
||||||
for (j = start_linkname; j < len; j++) {
|
mdt.code = 0;
|
||||||
if (txt[j] == ']' && (j == 0 || txt[j-1] != '\\')) {
|
}
|
||||||
end_linkname = j;
|
} else if (i + 1 < len && txt[i] == '*' && txt[i+1] == '*' && (i == 0 || txt[i-1] != '\\')) {
|
||||||
break;
|
if (!mdt.bold) {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (end_linkname != -1) {
|
|
||||||
for (j = end_linkname + 1; j < len; j++) {
|
|
||||||
if (txt[j] == '(' && (j == 0 || txt[j-1] != '\\')) {
|
|
||||||
start_linkurl = j + 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (start_linkurl != -1) {
|
|
||||||
for (j = start_linkurl; j < len; j++) {
|
|
||||||
if (txt[j] == ')' && (j == 0 || txt[j-1] != '\\')) {
|
|
||||||
end_linkurl = j;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (end_linkname != -1 && end_linkurl != -1) {
|
|
||||||
char linkname[100], linkurl[100];
|
|
||||||
int linkname_len = end_linkname - start_linkname;
|
|
||||||
int linkurl_len = end_linkurl - start_linkurl;
|
|
||||||
|
|
||||||
strncpy(linkname, txt + start_linkname, linkname_len);
|
|
||||||
linkname[linkname_len] = '\0';
|
|
||||||
|
|
||||||
strncpy(linkurl, txt + start_linkurl, linkurl_len);
|
|
||||||
linkurl[linkurl_len] = '\0';
|
|
||||||
|
|
||||||
fprintf(fpto, "<a href=\"%s\">%s</a>", linkurl, linkname);
|
|
||||||
|
|
||||||
i = end_linkurl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (txt[i] == '`' && (i == 0 || txt[i-1] != '\\')) {
|
|
||||||
if(!mdt.code) {
|
|
||||||
fprintf(fpto, "<code>");
|
|
||||||
mdt.code = 1;
|
|
||||||
} else {
|
|
||||||
fprintf(fpto, "</code>");
|
|
||||||
mdt.code = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (i + 1 < len && txt[i] == '*' && txt[i+1] == '*' && (i == 0 || txt[i-1] != '\\')) {
|
|
||||||
if(!mdt.bold) {
|
|
||||||
fprintf(fpto, "<b>");
|
fprintf(fpto, "<b>");
|
||||||
mdt.bold = 1;
|
mdt.bold = 1;
|
||||||
i++;
|
i++;
|
||||||
|
|
@ -263,36 +127,104 @@ else if (txt[i] == '[' && (i == 0 || txt[i-1] != '\\')) {
|
||||||
mdt.bold = 0;
|
mdt.bold = 0;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
fputc(txt[i], fpto);
|
fputc(txt[i], fpto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mdt.italic) fprintf(fpto, "</i>");
|
if (mdt.italic) fprintf(fpto, "</i>");
|
||||||
else if(mdt.code) fprintf(fpto, "</code>");
|
if (mdt.code) fprintf(fpto, "</code>");
|
||||||
else if(mdt.bold) fprintf(fpto, "</b>");
|
if (mdt.bold) fprintf(fpto, "</b>");
|
||||||
else if(mdt.quote) fprintf(fpto, "</blockquote>");
|
if (mdt.quote) fprintf(fpto, "</blockquote>");
|
||||||
else if(mdt.headingone) {
|
if (mdt.headingone) fprintf(fpto, "</h1>\n");
|
||||||
fprintf(fpto, "</h1>\n");
|
if (mdt.headingtwo) fprintf(fpto, "</h2>\n");
|
||||||
}
|
if (mdt.headingthree) fprintf(fpto, "</h3>\n");
|
||||||
else if(mdt.headingtwo) {
|
if (!(mdt.headingone || mdt.headingtwo || mdt.headingthree || mdt.quote)) fprintf(fpto, "</p>\n");
|
||||||
fprintf(fpto, "</h2>\n");
|
|
||||||
}
|
|
||||||
else if(mdt.headingthree) {
|
|
||||||
fprintf(fpto, "</h3>\n");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
fprintf(fpto, "</p>\n");
|
|
||||||
}
|
|
||||||
mdt.italic = mdt.bold = mdt.quote = mdt.headingone = mdt.headingtwo = mdt.headingthree = mdt.code = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(fpto, "</body>\n</html>");
|
fprintf(fpto, "</body>\n</html>");
|
||||||
fclose(fptr);
|
}
|
||||||
fclose(fpto);
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
printf("dokumen %s telah berhasil dibuat\n", of);
|
if (argc < 2) {
|
||||||
return 0;
|
printf("penggunaan: %s <command> [berkas_input]\natau %s -h untuk info lebih lanjut\n", argv[0], argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < argc; i++) {
|
||||||
|
if (strcmp("-v", argv[i]) == 0) {
|
||||||
|
printf("daisy © radhitya.org\n");
|
||||||
|
return 0;
|
||||||
|
} else if (strcmp("build", argv[i]) == 0) {
|
||||||
|
DIR *dir;
|
||||||
|
struct dirent *ent;
|
||||||
|
if ((dir = opendir("./source")) != NULL) {
|
||||||
|
while ((ent = readdir(dir)) != NULL) {
|
||||||
|
/* d_type dari dirent.h
|
||||||
|
* DT_REG == file reguler (Directory Type Regular file)
|
||||||
|
*/
|
||||||
|
if (ent->d_type == DT_REG) {
|
||||||
|
char path[MAX_LENGTH];
|
||||||
|
snprintf(path, sizeof(path), "./source/%s", ent->d_name);
|
||||||
|
fptr = fopen(path, "rt");
|
||||||
|
if (fptr == NULL) {
|
||||||
|
perror("galat membuka berkas");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *new_name = gantihuruf(ent->d_name);
|
||||||
|
snprintf(of, sizeof(of), "./target/%s", new_name);
|
||||||
|
|
||||||
|
fpto = fopen(of, "w");
|
||||||
|
if (fpto == NULL) {
|
||||||
|
perror("galat membuat berkas keluaran");
|
||||||
|
fclose(fptr);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
build();
|
||||||
|
|
||||||
|
fclose(fptr);
|
||||||
|
fclose(fpto);
|
||||||
|
printf("dokumen %s telah berhasil dibuat\n", of);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
closedir(dir);
|
||||||
|
} else {
|
||||||
|
perror("galat membuka direktori ./source");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
} else if (strcmp("-h", argv[i]) == 0) {
|
||||||
|
printf("-h : help\nnew : new site\nbuild : build all files in source/ to HTML\n");
|
||||||
|
return 0;
|
||||||
|
} else if (strcmp("new", argv[i]) == 0) {
|
||||||
|
struct stat st;
|
||||||
|
if (stat("./source", &st) == -1) mkdir("./source", 0700);
|
||||||
|
if (stat("./target", &st) == -1) mkdir("./target", 0700);
|
||||||
|
if (stat("./media", &st) == -1) mkdir("./media", 0700);
|
||||||
|
|
||||||
|
fptr = fopen("./source/data.txt", "r");
|
||||||
|
if (fptr == NULL) {
|
||||||
|
fptr = fopen("./source/data.txt", "w");
|
||||||
|
if (fptr == NULL) {
|
||||||
|
perror("galat buat ./source/data.txt (fptr)");
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
printf("sukses buat ./source/data.txt\n");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("./source/data.txt sudah ada\n");
|
||||||
|
fclose(fptr);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
printf("salah perintah\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("salah perintah\n");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user