чување 4a9bece4bbf3777ca27fc1f1abead502135b6675
родитељ cfda8051b2537e8245dfe0816c1dd9008f1e7af7
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Sun, 9 Jan 2022 01:52:56 +0100
WIP includes with YAML
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
измењених датотека: 6, додавања: 137(+), брисања: 47(-)
diff --git a/css01.css b/css01.css
@@ -0,0 +1,4 @@
+h1
+{
+ color: blue;
+}
diff --git a/css02.css b/css02.css
@@ -0,0 +1,9 @@
+h1
+{
+ color: red;
+}
+
+h2
+{
+ color: green;
+}
diff --git a/defs.h b/defs.h
@@ -22,7 +22,7 @@
#include "version.h"
#include "utf8.h"
-#define COPYRIGHTYEAR "2020, 2021"
+#define COPYRIGHTYEAR "2020, 2021, 2022"
#define MADEBY_URL "https://strahinja.srht.site/slweb"
#define BUFSIZE 1024
diff --git a/slweb.c b/slweb.c
@@ -37,6 +37,7 @@ static size_t tsv_template_size = 0;
static char* tsv_filename = NULL;
static long tsv_iter = 0;
static ULLONG state = ST_NONE;
+static BOOL in_included_file = FALSE;
#define CHECKEXITNOMEM(ptr) \
do \
@@ -166,7 +167,8 @@ slweb_cleanup()
}
int slweb_parse(FILE* output, const char* source_filename, const u8* buffer,
- const BOOL body_only, const BOOL read_yaml_macros_and_links);
+ const BOOL body_only, const BOOL parent_read_yaml,
+ const BOOL read_yaml_macros_and_links);
BOOL
startswith(const char* s, const char* what)
@@ -547,9 +549,24 @@ process_git_log(FILE* output)
}
int
-process_inline_stylesheet(FILE* output, const u8* css_filename)
+process_stylesheet(FILE* output, const u8* css_filename,
+ BOOL read_yaml_macros_and_links)
{
- FILE* css = NULL;
+ if (read_yaml_macros_and_links)
+ print_output(output, "stylesheet: %s\n", css_filename);
+ else
+ print_output(output,
+ "<link rel=\"stylesheet\" href=\"%s\" />\n",
+ css_filename);
+ output_firstcol = TRUE;
+ return 0;
+}
+
+int
+process_inline_stylesheet(FILE* output, const u8* css_filename,
+ BOOL read_yaml_macros_and_links)
+{
+ FILE* css = NULL;
u8* bufline = NULL;
if (!css_filename)
@@ -561,6 +578,15 @@ process_inline_stylesheet(FILE* output, const u8* css_filename)
(u8*)"process_inline_stylesheet: No such file: %s",
css_filename));
+ print_output(output, "process_inline_stylesheet:read_yml: %s\n",
+ read_yaml_macros_and_links ? "TRUE" : "FALSE");
+ if (read_yaml_macros_and_links)
+ {
+ print_output(output, "inline-stylesheet: %s\n", css_filename);
+ output_firstcol = TRUE;
+ goto process_inline_stylesheet_cleanup;
+ }
+
CALLOC(bufline, u8, BUFSIZE);
print_output(output, "<style>\n");
@@ -571,7 +597,9 @@ process_inline_stylesheet(FILE* output, const u8* css_filename)
print_output(output, "%s", bufline);
}
print_output(output, "</style>\n");
+ output_firstcol = TRUE;
+process_inline_stylesheet_cleanup:
fclose(css);
free(bufline);
@@ -1247,28 +1275,39 @@ int
process_include(FILE* output, const u8* token,
const BOOL read_yaml_macros_and_links)
{
- if (read_yaml_macros_and_links)
- return 0;
-
- if (!input_filename)
- return warning(1, (u8*)"Cannot use 'include' in stdin");
+ // TODO: if (read_yaml_macros_and_links), process only stylesheets:
+ // collect stylesheet names and add them to stylesheet list
+ /*
+ *if (read_yaml_macros_and_links)
+ * return 0;
+ */
u8* ptoken = (u8*)strchr((char*)token, ' ');
char* include_filename = NULL;
char* pinclude_filename = NULL;
+ pid_t pid = 0;
+ int pstatus = 0;
+
+ if (!input_filename)
+ return warning(1, (u8*)"Cannot use 'include' in stdin");
if (!ptoken)
exit(error(1,
(u8*)"Directive 'include' requires"
" an argument"));
+ print_output(output, "== Before include %s==\n",
+ read_yaml_macros_and_links ? "(read yaml) " : "");
fflush(output);
- pid_t pid = fork();
- int pstatus = 0;
+ pid = fork();
- if (pid > 0)
+ if (pid > 0) /* parent */
+ {
wait(&pstatus);
- else if (pid == 0)
+ print_output(output, "== After include %s==\n",
+ read_yaml_macros_and_links ? "(read yaml) " : "");
+ }
+ else if (pid == 0) /* child */
{
char* filename = NULL;
prctl(PR_SET_PDEATHSIG, SIGTERM);
@@ -1295,7 +1334,9 @@ process_include(FILE* output, const u8* token,
u8* buffer = NULL;
size_t buffer_size = 0;
int result = 0;
- ULLONG saved_state = ST_NONE;
+ /*
+ *ULLONG saved_state = ST_NONE;
+ */
read_file_into_buffer(&input, &buffer, &buffer_size, filename,
&input_dirname);
@@ -1318,28 +1359,38 @@ process_include(FILE* output, const u8* token,
inline_footnote_count = 0;
current_inline_footnote = 0;
- saved_state = state;
- state = ST_NONE;
+ /*
+ *saved_state = state;
+ */
+ state = ST_NONE;
+ in_included_file = TRUE;
+ print_output(output, "process_include:child(%s):read_yml==%s\n",
+ filename,
+ read_yaml_macros_and_links ? "TRUE" : "FALSE");
/* First pass: read YAML, macros and links */
- result = slweb_parse(output, filename, buffer, TRUE, TRUE);
+ result = slweb_parse(output, filename, buffer,
+ read_yaml_macros_and_links, TRUE, TRUE);
if (result)
- {
- slweb_cleanup();
- free(filename);
- free(buffer);
- return result;
- }
+ goto process_include_child_cleanup;
+
+ if (read_yaml_macros_and_links)
+ goto process_include_child_cleanup;
+
state = ST_NONE;
current_footnote = 0;
current_inline_footnote = 0;
/* Second pass: parse and output */
- result = slweb_parse(output, filename, buffer, TRUE, FALSE);
+ result = slweb_parse(output, filename, buffer,
+ read_yaml_macros_and_links, TRUE, FALSE);
- state = saved_state;
+ /*
+ *state = saved_state;
+ */
+ process_include_child_cleanup:
slweb_cleanup();
free(filename);
free(buffer);
@@ -1540,8 +1591,7 @@ process_incdir_subdir(FILE* output, const char* subdirname,
state = ST_NONE;
/* First pass: read YAML, macros and links */
- result = slweb_parse(output, filename, buffer, TRUE,
- TRUE);
+ result = slweb_parse(output, filename, buffer, FALSE, TRUE, TRUE);
if (result)
{
@@ -1560,8 +1610,7 @@ process_incdir_subdir(FILE* output, const char* subdirname,
current_inline_footnote = 0;
/* Second pass: parse and output */
- result = slweb_parse(output, filename, buffer, TRUE,
- FALSE);
+ result = slweb_parse(output, filename, buffer, FALSE, TRUE, FALSE);
state = saved_state;
@@ -2416,27 +2465,23 @@ begin_html_and_head(FILE* output)
}
int
-add_css(FILE* output)
+add_css(FILE* output, BOOL read_yaml_macros_and_links)
{
if (!vars)
return -1;
KeyValue* pvars = vars;
+ print_output(output, "=== add_css ===\n");
while (pvars < vars + vars_count)
{
if (!strcmp((char*)pvars->key, "stylesheet"))
- {
- print_output(output,
- "<link rel=\"stylesheet\" href=\"%s\" />\n",
- pvars->value);
- output_firstcol = TRUE;
- }
+ process_stylesheet(output, pvars->value,
+ read_yaml_macros_and_links);
else if (!strcmp((char*)pvars->key, "inline-stylesheet"))
- {
- process_inline_stylesheet(output, pvars->value);
- output_firstcol = TRUE;
- }
+ process_inline_stylesheet(output, pvars->value,
+ read_yaml_macros_and_links);
pvars++;
}
+ print_output(output, "=== /add_css ===\n");
return 0;
}
@@ -2564,7 +2609,8 @@ end_body_and_html(FILE* output)
int
slweb_parse(FILE* output, const char* source_filename, const u8* buffer,
- const BOOL body_only, const BOOL read_yaml_macros_and_links)
+ const BOOL body_only, const BOOL parent_read_yaml,
+ const BOOL read_yaml_macros_and_links)
{
u8* title = NULL;
u8* title_heading_level = NULL;
@@ -2644,11 +2690,23 @@ slweb_parse(FILE* output, const char* source_filename, const u8* buffer,
pfootnotes = footnotes;
lineno = 0;
- if (!read_yaml_macros_and_links && !body_only)
+ if (in_included_file)
+ {
+ if (parent_read_yaml)
+ {
+ print_output(output, "slweb_parse:add_css[1]\n");
+ add_css(output, parent_read_yaml);
+ }
+ }
+ else
{
- begin_html_and_head(output);
- add_css(output);
- end_head_start_body(output);
+ if (!read_yaml_macros_and_links && !body_only)
+ {
+ begin_html_and_head(output);
+ print_output(output, "slweb_parse:add_css[2]\n");
+ add_css(output, read_yaml_macros_and_links);
+ end_head_start_body(output);
+ }
}
if (!read_yaml_macros_and_links)
@@ -4959,7 +5017,8 @@ main(const int argc, const char** argv)
inline_footnote_count = 0;
/* First pass: read YAML, macros and links */
- result = slweb_parse(output, input_filename, buffer, body_only, TRUE);
+ result = slweb_parse(output, input_filename, buffer, FALSE, body_only,
+ TRUE);
if (result)
{
@@ -4973,7 +5032,8 @@ main(const int argc, const char** argv)
current_inline_footnote = 0;
/* Second pass: parse and output */
- result = slweb_parse(output, input_filename, buffer, body_only, FALSE);
+ result = slweb_parse(output, input_filename, buffer, FALSE, body_only,
+ FALSE);
slweb_cleanup();
free(buffer);
diff --git a/test01.slw b/test01.slw
@@ -0,0 +1,9 @@
+---
+inline-stylesheet: css01.css
+---
+# Include test
+
+Including test02.
+
+{include "test02"}
+
diff --git a/test02.slw b/test02.slw
@@ -0,0 +1,8 @@
+---
+inline-stylesheet: css02.css
+---
+
+## Included file
+
+This is the file test02.slw.
+