From 767e263512911e24f825eb14be87fc3b9055c4ab Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 27 Sep 2026 22:04:58 +0300 Subject: ui-tree: Print READMEs below file listings --- ui-summary.c | 41 +++++++++++++++++++++++++++++++++++++++++ ui-summary.h | 1 + ui-tree.c | 15 +++++++++++++-- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/ui-summary.c b/ui-summary.c index 947812a..77f4c48 100644 --- a/ui-summary.c +++ b/ui-summary.c @@ -146,3 +146,44 @@ void cgit_print_repo_readme(const char *path) done: cgit_print_layout_end(); } + +void cgit_print_repo_readme_component(const char *path) +{ + char *filename, *ref, *mimetype; + int free_filename = 0; + + mimetype = get_mimetype_for_filename(path); + if (mimetype && (!strncmp(mimetype, "image/", 6) || !strncmp(mimetype, "video/", 6))) { + free(mimetype); + return; + } + free(mimetype); + + if (ctx.repo->readme.nr == 0) + return; + + filename = ctx.repo->readme.items[0].string; + ref = ctx.repo->readme.items[0].util; + + if (path) { + free_filename = 1; + filename = fmtalloc("%s/%s", path, filename); + if (!filename) + return; + } + + /* Print the calculated readme, either from the git repo or from the + * filesystem, while applying the about-filter. + */ + html("
"); + cgit_open_filter(ctx.repo->about_filter, filename); + if (ref) + cgit_print_file(filename, ref, 1); + else + html_include(filename); + cgit_close_filter(ctx.repo->about_filter); + + html("
"); + if (free_filename) + free(filename); +} diff --git a/ui-summary.h b/ui-summary.h index cba696a..51d8ec0 100644 --- a/ui-summary.h +++ b/ui-summary.h @@ -3,5 +3,6 @@ extern void cgit_print_summary(void); extern void cgit_print_repo_readme(const char *path); +extern void cgit_print_repo_readme_component(const char *path); #endif /* UI_SUMMARY_H */ diff --git a/ui-tree.c b/ui-tree.c index 11e5494..b85e8d6 100644 --- a/ui-tree.c +++ b/ui-tree.c @@ -12,6 +12,8 @@ #include "ui-tree.h" #include "html.h" #include "ui-shared.h" +#include "ui-summary.h" +#include "ui-plain.h" struct walk_tree_context { char *curr_rev; @@ -91,7 +93,7 @@ static void print_binary_buffer(char *buf, unsigned long size) static void print_object(const struct object_id *oid, const char *path, const char *basename, const char *rev) { enum object_type type; - char *buf; + char *buf, *accept = NULL; unsigned long size; bool is_binary; @@ -112,6 +114,12 @@ static void print_object(const struct object_id *oid, const char *path, const ch cgit_set_title_from_path(path); + accept = getenv("HTTP_ACCEPT"); + if (accept && (!strncmp(accept, "image/", 6) || !strncmp(accept, "video/", 6))) { + cgit_print_plain(); + return; + } + cgit_print_layout_start(); htmlf("blob: %s (", oid_to_hex(oid)); cgit_plain_link("plain", NULL, NULL, ctx.qry.head, @@ -311,6 +319,7 @@ static void ls_head(void) static void ls_tail(void) { html("\n"); + cgit_print_repo_readme_component(ctx.qry.path); cgit_print_layout_end(); } @@ -413,8 +422,10 @@ void cgit_print_tree(const char *rev, char *path) &paths, walk_tree, &walk_tree_ctx); if (walk_tree_ctx.state == 1) ls_tail(); - else if (walk_tree_ctx.state == 2) + else if (walk_tree_ctx.state == 2) { + cgit_print_repo_readme_component(ctx.qry.path); cgit_print_layout_end(); + } else cgit_print_error_page(404, "Not found", "Path not found"); -- cgit v1.3.1-23-gd82d