add new templating functions

This commit is contained in:
Iris Lightshard 2021-04-17 15:02:02 -04:00
parent ff4700df5a
commit b369ba3a62
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398

87
main.c
View file

@ -54,7 +54,7 @@ char *scsw(char *s, char a, char b) { int i = 0; char c; while((c = s[i])) s[i++
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);
@ -96,7 +96,7 @@ int fpportal(FILE* f, Lexicon* l, char* s, int head) {
if (head)
fprintf(
f,
"<h2 id='%s'><a href='%s.html'>%s</a></h2>",
"<h3 id='%s'><a href='%s.html'>%s</a></h3>",
scsw(filename, ' ', '_'),
filename,
s);
@ -105,10 +105,91 @@ int fpportal(FILE* f, Lexicon* l, char* s, int head) {
return 1;
}
int fpimg(FILE* f, char* s) {
char id[1024] = {0};
char src[1024] = {0};
char alt[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;
}
}
fprintf(f, "<a id='%s' href='%s'>\n", id, src);
fprintf(f, "<img class='image' src='%s' alt='%s'/>\n", src, alt);
fputs("</a>", f);
return 1;
}
int fpimgl(FILE* f, char* s) {
char id[1024] = {0};
char href[1024] = {0};
char src[1024] = {0};
char alt[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;
}
}
fprintf(f, "<a id='%s' href='%s'>\n", id, href);
fprintf(f, "<img class='image' src='%s' alt='%s'/>\n", src, 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 fpimg(f, s + 1);
if (s[0] == '?')
return fpimgl(f, s + 1);
if (s[0] == '_')
return fpaudio(f, s + 1);
target = findf(l, s);
if (target < 0)
return error("Missing link", s);
@ -170,7 +251,7 @@ FILE* build(FILE* f, Lexicon* l, char* name, char* srcpath) {
f,
"<link rel='shortcut icon' href='/favicon.ico'>"
"<title>" NAME " &mdash; %s</title>",
name);
ismetanav(name) ? "home" : name);
fputs("</head>", f);
fputs("<body>", f);
/* header */