adonis-ssg

dead simple, C89 static site generator with markdown support and no external dependencies
git clone git://kqueue.dev/adonis-ssg.git
Log | Files | Refs | README | LICENSE

adonis-ssg.c (4491B)


      1 /* Copyright 2020-2023 kqueue <kqueue@cocaine.ninja>
      2 
      3 Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
      4 
      5 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
      6 SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      7 */
      8 #include <stdio.h>
      9 #include <string.h>
     10 #include <stdlib.h>
     11 #if defined(__Openbsd__)
     12 #include <unistd.h>
     13 #endif
     14 #include "config.h"
     15 int 		smu_convert(FILE * out, FILE * source, int supresshtml);
     16 /* function to find and replac the title */
     17 char           *
     18 replaceword(const char *s, const char *oldW,
     19 	    const char *newW)
     20 {
     21 	char           *result;
     22 	int 		i        , cnt = 0;
     23 	int 		newWlen = strlen(newW);
     24 	int 		oldWlen = strlen(oldW);
     25 	for (i = 0; s[i] != '\0'; i++) {
     26 		if (strstr(&s[i], oldW) == &s[i]) {
     27 			cnt++;
     28 			i += oldWlen - 1;
     29 		}
     30 	}
     31 	result = (char *) malloc(i + cnt * (newWlen - oldWlen) + 1);
     32 	i = 0;
     33 	while (*s) {
     34 		if (strstr(s, oldW) == s) {
     35 			strcpy(&result[i], newW);
     36 			i += newWlen;
     37 			s += oldWlen;
     38 		} else
     39 			result[i++] = *s++;
     40 	}
     41 
     42 	result[i] = '\0';
     43 	return result;
     44 }
     45 
     46 void
     47 generate_files(char *line)
     48 {
     49 	/* declare and open the template file */
     50 	FILE           *template;
     51 	template = fopen(htmltemplate, "r");
     52 	fseek(template, 0, SEEK_END);
     53 	/* get the template size */
     54 	long 		templatesize = ftell(template);
     55 	fseek(template, 0, SEEK_SET);
     56 	/* allocate the memory for reading the template file */
     57 	char           *file = malloc(templatesize + 1);
     58 	/* read the template file */
     59 	fread(file, 1, templatesize, template);
     60 	fclose(template);
     61 
     62 	char           *token, *stuff[3];
     63 	int 		i = 0;
     64 	token = strtok(line, "\t");
     65 	while (token != NULL) {
     66 		stuff[i] = token;
     67 		token = strtok(NULL, "\t");
     68 		i++;
     69 	}
     70 	/* find "INSERT-TITLE" and replace it with the specified title */
     71 	char           *result = replaceword(file, "INSERT-TITLE", stuff[1]);
     72 	free(file);
     73 	FILE           *article;
     74 	article = fopen(stuff[2], "w+");
     75 	fprintf(article, "%s", result);
     76 	free(result);
     77 	fclose(article);
     78 
     79 	FILE           *article2, *markdown;
     80 	article2 = fopen(stuff[2], "a+");
     81 	markdown = fopen(stuff[0], "rw+");
     82 	smu_convert(article2, markdown, 1);
     83 	if(genrss){
     84 	fseek(markdown, 0, SEEK_END);
     85 	long 		fpsize = ftell(markdown);
     86 	fseek(markdown, 0, SEEK_SET);
     87 	char           *file = malloc(fpsize + 1);
     88 	fread(file, 1, fpsize, markdown);
     89 	FILE *fp = fopen("rss.xml", "a+");
     90 	fprintf(fp, "\n\t<item>\n\t\t<title>%s</title>\n\t\t<link>%s%s</link>\n\t\t<description><![CDATA[<pre>\n%s\n</pre>]]></description>\n\t</item>", stuff[1], url, stuff[2] + 1, file);
     91 	free(file);
     92 	fclose (fp);
     93 	}
     94 	rewind(article2);
     95 	fputs(appendhtml, article2);
     96 	fclose(article2);
     97 	fclose(markdown);
     98 }
     99 
    100 int
    101 main(int argc, char *argv[])
    102 {
    103 #if defined(__Openbsd__)
    104 	pledge("stdio rpath wpath", NULL);
    105 #endif
    106 	if (argc < 2) {
    107 		printf("usage:\nadonis-ssg files.conf\n");
    108 		return 0;
    109 	}
    110 	FILE           *fp;
    111 	/* open file */
    112 	fp = fopen(argv[1], "r");
    113 	/* nc is the number of lines in the file */
    114 	int 		nc       , exit, wc, y;
    115 	exit = nc = 0;
    116 	while ((y = fgetc(fp)) != EOF) {
    117 		if (y == '\n')
    118 			nc++;
    119 	}
    120 	rewind(fp);
    121 	if(genrss){
    122 	FILE *rss = fopen("rss.xml", "w");
    123 	fprintf(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n<channel>\n<atom:link href=\"%s/rss.xml\" rel=\"self\" type=\"application/rss+xml\"/>\n\t<link>%s</link>\n\t<title>%s</title>\n\t<description>%s</description>\n\t<generator>https://kqueue.dev/git/adonis-ssg</generator>\n", url, url, rsstitle, rssdesc);
    124 	fclose(rss);
    125 	}
    126 	char 		c        [200];
    127 	wc = nc - 1;
    128 	fseek(fp, 0, SEEK_END);
    129 	long 		fpsize = ftell(fp);
    130 	fseek(fp, 0, SEEK_SET);
    131 	char           *file = malloc(fpsize + 1);
    132 	fread(file, 1, fpsize, fp);
    133 	char *lines[nc], *token;
    134 	int i = 0;
    135 	token = strtok(file, "\n");
    136 	while (token != NULL) {
    137 		lines[i] = token;
    138 		token = strtok(NULL, "\n");
    139 		i++;
    140 	}
    141 	while (!exit) {
    142 		/* build the pages */
    143 		generate_files(lines[wc]);
    144 		wc--;
    145 		if (wc == -1) {
    146 			exit = 1;
    147 		}
    148 	}
    149 	fclose(fp);
    150 	free(file);
    151 	return 0;
    152 }