all repos — eureka @ 83ccc9d32e6a244a74db55492ed6e0daed6ccc8a

static site generator based on the 100r.co engine

main.c (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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
/*
Copyright (c) 2021 Derek Stevens
Copyright (c) 2021 Devine Lu Linvega

Permission to use, copy, modify, and 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.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

/* clang-format off */

#define NAME "nilFM"
#define DOMAIN "https://nilfm.cc"
#define DESC  "lair of drkste aka nilix: programmer, artist, philosopher"
#define ABOUT "Derek Stevens &lt;"\
              "<a href='mailto://nilix@nilfm.cc'>nilix@nilfm.cc</a>&gt;<br/>"\
              "artist, programmer, philosopher<br/><br/>"\
              "verify my signature: <a href='/serv/signingKey.pub'>signing public key</a><br/>"\
              "send me an encrypted message: <a href='/serv/encryptionKey.pub'>encryption public key</a>"
#define CONTACT "contact: <a href='mailto://nilix@nilfm.cc'>nilix@nilfm.cc</a> (<a href='keys.html'>keys</a>)<br/>"
#define FOOTER "<a href='/git/'><img "\
                "src='/img/git.svg' alt='visit the nilFM hack lab.'/></a>&nbsp;"\
                "<a rel='me' href='https://fosstodon.org/@nilix'><img "\
                "src='/img/mastodon.svg' alt='visit me on Mastodon.'/></a>&nbsp;"\
                "<a href='https://webring.xxiivv.com'><img "\
                "src='/img/webring.svg' alt='visit the Webring.'/></a><br/>"
#define LICENSE "<a rel='license' "\
                "href='https://creativecommons.org/licenses/by-nc/4.0/"\
                "legalcode.txt'>CC-BY-NC 4</a>"
#define SITEROOT "../www/"
#define MAINCSS "/new.css"
#define FRONTCSS "/front.css"

/* clang-format on */

struct dirent* dir;

typedef struct Lexicon {
  int len, refs[512];
  char files[512][64];
} Lexicon;

/* clang-format off */

char  clca(char c) { return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; } /* char to lowercase */
char  cuca(char c) { return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c; } /* char to uppercase */
int   slen(char *s) { int i = 0; while(s[i] && s[++i]) { ; } return i; } /* string length */
char *st__(char *s, char (*fn)(char)) { int i = 0; char c; while((c = s[i])) s[i++] = fn(c); return s; }
char *stuc(char *s) { return st__(s, cuca); } /* string to uppercase */
char *stlc(char *s) { return st__(s, clca); } /* string to lowercase */
char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* string copy */
int   scmp(char *a, char *b) { int i = 0; while(a[i] == b[i]) if(!a[i++]) return 1; return 0; } /* string compare */
char *scsw(char *s, char a, char b) { int i = 0; char c; while((c = s[i])) s[i++] = c == a ? b : c; return s; } /* string char swap */
char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */
int   ssin(char *s, char *ss) { int a = 0, b = 0; while(s[a]) { if(s[a] == ss[b]) { if(!ss[b + 1]) return a - b; b++; } else b = 0; a++; } return -1; } /* string substring index */
char *ccat(char *dst, char c) { int len = slen(dst); dst[len] = c; dst[len + 1] = '\0'; return dst; }
int clin(char *str, char c) {int i = -1; int j = 0; while(str[j] != '\0'){if(str[j] == c) i = j;j++;}return i;} /* find last occurence of character in string */
/* clang-format on */

int fpinject(FILE* f, Lexicon* l, char* filepath);

int error(char* msg, char* val) {
  printf("Error: %s(%s)\n", msg, val);
  return 0;
}

int ismetanav(char* name) { return scmp(name, "meta.nav"); }

int findf(Lexicon* l, char* f) {
  int i;
  char filename[64];
  scat(scsw(stlc(scpy(f, filename, 64)), ' ', '_'), ".htm");
  for (i = 0; i < l->len; ++i)
    if (scmp(l->files[i], filename))
      return i;
  return -1;
}

int gettwtxt(FILE* f) {
  char buf[1024] = {0};
  char line[256];
  char datebuf[16] = {0};
  char msgbuf[240] = {0};
  char *l, *d, *m;
  FILE* twtxt = fopen(SITEROOT "twtxt.txt", "r");

  scat(buf, "<div id='twtxtFeed'><h2>recent activity:</h2><table>");
  if (!twtxt) {
    error("Get twtxt feed", "no twtxt.txt");
    return 0;
  } else {
    int i = 0;
    while (fgets(line, 256, twtxt) && i < 3) {
      l = line;
      while (*l != 'T') {
        ccat(datebuf, *l++);
      }
      while (*l != '\t') {
        l++;
      }
      scat(msgbuf, l);
      scat(buf, "<tr><td style='white-space:nowrap;vertical-align: top;'>");
      scat(buf, datebuf);
      scat(buf, ":</td><td> ");
      scat(buf, msgbuf);
      scat(buf, "</td></tr>");
      d = datebuf;
      while (*d) {
        *d++ = 0;
      }
      m = msgbuf;
      while (*m) {
        *m++ = 0;
      }
      i++;
    }
    scat(buf, "</table><a href='/twtxt.txt'>see all</a>");
    scat(buf, "</div>");
    fputs(buf, f);
    return 1;
  }
}

void fpedited(FILE* f, char* path) {
  struct stat attr;
  stat(path, &attr);
  fprintf(f, "Edited on %s<br/>", ctime(&attr.st_mtime));
}

int fpportal(FILE* f, Lexicon* l, char* s, int head) {
  int target;
  char srcpath[64], filename[64];
  target = findf(l, s);
  if (target < 0)
    return error("Missing portal", s);
  srcpath[0] = 0;
  filename[0] = 0;
  scat(scat(scat(srcpath, "inc/"), scpy(s, filename, 64)), ".htm");
  if (head)
    fprintf(
      f,
      "<h3 id='%s'><a href='%s.html'>%s</a></h3>",
      scsw(filename, ' ', '_'),
      filename,
      s);
  fpinject(f, l, srcpath);
  l->refs[target]++;
  return 1;
}

int fphref(FILE* f, char* s) {
  char href[1024] = {0};
  char txt[1024] = {0};

  char* c = s;
  int i = 0;
  while (i < 2) {
    if (*c == '|' || !(*c)) {
      i++;
      c++;
      continue;
    }
    switch (i) {
      case 0:
        ccat(href, *c++);
        break;
      case 1:
        ccat(txt, *c++);
        break;
    }
  }
  fprintf(f, "<a href='%s'>%s</a>", href, txt);
  return 1;
}

char* thumbtrans(char* s, char* thumb) {
  int i, j;
  char* ss;
  char* fnss;
  char fn[1024];

  scpy(s, thumb, slen(s));

  i = clin(thumb, '/');
  ss = thumb + i + 1;

  scpy(ss, fn, slen(thumb) - i + 1);
  scpy(".thumb/", ss, 8);

  j = clin(fn, '.');
  fnss = fn + j;
  scpy(".png", fnss, 5);
  scat(thumb, fn);
  return thumb;
}

int fpimg(FILE* f, char* s) {
  char id[1024] = {0};
  char src[1024] = {0};
  char alt[1024] = {0};
  char thumb[1024] = {0};

  char* c = s;
  int i = 0;
  while (i < 3) {
    if (*c == '|' || !(*c)) {
      i++;
      c++;
      continue;
    }
    switch (i) {
      case 0:
        ccat(id, *c++);
        break;
      case 1:
        ccat(src, *c++);
        break;
      case 2:
        ccat(alt, *c++);
        break;
    }
  }

  thumbtrans(src, thumb);
  fprintf(f, "<a id='%s' href='%s'>\n", id, src);
  fprintf(f, "<img class='image' src='%s' alt='%s'/>\n", thumb, alt);
  fputs("</a>", f);
  return 1;
}

int fphimg(FILE* f, char* s) {
  char id[1024] = {0};
  char href[1024] = {0};
  char src[1024] = {0};
  char alt[1024] = {0};
  char thumb[1024] = {0};

  char* c = s;
  int i = 0;
  while (i < 4) {
    if (*c == '|' || !(*c)) {
      i++;
      c++;
      continue;
    }
    switch (i) {
      case 0:
        ccat(id, *c++);
        break;
      case 1:
        ccat(href, *c++);
        break;
      case 2:
        ccat(src, *c++);
        break;
      case 3:
        ccat(alt, *c++);
        break;
    }
  }

  thumbtrans(src, thumb);

  fprintf(f, "<a id='%s' href='%s'>\n", id, href);
  fprintf(f, "<img class='image' src='%s' alt='%s'/>\n", thumb, alt);
  fputs("</a>", f);
  return 1;
}

int fpaudio(FILE* f, char* s) {
  fputs("<audio class='player' preload='metadata' controls>", f);
  fprintf(f, "<source src='%s' type='audio/mpeg'>[HTML5 audio]", s);
  fputs("</audio>", f);
  return 1;
}

int fptemplate(FILE* f, Lexicon* l, char* s) {
  int target;
  if (s[0] == '/')
    return fpportal(f, l, s + 1, 1);
  if (s[0] == '*')
    return fphref(f, s + 1);
  if (s[0] == ':')
    return fpimg(f, s + 1);
  if (s[0] == '?')
    return fphimg(f, s + 1);
  if (s[0] == '_')
    return fpaudio(f, s + 1);
  target = findf(l, s);
  if (target < 0)
    return error("Missing link", s);
  fprintf(f, "<a href='%s.html' class='local'>", scsw(stlc(s), ' ', '_'));
  fprintf(f, "%s</a>", scsw(stlc(s), '_', ' '));
  l->refs[target]++;
  return 1;
}

int fpinject(FILE* f, Lexicon* l, char* filepath) {
  FILE* inc;
  char c, s[1024];
  unsigned char t = 0;
  scsw(filepath, ' ', '_');
  if (!(inc = fopen(filepath, "r")))
    return error("Missing include", filepath);
  s[0] = 0;
  while ((c = fgetc(inc)) != EOF) {
    if (c == '}') {
      t = 0;
      if (!fptemplate(f, l, s))
        return 0;
      continue;
    }
    if (c == '{') {
      s[0] = 0;
      t = 1;
      continue;
    }
    if (slen(s) > 1023)
      return error("Templating error", filepath);
    if (t)
      ccat(s, c);
    else
      fprintf(f, "%c", c);
  }
  fclose(inc);
  return 1;
}

int fpfooter(FILE* f, char* name, char* path) {
  if (!f || !name || !path)
    return 0;
  fputs("<footer>", f);
  if (!ismetanav(name)) {
    fpedited(f, path);
    fputs(CONTACT, f);
  }
  fputs(FOOTER, f);
  fputs(LICENSE, f);
  fputs("</footer>", f);
  return 1;
}

FILE* build(FILE* f, Lexicon* l, char* name, char* srcpath) {
  if (!f)
    return f;
  /* begin */
  fputs("<!DOCTYPE html><html lang='en'>", f);
  fputs("<head>", f);
  fprintf(
    f,
    "<meta charset='utf-8'>"
    "<meta name='description' content='%s'/>"
    "<meta name='viewport' content='width=device-width,initial-scale=1'>",
    DESC);
  if (ismetanav(name)) {
    fputs("<link rel='stylesheet' type='text/css' href='" FRONTCSS "'>", f);
  } else {
    fputs("<link rel='stylesheet' type='text/css' href='" MAINCSS "'>", f);
  }
  fprintf(
    f,
    "<link rel='shortcut icon' href='/favicon.ico'>"
    "<title>" NAME " &mdash; %s</title>",
    ismetanav(name) ? "home" : name);
  fputs("</head>", f);
  fputs("<body>", f);
  /* header */
  fputs("<header>", f);
  if (ismetanav(name)) {
    fputs("<h1>" NAME "</h1>", f);
  } else {
    fputs("<h1><a href='/'>" NAME "</a></h1>", f);
  }

  fputs("</header>", f);
  /* about (main page only) */
  if (ismetanav(name)) {
    fputs("<div id='about'>" ABOUT "</div>", f);
  }
  /* nav */
  fputs("<nav>", f);
  if (!fpportal(f, l, "meta.nav", 0))
    printf(">>> Building failed: %s\n", name);
  fputs("</nav>", f);
  /* main */
  if (!ismetanav(name)) {
    fputs("<main>\n\n", f);
    fprintf(f, "<h2>%s</h2>", name);
    if (!fpinject(f, l, srcpath))
      printf(">>> Building failed: %s\n", name);
    fputs("\n\n</main>", f);
  } else {
    gettwtxt(f);
  }
  /* footer */
  if (!fpfooter(f, name, srcpath)) {
    printf(">>> Building failed: footer(%s)\n", name);
  }

  /* end */
  fputs("</body></html>", f);
  return f;
}

int generate(Lexicon* l) {
  int i = 0;
  char srcpath[64], dstpath[64], filename[64];
  for (i = 0; i < l->len; ++i) {
    srcpath[0] = 0;
    dstpath[0] = 0;
    filename[0] = 0;
    /* src */
    scpy(l->files[i], filename, ssin(l->files[i], ".htm") + 1);
    scat(srcpath, "inc/");
    scat(srcpath, filename);
    scat(srcpath, ".htm");
    /* dst */
    if (scmp(filename, "meta.nav")) {
      scat(dstpath, SITEROOT);
      scat(dstpath, "index.html");
    } else {
      scat(dstpath, SITEROOT);
      scat(dstpath, filename);
      scat(dstpath, ".html");
    }
    fclose(build(fopen(dstpath, "w"), l, scsw(filename, '_', ' '), srcpath));
  }
  printf("Generated %d files\n", i);
  return 1;
}

int index(Lexicon* l, DIR* d) {
  while ((dir = readdir(d)))
    if (ssin(dir->d_name, ".htm") > 0) {
      l->refs[l->len] = 0;
      scpy(dir->d_name, l->files[l->len++], 64);
    }
  closedir(d);
  printf("Indexed %d terms\n", l->len);
  return 1;
}

void inspect(Lexicon* l) {
  int i;
  for (i = 0; i < l->len; ++i)
    if (!l->refs[i])
      error("Orphaned", l->files[i]);
}

int main(void) {
  Lexicon lex;
  DIR* d;
  lex.len = 0;
  if (!(d = opendir("inc")))
    return error("Open", "Missing inc/ folder. ");
  if (!index(&lex, d))
    return error("Indexing", "Failed");
  if (!generate(&lex))
    return error("Generating", "Failed");
  inspect(&lex);
  return 0;
}