aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/atom.c
blob: 0f86e1cd9cd37a7d20d72d7b4f9b570d2dd0e8ab (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
/*
 * 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 <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "../common/error.h"
#include "../common/utils.h"
#include "settings.h"
#include "utils.h"
#include "atom.h"

static const char atom_template[] =
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
    "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n"
    "  <title type=\"text\">{{ SITE_TITLE }}{%% ifdef FILTER_TAG %%} - "
        "{{ FILTER_TAG }}{%% endif %%}</title>\n"
    "  <id>{{ BASE_DOMAIN }}{{ BASE_URL }}%s</id>\n"
    "  <updated>{{ DATE_FIRST_FORMATTED }}</updated>\n"
    "  <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/\" />\n"
    "  <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}%s\" rel=\"self\" />\n"
    "  <author>\n"
    "    <name>{{ AUTHOR_NAME }}</name>\n"
    "    <email>{{ AUTHOR_EMAIL }}</email>\n"
    "  </author>\n"
    "  <subtitle type=\"text\">{{ SITE_TAGLINE }}</subtitle>\n"
    "  {%%- block listing %%}\n"
    "  <entry>\n"
    "    <title type=\"text\">{{ TITLE }}</title>\n"
    "    <id>{{ BASE_DOMAIN }}{{ BASE_URL }}%s</id>\n"
    "    <updated>{{ DATE_FORMATTED }}</updated>\n"
    "    <published>{{ DATE_FORMATTED }}</published>\n"
    "    <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}%s\" />\n"
    "    <author>\n"
    "      <name>{{ AUTHOR_NAME }}</name>\n"
    "      <email>{{ AUTHOR_EMAIL }}</email>\n"
    "    </author>\n"
    "    <content type=\"html\"><![CDATA[{{ CONTENT }}]]></content>\n"
    "  </entry>\n"
    "  {%%- endblock %%}\n"
    "</feed>\n";


char*
bm_atom_generate(bm_settings_t *settings)
{
    if (settings == NULL)
        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");
    const char *post_ext = bc_trie_lookup(settings->settings, "html_ext");

    bc_string_t *atom_url = bc_string_new();

    if (atom_prefix[0] != '\0')
        bc_string_append_c(atom_url, '/');

    bc_string_append(atom_url, atom_prefix);
    bc_string_append(atom_url, "{% ifdef FILTER_TAG %}/{{ FILTER_TAG }}");

    if (atom_prefix[0] == '\0' && atom_ext[0] != '/')
        bc_string_append(atom_url, "{% else %}/index");

    bc_string_append(atom_url, "{% endif %}");
    bc_string_append(atom_url, atom_ext);

    char *post_url = bm_generate_filename(NULL, post_prefix, "{{ FILENAME }}",
        post_ext);

    char *rv = bc_strdup_printf(atom_template, atom_url->str, atom_url->str,
        post_url, post_url);

    bc_string_free(atom_url, true);
    free(post_url);

    return rv;
}


char*
bm_atom_deploy(bm_settings_t *settings, bc_error_t **err)
{
    if (settings == NULL || err == NULL || *err != NULL)
        return NULL;

    if (NULL != bc_trie_lookup(settings->settings, "atom_legacy_entry_id")) {
        *err = bc_error_new_printf(BLOGC_MAKE_ERROR_ATOM,
            "'atom_legacy_entry_id' setting is not supported anymore. see "
            "https://blogc.rgm.io/news/blogc-0.16.1/ for details");
        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;
    }

    char *content = bm_atom_generate(settings);
    if (content == NULL) {
        close(fd);
        unlink(fname);
        return NULL;
    }

    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);
}