Cleanup for 0.2

This commit is contained in:
Matteo Bini 2024-09-07 19:10:26 +02:00
parent f24e9c7f62
commit d83e30da19
2 changed files with 108 additions and 76 deletions

View File

@ -1,5 +1,5 @@
BIN = srohtml
VERSION = 0.1
VERSION = 0.2
PREFIX = /usr/local

182
srohtml.c
View File

@ -89,15 +89,20 @@ static void footer(void);
static void footnotes(void);
static int fputs_html(const char *str, FILE *f, const html_val_type t);
static size_t fread_line(FILE *file, char **str, size_t *len);
static void free_arrays(void);
static void indent_str(char **str, const int ind);
static void init_arrays(void);
static void nav_index(FILE *f);
static void outputs(FILE *out);
static void page_head(void);
static void pop_closure(array *a);
static void print_help(void);
static void print_usage(void);
static void print_version(void);
static void read_and_process(FILE *in);
static void *safe_malloc(const size_t size);
static void *safe_realloc(void *ptr, const size_t size);
static void srohtml(FILE *in, FILE *out);
static size_t stread_param(const char *src, char **str);
/* variables */
@ -867,6 +872,7 @@ footer(void)
max = 16;
array_strcat(&body, "\t\t<footer>\n");
setlocale(LC_ALL, "");
if (publication_date != NULL) {
array_strcat(&body, "\t\t\t<p>Pubblicazione: <time datetime=\"");
@ -1014,6 +1020,16 @@ fread_line(FILE *file, char **str, size_t *len)
return c == EOF ? EOF : i;
}
void
free_arrays(void)
{
free(head.data);
free(body.data);
free(figure_sizes.data);
free(index.data);
free(sitography.data);
}
void
indent_str(char **str, const int ind)
{
@ -1029,6 +1045,16 @@ indent_str(char **str, const int ind)
(*str)[ind] = '\0';
}
void
init_arrays(void)
{
array_init(&head, sizeof(char), BODY_START_SIZE / 4);
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(&sitography, sizeof(note), NOTES_START);
}
void
array_init(array *a, const size_t size, size_t num)
{
@ -1044,8 +1070,9 @@ int
main (int argc, char *argv[])
{
FILE *in, *out;
size_t len;
char *line;
in = NULL;
out = NULL;
if (argc == 2) {
if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
@ -1059,8 +1086,6 @@ main (int argc, char *argv[])
return 1;
}
setlocale(LC_ALL, "");
if (argc >= 2)
in = fopen(argv[1], "r");
else
@ -1070,53 +1095,6 @@ main (int argc, char *argv[])
return 1;
}
array_init(&head, sizeof(char), BODY_START_SIZE / 4);
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(&sitography, sizeof(note), NOTES_START);
array_strcat(&body, "\t<body>\n");
len = 0;
line = NULL;
while (fread_line(in, &line, &len) != EOF) {
if (!len)
cmd_close();
else if (len >= 2 && line[0] == '.')
cmd(line);
else if (len >= 2 && line[0] == '\\' && line[1] == '"')
continue;
else {
if (space &&
line[0] != '!' &&
line[0] != '\'' &&
line[0] != ',' &&
line[0] != '.' &&
line[0] != '.' &&
line[0] != ';' &&
line[0] != '?')
array_strcat(&body, " ");
array_strcat_html(&body, line, tag_content);
space = 1;
}
free(line);
len = 0;
line = NULL;
}
free(line);
if (in != stdin)
fclose(in);
if (strcmp(tag_closure, ""))
cmd_close();
page_head();
footnotes();
footer();
array_strcat(&body, "\t</body>\n</html>\n");
if (argc == 3)
out = fopen(argv[2], "w");
else
@ -1126,30 +1104,7 @@ main (int argc, char *argv[])
return 1;
}
fputs(head.data, out);
{
char *bodystr;
char c;
bodystr = body.data;
c = bodystr[index_position];
bodystr[index_position] = '\0';
fputs(bodystr, out);
if (index_position)
nav_index(out);
bodystr[index_position] = c;
fputs(bodystr + index_position, out);
}
fclose(out);
free(head.data);
free(body.data);
free(figure_sizes.data);
free(index.data);
free(sitography.data);
srohtml(in, out);
return 0;
}
@ -1248,6 +1203,25 @@ nav_index(FILE *f)
free(nav_cls.data);
}
void
outputs(FILE *out)
{
char *bodystr;
char c;
bodystr = body.data;
c = bodystr[index_position];
fputs(head.data, out);
bodystr[index_position] = '\0';
fputs(bodystr, out);
if (index_position)
nav_index(out);
bodystr[index_position] = c;
fputs(bodystr + index_position, out);
}
void
page_head(void)
{
@ -1506,6 +1480,42 @@ print_version(void)
exit(1);
}
void
read_and_process(FILE *in)
{
size_t len;
char *line;
len = 0;
line = NULL;
while (fread_line(in, &line, &len) != EOF) {
if (!len)
cmd_close();
else if (len >= 2 && line[0] == '.')
cmd(line);
else if (len >= 2 && line[0] == '\\' && line[1] == '"')
continue;
else {
if (space &&
line[0] != '!' &&
line[0] != '\'' &&
line[0] != ',' &&
line[0] != '.' &&
line[0] != '.' &&
line[0] != ';' &&
line[0] != '?')
array_strcat(&body, " ");
array_strcat_html(&body, line, tag_content);
space = 1;
}
free(line);
len = 0;
line = NULL;
}
free(line);
}
void
pop_closure(array *a)
{
@ -1551,6 +1561,28 @@ safe_realloc(void *ptr, const size_t size)
return new;
}
void
srohtml(FILE *in, FILE *out)
{
init_arrays();
array_strcat(&body, "\t<body>\n");
read_and_process(in);
fclose(in);
if (strcmp(tag_closure, ""))
cmd_close();
page_head();
footnotes();
footer();
array_strcat(&body, "\t</body>\n</html>\n");
outputs(out);
fclose(out);
free_arrays();
}
size_t
stread_param(const char *src, char **str)
{