feat(cgi): accept CDE .dp palettes
Parse eight-slot CDE palette files in addition to key=value themes. Map the active frame, inactive frame, text-area, application-background and front-panel slots, derive readable text and code colours, and add a sample palette plus tests. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
This commit is contained in:
1 parent
623b14f058
commit
1a62123fad
6 files changed
+179
-24
No files matched your search
@@ -60,6 +60,7 @@ storage.
|
||||
variables, or rounded corners.
|
||||
- The look is CDE/Motif: grey base, bevelled in/out borders, navy title bars, a
|
||||
Front Panel sidebar. Colours come from the palette, not hard-coded literals.
|
||||
`GOTEA_PALETTE` accepts key=value themes and eight-slot CDE `.dp` files.
|
||||
- C99 + POSIX only (`-D_POSIX_C_SOURCE=200809L`), no third-party libraries.
|
||||
Build with `-Wall -Wextra -Wpedantic -Werror`.
|
||||
- Keep `make -C cgi check` green. Tests may use `git` only to create fixtures.
|
||||
|
||||
@@ -77,8 +77,9 @@ auth layer lands.
|
||||
| `GOTEA_PAGES_ALLOW` | unset | File listing `owner/repo` sites that may be served |
|
||||
|
||||
Palette keys: `base`, `light`, `white`, `dark`, `shadow`, `text`, `link`,
|
||||
`visited`, `title_bg`, `title_fg`. Values must be `#rgb` or `#rrggbb`.
|
||||
See `cgi/palettes/cde-default.palette`.
|
||||
`visited`, `title_bg`, `title_fg`, `code_bg`, `code_fg`. Values must be `#rgb`
|
||||
or `#rrggbb`. See `cgi/palettes/cde-default.palette`. Real CDE `.dp` palettes
|
||||
(eight colour slots) are also accepted; see `cgi/palettes/cde-default.dp`.
|
||||
|
||||
## Pages
|
||||
|
||||
|
||||
+154
-22
@@ -54,6 +54,8 @@ struct palette {
|
||||
char visited[8];
|
||||
char title_bg[8];
|
||||
char title_fg[8];
|
||||
char code_bg[8];
|
||||
char code_fg[8];
|
||||
};
|
||||
|
||||
struct repo {
|
||||
@@ -276,6 +278,8 @@ palette_defaults(struct palette *p)
|
||||
strcpy(p->visited, "#551a8b");
|
||||
strcpy(p->title_bg, "#000080");
|
||||
strcpy(p->title_fg, "#ffffff");
|
||||
strcpy(p->code_bg, "#ffffff");
|
||||
strcpy(p->code_fg, "#000000");
|
||||
}
|
||||
|
||||
static int
|
||||
@@ -314,6 +318,8 @@ palette_set(struct palette *p, const char *key, const char *val)
|
||||
{ "visited", p->visited, sizeof(p->visited) },
|
||||
{ "title_bg", p->title_bg, sizeof(p->title_bg) },
|
||||
{ "title_fg", p->title_fg, sizeof(p->title_fg) },
|
||||
{ "code_bg", p->code_bg, sizeof(p->code_bg) },
|
||||
{ "code_fg", p->code_fg, sizeof(p->code_fg) },
|
||||
};
|
||||
size_t i;
|
||||
|
||||
@@ -327,11 +333,103 @@ palette_set(struct palette *p, const char *key, const char *val)
|
||||
}
|
||||
}
|
||||
|
||||
static int hexval(int c);
|
||||
|
||||
static int
|
||||
dp_component(const char *s)
|
||||
{
|
||||
int i, v = 0;
|
||||
|
||||
for (i = 0; i < 2; i++) {
|
||||
int h = hexval((unsigned char)s[i]);
|
||||
if (h < 0)
|
||||
return -1;
|
||||
v = v * 16 + h;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static int
|
||||
dp_color(const char *s, char out[8])
|
||||
{
|
||||
int r, g, b;
|
||||
|
||||
if (!strcasecmp(s, "black")) {
|
||||
strcpy(out, "#000000");
|
||||
return 1;
|
||||
}
|
||||
if (!strcasecmp(s, "white")) {
|
||||
strcpy(out, "#ffffff");
|
||||
return 1;
|
||||
}
|
||||
if (strlen(s) != 13 || s[0] != '#')
|
||||
return 0;
|
||||
r = dp_component(s + 1);
|
||||
g = dp_component(s + 5);
|
||||
b = dp_component(s + 9);
|
||||
if (r < 0 || g < 0 || b < 0)
|
||||
return 0;
|
||||
{
|
||||
static const char hx[] = "0123456789abcdef";
|
||||
out[0] = '#';
|
||||
out[1] = hx[(r >> 4) & 15];
|
||||
out[2] = hx[r & 15];
|
||||
out[3] = hx[(g >> 4) & 15];
|
||||
out[4] = hx[g & 15];
|
||||
out[5] = hx[(b >> 4) & 15];
|
||||
out[6] = hx[b & 15];
|
||||
out[7] = '\0';
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
dp_luma(const char *color)
|
||||
{
|
||||
int r, g, b;
|
||||
|
||||
if (strlen(color) != 7 || color[0] != '#')
|
||||
return 128;
|
||||
r = dp_component(color + 1);
|
||||
g = dp_component(color + 3);
|
||||
b = dp_component(color + 5);
|
||||
if (r < 0 || g < 0 || b < 0)
|
||||
return 128;
|
||||
return (299 * r + 587 * g + 114 * b) / 1000;
|
||||
}
|
||||
|
||||
/*
|
||||
* A CDE .dp file is eight colour slots: 1 active frame, 2 inactive frame,
|
||||
* 4 text areas, 5 application background, 8 front panel. The other slots
|
||||
* are not mapped.
|
||||
*/
|
||||
static void
|
||||
palette_from_dp(struct palette *p, char colors[8][8])
|
||||
{
|
||||
strcpy(p->base, colors[4]);
|
||||
strcpy(p->light, colors[3]);
|
||||
if (dp_luma(p->light) < 100)
|
||||
strcpy(p->light, "#d0d0d0");
|
||||
strcpy(p->white, "#ffffff");
|
||||
strcpy(p->dark, colors[1]);
|
||||
strcpy(p->shadow, colors[1]);
|
||||
strcpy(p->text, dp_luma(p->base) > 140 ? "#000000" : "#ffffff");
|
||||
strcpy(p->link, colors[0]);
|
||||
strcpy(p->visited, colors[7]);
|
||||
strcpy(p->title_bg, colors[0]);
|
||||
strcpy(p->title_fg,
|
||||
dp_luma(p->title_bg) > 140 ? "#000000" : "#ffffff");
|
||||
strcpy(p->code_bg, colors[3]);
|
||||
strcpy(p->code_fg,
|
||||
dp_luma(p->code_bg) > 140 ? "#000000" : "#ffffff");
|
||||
}
|
||||
|
||||
static void
|
||||
palette_load(struct palette *p, const char *file)
|
||||
{
|
||||
FILE *f;
|
||||
char line[256];
|
||||
int keyval = 0;
|
||||
|
||||
if (file == NULL || *file == '\0')
|
||||
return;
|
||||
@@ -339,28 +437,62 @@ palette_load(struct palette *p, const char *file)
|
||||
if (f == NULL)
|
||||
return;
|
||||
while (fgets(line, sizeof(line), f) != NULL) {
|
||||
char *eq, *key, *val, *end;
|
||||
if (strchr(line, '=') != NULL) {
|
||||
keyval = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
rewind(f);
|
||||
if (keyval) {
|
||||
while (fgets(line, sizeof(line), f) != NULL) {
|
||||
char *eq, *key, *val, *end;
|
||||
|
||||
key = line;
|
||||
while (*key == ' ' || *key == '\t')
|
||||
key++;
|
||||
if (*key == '#' || *key == '\n' || *key == '\0')
|
||||
continue;
|
||||
eq = strchr(key, '=');
|
||||
if (eq == NULL)
|
||||
continue;
|
||||
*eq = '\0';
|
||||
val = eq + 1;
|
||||
end = key + strlen(key);
|
||||
while (end > key && (end[-1] == ' ' || end[-1] == '\t'))
|
||||
*--end = '\0';
|
||||
while (*val == ' ' || *val == '\t')
|
||||
val++;
|
||||
end = val + strlen(val);
|
||||
while (end > val && (end[-1] == '\n' || end[-1] == '\r' ||
|
||||
end[-1] == ' ' || end[-1] == '\t'))
|
||||
*--end = '\0';
|
||||
palette_set(p, key, val);
|
||||
key = line;
|
||||
while (*key == ' ' || *key == '\t')
|
||||
key++;
|
||||
if (*key == '#' || *key == '\n' || *key == '\0')
|
||||
continue;
|
||||
eq = strchr(key, '=');
|
||||
if (eq == NULL)
|
||||
continue;
|
||||
*eq = '\0';
|
||||
val = eq + 1;
|
||||
end = key + strlen(key);
|
||||
while (end > key && (end[-1] == ' ' || end[-1] == '\t'))
|
||||
*--end = '\0';
|
||||
while (*val == ' ' || *val == '\t')
|
||||
val++;
|
||||
end = val + strlen(val);
|
||||
while (end > val && (end[-1] == '\n' ||
|
||||
end[-1] == '\r' || end[-1] == ' ' ||
|
||||
end[-1] == '\t'))
|
||||
*--end = '\0';
|
||||
palette_set(p, key, val);
|
||||
}
|
||||
} else {
|
||||
char colors[8][8];
|
||||
int n = 0, ok = 1;
|
||||
|
||||
while (fgets(line, sizeof(line), f) != NULL) {
|
||||
char *s = line, *end;
|
||||
|
||||
while (*s == ' ' || *s == '\t')
|
||||
s++;
|
||||
if (*s == '\n' || *s == '\r' || *s == '\0')
|
||||
continue;
|
||||
end = s + strlen(s);
|
||||
while (end > s && (end[-1] == '\n' ||
|
||||
end[-1] == '\r' || end[-1] == ' ' ||
|
||||
end[-1] == '\t'))
|
||||
*--end = '\0';
|
||||
if (n >= 8 || !dp_color(s, colors[n])) {
|
||||
ok = 0;
|
||||
break;
|
||||
}
|
||||
n++;
|
||||
}
|
||||
if (ok && n == 8)
|
||||
palette_from_dp(p, colors);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
@@ -780,7 +912,7 @@ css(struct buf *b)
|
||||
"border-top:1px solid %s;border-left:1px solid %s;"
|
||||
"border-right:1px solid %s;border-bottom:1px solid %s;"
|
||||
"padding:6px;font-family:Courier,monospace;font-size:12px}\n",
|
||||
p->white, p->text, p->shadow, p->shadow, p->light, p->light);
|
||||
p->code_bg, p->code_fg, p->shadow, p->shadow, p->light, p->light);
|
||||
b_printf(b, "th{background-color:%s;color:%s;text-align:left}\n",
|
||||
p->dark, p->text);
|
||||
b_printf(b, "tr.alt td{background-color:%s}\n", p->light);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#ededaaa87070
|
||||
#999999999999
|
||||
#c6c6b2b2a8a8
|
||||
#68686f6f8282
|
||||
#c6c6b2b2a8a8
|
||||
#c6c6b2b2a8a8
|
||||
#999999999999
|
||||
#9393ababbfbf
|
||||
@@ -10,3 +10,5 @@ link = #000080
|
||||
visited = #551a8b
|
||||
title_bg = #000080
|
||||
title_fg = #ffffff
|
||||
code_bg = #ffffff
|
||||
code_fg = #000000
|
||||
@@ -131,6 +131,17 @@ expect_contains "$pal" '#123456' "palette: custom base colour"
|
||||
expect_contains "$pal" 'bgcolor="#123456"' "palette: body fallback"
|
||||
echo "PASS palette"
|
||||
|
||||
pal_sample=$(run_palette / "$cgi/palettes/cde-default.palette")
|
||||
expect_contains "$pal_sample" 'bgcolor="#a0a0a0"' \
|
||||
"palette: key=value sample with comments"
|
||||
echo "PASS palette sample"
|
||||
|
||||
pal_dp=$(run_palette / "$cgi/palettes/cde-default.dp")
|
||||
expect_contains "$pal_dp" 'bgcolor="#c6b2a8"' "palette: .dp background"
|
||||
expect_contains "$pal_dp" 'pre{background-color:#686f82;color:#ffffff' \
|
||||
"palette: .dp text area contrast"
|
||||
echo "PASS palette dp"
|
||||
|
||||
flat=$(run /repo/flat/tree)
|
||||
expect_contains "$flat" 'README.md' "flat: tree"
|
||||
echo "PASS flat"
|
||||
|
||||
Reference in new issue
Block a user