Minified html

This commit is contained in:
Nate Choe
2022-06-06 16:02:40 -05:00
parent d21ae6b2ca
commit 46a95c1330
3 changed files with 44 additions and 3 deletions

View File

@@ -6,5 +6,6 @@ RUN make
FROM debian:stable-slim AS run FROM debian:stable-slim AS run
COPY --from=build /ncdg/build/ncdg /usr/bin/ncdg COPY --from=build /ncdg/build/ncdg /usr/bin/ncdg
RUN apt-get update && apt-get upgrade -y
ENTRYPOINT [ "ncdg", "/dev/stdin" ] ENTRYPOINT [ "ncdg", "/dev/stdin" ]

View File

@@ -3,6 +3,8 @@
A very simple tool to generate natechoe.dev pages. It's effectively just a A very simple tool to generate natechoe.dev pages. It's effectively just a
generic file preprocessor with HTML some auto escaping. generic file preprocessor with HTML some auto escaping.
Please not that ncdg minifies HTML so these examples aren't really correct.
## Usage ## Usage
There are only 3 features in ncdg: There are only 3 features in ncdg:
@@ -104,3 +106,5 @@ Result:
<pre><code> <pre><code>
#include &lt;stdio.h%gt; #include &lt;stdio.h%gt;
</code></pre> </code></pre>
Note that text inside of escaped sections are not minified.

View File

@@ -1,3 +1,4 @@
#include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -18,9 +19,19 @@ struct expandfile {
/* This is a vector of struct var */ /* This is a vector of struct var */
}; };
struct minstate {
int ignore;
/* If this is set, then ignore the current whitespace group */
int isspace;
/* If this is set, then we've seen whitespace on the previous char. */
};
static int expandfile(struct expandfile *ret, char *filename, int level); static int expandfile(struct expandfile *ret, char *filename, int level);
static int writefile(struct expandfile *file, FILE *out); static int writefile(struct expandfile *file, FILE *out);
static struct string *getstring(FILE *file, char end); static struct string *getstring(FILE *file, char end);
static void initminstate(struct minstate *state);
static void mputs(struct minstate *state, char *s, FILE *file);
static void mputc(struct minstate *state, char c, FILE *file);
int parsefile(char *template, FILE *out) { int parsefile(char *template, FILE *out) {
struct expandfile expanded; struct expandfile expanded;
@@ -59,13 +70,15 @@ error1:
static int writefile(struct expandfile *file, FILE *out) { static int writefile(struct expandfile *file, FILE *out) {
long i; long i;
struct minstate s;
initminstate(&s);
for (i = 0; i < file->data->len; ++i) { for (i = 0; i < file->data->len; ++i) {
if (file->data->data[i] == ESCAPE_CHAR) { if (file->data->data[i] == ESCAPE_CHAR) {
const struct string *data = file->data; const struct string *data = file->data;
const struct vector *vars = file->vars; const struct vector *vars = file->vars;
switch (data->data[++i]) { switch (data->data[++i]) {
case ESCAPE_CHAR: case ESCAPE_CHAR:
fputc(ESCAPE_CHAR, out); mputc(&s, ESCAPE_CHAR, out);
break; break;
case VAR_CHAR: { case VAR_CHAR: {
long start; long start;
@@ -84,7 +97,7 @@ static int writefile(struct expandfile *file, FILE *out) {
struct var, j); struct var, j);
if (strcmp(var.var->data, if (strcmp(var.var->data,
varname) == 0) { varname) == 0) {
fputs(var.value->data, mputs(&s, var.value->data,
out); out);
} }
} }
@@ -115,7 +128,7 @@ static int writefile(struct expandfile *file, FILE *out) {
} }
} }
else else
fputc(file->data->data[i], out); mputc(&s, file->data->data[i], out);
} }
return 0; return 0;
} }
@@ -130,6 +143,7 @@ static int expandfile(struct expandfile *ret, char *filename, int level) {
} }
linenum = 1; linenum = 1;
if (level >= MAX_INCLUDE_DEPTH) { if (level >= MAX_INCLUDE_DEPTH) {
fprintf(stderr, "The include depth has reached %d, quitting\n", fprintf(stderr, "The include depth has reached %d, quitting\n",
level); level);
fclose(file); fclose(file);
@@ -225,3 +239,25 @@ error:
freestring(ret); freestring(ret);
return NULL; return NULL;
} }
static void initminstate(struct minstate *state) {
memset(state, 0, sizeof *state);
}
static void mputs(struct minstate *state, char *s, FILE *file) {
int i;
for (i = 0; s[i] != '\0'; ++i)
mputc(state, s[i], file);
}
static void mputc(struct minstate *state, char c, FILE *file) {
if (isspace(c))
state->isspace = 1;
else {
if (!state->ignore && state->isspace && c != '<' && c != '>')
fputc(' ', file);
fputc(c, file);
state->ignore = c == '<' || c == '>';
state->isspace = 0;
}
}