ref: 9ee696abcfd19305fcaf906812aac448a809866a shayla/main.c -rw-r--r-- 11.9 KiB View raw
                                                                                
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include <log.h>
#include <shayla.h>
#include <dir.h>
#include <args.h>
#include <posts.h>
#include <inttypes.h>
#include <pool.h>
#include <shtml.h>
#include <rss.h>
#include <errno.h>

enum {
    OPT_VERSION = 0,
    OPT_TITLE,
    OPT_SRC,
    OPT_STYLE,
    OPT_LAYOUTS,
    OPT_DEST,
    OPT_ROOT,
    OPT_FAVICON,
    OPT_URL,
    OPT_IMG,
    OPT_THREADS,
    OPT_DEBUG
};

static opts_t options[] = {
    [OPT_VERSION] = {
        LONG_OPT("version"),
        SINGLE_OPT('v')
    },
    [OPT_TITLE] = {
        LONG_OPT("title"),
        SINGLE_OPT('t'),
        ARG_TYPE(OPT_TYPE_STRING)
    },
    [OPT_SRC] = {
        LONG_OPT("src"),
        SINGLE_OPT('s'),
        ARG_TYPE(OPT_TYPE_STRING)
    },
    [OPT_STYLE] = {
        LONG_OPT("style"),
        SINGLE_OPT('c'),
        ARG_TYPE(OPT_TYPE_STRING)
    },
    [OPT_LAYOUTS] = {
        LONG_OPT("layouts"),
        SINGLE_OPT('l'),
        ARG_TYPE(OPT_TYPE_STRING)
    },
    [OPT_DEST] = {
        LONG_OPT("dest"),
        SINGLE_OPT('d'),
        ARG_TYPE(OPT_TYPE_STRING)
    },
    [OPT_ROOT] = {
        LONG_OPT("root"),
        SINGLE_OPT('r'),
        ARG_TYPE(OPT_TYPE_STRING)
    },
    [OPT_FAVICON] = {
        LONG_OPT("favicon"),
        SINGLE_OPT('f'),
        ARG_TYPE(OPT_TYPE_FILE)
    },
    [OPT_URL] = {
        LONG_OPT("url"),
        SINGLE_OPT('u'),
        ARG_TYPE(OPT_TYPE_STRING)
    },
    [OPT_IMG] = {
        LONG_OPT("img"),
        SINGLE_OPT('i'),
        ARG_TYPE(OPT_TYPE_STRING)
    },
    [OPT_THREADS] = {
        LONG_OPT("threads"),
        SINGLE_OPT('t'),
        ARG_TYPE(OPT_TYPE_INT)
    },
    [OPT_DEBUG] = {
        LONG_OPT("debug")
    }
};

#define usage(str, ...) fprintf(stdout, str "\n", ##__VA_ARGS__)
static void help(void)
{
    usage("Usage: " SDOWN_NAME " -[vhtsldrfuit]");
    usage("Generate an HTML static site from markdown sources.");
    usage ("If used with no options, " SDOWN_NAME " will look for the directory in the current path.");
    usage("");
    usage("Options:");
    usage("  -v, --version            Print software version");
    usage("  -h, --help               Show this message");
    usage("  -t, --title=TITLE        Title to be used in the final site");
    usage("  -s, --src=DIR            Markdown sources directory");
    usage("  -c, --style=DIR          Style sources directory");
    usage("  -l, --layouts=DIR        Layouts directory");
    usage("  -d, --dest=DIR           Destination directory");
    usage("  -r, --root=ROOT          Root URL of the website");
    usage("  -f, --favicon=FILE       Favicon to use");
    usage("  -u, --url=URL            Url of the website");
    usage("  -i, --img=DIR            Images directory");
    usage("  -t, --threads=NUM        Number of threads to launch");
    usage("      --debug              Print more information");
    usage("");
    usage("Default required tree:");
    usage("  .");
    usage("  ├── img");
    usage("  ├── layouts");
    usage("  │   ├── footer.html");
    usage("  │   ├── header.html");
    usage("  │   └── intro.html");
    usage("  ├── markdown");
    usage("  └── styles");
    usage("");
    usage("All posts passed to " SDOWN_NAME " (either in the default 'markdown'");
    usage("directory or through the --src option), *must* contain the following");
    usage("header:");
    usage("     ---");
    usage("     title: Article title");
    usage("     summary: Article summary");
    usage("     ---");
    usage("Optional headers can be passed to:");
    usage("- 'route': Route of article (/argument-value.html)");
    usage("- 'date': Date of the article, in YYYY-MM-DD format. If this option");
    usage("  is not here, shayla will look for the last modified timestamp from");
    usage("  the filesystem in order to establish a chronology.");
    usage("- 'list': Boolean option, whether or not the article should be listed");
    usage("  on the index.html. Default is true.");
    usage("");
    usage("Layouts can be used to define header / footer content in the final");
    usage("website. " SDOWN_NAME " will look for them under the layouts directory");
    usage("(either in the default 'layouts' directory or through the --layouts");
    usage("option), and will expect HTML files named 'footer.html', 'header.html'");
    usage("and 'intro.html'. Any other file will be ignored.");
    usage("");
    usage("This program is free software: you can redistribute it and/or modify");
    usage("it under the terms of the GNU General Public License as published by");
    usage("the Free Software Foundation, either version 3 of the License, or");
    usage("(at your option) any later version.");
    usage("");
    usage("Sources are available at <https://git.ne02ptzero.me/shayla/>");
    usage("Found a bug? Please shoot me an email!");
    usage("Louis Solofrizzo <louis at ne02ptzero dot me>");
}

static void version(void)
{
    usage(SDOWN_NAME " " SDOWN_MAJOR "." SDOWN_MINOR);
    usage("License GPLv3+: GNU GPL version 3 or later "
        "<https://gnu.org/licenses/gpl.html>.");
    usage("This is free software: you are free to change and redistribute it.");
    usage("There is NO WARRANTY, to the extent permitted by law.");
    usage("");
    usage("Sources are available at <https://git.ne02ptzero.me/shayla/>");
    usage("Found a bug? Please shoot me an email!");
    usage("Louis Solofrizzo <louis at ne02ptzero dot me>");
}
#undef usage

typedef struct {
    size_t      off;    /*!< Structure variable offset */
#define shayla_offset(member) offsetof(shayla_t, member)
    const char  *str;   /*!< Named value */
} shayla_named_offset_t;

static bool shayla_options(shayla_t *ctx)
{
    static const shayla_named_offset_t addrs[] = {
        [OPT_TITLE]     = { shayla_offset(title),       SDOWN_NAME },
        [OPT_SRC]       = { shayla_offset(posts_dir),   "markdown" },
        [OPT_STYLE]     = { shayla_offset(styles_dir),  "styles" },
        [OPT_LAYOUTS]   = { shayla_offset(layouts_dir), "layouts" },
        [OPT_DEST]      = { shayla_offset(dest),        "build" },
        [OPT_ROOT]      = { shayla_offset(root),        "/"},
        [OPT_FAVICON]   = { shayla_offset(favicon),     NULL },
        [OPT_URL]       = { shayla_offset(url),         NULL },
        [OPT_IMG]       = { shayla_offset(img_dir),     "img" }
    };

    if (IS_ARG_HERE(options, OPT_VERSION))
    {
        version();
        return false;
    }

    STATIC_ARRAY_FOREACH_I(i, const shayla_named_offset_t *opt, addrs)
    {
        uintptr_t           ptr;
        char                **str;

        if (opt->off == 0)
            continue;

        ptr = (uintptr_t)ctx + (uintptr_t)opt->off;
        str = (char **)ptr;

        if (IS_ARG_HERE(options, i))
            *str = strdup(GET_STR_ARG(options, i));
        else if (opt->str != NULL)
            *str = strdup(opt->str);

        if ((*str) == NULL && opt->str != NULL)
        {
            ERROR("No memory, could not parse arguments\n");
            return false;
        }
    }

    if (IS_ARG_HERE(options, OPT_DEBUG))
        set_debug(true);

   /* Static non-string options */
    if (IS_ARG_HERE(options, OPT_THREADS))
        pool_init(GET_INT_ARG(options, OPT_THREADS));
    else
        pool_init(DEFAULT_THREADS);

    return true;
}

static bool list_and_parse_posts(shayla_t *ctx)
{
    SET_PREFIX("Posts:");
    if (!list_dir(&ctx->posts, ctx->posts_dir, ".md"))
        goto fail;

    if (!posts_parse_all(&ctx->posts))
        goto fail;

    SET_PREFIX("Layouts:");
    if (!list_dir(&ctx->layouts, ctx->layouts_dir, ".html"))
        goto fail;

    SET_PREFIX("Styles:");
    if (!list_dir(&ctx->styles, ctx->styles_dir, "css"))
        goto fail;

    SET_PREFIX("Images:");
    if (!list_dir(&ctx->img, ctx->img_dir, NULL))
        goto fail;

    return true;
fail:
    return false;
}

static bool read_layouts(shayla_t *ctx)
{
    static const shayla_named_offset_t  layouts[] = {
        { shayla_offset(header), "header.html" },
        { shayla_offset(footer), "footer.html" },
        { shayla_offset(intro), "intro.html" }
    };
    file_t      *ptr;
    file_t      **to;
    bool        found;

    list_for_each_entry(ptr, &ctx->layouts, list)
    {
        found = false;
        STATIC_ARRAY_FOREACH(const shayla_named_offset_t *lyt, layouts)
        {
            if (strcmp(lyt->str, get_filename(ptr->path)) == 0)
            {
                found = true;
                to = (file_t **)((uintptr_t)ctx + (uintptr_t)lyt->off);
                break;
            }
        }

        if (!found)
        {
            WARNING("Ignoring file %s in layouts...\n", ptr->path);
            continue;
        }

        if (!file_read(ptr))
        {
            ERROR("Could not read file %s\n", ptr->path);
            return false;
        }

        *to = ptr;
    }

    return true;
}

static bool create_final_directories_and_copy(shayla_t *ctx)
{
    char        dir[PATH_MAX];
    char        path[PATH_MAX * 2];
    file_t      *ptr;

    if (!create_dir(ctx->dest))
    {
        ERROR("Could not create directory %s: %s\n", ctx->dest, strerror(errno));
        return false;
    }

    if (list_count(&ctx->img) > 0)
    {
        snprintf(dir, sizeof(dir), "%s/img", ctx->dest);
        if (!create_dir(dir))
            goto fail;

        list_for_each_entry(ptr, &ctx->img, list)
        {
            snprintf(path, sizeof(path), "%s/%s", dir, get_filename(ptr->path));
            if (!fast_copy(ptr->path, path))
            {
                ERROR("Could not copy %s to %s: %s\n", ptr->path, path,
                    strerror(errno));
                return false;
            }
        }
    }

    if (list_count(&ctx->styles) > 0)
    {
        snprintf(dir, sizeof(dir), "%s/styles", ctx->dest);
        if (!create_dir(dir))
            goto fail;

        list_for_each_entry(ptr, &ctx->styles, list)
        {
            snprintf(path, sizeof(path), "%s/%s", dir, get_filename(ptr->path));
            if (!fast_copy(ptr->path, path))
            {
                ERROR("Could not copy %s to %s: %s\n", ptr->path, path,
                    strerror(errno));
                return false;
            }
        }
    }

    if (ctx->favicon != NULL)
    {
        snprintf(dir, sizeof(dir), "%s/favicon.ico", ctx->dest);
        if (!fast_copy(ctx->favicon, dir))
        {
            ERROR("Could not copy %s to %s: %s\n", ctx->favicon, path,
                strerror(errno));
            return false;
        }
    }

    return true;

fail:
    ERROR("Could not create directory %s: %s\n", dir, strerror(errno));
    return false;
}

int main(int ac, const char **av)
{
    shayla_t    ctx = { 0 };
    int         ret = 1;

    shayla_init(&ctx);

    if (!parse_args(ac, av, options, COUNT_OF(options), help))
        return 1;

    if (!shayla_options(&ctx))
        goto end;

    if (!list_and_parse_posts(&ctx))
        goto end;

    if (!read_layouts(&ctx))
        goto end;

    if (!create_final_directories_and_copy(&ctx))
        goto end;

    if (!dump_site(&ctx))
        goto end;

    if (!generate_rss(&ctx))
        goto end;

    if (!generate_sitemap(&ctx))
        goto end;

    OK("All good, you can find your website in %s\n", ctx.dest);
    ret = 0;
end:
    free_args(options, COUNT_OF(options));
    pool_cleanup();
    shayla_dtr(&ctx);
    DEBUG("Quitting, ret = %d\n", ret);
    return ret;
}