shayla

a simple yet fast static site builder
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/shayla

commit 54ec38a8e6a2fa987e837605850f65804b06ae1f
parent cbec05b205c16fd59a5bc9ac2304ddb3b2c13bbd
Author: Louis Solofrizzo <lsolofrizzo@online.net>
Date:   Fri, 28 Sep 2018 10:36:42 +0200

Rss: Now generating feed and sitemap

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>

Diffstat:
MCMakeLists.txt | 1+
Mmain.c | 7+++++++
Arss.c | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Arss.h | 32++++++++++++++++++++++++++++++++
4 files changed, 145 insertions(+), 0 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -46,6 +46,7 @@ add_executable(${SDOW_NAME} main.c dir.c pool.c shtml.c + rss.c ./sundown/src/autolink.c ./sundown/src/buffer.c ./sundown/src/markdown.c diff --git a/main.c b/main.c @@ -21,6 +21,7 @@ #include <inttypes.h> #include <pool.h> #include <shtml.h> +#include <rss.h> #include <errno.h> enum { @@ -393,6 +394,12 @@ int main(int ac, const char **av) if (!dump_site(&ctx)) goto end; + if (!generate_rss(&ctx)) + goto end; + + if (!generate_sitemap(&ctx)) + goto end; + ret = 0; end: free_args(options, COUNT_OF(options)); diff --git a/rss.c b/rss.c @@ -0,0 +1,105 @@ +/** + * 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.xml</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; +} diff --git a/rss.h b/rss.h @@ -0,0 +1,32 @@ +/** + * 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/>. +*/ + +#ifndef RSS_H +# define RSS_H + +# include <shayla.h> +# include <stdbool.h> + +/*! + * \brief Generate the RSS flux from posts + */ +bool generate_rss(const shayla_t *ctx); + +/*! + * \brief Generate a sitemap from posts + */ +bool generate_sitemap(const shayla_t *ctx); + +#endif /* RSS_H */