54ec38a8e6a2fa987e837605850f65804b06ae1f — Louis Solofrizzo 1 year, 25 days ago cbec05b
Rss: Now generating feed and sitemap

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>
4 files changed, 145 insertions(+), 0 deletions(-)

M CMakeLists.txt
M main.c
A rss.c
A rss.h
M CMakeLists.txt => CMakeLists.txt +1 -0
@@ 46,6 46,7 @@ dir.c
      pool.c
      shtml.c
+     rss.c
      ./sundown/src/autolink.c
      ./sundown/src/buffer.c
      ./sundown/src/markdown.c

M main.c => main.c +7 -0
@@ 21,6 21,7 @@ #include <inttypes.h>
  #include <pool.h>
  #include <shtml.h>
+ #include <rss.h>
  #include <errno.h>
  
  enum {


@@ 393,6 394,12 @@ 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));

A rss.c => rss.c +105 -0
@@ 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;
+ }

A rss.h => rss.h +32 -0
@@ 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 */