kisscommunity

kisscommunity.bvnf.space site sources
git clone git://bvnf.space/home/kiss/kisscommunity.git
Log | Files | Refs | Submodules | README | LICENSE

build-page.c (6670B)


      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
#define _POSIX_C_SOURCE 200809L

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <dirent.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

#define CONVERTER "smu", "-n"
#define LEN(x) (sizeof(x) / sizeof(x[0]))

char *html_header =
    "<!doctype html>\n"
    "<html>\n"
    "<head>\n"
    "\t<meta charset=\"utf-8\"/>\n"
    "\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n"
    "\t<title>%s - KISS Community Wiki</title>\n"
    "\t<link rel=\"icon\" href=\"data:image/gif;base64,R0lGODlhEAAQAPEDAAAAAP8AAP///wAAACH5BAX//wMALAAAAAAQABAAAAImnB+ni8kf4mNRzmbvqE5zPQDiqI3kBQhmqZ5TuqKtyMavCZT4UgAAOw==\"/>\n"
    "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"/style.css\"/>\n"
    "</head>\n";

char *html_nav_bar = "\t<span class=\"right\">\n"
                     "\t\t<a href=\"/cgi-bin/search\">Search</a>\n"
                     "\t\t<a href=\"https://codeberg.org/kiss-community\">Codeberg</a>\n"
                     "\t\t<a href=\"https://kisslinux.org\">Official site</a>\n"
                     "\t</span>\n";

char *html_footer = "</html>\n";

void die_perror(char *fmt, ...) {
  va_list ap;

  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap);
  va_end(ap);
  fputs(": ", stderr);
  perror(NULL);
  exit(1);
}

void die(char *fmt, ...) {
  va_list ap;

  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap);
  va_end(ap);
  fputc('\n', stderr);
  exit(1);
}

char *xstrdup(const char *s) {
  char *p = strdup(s);

  if (!p)
    die_perror("strdup");

  return p;
}

int stat_isdir(const char *f) {
  struct stat s;

  if (stat(f, &s) == -1) {
    perror(f);
    return 0;
  }
  return S_ISDIR(s.st_mode);
}

int stat_isfile(const char *f) {
  struct stat s;

  if (stat(f, &s) == -1) {
    return 0;
  }
  return S_ISREG(s.st_mode);
}

int spawn_wait(char **argv) {
  int status;

  switch (fork()) {
  case 0:
    execvp(argv[0], argv);
    exit(126);
  case -1:
    return -1;
  }
  if (wait(&status) == -1)
    return -1;

  return WIFEXITED(status) ? WEXITSTATUS(status) ? -1 : 0 : -1;
}

void print_name(const char *name) {
  int c;

  for (; (c = *name); ++name)
    putchar((c == '_' || c == '-') ? ' ' : c);
}

void print_nav_bar(void) {
  puts("<div id=\"menu\">");
  puts("\t<a href=\"/\"><b>KISS Community Wiki</b></a>\n");

  fputs(html_nav_bar, stdout);
  puts("</div>");
}

int qsort_strcmp(const void *a, const void *b) {
  return strcasecmp(*(const char **)a, *(const char **)b);
}

int has_subdirs(char *this) {
  DIR *dp;
  struct dirent *de;
  char newdir[PATH_MAX];
  int dir;

  if ((dp = opendir(this ? this : ".")) == NULL)
    die_perror("opendir: %s", this ? this : ".");

  dir = 0;
  while (dir == 0 && (de = readdir(dp))) {
    if (de->d_name[0] == '.')
      continue;
    snprintf(newdir, sizeof(newdir), this ? "%2$s/%1$s" : "%s", de->d_name,
             this);
    if (stat_isdir(newdir))
      dir = 1;
  }
  closedir(dp);

  return dir;
}

void menu_panel(char *page, char *this, int depth) {
  DIR *dp;
  struct dirent *de;
  char newdir[PATH_MAX];
  char *d_list[PATH_MAX], *d;
  size_t d_len, l;
  int i, highlight;

  if ((dp = opendir(this ? this : ".")) == NULL)
    die_perror("opendir: %s", this ? this : ".");

  d_len = 0;
  while (d_len < LEN(d_list) && (de = readdir(dp)))
    d_list[d_len++] = xstrdup(de->d_name);
  closedir(dp);

  qsort(d_list, d_len, sizeof *d_list, qsort_strcmp);

  for (l = 0; l < d_len; free(d_list[l++])) {
    d = d_list[l];
    if (*d == '.')
      continue;
    snprintf(newdir, sizeof(newdir), this ? "%2$s/%1$s" : "%s", d, this);
    if (!stat_isdir(newdir))
      continue;

    highlight = page && !strncmp(newdir, page, strlen(newdir)) &&
                strchr("/", page[strlen(newdir)]); /* / or NUL */

    for (i = 0; i < depth + 1; ++i)
      putchar('\t');
    fputs("<li>", stdout);
    if (highlight) {
      printf("<a href=\"/%s/\"><b>", newdir);
      print_name(d);
      fputs("/</b></a>", stdout);
    } else {
      printf("<a href=\"/%s/\">", newdir);
      print_name(d);
      fputs("/</a>", stdout);
    }

    if (highlight && has_subdirs(newdir)) {
      putchar('\n');
      for (i = 0; i < depth + 2; ++i)
        putchar('\t');
      puts("<ul>");
      menu_panel(page, newdir, depth + 1);
      for (i = 0; i < depth + 2; ++i)
        putchar('\t');
      puts("</ul>");
      for (i = 0; i < depth + 1; ++i)
        putchar('\t');
    }
    puts("</li>");
  }
}

void print_menu_panel(char *page) {
  fputs("<div id=\"nav\">\n\t<ul>\n\t<li>", stdout);
  if (!page)
    puts("<a href=\"/\"><b>Home</b></a></li>");
  else
    puts("<a href=\"/\">Home</a></li>");
  menu_panel(page, NULL, 0);
  puts("\t</ul>");
  puts("</div>");
}

void print_content(char *page) {
  char indexmd[PATH_MAX];
  char indextxt[PATH_MAX];
  char *argv[] = {CONVERTER, indexmd, NULL};

  snprintf(indexmd, sizeof(indexmd), page ? "%2$s/%1$s" : "%s", "index.md",
           page);
  snprintf(indextxt, sizeof(indextxt), page ? "%2$s/%1$s" : "%s", "index.txt",
           page);

  puts("<div id=\"main\">\n");
  if (stat_isfile(indexmd)) {
    fflush(stdout);
    if (spawn_wait(argv) == -1)
      die_perror("spawn: %s/%s", page, indexmd);
  } else if (stat_isfile(indextxt)) {
    puts("<pre>");
    FILE *fptr = fopen(indextxt, "r");
    char c = fgetc(fptr);
    while (c != EOF) {
      printf("%c", c);
      c = fgetc(fptr);
    }

    fclose(fptr);
    puts("</pre>");
  }

  puts("</div>\n");
}

void print_footer(void) { fputs(html_footer, stdout); }

int has_index(char *this) {
  DIR *dp;
  struct dirent *de;
  char newdir[PATH_MAX];
  int index;

  if ((dp = opendir(this ? this : ".")) == NULL)
    die_perror("opendir: %s", this ? this : ".");

  index = 0;
  while (index == 0 && (de = readdir(dp))) {
    if (de->d_name[0] == '.')
      continue;
    snprintf(newdir, sizeof(newdir), this ? "%2$s/%1$s" : "%s", de->d_name,
             this);
    if (stat_isfile(newdir) && strcmp(de->d_name, "index.md") == 0)
      index = 1;
  }
  closedir(dp);

  return index;
}

void usage(char *argv0) { die("usage: %s directory", argv0); }

int main(int argc, char *argv[]) {
  char *dir, *page;

  if (argc != 2)
      usage(argv[0]);
  dir = argv[1];

  if ((page = strchr(dir, '/'))) {
    *page++ = '\0';
    if (strlen(page) == 0)
      page = NULL;
  }
  if (chdir(dir) == -1)
    die_perror("chdir: %s", dir);

  printf(html_header, page ? page : "Home");
  print_nav_bar();
  puts("<div id=\"content\">");
  print_menu_panel(page);
  print_content(page);
  puts("</div>\n");
  print_footer();

  return 0;
}