From 8cac2c3e3a61b64b9a9855dec413239bcec7f9d2 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Tue, 27 Dec 2016 02:29:54 +0100 Subject: make: implemented a build tool for blogc so, this is basically what happens when you don't have anything better to do in the christmas weekend. most of this code was written in the last 2 or 3 days. i'd like to thank the chivas brothers, the weather and my psychological problems for this achievement. on a serious note, this tool still needs a man page, more tests, and the aws lambda function should be adapted to use it instead of (or together with) make/busybox. also, while talking about aws lambda, this tool can be nicely embedded into the blogc binary, to produce a single "small" static binary for usage in lambda ;) --- src/blogc-make/atom.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/blogc-make/atom.c (limited to 'src/blogc-make/atom.c') diff --git a/src/blogc-make/atom.c b/src/blogc-make/atom.c new file mode 100644 index 0000000..c7f98a5 --- /dev/null +++ b/src/blogc-make/atom.c @@ -0,0 +1,93 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include +#include +#include +#include +#include "../common/error.h" +#include "../common/utils.h" +#include "settings.h" +#include "atom.h" + +static const char atom_template[] = + "\n" + "\n" + " {{ SITE_TITLE }}{%% ifdef FILTER_TAG %%} - " + "{{ FILTER_TAG }}{%% endif %%}\n" + " {{ BASE_URL }}/%s{%% ifdef FILTER_TAG %%}/{{ FILTER_TAG }}" + "{%% endif %%}%s\n" + " {{ DATE_FIRST_FORMATTED }}\n" + " \n" + " \n" + " \n" + " {{ AUTHOR_NAME }}\n" + " {{ AUTHOR_EMAIL }}\n" + " \n" + " {{ SITE_TAGLINE }}\n" + " {%% block listing %%}\n" + " \n" + " {{ TITLE }}\n" + " {{ BASE_URL }}/%s/{{ FILENAME }}/\n" + " {{ DATE_FORMATTED }}\n" + " {{ DATE_FORMATTED }}\n" + " \n" + " \n" + " {{ AUTHOR_NAME }}\n" + " {{ AUTHOR_EMAIL }}\n" + " \n" + " \n" + " \n" + " {%% endblock %%}\n" + "\n"; + + +char* +bm_atom_deploy(bm_settings_t *settings, bc_error_t **err) +{ + if (err == NULL || *err != NULL) + return NULL; + + // this is not really portable + char fname[] = "/tmp/blogc-make_XXXXXX"; + int fd; + if (-1 == (fd = mkstemp(fname))) { + *err = bc_error_new_printf(BLOGC_MAKE_ERROR_ATOM, + "Failed to create temporary atom template: %s", strerror(errno)); + return NULL; + } + + const char *atom_prefix = bc_trie_lookup(settings->settings, "atom_prefix"); + const char *atom_ext = bc_trie_lookup(settings->settings, "atom_ext"); + const char *post_prefix = bc_trie_lookup(settings->settings, "post_prefix"); + + char *content = bc_strdup_printf(atom_template, atom_prefix, atom_ext, + atom_prefix, atom_ext, post_prefix, post_prefix); + + if (-1 == write(fd, content, strlen(content))) { + *err = bc_error_new_printf(BLOGC_MAKE_ERROR_ATOM, + "Failed to write to temporary atom template: %s", strerror(errno)); + free(content); + close(fd); + unlink(fname); + return NULL; + } + + free(content); + close(fd); + + return bc_strdup(fname); +} + + +void +bm_atom_destroy(const char *fname) +{ + unlink(fname); +} -- cgit v1.2.3-18-g5258