summaryrefslogtreecommitdiff
path: root/ui-wiki.c
blob: 9fdb4b9f5582a0f5111ac4716842745255553557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* ui-wiki.c: functions for wiki output
 *
 * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
 * Copyright (C) 2025 Freya Murphy <freya@freyacat.org>
 *
 * Licensed under GNU General Public License v2
 *   (see COPYING for full license text)
 */

 #define USE_THE_REPOSITORY_VARIABLE

#include "cgit.h"
#include "ui-wiki.h"
#include "html.h"
#include "ui-shared.h"

 #define WIKI_PATH ".wiki"

struct walk_wiki_context {
	char *curr_rev;
	int state;
	int index;
};

static void print_page(const struct object_id *oid, const char *path, const char *basename, const char *rev)
{
	enum object_type type;
	unsigned long size;
	char *buf;

	type = oid_object_info(the_repository, oid, &size);
	if (type == OBJ_BAD) {
		cgit_print_error_page(404, "Not found",
			"Bad object name: %s", oid_to_hex(oid));
		return;
	}

	buf = repo_read_object_file(the_repository, oid, &type, &size);
	if (!buf) {
		cgit_print_error_page(500, "Internal server error",
			"Error reading object %s", oid_to_hex(oid));
		return;
	}

	html("<div id='summary'>");
	cgit_open_filter(ctx.repo->about_filter, basename);
	html_raw(buf, size);
	cgit_close_filter(ctx.repo->about_filter);
	html("</div>");

	free(buf);
}

static int ls_wiki(const struct object_id *oid, const char *pathname, struct walk_wiki_context *walk_wiki_ctx)
{
	char *name, *link, *ptr;

	if (isdigit(*pathname)) {
		long index = strtol(pathname, &ptr, 10);
		if (ptr && *ptr == '-') {
			pathname = ptr + 1;
			walk_wiki_ctx->index = index;
		}
	}

	name = xstrdup(pathname);
	ptr = strrchr(name, '.');
	if (ptr)
		*ptr = '\0';
	link = xstrdup(name);
	for (ptr = name; *ptr; ptr++) {
		if (*ptr == '-')
			*ptr = ' ';
	}
	for (ptr = link; *ptr; ptr++)
		*ptr = tolower(*ptr);

	html("<tr><td class='ls-index'>");
	htmlf("%d", walk_wiki_ctx->index);
	html("</td><td class>");
	cgit_wiki_link(name, NULL, "ls-link", ctx.qry.head, walk_wiki_ctx->curr_rev, link);
	html("</td></tr>\n");

	walk_wiki_ctx->index++;
	free(name);
	free(link);
	return 0;
}

static void ls_head(void)
{
	cgit_print_layout_start();
	html("<table summary='wiki listing' class='list'>\n");
	html("<tr class='nohover'>");
	html("<th class='left'>Index</th>");
	html("<th class='left'>Title</th>");
	html("<th/>");
	html("</tr>\n");
}

static void ls_tail(void)
{
	html("</table>\n");
}

static int walk_wiki(const struct object_id *oid, struct strbuf *base,
		const char *pathname, unsigned mode, void *cbdata)
{
	struct walk_wiki_context *walk_wiki_ctx = cbdata;

	if (!strcmp(pathname, WIKI_PATH))
		return READ_TREE_RECURSIVE;

	if (walk_wiki_ctx->state < 2) {
		if (walk_wiki_ctx->state == 0) {
			walk_wiki_ctx->state = 1;
			ls_head();
		}

		ls_wiki(oid, pathname, walk_wiki_ctx);
	} else if (walk_wiki_ctx->state == 2) {
		const char *name;

		name = pathname;
		if (isdigit(*name)) {
			char *ptr;
			strtol(name, &ptr, 10);
			if (ptr && *ptr == '-')
				name = ptr + 1;
		}

		if (!strncasecmp(name, ctx.qry.path, strlen(ctx.qry.path))) {
			struct strbuf buffer = STRBUF_INIT;

			strbuf_addbuf(&buffer, base);
			strbuf_addstr(&buffer, pathname);

			walk_wiki_ctx->state = 3;
			print_page(oid, pathname, buffer.buf, walk_wiki_ctx->curr_rev);
			strbuf_release(&buffer);
		}
	}

	return 0;
}

/*
 * Show repo wiki
 *   rev:  the commit pointing at the root ui object
 *   path: partial name of a wiki md file
 */
void cgit_print_wiki(const char *rev)
{
	struct object_id oid;
	struct commit *commit;
	struct pathspec_item path_items = {
		.match = WIKI_PATH,
		.len = sizeof(WIKI_PATH) - 1,
	};
	struct pathspec paths = {
		.nr = 1,
		.items = &path_items
	};
	struct walk_wiki_context walk_wiki_ctx = {
		.state = 0,
		.index = 1
	};

	if (!rev)
		rev = ctx.qry.head;

	if (repo_get_oid(the_repository, rev, &oid)) {
		cgit_print_error_page(404, "Not found",
			"Invalid revision name: %s", rev);
		return;
	}
	commit = lookup_commit_reference(the_repository, &oid);
	if (!commit || repo_parse_commit(the_repository, commit)) {
		cgit_print_error_page(404, "Not found",
			"Invalid commit reference: %s", rev);
		return;
	}

	walk_wiki_ctx.curr_rev = xstrdup(rev);

	// print wiki pages
	read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
		  &paths, walk_wiki, &walk_wiki_ctx);

	if (walk_wiki_ctx.state == 0) {
		cgit_print_error_page(404, "Not found", "Wiki not found");
		goto cleanup;
	}

	ls_tail();

	// print active wiki page
	if (ctx.qry.path) {
		html("<hr>\n");

		walk_wiki_ctx.state = 2;
		read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
			  &paths, walk_wiki, &walk_wiki_ctx);

		if (walk_wiki_ctx.state == 2)
			cgit_print_error("Wiki page not found");
	}

	cgit_print_layout_end();

cleanup:
	free(walk_wiki_ctx.curr_rev);
}