aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/ctx.c
blob: 2a4426d2584bfca4671f1faa75f7e594f4bc1b22 (plain)
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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2016 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <sys/stat.h>
#include <libgen.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include "../common/error.h"
#include "../common/file.h"
#include "../common/utils.h"
#include "atom.h"
#include "settings.h"
#include "ctx.h"


bm_filectx_t*
bm_filectx_new(bm_ctx_t *ctx, const char *filename)
{
    if (ctx == NULL || filename == NULL)
        return NULL;

    char *f = filename[0] == '/' ? bc_strdup(filename) :
        bc_strdup_printf("%s/%s", ctx->root_dir, filename);

    bm_filectx_t *rv = bc_malloc(sizeof(bm_filectx_t));
    rv->path = f;
    rv->short_path = bc_strdup(filename);

    struct stat buf;

    if (0 != stat(f, &buf)) {
        struct timespec ts;
        ts.tv_sec = 0;
        ts.tv_nsec = 0;
        rv->timestamp = ts;
        rv->readable = false;
    }
    else {
        rv->timestamp = buf.st_mtim;
        rv->readable = true;
    }

    return rv;
}


void
bm_filectx_free(bm_filectx_t *fctx)
{
    if (fctx == NULL)
        return;
    free(fctx->path);
    free(fctx->short_path);
    free(fctx);
}


bm_ctx_t*
bm_ctx_new(const char *settings_file, bc_error_t **err)
{
    if (settings_file == NULL || err == NULL || *err != NULL)
        return NULL;

    size_t content_len;
    char *content = bc_file_get_contents(settings_file, true, &content_len,
        err);
    if (*err != NULL)
        return NULL;

    bm_settings_t *settings = bm_settings_parse(content, content_len, err);
    if (*err != NULL) {
        free(content);
        return NULL;
    }
    free(content);

    // fix output_dir, if forced from environment variable
    const char *output_dir_env = getenv("OUTPUT_DIR");
    if (output_dir_env != NULL) {
        bc_trie_insert(settings->settings, "output_dir",
            bc_strdup(output_dir_env));
    }

    char *atom_template = bm_atom_deploy(settings, err);
    if (*err != NULL) {
        return NULL;
    }

    bm_ctx_t *rv = bc_malloc(sizeof(bm_ctx_t));
    rv->settings = settings;

    char *real_filename = realpath(settings_file, NULL);
    rv->settings_fctx = bm_filectx_new(rv, real_filename);
    rv->root_dir = realpath(dirname(real_filename), NULL);
    free(real_filename);

    const char *output_dir = bc_trie_lookup(settings->settings, "output_dir");
    if (output_dir[0] == '/') {
        rv->output_dir = realpath(output_dir, NULL);
    }
    else {
        char *tmp = bc_strdup_printf("%s/%s", rv->root_dir, output_dir);
        rv->output_dir = realpath(tmp, NULL);
        free(tmp);
    }

    const char *template_dir = bc_trie_lookup(settings->settings,
        "template_dir");

    char *main_template = bc_strdup_printf("%s/%s", template_dir,
        bc_trie_lookup(settings->settings, "main_template"));
    rv->main_template_fctx = bm_filectx_new(rv, main_template);
    free(main_template);

    rv->atom_template_fctx = bm_filectx_new(rv, atom_template);
    free(atom_template);

    const char *content_dir = bc_trie_lookup(settings->settings, "content_dir");
    const char *post_prefix = bc_trie_lookup(settings->settings, "post_prefix");
    const char *source_ext = bc_trie_lookup(settings->settings, "source_ext");

    rv->posts_fctx = NULL;
    if (settings->posts != NULL) {
        for (size_t i = 0; settings->posts[i] != NULL; i++) {
            char *f = bc_strdup_printf("%s/%s/%s%s", content_dir, post_prefix,
                settings->posts[i], source_ext);
            rv->posts_fctx = bc_slist_append(rv->posts_fctx,
                bm_filectx_new(rv, f));
            free(f);
        }
    }

    rv->pages_fctx = NULL;
    if (settings->pages != NULL) {
        for (size_t i = 0; settings->pages[i] != NULL; i++) {
            char *f = bc_strdup_printf("%s/%s%s", content_dir,
                settings->pages[i], source_ext);
            rv->pages_fctx = bc_slist_append(rv->pages_fctx,
                bm_filectx_new(rv, f));
            free(f);
        }
    }

    rv->copy_files_fctx = NULL;
    if (settings->copy_files != NULL) {
        for (size_t i = 0; settings->copy_files[i] != NULL; i++) {
            rv->copy_files_fctx = bc_slist_append(rv->copy_files_fctx,
                bm_filectx_new(rv, settings->copy_files[i]));
        }
    }

    return rv;
}


void
bm_ctx_free(bm_ctx_t *ctx)
{
    if (ctx == NULL)
        return;

    bm_settings_free(ctx->settings);

    free(ctx->root_dir);
    free(ctx->output_dir);

    bm_atom_destroy(ctx->atom_template_fctx->path);

    bm_filectx_free(ctx->main_template_fctx);
    bm_filectx_free(ctx->atom_template_fctx);
    bm_filectx_free(ctx->settings_fctx);

    bc_slist_free_full(ctx->posts_fctx, (bc_free_func_t) bm_filectx_free);
    bc_slist_free_full(ctx->pages_fctx, (bc_free_func_t) bm_filectx_free);
    bc_slist_free_full(ctx->copy_files_fctx, (bc_free_func_t) bm_filectx_free);

    free(ctx);
}