ref: 9ee696abcfd19305fcaf906812aac448a809866a shayla/rss.c -rw-r--r-- 3.3 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
/**
 *  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 <rss.h>
#include <stdio.h>

#define xml(str, ...) fprintf(fd, str, ##__VA_ARGS__);

bool generate_rss(const shayla_t *ctx)
{
    char        path[PATH_MAX] = { 0 };
    FILE        *fd;
    file_t      *ptr;

    snprintf(path, sizeof(path), "%s/rss.xml", ctx->dest);
    fd = fopen(path, "w+");
    if (fd == NULL)
    {
        ERROR("Could not open %s\n", path);
        return false;
    }
    INFO("Generating rss.xml\n");

    xml("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    xml("<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">") {
        xml("<channel>") {
            xml("<title>%s</title>", ctx->title);
            xml("<link>%s%s</link>", ctx->url, ctx->root);

            list_for_each_entry(ptr, &ctx->posts, list)
            {
                if (!ptr->is_listed)
                    continue;

                xml("<item>") {
                    xml("<title>%s</title>", ptr->title);
                    xml("<link>%s%s%s.html</link>", ctx->url, ctx->root, ptr->route);
                    xml("<description>%s</description>", ptr->summary);
                } xml("</item>");
            }

        } xml("</channel>");
    } xml("</rss>");

    fclose(fd);
    return true;
}

bool generate_sitemap(const shayla_t *ctx)
{
    char        path[PATH_MAX] = { 0 };
    char        str[1024] = { 0 };
    FILE        *fd;
    file_t      *ptr;
    time_t      now = time(NULL);

    snprintf(path, sizeof(path), "%s/sitemap.xml", ctx->dest);
    fd = fopen(path, "w+");
    if (fd == NULL)
    {
        ERROR("Could not open %s\n", path);
        return false;
    }
    INFO("Generating sitemap.xml\n");

    xml("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    xml("<urlset ");
    xml("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
    xml("xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9");
    xml("http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\" ");
    xml("xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");

    list_for_each_entry(ptr, &ctx->posts, list)
    {
        strftime(str, sizeof(str), "%Y-%m-%d", localtime(&ptr->ts));
        xml("<url>") {
            xml("<loc>%s%s%s.html</loc>", ctx->url, ctx->root, ptr->route);
            xml("<lastmod>%s</lastmod>", str);
            xml("<priority>1.0</priority>");
        } xml("</url>");
    }

    strftime(str, sizeof(str), "%Y-%m-%d", localtime(&now));
    xml("<url>") {
        xml("<loc>%s%sindex.html</loc>", ctx->url, ctx->root);
        xml("<lastmod>%s</lastmod>", str);
        xml("<priority>1.0</priority>");
    } xml("</url>");

    xml("</urlset>");
    fclose(fd);
    return true;
}