Compare commits
No commits in common. "master" and "0.2" have entirely different histories.
2
README
2
README
|
@ -4,7 +4,7 @@ the suckless philosophy [2].
|
|||
|
||||
The idea of updating a website with a roff-like syntax comes from my
|
||||
preference for the groff typesetting system, obviously, and because my
|
||||
texts are written without discarding the possibility of a hard copy.
|
||||
texts are written without discarding the possibility of an hard copy.
|
||||
With the right macro package you could ask groff to make a PDF out of my
|
||||
web page sources.
|
||||
|
||||
|
|
221
srohtml.c
221
srohtml.c
|
@ -1,3 +1,4 @@
|
|||
#include <locale.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -6,7 +7,6 @@
|
|||
/* macros */
|
||||
#define BODY_START_SIZE 26624 /* 26 x 1024 byte = 26 KiB */
|
||||
#define CLS_MAX 16
|
||||
#define DATE_FORMAT "%d/%m/%Y"
|
||||
#define DATE_LEN 10
|
||||
#define FAVICON "data:image/gif;base64,R0lGODlhEAAQAKEAAAAAcP///wAAcAAAcCH5BAEKAAIALAAAAAAQABAAQAIwlBWZxxwAQWjtTQRvlZhTnGSfYT3eZ3WaE4mjGa1UasoH7c6tzYanfqmhJMKXglcAADs="
|
||||
#define FIGCAPTION_TAG_LEN 12
|
||||
|
@ -17,7 +17,6 @@
|
|||
#define LINE_START 80
|
||||
#define NOTES_START 8
|
||||
#define NOTES_MAX_DIGITS 3
|
||||
#define TIME_FORMAT "%H:%M"
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
|
@ -90,7 +89,9 @@ 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);
|
||||
|
@ -176,17 +177,6 @@ array_at(array *a, const size_t i)
|
|||
return (char *) a->data + i * a->unit_size;
|
||||
}
|
||||
|
||||
void
|
||||
array_init(array *a, const size_t size, size_t num)
|
||||
{
|
||||
num = num == 0 ? 1 : num;
|
||||
|
||||
a->data = safe_malloc(size * num);
|
||||
a->length = 0;
|
||||
a->size = num;
|
||||
a->unit_size = size;
|
||||
}
|
||||
|
||||
void *
|
||||
array_pop(array *a)
|
||||
{
|
||||
|
@ -209,54 +199,6 @@ array_push(array *a)
|
|||
return (char *) a->data + a->length++ * a->unit_size;
|
||||
}
|
||||
|
||||
char *
|
||||
array_strcat(array *dst, const char *src)
|
||||
{
|
||||
char *c;
|
||||
size_t i, len;
|
||||
|
||||
len = strlen(src);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
c = array_push(dst);
|
||||
*c = src[i];
|
||||
}
|
||||
c = array_push(dst);
|
||||
*c = '\0';
|
||||
dst->length--;
|
||||
|
||||
return dst->data;
|
||||
}
|
||||
|
||||
char *
|
||||
array_strcat_html(array *dst, const char *src, const html_val_type t)
|
||||
{
|
||||
char *c;
|
||||
size_t i, len;
|
||||
|
||||
len = strlen(src);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (t == tag_attribute && src[i] == '"')
|
||||
array_strcat(dst, """);
|
||||
else if (src[i] == '&')
|
||||
array_strcat(dst, "&");
|
||||
else if (src[i] == '<')
|
||||
array_strcat(dst, "<");
|
||||
else if (src[i] == '>')
|
||||
array_strcat(dst, ">");
|
||||
else {
|
||||
c = array_push(dst);
|
||||
*c = src[i];
|
||||
}
|
||||
}
|
||||
c = array_push(dst);
|
||||
*c = '\0';
|
||||
dst->length--;
|
||||
|
||||
return dst->data;
|
||||
}
|
||||
|
||||
void
|
||||
cmd(char *line)
|
||||
{
|
||||
|
@ -922,14 +864,15 @@ footer(void)
|
|||
{
|
||||
size_t max;
|
||||
struct tm *now;
|
||||
char str[22];
|
||||
char str[16];
|
||||
time_t s;
|
||||
int tm_mday, tm_mon, tm_year;
|
||||
struct tm tm_pd = { 0 };
|
||||
|
||||
max = 22;
|
||||
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=\"");
|
||||
|
@ -940,7 +883,7 @@ footer(void)
|
|||
tm_pd.tm_mday = tm_mday;
|
||||
tm_pd.tm_mon = --tm_mon;
|
||||
tm_pd.tm_year = tm_year - 1900;
|
||||
strftime(str, max, DATE_FORMAT, &tm_pd);
|
||||
strftime(str, max, "%x", &tm_pd);
|
||||
array_strcat(&body, str);
|
||||
array_strcat(&body, "</time></p>\n");
|
||||
|
||||
|
@ -951,11 +894,21 @@ footer(void)
|
|||
now = localtime(&s);
|
||||
|
||||
array_strcat(&body, "\t\t\t<p>Revisione: <time datetime=\"");
|
||||
strftime(str, max, "%Y-%m-%dT%H:%M%z", now);
|
||||
strftime(str, max, "%Y-%m-%d", now);
|
||||
array_strcat(&body, str);
|
||||
|
||||
strftime(str, max, "%R", now);
|
||||
array_strcat(&body, "T");
|
||||
array_strcat(&body, str);
|
||||
|
||||
strftime(str, max, "%z", now);
|
||||
array_strcat(&body, str);
|
||||
array_strcat(&body, "\">");
|
||||
|
||||
strftime(str, max, DATE_FORMAT " " TIME_FORMAT, now);
|
||||
strftime(str, max, "%x", now);
|
||||
array_strcat_html(&body, str, tag_content);
|
||||
|
||||
strftime(str, max, " %R", now);
|
||||
array_strcat_html(&body, str, tag_content);
|
||||
array_strcat(&body, "</time></p>\n");
|
||||
|
||||
|
@ -1067,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)
|
||||
{
|
||||
|
@ -1082,8 +1045,29 @@ 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)
|
||||
{
|
||||
num = num == 0 ? 1 : num;
|
||||
|
||||
a->data = safe_malloc(size * num);
|
||||
a->length = 0;
|
||||
a->size = num;
|
||||
a->unit_size = size;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
FILE *in, *out;
|
||||
|
||||
|
@ -1122,9 +1106,6 @@ main(int argc, char *argv[])
|
|||
|
||||
srohtml(in, out);
|
||||
|
||||
fclose(in);
|
||||
fclose(out);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1462,23 +1443,6 @@ page_head(void)
|
|||
array_strcat(&head, "\t</head>\n");
|
||||
}
|
||||
|
||||
void
|
||||
pop_closure(array *a)
|
||||
{
|
||||
closure *tag;
|
||||
|
||||
if (a->length <= 0)
|
||||
return;
|
||||
|
||||
tag = array_at(a, a->length - 1);
|
||||
if (tag == NULL) {
|
||||
fprintf(stderr, BIN ": Can't find tag closure at position %i, in array type.\n", a->length - 1);
|
||||
exit(1);
|
||||
}
|
||||
free(tag->name);
|
||||
array_pop(a);
|
||||
}
|
||||
|
||||
void
|
||||
print_help(void)
|
||||
{
|
||||
|
@ -1550,9 +1514,23 @@ read_and_process(FILE *in)
|
|||
line = NULL;
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
|
||||
if (strcmp(tag_closure, ""))
|
||||
cmd_close();
|
||||
void
|
||||
pop_closure(array *a)
|
||||
{
|
||||
closure *tag;
|
||||
|
||||
if (a->length <= 0)
|
||||
return;
|
||||
|
||||
tag = array_at(a, a->length - 1);
|
||||
if (tag == NULL) {
|
||||
fprintf(stderr, BIN ": Can't find tag closure at position %i, in array type.\n", a->length - 1);
|
||||
exit(1);
|
||||
}
|
||||
free(tag->name);
|
||||
array_pop(a);
|
||||
}
|
||||
|
||||
void *
|
||||
|
@ -1586,26 +1564,23 @@ safe_realloc(void *ptr, const size_t size)
|
|||
void
|
||||
srohtml(FILE *in, FILE *out)
|
||||
{
|
||||
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);
|
||||
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(head.data);
|
||||
free(body.data);
|
||||
free(figure_sizes.data);
|
||||
free(index.data);
|
||||
free(sitography.data);
|
||||
free_arrays();
|
||||
}
|
||||
|
||||
size_t
|
||||
|
@ -1640,3 +1615,51 @@ stread_param(const char *src, char **str)
|
|||
|
||||
return len + p;
|
||||
}
|
||||
|
||||
char *
|
||||
array_strcat(array *dst, const char *src)
|
||||
{
|
||||
char *c;
|
||||
size_t i, len;
|
||||
|
||||
len = strlen(src);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
c = array_push(dst);
|
||||
*c = src[i];
|
||||
}
|
||||
c = array_push(dst);
|
||||
*c = '\0';
|
||||
dst->length--;
|
||||
|
||||
return dst->data;
|
||||
}
|
||||
|
||||
char *
|
||||
array_strcat_html(array *dst, const char *src, const html_val_type t)
|
||||
{
|
||||
char *c;
|
||||
size_t i, len;
|
||||
|
||||
len = strlen(src);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (t == tag_attribute && src[i] == '"')
|
||||
array_strcat(dst, """);
|
||||
else if (src[i] == '&')
|
||||
array_strcat(dst, "&");
|
||||
else if (src[i] == '<')
|
||||
array_strcat(dst, "<");
|
||||
else if (src[i] == '>')
|
||||
array_strcat(dst, ">");
|
||||
else {
|
||||
c = array_push(dst);
|
||||
*c = src[i];
|
||||
}
|
||||
}
|
||||
c = array_push(dst);
|
||||
*c = '\0';
|
||||
dst->length--;
|
||||
|
||||
return dst->data;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue