viewing: generator.c.html     use J/K or arrows to scroll, SEMICOLON to change theme




/*
* TODO:
* fix some syntax highlighting issues
* add support for \ to ignore highlighting next character
*/
#include <stdio.h>
#include <stdbool.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
//modify this to use diferent directory
#define FILEDIR "files/"
#define MAXLEN 256
#define COLOR_BLU "
<highlight style=\"color: #8AADF4\">%c</highlight>"
#define COLOR_PURPLE "
<highlight style=\"color: #c6a0f6\">%c</highlight>"
//orange and green are different because theyre used to highlight strings and chars
#define COLOR_ORANGE "
<highlight style=\"color: #f5a97f\">%c"
#define COLOR_GREEN "
<highlight style=\"color: #a6da95\">%c"
#define COLOR_PINK "
<highlight style=\"color: #f5bde6\">%c</highlight>"
#define COLOR_CYAN "
<highlight style=\"color: #91d7e3\">%c</highlight>"

const char *FILEPREFIX = "
<!-- do not edit this file manually. will be overwritten! -->"
"
<!DOCTYPE html>"
"
<html>"
"
<meta charset=\"utf-8\">"
"
<link rel=stylesheet href=\"style.css\">"
"
<script src=\"main.js\"></script>"
"
<title> mer.png</title>"
"
<link rel=\"icon\" href=\"favicon.ico\">"
"
<header id=titlebar> viewing: %s &emsp;&emsp;&emsp; use J/K or arrows to scroll, SEMICOLON to change theme </header>"
"
<body id=\"main\">\n<br><br><br><br>";
const char *FILESUFFIX = "
<br><br><br>note: this website is best viewed with unix v4 on a pdp-11/45 system<br><br></body>"
"
<footer id=progressbar> ---MORE--- </footer>"
"
</html>";
const char *INDEXTEXT = "
WELCOME TO KONIKNIEUMIEIPV4 :3"
 "
<br><br>the file index below contains all the content and source code of this website.<br>"
"
<br> more coming soon <br><br><br><br><a href=\"images/\">filedump(index of /ass)</a><br><br>";

bool isVisible(char *filename); //this is to ignore files starting with .
void parse(const char *src, const char *dst, const char *name);

int main() {
 FILE *clear = fopen("
index.html", "w");
 fprintf(clear, FILEPREFIX, "
index");
 fprintf(clear, INDEXTEXT);
 fprintf(clear, "
index of %s<br><br>\n", FILEDIR);
 fclose(clear);
 struct dirent *dir;
 DIR *dirptr = opendir(FILEDIR);
 if (!dirptr) {
  printf("
error: no such file or directory\n");
  exit(1);
}
 char filename[MAXLEN];
 char path[MAXLEN];
 char name[MAXLEN];
 while ((dir = readdir(dirptr)) != 0) {
  printf("
parsing %s...\n", dir->d_name);
  if (isVisible(dir->d_name)) {
   strcpy(filename, dir->d_name);
   strcpy(path, FILEDIR);
   strcpy(name, filename); //alternative name without .html and files/
   //convert it to relative path
   strcat(filename, "
.html");
   strcat(path, dir->d_name);
   printf("
\n%s\n", path);
   parse(path, filename, name);
  }
}
 closedir(dirptr);
 clear = fopen("
index.html", "a");
 fprintf(clear, FILESUFFIX);
 fclose(clear);
 return 0;
}

bool isVisible(char *filename) {
 return (filename[0] != '
.');
}

void parse(const char *src, const char *dst, const char *name) {
 FILE *index = fopen("
index.html", "a");
 if (index == 0x0) {
  printf("
error: could not open index.html");
  exit(1);
}
 FILE *destination = fopen(dst, "
w");
 if (destination == 0) {
  printf("
error: could not open %s", dst);
  exit(1);
}
 FILE *source = fopen(src, "
r");
 if (!source) {
  printf("
error: could not open %s", src);
  exit(1);
}
 fprintf(index, "
\n<a href=\"%s\"> %s</a><br>\n", dst, name);
 fprintf(destination, FILEPREFIX, dst);
 bool isCode;
 bool isColored;
 int ch;
 while ((ch = fgetc(source)) != EOF) {
  switch (ch) {
  //case '
':
  // fprintf(destination, "
&nbsp;");
  // break;
  case 10:
   //newline
   fprintf(destination, "
<br>\n");
   break;
  case 9:
   //tab
   fprintf(destination, "
&emsp;");
   break;
  case '
':
   if (isCode)
    isCode = 0;
   else
    isCode = 1;
   break;
  case '/':
  case ':':
  case ';':
  case '*':
  case '+':
  case '-':
  case '&':
  case '^':
  case '|':
  case '%':
  case '#':
   if (isCode)
    //blue
    fprintf(destination, COLOR_BLU, ch);
   else
    fputc(ch, destination);
   break;
  case '[':
  case ']':
  case '{':
  case '}':
  case '(':
  case ')':
   if (isCode)
    fprintf(destination, COLOR_CYAN, ch);
   else
    fputc(ch, destination);
   break;
  case '\'':
   //single quote
   if (isCode) {
    if (!isColored) {
     fprintf(destination, COLOR_ORANGE, '\'');
     isColored = 1;
    } else {
     fprintf(destination, "\'");
     isColored = 0;
    }
   } else
    fputc(ch, destination);
   break;
  case '\"':
   //quotation mark
   if (isCode) {
    if (!isColored) {
     fprintf(destination, COLOR_GREEN, '"');
     isColored = 1;
    } else {
     fprintf(destination, "\"");
     isColored = 0;
    }
   } else
    fputc(ch, destination);
   break;
  case '<':
   if (isCode)
    fprintf(destination, "<");
   else
    fputc(ch, destination);
   break;
  case '>':
   if (isCode)
    fprintf(destination, ">");
   else
    fputc(ch, destination);
   break;
  default:
   fputc(ch, destination);
   break;
  }
 }
 fprintf(destination, FILESUFFIX);
 fclose(destination);
 fclose(source);
fclose(index);
}



note: this website is best viewed with unix v4 on a pdp-11/45 system