--- lbbs/src/article_cache.c 2025/06/01 14:33:50 1.9 +++ lbbs/src/article_cache.c 2025/06/04 13:42:53 1.13 @@ -16,6 +16,7 @@ #include "article_cache.h" #include "log.h" +#include "lml.h" #include #include #include @@ -28,6 +29,7 @@ #include #define ARTICLE_HEADER_MAX_LEN 4096 +#define ARTICLE_CONTENT_MAX_LEN 1024 * 1024 * 4 // 4MB #define ARTICLE_FOOTER_MAX_LEN 4096 #define SUB_DT_MAX_LEN 50 @@ -66,6 +68,7 @@ int article_cache_generate(const char *c char header[ARTICLE_HEADER_MAX_LEN]; size_t header_len; long header_line_cnt; + char content_f[ARTICLE_CONTENT_MAX_LEN]; char footer[ARTICLE_FOOTER_MAX_LEN]; size_t footer_len; long footer_line_cnt; @@ -115,11 +118,26 @@ int article_cache_generate(const char *c header_len = strnlen(header, sizeof(header)); footer_len = strnlen(footer, sizeof(footer)); - cache.data_len = header_len + strlen(content); - header_line_cnt = split_data_lines(header, SCREEN_COLS, cache.line_offsets, MAX_SPLIT_FILE_LINES); + + if (header_len != cache.line_offsets[header_line_cnt]) + { + log_common("Header of article(aid=%d) is truncated from %ld to %ld\n", p_article->aid, header_len, cache.line_offsets[header_line_cnt]); + header_len = (size_t)cache.line_offsets[header_line_cnt]; + } + + // Apply LML render to content body + cache.data_len = header_len + (size_t)lml_plain(content, content_f, ARTICLE_CONTENT_MAX_LEN); + cache.line_total = header_line_cnt + - split_data_lines(content, SCREEN_COLS, cache.line_offsets + header_line_cnt, MAX_SPLIT_FILE_LINES - header_line_cnt); + split_data_lines(content_f, SCREEN_COLS, cache.line_offsets + header_line_cnt, MAX_SPLIT_FILE_LINES - header_line_cnt); + + if (cache.data_len - header_len != (size_t)cache.line_offsets[cache.line_total]) + { + log_common("Body of article(aid=%d) is truncated from %ld to %ld\n", + p_article->aid, cache.data_len - header_len, cache.line_offsets[cache.line_total]); + cache.data_len = header_len + (size_t)(cache.line_offsets[cache.line_total]); + } for (i = header_line_cnt; i <= cache.line_total; i++) { @@ -128,6 +146,13 @@ int article_cache_generate(const char *c footer_line_cnt = split_data_lines(footer, SCREEN_COLS, cache.line_offsets + cache.line_total, MAX_SPLIT_FILE_LINES - cache.line_total); + if (footer_len != cache.line_offsets[cache.line_total + footer_line_cnt]) + { + log_common("Footer of article(aid=%d) is truncated from %ld to %ld\n", + p_article->aid, footer_len, cache.line_offsets[cache.line_total + footer_line_cnt]); + footer_len = (size_t)(cache.line_offsets[cache.line_total + footer_line_cnt]); + } + for (i = 0; i <= footer_line_cnt; i++) { cache.line_offsets[cache.line_total + i] += (long)cache.data_len; @@ -149,7 +174,7 @@ int article_cache_generate(const char *c close(fd); return -3; } - if (write(fd, content, cache.data_len - header_len - footer_len) == -1) + if (write(fd, content_f, cache.data_len - header_len - footer_len) == -1) { log_error("write(%s, content) error (%d)\n", data_file, errno); close(fd);