slw2gmi

Конвертор из slweb-а у Џемини
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување cd11764798dbbd7166af3b3ff80dc3cda732597b
родитељ 2e89ed6038a4a22c4af2dcdb3dcb7302142ceb59
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Mon, 22 Mar 2021 16:20:00 +0100

Changed output_firstcol to BOOL

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

Diffstat:
Mslw2gmi.c | 112+++++++++++++++++++++++++++++++++++++++----------------------------------------
измењених датотека: 1, додавања: 55(+), брисања: 57(-)

diff --git a/slw2gmi.c b/slw2gmi.c @@ -21,7 +21,7 @@ static size_t lineno = 0; static size_t colno = 1; -static size_t output_colno = 1; +static BOOL output_firstcol = TRUE; static char* input_filename = NULL; static char* input_dirname = NULL; static char* basedir = NULL; @@ -348,7 +348,7 @@ print_command(const char* command, strip_newlines ? "" : "\n"); } if (!strip_newlines) - output_colno = 1; + output_firstcol = TRUE; free(cmd_output_line); fclose(cmd_output); @@ -416,7 +416,7 @@ process_heading_start(FILE* output, UBYTE heading_level) for (size_t i = 0; i < heading_level; i++) print_output(output, "#"); print_output(output, " "); - output_colno = heading_level + 1; + output_firstcol = FALSE; return 0; } @@ -427,7 +427,7 @@ process_heading(const uint8_t* token, FILE* output) warning(1, (uint8_t*)"Empty heading"); print_output(output, "%s\n\n", token ? (char*)token : ""); - output_colno = 1; + output_firstcol = TRUE; return 0; } @@ -456,7 +456,7 @@ process_git_log(FILE* output) (const uint8_t**)CMD_GIT_LOG_ARGS, (const uint8_t**)pipe_args, output, FALSE); print_output(output, "\n"); - output_colno = 1; + output_firstcol = TRUE; free(basename); @@ -919,7 +919,7 @@ process_list_start(FILE* output) int process_list_item_start(FILE* output) { - fprintf(output, "%s* ", output_colno == 1 ? "" : "\n"); + fprintf(output, "%s* ", output_firstcol ? "" : "\n"); state |= ST_PARA_OPEN; return 0; } @@ -934,7 +934,7 @@ process_list_item_end(const uint8_t* link_prefix, FILE* output) { state &= ~ST_PARA_OPEN; fprintf(output, "\n"); - output_colno = 1; + output_firstcol = TRUE; process_paralinks(link_prefix, output); } return 0; @@ -1029,7 +1029,7 @@ process_incdir_subdir(const char* subdirname, const uint8_t* link_prefix, char* abs_subdirname = NULL; CALLOC(abs_subdirname, char, BUFSIZE) - snprintf(abs_subdirname, BUFSIZE, "%s/%s/%s", basedir, incdir, subdirname); + snprintf(abs_subdirname, BUFSIZE, "%s/%s", basedir, subdirname); if ((names_total = scandir(abs_subdirname, &namelist, &filter_slw, &reverse_alphacompare)) < 0) @@ -1053,6 +1053,7 @@ process_incdir_subdir(const char* subdirname, const uint8_t* link_prefix, fflush(output); pid_t pid = fork(); + if (pid > 0) wait(&pstatus); else if (pid == 0) @@ -1224,7 +1225,6 @@ process_incdir(uint8_t* token, const uint8_t* link_prefix, FILE* output, } } - ; if ((names_total = scandir(incdir, &namelist, &filter_subdirs, &reverse_alphacompare)) < 0) { @@ -1254,7 +1254,7 @@ process_incdir(uint8_t* token, const uint8_t* link_prefix, FILE* output, names_output++; } print_output(output, "\n"); - output_colno = 1; + output_firstcol = TRUE; while (names_total--) free(namelist[names_total]); @@ -1268,8 +1268,8 @@ BOOL url_is_local(const char* url); int -process_timestamp(FILE* output, const uint8_t* link_prefix, const char* link, uint8_t* permalink_macro, - uint8_t* date, uint8_t* title) +process_timestamp(FILE* output, const uint8_t* link_prefix, const char* link, + uint8_t* permalink_macro, uint8_t* date, uint8_t* title) { uint8_t* day = NULL; uint8_t* month = NULL; @@ -1322,7 +1322,7 @@ process_timestamp(FILE* output, const uint8_t* link_prefix, const char* link, ui url_is_local(link) && link_prefix ? (const char*)link_prefix : "", link, formatted_date); - output_colno = 1; + output_firstcol = TRUE; } } } @@ -1354,11 +1354,13 @@ process_macro(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links, if (seen) { uint8_t* eol = u8_strrchr(macro_body, '\n'); + size_t macro_body_len = u8_strlen(macro_body); if (eol) - output_colno = macro_body + u8_strlen(macro_body) - - u8_strrchr(macro_body, '\n') + 1; + output_firstcol = macro_body + macro_body_len - + u8_strrchr(macro_body, '\n') > 0; else - output_colno += u8_strlen(macro_body); + output_firstcol = !*macro_body + || *(macro_body+macro_body_len-1) == '\n'; print_output(output, "%s", macro_body); } else @@ -1409,7 +1411,7 @@ process_tag(uint8_t* token, const uint8_t* link_prefix, FILE* output, { print_output(output, "Generated by slw2gmi © %s Strahinya Radich.\n", COPYRIGHTYEAR); - output_colno = 1; + output_firstcol = TRUE; } else if (startswith((char*)token, "csv")) /* {csv} */ { @@ -1458,12 +1460,12 @@ process_blockquote(FILE* output, BOOL end_tag) if (end_tag) { print_output(output, "\n\n"); - output_colno = 1; + output_firstcol = TRUE; } else { - print_output(output, "%s>", output_colno == 1 ? "" : "\n"); - output_colno = 2; + print_output(output, "%s>", output_firstcol ? "" : "\n"); + output_firstcol = FALSE; } return 0; } @@ -1609,7 +1611,7 @@ process_inline_link(uint8_t* link_text, uint8_t* link_macro_body, pparalinks->value = u8_strdup(link_text); print_output(output, "%s", link_text); - output_colno += u8_strlen(link_text); + output_firstcol = FALSE; return 0; } @@ -1642,7 +1644,8 @@ process_inline_image(uint8_t* image_text, uint8_t* image_url, FILE* output, pparalinks->value = u8_strdup(image_text); print_output(output, "%s", image_text); - output_colno += u8_strlen(image_text); + if (!*image_text) + output_firstcol = FALSE; return 0; } @@ -1665,11 +1668,11 @@ process_paralinks(const uint8_t* link_prefix, FILE* output) if (pparalink->key) { print_output(output, "%s=> %s%s\t%s\n", - output_colno == 1 ? "" : "\n", + output_firstcol ? "" : "\n", url_is_local((const char*)pparalink->key) && link_prefix ? (const char*)link_prefix : "", pparalink->key, pparalink->value); - output_colno = 1; + output_firstcol = TRUE; } pparalink++; } @@ -1689,8 +1692,8 @@ process_paralinks(const uint8_t* link_prefix, FILE* output) } int -process_line_start(uint8_t* line, const uint8_t* link_prefix, BOOL - first_line_in_doc, BOOL previous_line_blank, +process_line_start(uint8_t* line, const uint8_t* link_prefix, + BOOL first_line_in_doc, BOOL previous_line_blank, BOOL read_yaml_macros_and_links, BOOL list_para, FILE* output, uint8_t** token, uint8_t** ptoken) { @@ -1724,7 +1727,7 @@ process_line_start(uint8_t* line, const uint8_t* link_prefix, BOOL if (!read_yaml_macros_and_links && (state & ST_PARA_OPEN)) { print_output(output, "\n\n"); - output_colno = 1; + output_firstcol = TRUE; process_paralinks(link_prefix, output); } state &= ~(ST_FOOTNOTE_TEXT | ST_PARA_OPEN); @@ -1758,9 +1761,9 @@ process_text_token(uint8_t* line, const uint8_t* link_prefix, print_output(output, "%s", *token); uint8_t* eol = u8_strrchr(*token, '\n'); if (eol) - output_colno = *token + u8_strlen(*token) - eol + 1; + output_firstcol = *token + u8_strlen(*token) - eol > 0; else - output_colno += u8_strlen(*token); + output_firstcol = FALSE; } } RESET_TOKEN(*token, *ptoken, *token_size) @@ -1790,7 +1793,7 @@ process_inline_footnote(uint8_t* token, BOOL read_yaml_macros_and_links, else { print_output(output, "(%d)", current_inline_footnote); - output_colno += 3; // We only care if it's the first column anyway + output_firstcol = FALSE; } return 0; @@ -1821,7 +1824,7 @@ process_footnote(uint8_t* token, BOOL footnote_definition, BOOL footnote_output, if (footnote_output) { print_output(output, "(%d)", current_footnote); - output_colno += 3; + output_firstcol = FALSE; } return 0; @@ -1834,7 +1837,7 @@ process_horizontal_rule(FILE* output) if (state & ST_PARA_OPEN) print_output(output, "\n\n"); print_output(output, "————————————————————\n\n"); - output_colno = 1; + output_firstcol = TRUE; return 0; } @@ -1858,16 +1861,10 @@ begin_article(FILE* output, const BOOL add_article_header, } if (header_text) - { print_output(output, "%s\n\n", (char*)header_text); - output_colno = 1; - } if (author) - { print_output(output, "%s\n\n", author); - output_colno = 1; - } if (date && input_filename) { @@ -1905,6 +1902,8 @@ begin_article(FILE* output, const BOOL add_article_header, free(link); } + output_firstcol = author || date || header_text || title; + return 0; } @@ -1916,7 +1915,7 @@ end_footnotes(FILE* output, const uint8_t* link_prefix, BOOL add_footnote_div) if (state & ST_PARA_OPEN) { print_output(output, "\n\n"); - output_colno = 1; + output_firstcol = TRUE; state &= ~ST_PARA_OPEN; process_paralinks(link_prefix, output); } @@ -1927,7 +1926,7 @@ end_footnotes(FILE* output, const uint8_t* link_prefix, BOOL add_footnote_div) { print_output(output, "%d. %s\n\n", footnote+1, (char*)inline_footnotes[footnote]); - output_colno = 1; + output_firstcol = TRUE; } KeyValue* pfootnote = footnotes; @@ -1935,7 +1934,7 @@ end_footnotes(FILE* output, const uint8_t* link_prefix, BOOL add_footnote_div) while (pfootnote && footnote < footnote_count) { print_output(output, "%d. %s\n\n", footnote+1, (char*)pfootnote->value); - output_colno = 1; + output_firstcol = TRUE; pfootnote++; footnote++; } @@ -1957,7 +1956,7 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) uint8_t* var_add_image_links = NULL; uint8_t* var_add_figcaption = NULL; uint8_t* var_add_footnote_div = NULL; - uint8_t* link_prefix = NULL; + uint8_t* link_prefix = NULL; uint8_t* pbuffer = NULL; uint8_t* line = NULL; uint8_t* pline = NULL; @@ -2010,7 +2009,7 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) add_figcaption = !(var_add_figcaption && *var_add_figcaption == '0'); var_add_footnote_div = get_value(vars, vars_count, (uint8_t*)"add-footnote-div", NULL); add_footnote_div = var_add_footnote_div && *var_add_footnote_div == '1'; - link_prefix = get_value(vars, vars_count, (uint8_t*)"link-prefix", NULL); + link_prefix = get_value(vars, vars_count, (uint8_t*)"link-prefix", NULL); CALLOC(line, uint8_t, BUFSIZE) token_size = BUFSIZE; @@ -2050,7 +2049,7 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) lineno++; colno = 1; - output_colno = 1; + output_firstcol = TRUE; processed_start_of_line = FALSE; skip_eol = FALSE; /*list_item = FALSE;*/ @@ -2201,7 +2200,7 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) print_output(output, "\n```\n"); else print_output(output, "\n```\n\n"); - output_colno = 1; + output_firstcol = TRUE; } /* Skip the rest of the line (language) */ @@ -2576,7 +2575,7 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) u8_strncat(ptoken, (uint8_t*)"\n", BUFSIZE-u8_strlen(token)-1); ptoken += strlen("\n"); - output_colno = 1; + output_firstcol = TRUE; pline++; colno++; } @@ -2855,7 +2854,8 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) if (!read_yaml_macros_and_links) { print_output(output, "%s", token); - output_colno += u8_strlen(token); + if (*token) + output_firstcol = FALSE; if (u8_strlen(pline) > 1) process_table_header_cell(output); else @@ -2871,7 +2871,8 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) if (!read_yaml_macros_and_links) { print_output(output, "%s", token); - output_colno += u8_strlen(token); + if (*token) + output_firstcol = FALSE; if (u8_strlen(pline) > 1) process_table_body_cell(output); else @@ -3047,7 +3048,7 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) if (!read_yaml_macros_and_links && (state & ST_PARA_OPEN)) { print_output(output, "\n\n"); - output_colno = 1; + output_firstcol = TRUE; process_paralinks(link_prefix, output); } state &= ~(ST_FOOTNOTE_TEXT | ST_PARA_OPEN); @@ -3681,10 +3682,10 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) if (!read_yaml_macros_and_links) { print_output(output, "%s\n", - (line_len == 0) || (output_colno == 1) + (line_len == 0) || output_firstcol || previous_line_blank ? "" : "\n"); - output_colno = 1; + output_firstcol = TRUE; process_paralinks(link_prefix, output); } if (state & ST_LIST) @@ -3774,15 +3775,12 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) if (!skip_eol && !keep_token && !read_yaml_macros_and_links && line_len > 0 - && output_colno > 1 + && !output_firstcol && !ANY(state, ST_YAML | ST_YAML_VAL | ST_LINK_SECOND_ARG | ST_LINK_SECOND_ARG_END)) { print_output(output, "%s", state & ST_PRE ? "\n" : " "); - if (state & ST_PRE) - output_colno = 1; - else - output_colno++; + output_firstcol = state & ST_PRE; } if (!keep_token) @@ -3809,7 +3807,7 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) if (!read_yaml_macros_and_links) { print_output(output, "\n"); - output_colno = 1; + output_firstcol = TRUE; } if (link_text)