abouttreesummaryrefslogcommitdiff
diff options
context:
space:
mode:
authorPatrick2025-12-23 23:35:13 +0100
committerPatrick2025-12-23 23:35:13 +0100
commit7dfc2b975fd4a735073536914f5a49b8d80a1104 (patch)
tree69ec9e78cd5ad255ae17435f962952a1e4ebdc2c
parent13ff15345e1fe7ecc72967e1e0d9a473428f241f (diff)
downloadps-cgit-7dfc2b975fd4a735073536914f5a49b8d80a1104.tar.gz
ps-cgit-7dfc2b975fd4a735073536914f5a49b8d80a1104.zip
buncha features
-rw-r--r--cgithub/head-include.html10
-rw-r--r--ui-blob.c1
-rw-r--r--ui-summary.c63
-rw-r--r--ui-summary.h2
-rw-r--r--ui-tree.c25
5 files changed, 77 insertions, 24 deletions
diff --git a/cgithub/head-include.html b/cgithub/head-include.html
index 204e078..df6e059 100644
--- a/cgithub/head-include.html
+++ b/cgithub/head-include.html
@@ -3,6 +3,16 @@
<link rel="stylesheet" type="text/css" href="/git/static/cgithub/cgithub/github-markdown-dark.css"/>
<link rel="stylesheet" type="text/css" href="/git/static/cgithub/cgithub/cgithub.css"/>
<link rel="stylesheet" type="text/css" href="/git/static/cgithub/cgithub/prism-one-dark.css"/>
+<style>
+/* https://stackoverflow.com/a/20104746 */
+.tab-folder > .tab-content:target ~ .tab-content:last-child, .tab-folder > .tab-content {
+ display: none;
+}
+.tab-folder > :last-child, .tab-folder > .tab-content:target {
+ display: block;
+}
+
+</style>
<script type="module">
const blob = document.querySelector('div#cgit table.blob td.lines pre code');
diff --git a/ui-blob.c b/ui-blob.c
index cc88d76..3267ed1 100644
--- a/ui-blob.c
+++ b/ui-blob.c
@@ -72,7 +72,6 @@ int cgit_print_oid(const struct object_id *oid)
unsigned long size;
type = oid_object_info(the_repository, oid, &size);
- html_txtf("trying to print %x<br/>", oid);
if (type == OBJ_BAD)
return -1;
buf = read_object_file(oid, &type, &size);
diff --git a/ui-summary.c b/ui-summary.c
index b3e5cc6..bf75039 100644
--- a/ui-summary.c
+++ b/ui-summary.c
@@ -155,42 +155,69 @@ done:
// int stage, const struct pathspec *pathspec,
// read_tree_fn_t fn, void *context);
struct get_readme_oid_ctx {
- const struct object_id *oid;
+ struct object_id *oid;
char **filename;
+ const char *path;
+ bool found;
};
static int get_readme_oid_cb(const struct object_id *oid, struct strbuf *base,
const char *pathname, unsigned mode, int stage, void *cbdata) {
+ struct get_readme_oid_ctx *ctx = (struct get_readme_oid_ctx*)cbdata;
+
// TODO: make readme.md configurable
const char *readme_name = "readme.md";
- html(pathname);
- if (strcmp(pathname, readme_name) == 0) {
- struct get_readme_oid_ctx *ctx = (struct get_readme_oid_ctx*)cbdata;
- ctx->oid = oid;
+
+ struct strbuf buffer = STRBUF_INIT;
+ if (base->len > 0)
+ strbuf_addbuf(&buffer, base);
+
+ bool match_base = false;
+ if (ctx->path == NULL)
+ match_base = base->len == 0;
+ else if (strncmp(buffer.buf, ctx->path, buffer.len > 0 ? buffer.len - 1 : 0) == 0)
+ match_base = true;
+
+ strbuf_addstr(&buffer, pathname);
+
+ bool match_path = false;
+ if (ctx->path == NULL)
+ match_path = false;
+ else if (strcmp(buffer.buf, ctx->path) == 0)
+ match_path = true;
+
+ strbuf_release(&buffer);
+
+ if (match_base && strcmp(pathname, readme_name) == 0) {
+ *(ctx->oid) = *oid;
*(ctx->filename) = xstrdup(pathname);
- // TODO: maybe skip by returning != 0
+ ctx->found = true;
+ }
+ else if (match_path) {
+ return READ_TREE_RECURSIVE;
}
return 0;
}
-const struct object_id * get_readme_oid(const struct tree *tree, char **filename) {
- struct pathspec paths = {
- .nr = 0
- };
- struct get_readme_oid_ctx ctx = { NULL, filename };
- read_tree_recursive(the_repository, tree, "", 0, 1,
- &paths, get_readme_oid_cb, &ctx);
- return ctx.oid;
+bool get_readme_oid(const struct tree *tree, struct pathspec *paths, struct object_id *oid, char **filename) {
+ struct get_readme_oid_ctx gro_ctx = (struct get_readme_oid_ctx){
+ .oid = oid,
+ .filename = filename,
+ .path = (paths->nr == 1 ? paths->items[0].match : NULL),
+ .found = false };
+ read_tree_recursive(the_repository, tree, "", 0, 0,
+ paths, get_readme_oid_cb, &gro_ctx);
+ return gro_ctx.found;
}
-void cgit_print_repo_readme_no_layout(const struct tree *tree)
+void cgit_print_repo_readme_no_layout(const struct tree *tree, struct pathspec *paths)
{
char *filename = NULL;
- const struct object_id *readme_oid = get_readme_oid(tree, &filename);
- if (readme_oid) {
+ struct object_id readme_oid;
+ if (get_readme_oid(tree, paths, &readme_oid, &filename)) {
html("<div id='summary'>");
cgit_open_filter(ctx.repo->about_filter, filename);
- cgit_print_oid(readme_oid);
+ cgit_print_oid(&readme_oid);
cgit_close_filter(ctx.repo->about_filter);
html("</div>");
diff --git a/ui-summary.h b/ui-summary.h
index 03ee5e4..29b6c1c 100644
--- a/ui-summary.h
+++ b/ui-summary.h
@@ -3,6 +3,6 @@
extern void cgit_print_summary(void);
extern void cgit_print_repo_readme(const char *path);
-extern void cgit_print_repo_readme_no_layout(const struct tree *tree);
+extern void cgit_print_repo_readme_no_layout(const struct tree *tree, struct pathspec *paths);
#endif /* UI_SUMMARY_H */
diff --git a/ui-tree.c b/ui-tree.c
index 5e78f64..61049e7 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -23,6 +23,11 @@ static void print_text_buffer(const char *name, char *buf, unsigned long size)
unsigned long lineno, idx;
const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
+ bool is_markdown = (strncmp(".md", &name[strlen(name)-3], 3) == 0);
+ if (is_markdown) {
+ html("<div class=\"tab-folder\"><div id=\"code\" class=\"tab-content\">");
+ }
+
html("<table summary='blob content' class='blob'>\n");
if (ctx.cfg.enable_tree_linenumbers) {
@@ -58,6 +63,16 @@ static void print_text_buffer(const char *name, char *buf, unsigned long size)
html("<td class='lines'><pre><code>");
html_txt(buf);
html("</code></pre></td></tr></table>\n");
+
+ if (is_markdown) {
+ html("</div><div id=\"preview\" class=\"tab-content\">");
+
+ cgit_open_filter(ctx.repo->about_filter, name);
+ html_raw(buf, size);
+ cgit_close_filter(ctx.repo->about_filter);
+
+ html("</div></div>");
+ }
}
#define ROWLEN 32
@@ -109,6 +124,8 @@ static void print_object(const struct object_id *oid, const char *path, const ch
cgit_print_layout_start();
htmlf("blob: %s (", oid_to_hex(oid));
+ html("<a href=\"#preview\">preview</a>) (");
+ html("<a href=\"#code\">code</a>) (");
cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
rev, path);
if (ctx.repo->enable_blame) {
@@ -274,11 +291,11 @@ static void ls_head(void)
html("</tr>\n");
}
-static void ls_tail(const struct tree *tree)
+static void ls_tail(const struct tree *tree, struct pathspec *paths)
{
html("</table>\n");
if (tree)
- cgit_print_repo_readme_no_layout(tree);
+ cgit_print_repo_readme_no_layout(tree, paths);
cgit_print_layout_end();
}
@@ -299,7 +316,7 @@ static void ls_tree(const struct object_id *oid, const char *path, struct walk_t
ls_head();
read_tree_recursive(the_repository, tree, "", 0, 1,
&paths, ls_item, walk_tree_ctx);
- ls_tail(tree);
+ ls_tail(tree, &paths);
}
@@ -383,7 +400,7 @@ void cgit_print_tree(const char *rev, char *path)
"", 0, 0,
&paths, walk_tree, &walk_tree_ctx);
if (walk_tree_ctx.state == 1) {
- ls_tail(tree);
+ ls_tail(tree, &paths);
}
else if (walk_tree_ctx.state == 2) {
cgit_print_layout_end();