slweb

Једноставни генератор статичких веб страна
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување c70c748acb77425ac4d1bfbf3d9a5b6a95b0a4f9
родитељ 38e5e777d47aef8141d9216f1583938376a33243
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Thu,  8 Jun 2023 16:26:15 +0200

Add article summary: /- -/, incdir-only-summary, incdir-footer-permalink-text

Signed-off-by: Страхиња Радић <contact@strahinja.org>

Diffstat:
Mdefs.h | 3++-
Mslweb.1.in | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mslweb.c | 132+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
измењених датотека: 3, додавања: 200(+), брисања: 27(-)

diff --git a/defs.h b/defs.h @@ -116,7 +116,8 @@ typedef int (*tsv_callback_t)(FILE* output, u8** tsv_header, u8** tsv_register); #define ST_TABLE_LINE ((ULLONG)1 << 31) #define ST_TABLE ((ULLONG)1 << 32) #define ST_STRIKE ((ULLONG)1 << 33) -#define ST_ENT ((ULLONG)1 << 33) +#define ST_SUMMARY ((ULLONG)1 << 34) +#define ST_INCDIR ((ULLONG)1 << 35) #define ST_CS_NONE 0 #define ST_CS_HEADER 1 diff --git a/slweb.1.in b/slweb.1.in @@ -642,6 +642,83 @@ directive. (See .BR Includes " and " date ", " ext-in-permalink ", " permalink-url ", " .BR samedir-permalink " variables.)" The output of each file will be surrounded by an \fC<article>\fR tag. +. +.PP +If the included file has the variable +.B incdir-only-summary +set to \[lq]1\[rq], only the text within the summary marks (\fC/-\fR and +\fC-/\fR) will be output, and if the variable +.B incdir-footer-permalink-text +is also set, \fC<footer>\fR element with a permalink to the article will be +added after it. +Example: (\fCposts/blog-post.slw\fR) +.CDS 4 +--- +incdir-only-summary: 1 +--- + +/-This article is great!-/ The quick brown fox jumps over the lazy sleeping dog. +The quick brown fox jumps over the lazy sleeping dog. The quick brown fox jumps +over the lazy sleeping dog. The quick brown fox jumps over the lazy sleeping +dog. The quick brown fox jumps over the lazy sleeping dog. The quick brown fox +jumps over the lazy sleeping dog. +.CDE +(\fCblog-index.slw\fR) +.CDS 4 +--- +incdir-footer-permalink-text: Click here! +--- +{incdir "."} +.CDE +will produce +.CDS 4 +<!DOCTYPE html> +<html lang="en"> +<head> +<title></title> +<meta charset="utf-8"> +<meta name="viewport" content="width=device-width, initial-scale=1"> +<meta name="generator" content="slweb"> +</head> +<body> +<ul class="incdir"> +<li> +<details open> +<summary>posts</summary> +<div> +<article> +This article is great!<footer> +<a href="././posts/blog-post">Click here!</a> +</footer> +</article> +</div> +</details> +</li> +</ul> +</body> +</html> +.CDE +for \fCblog-index.slw\fR, and +.CDS 4 +<!DOCTYPE html> +<html lang="en"> +<head> +<title></title> +<meta charset="utf-8"> +<meta name="viewport" content="width=device-width, initial-scale=1"> +<meta name="generator" content="slweb"> +</head> +<body> + +<p>This article is great! The quick brown fox jumps over the lazy sleeping dog. +The quick brown fox jumps over the lazy sleeping dog. The quick brown fox jumps +over the lazy sleeping dog. The quick brown fox jumps over the lazy sleeping +dog. The quick brown fox jumps over the lazy sleeping dog. The quick brown fox +jumps over the lazy sleeping dog.</p> +</body> +</html> +.CDE +for \fCposts/blog-post.slw\fR. .RE . .IP \[bu] @@ -829,6 +906,21 @@ actual file path to pass to the command. Otherwise, \fCsrc\fR is appended to \[lq].\[rq]. . .IP \[bu] +.BR incdir-footer-permalink-text . +If set, and the variable +.B incdir-only-summary +is set to \[lq]1\[rq], when the file where this variable is set is included by +the \fC{incdir}\fR directive, \fC<footer>\fR tag containing the permalink to the +calling file will be appended to the text between the summary marks \fC/-\fR and +\fC-/\fR. +. +.IP \[bu] +.BR incdir-only-summary . +If set to \[lq]1\[rq], when the file where this variable is set is included by +the \fC{incdir}\fR directive, only the text within the summary marks \fC/-\fR +and \fC-/\fR will be output. +. +.IP \[bu] .BR inline-stylesheet . The contents of this variable will be treated as a .SM CSS 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 int incdir_only_summary = 0; #define CHECKEXITNOMEM(ptr) \ do \ @@ -309,12 +310,23 @@ print_output(FILE* output, const char* fmt, ...) { char buf[BUFSIZE]; va_list args; + u8* var_incdir_only_summary = NULL; + int incdir_only_summary = 0; if (!output || !fmt) exit(error(EINVAL, (u8*)"print_output: Invalid argument")); va_start(args, fmt); - if (IN(state, ST_CSV_BODY)) + + var_incdir_only_summary + = get_value(vars, vars_count, (u8*)"incdir-only-summary"); + incdir_only_summary + = var_incdir_only_summary && *var_incdir_only_summary == '1'; + + if (IN(state, ST_INCDIR) && !IN(state, ST_SUMMARY) + && incdir_only_summary) + return 0; + else if (IN(state, ST_CSV_BODY)) { size_t buf_len = 0; vsnprintf((char*)buf, BUFSIZE, fmt, args); @@ -581,8 +593,7 @@ process_stylesheet(FILE* output, const u8* css_filename, int output_yaml) if (output_yaml) print_output(output, "stylesheet: %s\n", css_filename); else - print_output(output, - "<link rel=\"stylesheet\" href=\"%s\">\n", + print_output(output, "<link rel=\"stylesheet\" href=\"%s\">\n", css_filename); output_firstcol = 1; return 0; @@ -1449,14 +1460,14 @@ process_include(FILE* output, const u8* token, int process_list_start(FILE* output) { - fprintf(output, "<ul>"); + print_output(output, "<ul>"); return 0; } int process_list_item_start(FILE* output) { - fprintf(output, "%s<li><p>", output_firstcol ? "" : "\n"); + print_output(output, "%s<li><p>", output_firstcol ? "" : "\n"); return 0; } @@ -1466,10 +1477,10 @@ process_list_item_end(FILE* output) if (IN(state, ST_PARA_OPEN)) { state &= ~ST_PARA_OPEN; - fprintf(output, "</p>"); + print_output(output, "</p>"); output_firstcol = 0; } - fprintf(output, "</li>"); + print_output(output, "</li>"); output_firstcol = 0; return 0; } @@ -1477,7 +1488,7 @@ process_list_item_end(FILE* output) int process_list_end(FILE* output) { - fprintf(output, "</ul>\n"); + print_output(output, "</ul>\n"); output_firstcol = 1; return 0; } @@ -1485,7 +1496,7 @@ process_list_end(FILE* output) int process_numlist_start(FILE* output) { - fprintf(output, "<ol>"); + print_output(output, "<ol>"); output_firstcol = 0; return 0; } @@ -1493,7 +1504,7 @@ process_numlist_start(FILE* output) int process_numlist_end(FILE* output) { - fprintf(output, "</ol>\n"); + print_output(output, "</ol>\n"); output_firstcol = 1; return 0; } @@ -1557,12 +1568,14 @@ int process_timestamp(FILE* output, const u8* link_prefix, const char* link, int process_incdir_subdir(FILE* output, const char* subdirname, - const u8* link_prefix, const int details_open, const u8* macro_body) + const u8* link_prefix, const int details_open, const u8* macro_body, + const u8* footer_permalink_text, const char* permalink_url, + int ext_in_permalink) { print_output(output, "<li>\n<details%s>\n<summary>", details_open ? " open" : ""); if (macro_body) - fprintf(stdout, "%s", macro_body); + print_output(output, "%s", macro_body); print_output(output, "%s</summary>\n<div>\n", subdirname); struct dirent** namelist; @@ -1634,7 +1647,7 @@ process_incdir_subdir(FILE* output, const char* subdirname, current_inline_footnote = 0; saved_state = state; - state = ST_NONE; + state = ST_INCDIR; /* First pass: read YAML, macros and links */ result = slweb_parse(output, filename, buffer, 1, 1); @@ -1651,7 +1664,7 @@ process_incdir_subdir(FILE* output, const char* subdirname, return result; } - state = ST_NONE; + state = ST_INCDIR; current_footnote = 0; current_inline_footnote = 0; @@ -1660,6 +1673,23 @@ process_incdir_subdir(FILE* output, const char* subdirname, state = saved_state; + if (footer_permalink_text && incdir_only_summary) + { + char* link = strip_ext(filename); + + if (ext_in_permalink) + strcat(link, timestamp_output_ext); + + print_output(output, + "<footer>\n" + "<a href=\"%s\">%s</a>\n" + "</footer>\n", + permalink_url ? permalink_url : link, + footer_permalink_text); + + free(link); + } + fflush(output); slweb_cleanup(); free(filename); @@ -1691,7 +1721,8 @@ process_incdir_subdir(FILE* output, const char* subdirname, int process_incdir(FILE* output, const u8* token, const u8* link_prefix, - const int read_yaml_macros_and_links) + const u8* footer_permalink_text, const char* permalink_url, + const int ext_in_permalink, const int read_yaml_macros_and_links) { u8* saveptr = NULL; /* skipping the first token (incdir) */ @@ -1771,7 +1802,8 @@ process_incdir(FILE* output, const u8* token, const u8* link_prefix, while (names_output < MIN(names_total, num)) { process_incdir_subdir(output, (*pnamelist)->d_name, link_prefix, - details_open, macro_body); + details_open, macro_body, footer_permalink_text, + permalink_url, ext_in_permalink); details_open = 0; pnamelist++; names_output++; @@ -1914,7 +1946,9 @@ process_macro(FILE* output, const u8* token, const int end_tag) } int -process_tag(FILE* output, const u8* token, const u8* link_prefix, +process_tag(FILE* output, const u8* token, const char* source_filename, + const u8* link_prefix, const u8* footer_permalink_text, + const char* permalink_url, const int ext_in_permalink, const int read_yaml_macros_and_links, int* skip_eol, const int end_tag) { if (!token || strlen((char*)token) < 1) @@ -1938,6 +1972,7 @@ process_tag(FILE* output, const u8* token, const u8* link_prefix, else if (startswith((char*)token, "incdir")) /* {incdir} */ { process_incdir(output, token, link_prefix, + footer_permalink_text, permalink_url, ext_in_permalink, read_yaml_macros_and_links); *skip_eol = 1; } @@ -2549,6 +2584,9 @@ begin_article(FILE* output, const char* source_filename, const u8* link_prefix, const u8* header_text, const char* title_heading_level, u8* date, const int ext_in_permalink, const char* permalink_url) { + int save_incdir = IN(state, ST_INCDIR); + state &= ~ST_INCDIR; + if (author || date || header_text || title) print_output(output, "<header>\n"); @@ -2566,11 +2604,9 @@ begin_article(FILE* output, const char* source_filename, const u8* link_prefix, if (date && source_filename) { - char* link = strip_ext((const char*)source_filename); - char* real_link = NULL; + char* link = strip_ext((const char*)source_filename); u8* permalink_macro = get_value(macros, macros_count, (u8*)"permalink"); - CALLOC(real_link, char, BUFSIZE); if (ext_in_permalink) strcat(link, timestamp_output_ext); @@ -2582,7 +2618,6 @@ begin_article(FILE* output, const char* source_filename, const u8* link_prefix, process_timestamp(output, link_prefix, link, permalink_macro, date); - free(real_link); free(link); } @@ -2591,6 +2626,9 @@ begin_article(FILE* output, const char* source_filename, const u8* link_prefix, output_firstcol = author || date || header_text || title; + if (save_incdir) + state |= ST_INCDIR; + return 0; } @@ -2750,6 +2788,8 @@ slweb_parse(FILE* output, const char* source_filename, const u8* buffer, u8* var_add_image_links = NULL; u8* var_add_figcaption = NULL; u8* var_add_footnote_div = NULL; + u8* footer_permalink_text = NULL; + u8* var_incdir_only_summary = NULL; u8* image_file_prefix = NULL; u8* link_prefix = NULL; const u8* pbuffer = NULL; @@ -2804,6 +2844,12 @@ slweb_parse(FILE* output, const char* source_filename, const u8* buffer, = get_value(vars, vars_count, (u8*)"add-footnote-div"); add_footnote_div = var_add_footnote_div && *var_add_footnote_div == '1'; link_prefix = get_value(vars, vars_count, (u8*)"link-prefix"); + footer_permalink_text = get_value(vars, vars_count, + (u8*)"incdir-footer-permalink-text"); + var_incdir_only_summary + = get_value(vars, vars_count, (u8*)"incdir-only-summary"); + incdir_only_summary + = var_incdir_only_summary && *var_incdir_only_summary == '1'; CALLOC(line, u8, BUFSIZE); token_size = BUFSIZE; @@ -2885,8 +2931,7 @@ do_line: } pline = NULL; } - else if (colno == 1 && strlen((char*)pline) > 1 - && *(pline + 1) == ' ') + else if (colno == 1 && *(pline + 1) == ' ') { if (IN(state, ST_NUMLIST)) { @@ -2915,6 +2960,21 @@ do_line: pline += 2; colno += 2; } + else if (IN(state, ST_SUMMARY) && *(pline + 1) == '/') + { + /* Output existing text up to -/ */ + process_text_token(output, line, link_prefix, + first_line_in_doc, previous_line_blank, + &previous_line_block, processed_start_of_line, + read_yaml_macros_and_links, list_para, &token, + &ptoken, &token_size, 1, 1); + processed_start_of_line = 1; + + state &= ~ST_SUMMARY; + + pline += 2; + colno += 2; + } else { CHECKCOPY(token, ptoken, token_size, pline); @@ -3691,6 +3751,21 @@ do_line: end_tag = 1; pline++; } + else if (*(pline + 1) == '-') + { + /* Output existing text up to /- */ + process_text_token(output, line, link_prefix, + first_line_in_doc, previous_line_blank, + &previous_line_block, processed_start_of_line, + read_yaml_macros_and_links, list_para, &token, + &ptoken, &token_size, 1, 1); + processed_start_of_line = 1; + + state |= ST_SUMMARY; + + pline += 2; + colno++; + } else CHECKCOPY(token, ptoken, token_size, pline); @@ -3749,7 +3824,11 @@ do_line: if (!strcmp((char*)token, "made-by")) madeby_present = 1; else - process_tag(output, token, link_prefix, + process_tag(output, token, source_filename, + link_prefix, footer_permalink_text, + (const char*)permalink_url, + ext_in_permalink + && *ext_in_permalink != '0', read_yaml_macros_and_links, &skip_eol, end_tag); @@ -4266,7 +4345,8 @@ do_line: token); else { - if (add_figcaption && IN(state, ST_PARA_OPEN)) + if (add_figcaption + && IN(state, ST_PARA_OPEN)) { print_output(output, "</p>\n"); state &= ~ST_PARA_OPEN; @@ -4952,7 +5032,7 @@ done_line: { process_blockquote(output, 1); previous_line_block = 1; - output_firstcol = 0; + output_firstcol = 0; } }