git.mcksp
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define BUF_SIZE 256
#define MAX_FILES 1024
#define MAX_FILE_SIZE 65536
#define RSS_SIZE 5
#define DOMAIN "mcksp.com"

struct conf {
    char filename[BUF_SIZE];
    char title[BUF_SIZE];
    char date[BUF_SIZE];
    char description[BUF_SIZE];
    char article[MAX_FILE_SIZE];
};

void header(FILE *f, char *title, char *date, char *description) {
    fprintf(f, "\
<!doctype html>\n\
<!--title:%s-->\n\
<!--date:%s-->\n",
        title, date);
    if (strlen(description) != 0) {
        fprintf(f, "<!--description:%s-->\n", description);
    }

    fprintf(f, "\
<html lang='en'>\n\
    <head>\n\
        <meta charset='UTF-8'>\n\
        <meta name='viewport' content='width=device-width, initial-scale=1.0'>\n\
        <meta name='author' content='Maciej Spychala' />\n");

    if (strlen(description) != 0) {
        fprintf(f, "\
        <meta name='description' content='%s'/>\n", description);
    }

    fprintf(f, "\
        <title>%s</title>\n\
        <link type='text/css' rel='stylesheet' href='/assets/style.css'>\n\
    </head>\n\
    <body>\n\
        <header>\n\
            <a id='title' href='/'>mcksp</a></span>\n\
            <div id='navbar'>\n\
                <a href='/blog'>blog</a>\n\
                <a href='/tech'>tech</a>\n\
                <a href='https://git.mcksp.com'>git</a>\n\
                <a href='/about'>about</a>\n\
                <a href='/rss.xml'>rss</a>\n\
            </div>\n\
        </header>\n",
        title
    );
}

void footer(FILE *f) {
    fputs("\
        <footer>\n\
            made with <a href='https://git.mcksp.com/brcl'>brcl</a>\n\
        </footer>\n\
    </body>\n\
</html>\n",
        f);
}

void list_item(FILE *f, struct conf *conf) {
    fprintf(f,
        "<div class='item'>"
            "<a href='/%s'>%s</a> <span class='date'>%s</span>"
        "</div>\n",
        conf->filename, conf->title, conf->date
    );
}

enum html_part {
    HEADER,
    BODY,
    FOOTER
};

void rss_header(FILE *f) {
    fprintf(f,
"<rss version='2.0'>\n\
<channel><title>%s</title><link>https://%s</link><description>%s</description>\n",
        DOMAIN, DOMAIN, DOMAIN);
}

void html_encode(FILE *f, const char *s) {
    while(*s) {
        switch(*s) {
            case '<':  fputs("&lt;",   f); break;
            case '>':  fputs("&gt;",   f); break;
            case '\'': fputs("&#39;",  f); break;
            case '&':  fputs("&amp;",  f); break;
            case '"':  fputs("&quot;", f); break;
            default:   fputc(*s, f); break;
        }
        s++;
    }
}

void rss_date(char *dst, char *str) {
    struct tm date = {};
    int year, month, day;
    if (sscanf(str, "%d/%d/%d", &year, &month, &day) != 3) {
        fprintf(stderr, "couldn't parse date %s\n", str);
        exit(1);
    }
    date.tm_year = year - 1900;
    date.tm_mon = month - 1;
    date.tm_mday = day;
    date.tm_hour = 12;
    time_t timestamp = mktime(&date);
    gmtime_r(&timestamp, &date); // get correct weekday without strptime
    strftime(dst, BUF_SIZE, "%a, %d %b %Y 12:00:00 +0000", &date);
}


void rss_item(FILE *f, struct conf *conf) {
    char dateBuf[BUF_SIZE];
    rss_date(dateBuf, conf->date);

    fprintf(f, "\
<item>\n\
    <guid>https://%s/%s</guid>\n\
    <title>%s</title>\n\
    <link>https://%s/%s</link>\n\
    <description>",
        DOMAIN, conf->filename, conf->title, DOMAIN, conf->filename);
    html_encode(f, conf->article);
    fprintf(f, "\
    </description>\n\
    <pubDate>%s</pubDate>\n\
</item>",
        dateBuf);
}

void rss_footer(FILE *f) {
    fprintf(f, "</channel></rss>\n");
}

void generate_rss(struct conf *rss[]) {
    FILE *file = fopen("rss.xml", "w");
    if (file == NULL) {
        fprintf(stderr, "couldn't open rss.xml");
        exit(1);
    }

    rss_header(file);
    for (int i = 0; i < RSS_SIZE; i++) {
        if (rss[i]->title[0] != '\0') {
            rss_item(file, rss[i]);
        }
    }
    rss_footer(file);
}


int compare(const void *_a, const void *_b) {
    struct conf *a = *(struct conf**) _a;
    struct conf *b = *(struct conf**) _b;
    if (!a && !b) { return 0; }
    if (!a) { return 1; }
    if (!b) { return -1; }
    return strcmp(b->date, a->date);
}

void get_var(char *line, struct conf *conf) {
    char key[BUF_SIZE], value[BUF_SIZE];

    char *comment = strstr(line, "<!--");
    if (!comment) {
        return;
    }

    if (sscanf(comment + 4, "%[^:]:%[^-]", key, value) != 2) {
        return;
    }

    if (strncmp(key, "title", 5) == 0) {
        strncpy(conf->title, value, BUF_SIZE);
    } else if (strncmp(key, "date", 4) == 0) {
        strncpy(conf->date, value, BUF_SIZE);
    } else if (strncmp(key, "description", 11) == 0) {
        strncpy(conf->description, value, BUF_SIZE);
    }
}

void file_path(char *dest, char *dir, char *filename) {
    if (strncmp(dir, ".", 1) == 0) {
        strncpy(dest, filename, BUF_SIZE);
    } else if (dir[strlen(dir) - 1] == '/') {
        snprintf(dest, BUF_SIZE, "%s%s", dir, filename);
    } else {
        snprintf(dest, BUF_SIZE, "%s/%s", dir, filename);
    }
}

void save_article(struct conf *conf) {
    FILE *file = fopen(conf->filename, "w");
    if (file == NULL) {
        fprintf(stderr, "couldn't open file %s\n", conf->filename);
        exit(1);
    }
    header(file, conf->title, conf->date, conf->description);
    fputs(conf->article, file);
    footer(file);
    fclose(file);
}

struct conf *read_article(char *dir_name, char *filename) {
        struct conf *conf = calloc(1, sizeof(struct conf));

        file_path(conf->filename, dir_name, filename);
        FILE *file = fopen(conf->filename, "r");

        if (file == NULL) {
            fprintf(stderr, "couldn't open file %s\n", conf->filename);
            exit(1);
        }

        char line[BUF_SIZE];
        enum html_part part = HEADER;
        while (fgets(line, BUF_SIZE, file)) {
            switch (part) {
                case HEADER:
                    get_var(line, conf);
                    if (strstr(line, "</header>")) {
                        part = BODY;
                    }
                    break;
                case BODY:
                    if (strstr(line, "<footer>")) {
                        part = FOOTER;
                        break;
                    }
                    strncat(
                        conf->article,
                        line,
                        MAX_FILE_SIZE - strlen(conf->article) - 1
                    );
                    break;
                case FOOTER:
                    break;
            }

        }
        fclose(file);
        return conf;
}

void handle_dir(char *dir_name, bool indexable, struct conf **rss, char *description) {
    DIR *dir;
    struct dirent *dirEntry;
    dir = opendir(dir_name);
    if (dir == NULL) {
        fprintf(stderr, "couldn't open directory\n");
        exit(1);
    }

    int file_index = 0;
    struct conf *confs[MAX_FILES] = {};

    while ((dirEntry = readdir(dir)) != NULL && file_index < MAX_FILES) {
        char *extension = strrchr(dirEntry->d_name, '.');
        if (extension == NULL || strcmp(extension, ".html") != 0) {
            continue;
        }
        if (indexable && strcmp(dirEntry->d_name, "index.html") == 0) {
            continue;
        }

        confs[file_index] = read_article(dir_name, dirEntry->d_name);
        save_article(confs[file_index]);
        file_index++;
    }


    if (indexable) {
        char index_path[BUF_SIZE];
        file_path(index_path, dir_name, "index.html");

        qsort(confs, file_index, sizeof(confs[0]), compare);

        FILE *file = fopen(index_path, "w");
        if (file == NULL) {
            fprintf(stderr, "couldn't open file %s\n", index_path);
            exit(1);
        }

        header(file, dir_name, "", description);
        for (int i = 0; i < file_index; i++) {
            list_item(file, confs[i]);
        }
        footer(file);

        for (int i = 0; i < file_index && i < RSS_SIZE; i++) {
            *rss[RSS_SIZE + i] = *confs[i];
        }
        qsort(rss, RSS_SIZE * 2, sizeof(rss[0]), compare);
    }

    for (int i = 0; i < MAX_FILES; i++) {
        if (confs[i] != NULL) {
            free(confs[i]);
        }
    }

    closedir(dir);
}

void create_new_article(int argc, char *argv[]) {
    char title[BUF_SIZE] = "";
    if (argc == 3) {
        strncpy(title, argv[2], BUF_SIZE - 1);
    } else {
        for (int i = 3; i < argc; i++) {
            strncat(title, argv[i], BUF_SIZE - strlen(title) - 1);
            if (i < argc - 1) {
                strncat(title, " ", BUF_SIZE - strlen(title) - 1);
            }
        }
    }

    time_t t = time(NULL);
    struct tm tm = *localtime(&t);

    struct conf new_file;
    char filename[BUF_SIZE];
    if (strrchr(argv[2], '.') == NULL) {
        snprintf(filename, BUF_SIZE, "%s.html", argv[2]);
    } else {
        strncpy(filename, argv[2], BUF_SIZE - 1);
    }
    file_path(new_file.filename, argv[1], filename);
    strncpy(new_file.title, title, BUF_SIZE - 1);
    snprintf(new_file.date, BUF_SIZE, "%d/%02d/%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
    strncpy(new_file.article, "TODO: write something\n", MAX_FILE_SIZE - 1);
    save_article(&new_file);
    printf("created new file: %s\n", new_file.filename);
}


void generate_blog() {
    struct conf *rss[RSS_SIZE * 2] = {};
    for (int i = 0; i < RSS_SIZE * 2; i++) {
        rss[i] = calloc(1, sizeof(struct conf));
    }

    handle_dir(".", false, NULL, "");
    handle_dir("blog", true, rss, "Here's a list of all my blog entries");
    handle_dir("tech", true, rss, "Here's a list of all tech related entries");

    generate_rss(rss);

    for (int i = 0; i < RSS_SIZE * 2; i++) {
        if (rss[i] != NULL) {
            free(rss[i]);
        }
    }
}


int main(int argc, char *argv[]) {
    if (argc > 1) {
        if (argc == 2) {
            printf("usage: brcl <dir> <filename> [title...]\n");
            return 0;
        }
        create_new_article(argc, argv);
    }

    generate_blog();
    return 0;
}