History

Extract clone URL printing to ui-shared.c

This will allow us to reuse the same logic to add clone URL <link/> elements to the header of all repo-specific pages in order to support the rel-vcs microformat.

John Keeping committed 12 years ago

Commit bbfa0061 parent 485b0993 files · +51 −46Browse files

Unified · Split

3 files
  1. ui-shared.c +37 −0
  2. ui-shared.h +2 −0
  3. ui-summary.c +12 −46
ui-shared.c+37−0
@@ -727,6 +727,43 @@ void cgit_print_docend() · 726 unmodified lines
727727 html("</body>\n</html>\n");
728728 }
729729
730+static void add_clone_urls(void (*fn)(const char *), char *txt, char *suffix)
731+{
732+ struct strbuf buf = STRBUF_INIT;
733+ char *h = txt, *t, c;
734+
735+ while (h && *h) {
736+ while (h && *h == ' ')
737+ h++;
738+ if (!*h)
739+ break;
740+ t = h;
741+ while (t && *t && *t != ' ')
742+ t++;
743+ c = *t;
744+ *t = 0;
745+
746+ if (suffix && *suffix) {
747+ strbuf_reset(&buf);
748+ strbuf_addf(&buf, "%s/%s", h, suffix);
749+ h = buf.buf;
750+ }
751+ fn(h);
752+ *t = c;
753+ h = t;
754+ }
755+
756+ strbuf_release(&buf);
757+}
758+
759+void cgit_add_clone_urls(void (*fn)(const char *))
760+{
761+ if (ctx.repo->clone_url)
762+ add_clone_urls(fn, expand_macros(ctx.repo->clone_url), NULL);
763+ else if (ctx.cfg.clone_prefix)
764+ add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url);
765+}
766+
730767 static int print_branch_option(const char *refname, const unsigned char *sha1,
731768 int flags, void *cb_data)
732769 {
ui-shared.h+2−0
@@ -11,6 +11,8 @@ extern char *cgit_fileurl(const char *reponame, const char *pagename, · 10 unmodified lines
1111 extern char *cgit_pageurl(const char *reponame, const char *pagename,
1212 const char *query);
1313
14+extern void cgit_add_clone_urls(void (*fn)(const char *));
15+
1416 extern void cgit_index_link(const char *name, const char *title,
1517 const char *class, const char *pattern, const char *sort, int ofs);
1618 extern void cgit_summary_link(const char *name, const char *title,
ui-summary.c+12−46
@@ -12,62 +12,30 @@ · 11 unmodified lines
1212 #include "ui-log.h"
1313 #include "ui-refs.h"
1414 #include "ui-blob.h"
15+#include "ui-shared.h"
1516 #include <libgen.h>
1617
17−static void print_url(char *base, char *suffix)
18+static int urls;
19+
20+static void print_url(const char *url)
1821 {
1922 int columns = 3;
20− struct strbuf basebuf = STRBUF_INIT;
2123
2224 if (ctx.repo->enable_log_filecount)
2325 columns++;
2426 if (ctx.repo->enable_log_linecount)
2527 columns++;
2628
27− if (!base || !*base)
28− return;
29− if (suffix && *suffix) {
30− strbuf_addf(&basebuf, "%s/%s", base, suffix);
31− base = basebuf.buf;
29+ if (urls++ == 0) {
30+ htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
31+ htmlf("<tr><th class='left' colspan='%d'>Clone</th></tr>\n", columns);
3232 }
33+
3334 htmlf("<tr><td colspan='%d'><a href='", columns);
34− html_url_path(base);
35+ html_url_path(url);
3536 html("'>");
36− html_txt(base);
37+ html_txt(url);
3738 html("</a></td></tr>\n");
38− strbuf_release(&basebuf);
39−}
40−
41−static void print_urls(char *txt, char *suffix)
42−{
43− char *h = txt, *t, c;
44− int urls = 0;
45− int columns = 3;
46−
47− if (ctx.repo->enable_log_filecount)
48− columns++;
49− if (ctx.repo->enable_log_linecount)
50− columns++;
51−
52−
53− while (h && *h) {
54− while (h && *h == ' ')
55− h++;
56− if (!*h)
57− break;
58− t = h;
59− while (t && *t && *t != ' ')
60− t++;
61− c = *t;
62− *t = 0;
63− if (urls++ == 0) {
64− htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
65− htmlf("<tr><th class='left' colspan='%d'>Clone</th></tr>\n", columns);
66− }
67− print_url(h, suffix);
68− *t = c;
69− h = t;
70− }
7139 }
7240
7341 void cgit_print_summary()
@@ -88,10 +56,8 @@ void cgit_print_summary() · 14 unmodified lines
8856 cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL,
8957 NULL, NULL, 0, 0, 0);
9058 }
91− if (ctx.repo->clone_url)
92− print_urls(expand_macros(ctx.repo->clone_url), NULL);
93− else if (ctx.cfg.clone_prefix)
94− print_urls(ctx.cfg.clone_prefix, ctx.repo->url);
59+ urls = 0;
60+ cgit_add_clone_urls(print_url);
9561 html("</table>");
9662 }
9763

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t