[PATCH v2 15/15] render: adapt for providing extra filter args for plain

Andy Green andy at warmcat.com
Mon Jun 18 04:58:31 CEST 2018

This changes the render filter exec part to provide a second
and third argument, which are used by md2html to fix up the url
path for "plain" for the repo, eg, "/cgit/plain/" and
"?h=mybranch", as required by the modifications to md2html in
the previous patches.

The combination means cgit becomes able to serve assets using
markdown urls starting from the repo root dir, without mentioning
any virtual url part specific to a cgit or other web rendering
instance, while respecting the version context.

Eg, continuing the example of the arguments being
"/cgit/plain/" and "?h=mybranch" from above, if the markdown has


the img src will be fixed up to


If the same document is viewed from a different rev in cgit, the
processed markdown url will change to match the cgit context, even
though the markdown relative URL is the same for all versions.

Signed-off-by: Andy Green <andy at warmcat.com>
 filter.c  |    5 ++++-
 ui-tree.c |   13 +++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/filter.c b/filter.c
index 4ae4aaa..7c1f188 100644
--- a/filter.c
+++ b/filter.c
@@ -424,6 +424,10 @@ struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype)
 			argument_count = 12;
+		case RENDER:
+			argument_count = 3;
+			break;
 		case EMAIL:
 			argument_count = 2;
@@ -434,7 +438,6 @@ struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype)
 		case SOURCE:
 		case ABOUT:
-		case RENDER:
 			argument_count = 1;
diff --git a/ui-tree.c b/ui-tree.c
index 9f59d18..5bbe30d 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -102,16 +102,25 @@ static void print_buffer(const char *basename, char *buf, unsigned long size)
 static void render_buffer(struct cgit_filter *render, const char *name,
-		char *buf, unsigned long size)
+			  char *buf, unsigned long size)
 	char *filter_arg = xstrdup(name);
+	char *repo_url = cgit_repourl(ctx.repo->url);
+	struct strbuf sb_plain = STRBUF_INIT, sb_postfix = STRBUF_INIT;
+	strbuf_addf(&sb_plain, "%splain/", repo_url);
+	if (ctx.qry.head)
+		strbuf_addf(&sb_postfix, "?h=%s", ctx.qry.head);
+	free(repo_url);
 	html("<div class='blob'>");
-	cgit_open_filter(render, filter_arg);
+	cgit_open_filter(render, filter_arg, sb_plain.buf, sb_postfix.buf);
 	html_raw(buf, size);
+	strbuf_release(&sb_plain);
+	strbuf_release(&sb_postfix);

More information about the CGit mailing list