Complete figure/img support

Default or specific sizes for each one, with optional different values
for print media query
This commit is contained in:
Matteo Bini 2024-09-01 10:59:50 +02:00
parent 337061a3f4
commit 9b7cfa66ce
1 changed files with 174 additions and 29 deletions

199
srohtml.c
View File

@ -10,6 +10,7 @@
#define DATE_LEN 10 #define DATE_LEN 10
#define FAVICON "data:image/gif;base64,R0lGODlhEAAQAKEAAAAAcP///wAAcAAAcCH5BAEKAAIALAAAAAAQABAAQAIwlBWZxxwAQWjtTQRvlZhTnGSfYT3eZ3WaE4mjGa1UasoH7c6tzYanfqmhJMKXglcAADs=" #define FAVICON "data:image/gif;base64,R0lGODlhEAAQAKEAAAAAcP///wAAcAAAcCH5BAEKAAIALAAAAAAQABAAQAIwlBWZxxwAQWjtTQRvlZhTnGSfYT3eZ3WaE4mjGa1UasoH7c6tzYanfqmhJMKXglcAADs="
#define FIGCAPTION_TAG_LEN 12 #define FIGCAPTION_TAG_LEN 12
#define FIGURE_SIZES_START 4
#define INDEX_START 16 #define INDEX_START 16
#define LANG_LEN 2 #define LANG_LEN 2
#define LINE_MAX 1000 #define LINE_MAX 1000
@ -34,6 +35,12 @@ typedef enum {
tag_attribute = 1 tag_attribute = 1
} html_val_type; } html_val_type;
typedef struct {
char *id;
char *print_width;
char *width;
} img;
typedef struct { typedef struct {
char *desc; char *desc;
char *id; char *id;
@ -60,6 +67,7 @@ static void cmd_a(const char *line);
static void cmd_au_de_ti(char **dst, const char *line); static void cmd_au_de_ti(char **dst, const char *line);
static void cmd_br(void); static void cmd_br(void);
static void cmd_close(void); static void cmd_close(void);
static void cmd_default_figure_sizes(const char *line);
static void cmd_em(const char *line); static void cmd_em(const char *line);
static void cmd_figure(const char *line); static void cmd_figure(const char *line);
static void cmd_h(const char *line); static void cmd_h(const char *line);
@ -97,6 +105,7 @@ char *author = NULL;
array body; array body;
char *description = NULL; char *description = NULL;
int figcaption_presence = 0; int figcaption_presence = 0;
array figure_sizes;
array head; array head;
size_t heading_position = 0; size_t heading_position = 0;
int img_presence = 0; int img_presence = 0;
@ -199,6 +208,8 @@ cmd(char *line)
cmd_au_de_ti(&author, line); cmd_au_de_ti(&author, line);
else if (!strncmp(line, "BR", cmdlen)) else if (!strncmp(line, "BR", cmdlen))
cmd_br(); cmd_br();
else if (!strncmp(line, "DEFAULT_FIGURE_SIZES", cmdlen))
cmd_default_figure_sizes(line);
else if (!strncmp(line, "DESCRIPTION", cmdlen)) else if (!strncmp(line, "DESCRIPTION", cmdlen))
cmd_au_de_ti(&description, line); cmd_au_de_ti(&description, line);
else if (!strncmp(line, "TIME", cmdlen)) else if (!strncmp(line, "TIME", cmdlen))
@ -366,32 +377,35 @@ cmd_close(void)
} }
void void
cmd_time(const char *line) cmd_default_figure_sizes(const char *line)
{ {
char *datetime, *in; char *print_width, *width;
img *sizes;
datetime = NULL; print_width = NULL;
in = NULL; width = NULL;
/* move past "TIME" */ /* move past "DEFAULT_FIGURE_SIZES" */
line += 4; line += 20;
/* move past arguments, once found them */ /* move past arguments, once found them */
line += stread_param(line, &datetime); line += stread_param(line, &width);
line += stread_param(line, &in); line += stread_param(line, &print_width);
if (space) if (strcmp(width, "")) {
array_strcat(&body, " "); sizes = array_at(&figure_sizes, 0);
array_strcat(&body, "<time datetime=\""); if (sizes == NULL)
array_strcat_html(&body, datetime, tag_attribute); sizes = array_push(&figure_sizes);
array_strcat(&body, "\">"); sizes->id = NULL;
if (!strcmp(in, "")) sizes->print_width = NULL;
array_strcat_html(&body, datetime, tag_content); sizes->width = width;
else if (strcmp(print_width, ""))
array_strcat_html(&body, in, tag_content); sizes->print_width = print_width;
array_strcat(&body, "</time>"); }
free(datetime); if (!strcmp(print_width, ""))
free(in); free(print_width);
if (!strcmp(width, ""))
free(width);
} }
void void
@ -413,15 +427,22 @@ cmd_em(const char *line)
void void
cmd_figure(const char *line) cmd_figure(const char *line)
{ {
char *ind, *src; char *id, *ind, *print_width, *src, *width;
img *sizes;
id = NULL;
ind = NULL; ind = NULL;
print_width = NULL;
src = NULL; src = NULL;
width = NULL;
/* move past "FIGURE" */ /* move past "FIGURE" */
line += 6; line += 6;
/* move past argument, once found it */ /* move past arguments, once found them */
line += stread_param(line, &src); line += stread_param(line, &src);
line += stread_param(line, &id);
line += stread_param(line, &width);
line += stread_param(line, &print_width);
indent_str(&ind, indentation); indent_str(&ind, indentation);
@ -429,8 +450,14 @@ cmd_figure(const char *line)
array_strcat(&body, "<figure>\n"); array_strcat(&body, "<figure>\n");
indent_str(&ind, ++indentation); indent_str(&ind, ++indentation);
array_strcat(&body, ind); array_strcat(&body, ind);
array_strcat(&body, "<img src=\""); array_strcat(&body, "<img");
array_strcat(&body, src); if (strcmp(id, "")) {
array_strcat(&body, " id=\"");
array_strcat_html(&body, id, tag_attribute);
array_strcat(&body, "\"");
}
array_strcat(&body, " src=\"");
array_strcat_html(&body, src, tag_attribute);
array_strcat(&body, "\" alt=\"\">\n"); array_strcat(&body, "\" alt=\"\">\n");
img_presence = 1; img_presence = 1;
@ -439,8 +466,23 @@ cmd_figure(const char *line)
strcpy(tag_closure, "</figure>"); strcpy(tag_closure, "</figure>");
if (strcmp(id, "") && strcmp(width, "")) {
sizes = array_push(&figure_sizes);
sizes->id = id;
sizes->print_width = NULL;
sizes->width = width;
if (strcmp(print_width, ""))
sizes->print_width = print_width;
}
if (!strcmp(width, ""))
free(id);
free(ind); free(ind);
if (!strcmp(print_width, ""))
free(print_width);
free(src); free(src);
if (!strcmp(width, ""))
free(width);
} }
void void
@ -575,6 +617,21 @@ cmd_license_start(void)
free(ind); free(ind);
} }
void
cmd_nav_end(const char *line)
{
char *ind;
ind = NULL;
indentation--;
indent_str(&ind, indentation);
array_strcat(&body, ind);
array_strcat(&body, "</nav>\n");
free(ind);
}
void void
cmd_note(const char *line) cmd_note(const char *line)
{ {
@ -698,6 +755,35 @@ cmd_strong(const char *line)
array_strcat(&body, "</strong>"); array_strcat(&body, "</strong>");
} }
void
cmd_time(const char *line)
{
char *datetime, *in;
datetime = NULL;
in = NULL;
/* move past "TIME" */
line += 4;
/* move past arguments, once found them */
line += stread_param(line, &datetime);
line += stread_param(line, &in);
if (space)
array_strcat(&body, " ");
array_strcat(&body, "<time datetime=\"");
array_strcat_html(&body, datetime, tag_attribute);
array_strcat(&body, "\">");
if (!strcmp(in, ""))
array_strcat_html(&body, datetime, tag_content);
else
array_strcat_html(&body, in, tag_content);
array_strcat(&body, "</time>");
free(datetime);
free(in);
}
void void
cmd_url(const char *line) cmd_url(const char *line)
{ {
@ -946,6 +1032,7 @@ main (int argc, char *argv[])
array_init(&head, sizeof(char), BODY_START_SIZE / 4); array_init(&head, sizeof(char), BODY_START_SIZE / 4);
array_init(&body, sizeof(char), BODY_START_SIZE); array_init(&body, sizeof(char), BODY_START_SIZE);
array_init(&figure_sizes, sizeof(img), FIGURE_SIZES_START);
array_init(&index, sizeof(index_title), INDEX_START); array_init(&index, sizeof(index_title), INDEX_START);
array_init(&sitography, sizeof(note), NOTES_START); array_init(&sitography, sizeof(note), NOTES_START);
@ -1020,6 +1107,7 @@ main (int argc, char *argv[])
free(head.data); free(head.data);
free(body.data); free(body.data);
free(figure_sizes.data);
free(index.data); free(index.data);
free(sitography.data); free(sitography.data);
@ -1123,6 +1211,9 @@ nav_index(FILE *f)
void void
page_head(void) page_head(void)
{ {
size_t i;
img *sizes;
array_strcat(&head, "<!doctype html>\n"); array_strcat(&head, "<!doctype html>\n");
array_strcat(&head, "<html"); array_strcat(&head, "<html");
@ -1192,10 +1283,38 @@ page_head(void)
" }\n" \ " }\n" \
"\n" \ "\n" \
" img {\n" \ " img {\n" \
" max-width: 100%;\n" \ " max-width: 100%;\n"
" width: 7em;\n" \
" }\n"
); );
if (figure_sizes.length > 0) {
array_strcat(&head, \
" width: " \
);
sizes = array_at(&figure_sizes, 0);
array_strcat(&head, sizes->width);
array_strcat(&head, ";\n");
} else {
array_strcat(&head, \
" width: 7em;\n" \
);
}
array_strcat(&head, \
" }\n" \
);
for (i = 1; i < figure_sizes.length; i++) {
sizes = array_at(&figure_sizes, i);
array_strcat(&head, "\n" \
" img#" \
);
array_strcat(&head, sizes->id);
array_strcat(&head, " {\n" \
" width: " \
);
array_strcat(&head, sizes->width);
array_strcat(&head, ";\n" \
" }\n" \
);
}
} }
if (figcaption_presence) { if (figcaption_presence) {
@ -1267,7 +1386,33 @@ page_head(void)
" a.nav {\n" \ " a.nav {\n" \
" color: inherit;\n" \ " color: inherit;\n" \
" text-decoration: none;\n" \ " text-decoration: none;\n" \
" }\n"
);
for (i = 0; i < figure_sizes.length; i++) {
sizes = array_at(&figure_sizes, i);
if (sizes->print_width) {
array_strcat(&head, "\n" \
" img" \
);
if (i != 0) {
array_strcat(&head, "#");
array_strcat(&head, sizes->id);
}
array_strcat(&head, " {\n" \
" width: " \
);
array_strcat(&head, sizes->print_width);
array_strcat(&head, ";\n" \
" }\n" \ " }\n" \
);
}
free(sizes->id);
free(sizes->print_width);
free(sizes->width);
}
array_strcat(&head, \
" }\n" " }\n"
); );