stacinhtml

Static C in HTML - simple static site generator
git clone git://git.kocotian.pl/stacinhtml.git
Log | Files | Refs | README

commit cd4d169e0a0dfcea46d2c9cb42fed6fa8f9fe161
parent 1b8dbdadae276308dabd8de26b15556461864795
Author: kocotian <kocotian@kocotian.pl>
Date:   Sun, 21 Mar 2021 17:26:39 +0100

Assemble files, makefile (finally everything works)

Diffstat:
MMakefile | 34+++++++++++++++++++---------------
Aassemble.c | 18++++++++++++++++++
Aassemble.h | 31+++++++++++++++++++++++++++++++
Mgenerate.c | 6++++++
4 files changed, 74 insertions(+), 15 deletions(-)

diff --git a/Makefile b/Makefile @@ -15,7 +15,7 @@ OBJLIB = ${SRCLIB:.c=.o} SRC = generate.c EXE = ${SRC:.c=} -generator: buildoptions ${EXE} +generator: buildoptions ${EXE} assemble.o buildoptions: @echo build options: @@ -36,29 +36,27 @@ ${OBJLIB}: ${SRCLIB} %: %.c ${OBJLIB} ${CC} ${CCFLAGS} ${LDFLAGS} -o $@ $^ -clean: - rm -f ${OBJLIB} ${EXE} - -.PHONY: clean - # == Section 2: Generating pages == -INDIR = in +INDIR = in/ INDIRS = $(sort $(shell find $(INDIR)/ -type d)) IN = $(sort $(shell find $(INDIR)/ -type f -name '*.stac')) -METADIR = meta +METADIR = meta/ METADIRS = $(subst $(INDIR),$(METADIR),$(INDIRS)) META = $(subst $(INDIR),$(METADIR),$(IN:.stac=.c)) BIN = $(META:.c=.bin) -OUTDIR = out +OUTDIR = out/ OUTDIRS = $(subst $(METADIR),$(OUTDIR),$(METADIRS)) -OUT = $(subst $(METADIR),$(OUTDIR),$(META:.bin=.html)) +OUT = $(subst $(METADIR),$(OUTDIR),$(BIN:.bin=.html)) GENERATOR = ./generate -pages: generator ${OUT} ${GENERATOR} +pages: genpages + rm -rf ${METADIR} + +genpages: ${OUT} ${OUT}: ${META} ${META}: mkpagedirs @@ -66,13 +64,19 @@ ${META}: mkpagedirs mkpagedirs: ${IN} mkdir -p $(OUTDIRS) $(METADIRS) +${IN}: generator ${GENERATOR} + ${META}: ${IN} ${GENERATOR} -o $@ $(subst $(METADIR),$(INDIR),$(@:.c=.stac)) -%.bin: %.c - ${CC} -o $@ $(@:.bin=.c) +${BIN}: ${META} + ${CC} -o $@ -I. $(@:.bin=.c) assemble.o ${OUT}: ${BIN} - ${GENERATOR} -o $@ $(subst $(OUTDIR),$(BINDIR),$(@:.html=.bin)) + ./$(subst $(OUTDIR),$(METADIR),$(@:.html=.bin)) > $@ + +clean: + rm -f ${OBJLIB} ${EXE} *.o + rm -rf ${METADIR} ${OUTDIR} -.PHONY: mkpagedirs +.PHONY: mkpagedirs clean diff --git a/assemble.c b/assemble.c @@ -0,0 +1,18 @@ +#include "assemble.h" + +/* Globals */ +Variable vs[VS_MAX]; /* Variable stack */ +size_t vss; /* Variable stack size */ + +/* Functions */ +String +getVariableValue(char *v) +{ + size_t n; + String s = { .data = v, .len = strlen(v) }; + n = vss; + while (--n >= 0) { + if (!Strcmp(vs[n].name, s)) + return vs[n].value; + } +} diff --git a/assemble.h b/assemble.h @@ -0,0 +1,31 @@ +#ifndef _ASSEMBLE_H +#define _ASSEMBLE_H + +#include <unistd.h> +#include <string.h> + +#include "str.h" + +/* Macros */ +#define $(VARNAME) getVariableValue(#VARNAME) +#define DECLVAR(VARNAME, VALUE) \ + (vs[vss].name.len = strlen(vs[vss].name.data = #VARNAME), \ + vs[vss].value.len = strlen(vs[vss].value.data = VALUE), \ + ++vss) +#define VS_MAX 256 +#define fd 1 /* stdout */ + +/* Types */ +typedef struct { + String name; + String value; +} Variable; + +/* Prototypes */ +String getVariableValue(char *varname); + +/* Externs */ +extern Variable vs[VS_MAX]; /* Variable stack */ +extern size_t vss; /* Variable stack size */ + +#endif diff --git a/generate.c b/generate.c @@ -193,6 +193,9 @@ main(int argc, char *argv[]) if ((outputfd = open(outputfn, O_WRONLY | O_CREAT, 0644)) < 0) die("open (output):"); + /* Writing beginning of main() */ + write(outputfd, "#include <assemble.h>\nint main(void) {", 38); + /* Declaring variables */ for (viter = 0; viter < vss; ++viter) dprintf(outputfd, "DECLVAR(%.*s, \"%.*s\"); ", @@ -202,6 +205,9 @@ main(int argc, char *argv[]) /* And finally, generating C code to output */ generateC(outputfd, input); + /* After this, closing main() */ + write(outputfd, "\n}", 2); + /* Closing an output */ close(outputfd);