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

#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "datetime-parser.h"
#include "source-parser.h"
#include "template-parser.h"
#include "loader.h"
#include "../common/error.h"
#include "../common/file.h"
#include "../common/utils.h"
#include "../common/sort.h"


char*
blogc_get_filename(const char *f)
{
    if (f == NULL)
        return NULL;

    if (strlen(f) == 0)
        return NULL;

    char *filename = bc_strdup(f);

    // keep a pointer to original string
    char *tmp = filename;

    bool removed_dot = false;
    for (int i = strlen(tmp); i >= 0 ; i--) {

        // remove last extension
        if (!removed_dot && tmp[i] == '.') {
            tmp[i] = '\0';
            removed_dot = true;
            continue;
        }

        if (tmp[i] == '/' || tmp[i] == '\\') {
            tmp += i + 1;
            break;
        }
    }

    char *final_filename = bc_strdup(tmp);
    free(filename);

    return final_filename;
}


bc_slist_t*
blogc_template_parse_from_file(const char *f, bc_error_t **err)
{
    if (err == NULL || *err != NULL)
        return NULL;

    size_t len;
    char *s = bc_file_get_contents(f, true, &len, err);
    if (s == NULL)
        return NULL;
    bc_slist_t *rv = blogc_template_parse(s, len, err);
    free(s);
    return rv;
}


bc_trie_t*
blogc_source_parse_from_file(const char *f, bc_error_t **err)
{
    if (err == NULL || *err != NULL)
        return NULL;

    size_t len;
    char *s = bc_file_get_contents(f, true, &len, err);
    if (s == NULL)
        return NULL;
    bc_trie_t *rv = blogc_source_parse(s, len, err);

    // set FILENAME variable
    if (rv != NULL) {
        char *filename = blogc_get_filename(f);
        if (filename != NULL)
            bc_trie_insert(rv, "FILENAME", filename);
    }

    free(s);
    return rv;
}


static int
sort_source(const void *a, const void *b)
{
    const char *ca = bc_trie_lookup((bc_trie_t*) a, "c");
    const char *cb = bc_trie_lookup((bc_trie_t*) b, "c");

    if (ca == NULL || cb == NULL) {
        return 0;  // wat
    }

    return strcmp(cb, ca);
}


static int
sort_source_reverse(const void *a, const void *b)
{
    return sort_source(b, a);
}


bc_slist_t*
blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, bc_error_t **err)
{
    if (err == NULL || *err != NULL)
        return NULL;

    bool sort = bc_str_to_bool(bc_trie_lookup(conf, "FILTER_SORT"));

    bc_slist_t* sources = NULL;
    bc_error_t *tmp_err = NULL;
    size_t with_date = 0;
    for (bc_slist_t *tmp = l; tmp != NULL; tmp = tmp->next) {
        char *f = tmp->data;
        bc_trie_t *s = blogc_source_parse_from_file(f, &tmp_err);
        if (s == NULL) {
            *err = bc_error_new_printf(BLOGC_ERROR_LOADER,
                "An error occurred while parsing source file: %s\n\n%s",
                f, tmp_err->msg);
            bc_error_free(tmp_err);
            bc_slist_free_full(sources, (bc_free_func_t) bc_trie_free);
            return NULL;
        }

        const char *date = bc_trie_lookup(s, "DATE");
        if (date != NULL) {
            with_date++;
        }

        if (sort) {
            if (date == NULL) {
                *err = bc_error_new_printf(BLOGC_ERROR_LOADER,
                    "'FILTER_SORT' requires that 'DATE' variable is set for "
                    "every source file: %s", f);
                bc_trie_free(s);
                bc_slist_free_full(sources, (bc_free_func_t) bc_trie_free);
                return NULL;
            }

            char *timestamp = blogc_convert_datetime(date, "%s", &tmp_err);
            if (timestamp == NULL) {
                *err = bc_error_new_printf(BLOGC_ERROR_LOADER,
                    "An error occurred while parsing 'DATE' variable: %s"
                    "\n\n%s", f, tmp_err->msg);
                bc_error_free(tmp_err);
                bc_trie_free(s);
                bc_slist_free_full(sources, (bc_free_func_t) bc_trie_free);
                return NULL;
            }

            bc_trie_insert(s, "c", timestamp);
        }

        sources = bc_slist_append(sources, s);
    }

    if (with_date > 0 && with_date < bc_slist_length(l)) {
        *err = bc_error_new_printf(BLOGC_ERROR_LOADER,
            "'DATE' variable provided for at least one source file, but not "
            "for all source files. It must be provided for all files.");
        bc_slist_free_full(sources, (bc_free_func_t) bc_trie_free);
        return NULL;
    }

    bool reverse = bc_str_to_bool(bc_trie_lookup(conf, "FILTER_REVERSE"));

    if (sort) {
        sources = bc_slist_sort(sources,
            (bc_sort_func_t) (reverse ? sort_source_reverse : sort_source));
    }
    else if (reverse) {
        bc_slist_t *tmp_sources = NULL;
        for (bc_slist_t *tmp = sources; tmp != NULL; tmp = tmp->next) {
            tmp_sources = bc_slist_prepend(tmp_sources, tmp->data);
        }
        bc_slist_t *tmp = sources;
        sources = tmp_sources;
        bc_slist_free(tmp);
    }

    const char *filter_tag = bc_trie_lookup(conf, "FILTER_TAG");
    const char *filter_page = bc_trie_lookup(conf, "FILTER_PAGE");
    const char *filter_per_page = bc_trie_lookup(conf, "FILTER_PER_PAGE");

    const char *ptr;
    char *endptr;

    ptr = filter_page != NULL ? filter_page : "";
    long page = strtol(ptr, &endptr, 10);
    if (*ptr != '\0' && *endptr != '\0')
        fprintf(stderr, "warning: invalid value for 'FILTER_PAGE' variable: "
            "%s. using %ld instead\n", ptr, page);
    if (page <= 0)
        page = 1;

    ptr = filter_per_page != NULL ? filter_per_page : "10";
    long per_page = strtol(ptr, &endptr, 10);
    if (*ptr != '\0' && *endptr != '\0')
        fprintf(stderr, "warning: invalid value for 'FILTER_PER_PAGE' variable: "
            "%s. using %ld instead\n", ptr, per_page);
    if (per_page < 0)
        per_page = 0;

    // poor man's pagination
    size_t start = (page - 1) * per_page;
    size_t end = start + per_page;
    size_t counter = 0;

    bc_slist_t *rv = NULL;
    for (bc_slist_t *tmp = sources; tmp != NULL; tmp = tmp->next) {
        bc_trie_t *s = tmp->data;
        if (filter_tag != NULL) {
            const char *tags_str = bc_trie_lookup(s, "TAGS");
            // if user wants to filter by tag and no tag is provided, skip it
            if (tags_str == NULL) {
                bc_trie_free(s);
                continue;
            }
            char **tags = bc_str_split(tags_str, ' ', 0);
            bool found = false;
            for (size_t i = 0; tags[i] != NULL; i++) {
                if (tags[i][0] == '\0')
                    continue;
                if (0 == strcmp(tags[i], filter_tag))
                    found = true;
            }
            bc_strv_free(tags);
            if (!found) {
                bc_trie_free(s);
                continue;
            }
        }
        if (filter_page != NULL) {
            if (counter < start || counter >= end) {
                counter++;
                bc_trie_free(s);
                continue;
            }
            counter++;
        }
        rv = bc_slist_append(rv, s);
    }

    bc_slist_free(sources);

    bool first = true;
    for (bc_slist_t *tmp = rv; tmp != NULL; tmp = tmp->next) {
        bc_trie_t *s = tmp->data;
        if (first) {
            const char *val = bc_trie_lookup(s, "DATE");
            if (val != NULL)
                bc_trie_insert(conf, "DATE_FIRST", bc_strdup(val));
            val = bc_trie_lookup(s, "FILENAME");
            if (val != NULL)
                bc_trie_insert(conf, "FILENAME_FIRST", bc_strdup(val));
            first = false;
        }
        if (tmp->next == NULL) {  // last
            const char *val = bc_trie_lookup(s, "DATE");
            if (val != NULL)
                bc_trie_insert(conf, "DATE_LAST", bc_strdup(val));
            val = bc_trie_lookup(s, "FILENAME");
            if (val != NULL)
                bc_trie_insert(conf, "FILENAME_LAST", bc_strdup(val));
        }
    }

    if (filter_page != NULL) {
        size_t last_page = ceilf(((float) counter) / per_page);
        bc_trie_insert(conf, "CURRENT_PAGE", bc_strdup_printf("%ld", page));
        if (page > 1)
            bc_trie_insert(conf, "PREVIOUS_PAGE", bc_strdup_printf("%ld", page - 1));
        if (page < last_page)
            bc_trie_insert(conf, "NEXT_PAGE", bc_strdup_printf("%ld", page + 1));
        if (bc_slist_length(rv) > 0)
            bc_trie_insert(conf, "FIRST_PAGE", bc_strdup("1"));
        if (last_page > 0)
            bc_trie_insert(conf, "LAST_PAGE", bc_strdup_printf("%d", last_page));
    }

    return rv;
}