Skip to content

Commit f825fc8

Browse files
committed
Add help links to rendered indexes
1 parent a11ac90 commit f825fc8

2 files changed

Lines changed: 77 additions & 18 deletions

File tree

src/mux/help/help_render.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ static void help_text_buffer_append_quoted(HelpTextBuffer *buffer,
9898
}
9999
}
100100

101+
static void help_text_buffer_append_help_link(HelpTextBuffer *buffer,
102+
const char *topic) {
103+
help_text_buffer_append_str(buffer, "[send=\"help ");
104+
help_text_buffer_append_quoted(buffer, topic);
105+
help_text_buffer_append_str(buffer, "\"]");
106+
help_text_buffer_append_code(buffer, topic);
107+
help_text_buffer_append_str(buffer, "[/]");
108+
}
109+
101110
static void help_render_ensure_blank_line(HelpTextBuffer *buffer) {
102111
if (buffer->length == 0)
103112
return;
@@ -170,10 +179,12 @@ static void help_render_index_section(const HelpIndex *index,
170179
help_render_ensure_blank_line(out);
171180
if (index_article->index_style == HELP_INDEX_STYLE_COLUMNAR) {
172181
for (i = 0; i < count; i++) {
173-
char column[32];
182+
const char *topic = entries[i]->keywords.items[0];
183+
size_t topic_length = strlen(topic);
174184

175-
snprintf(column, sizeof(column), "%-20s", entries[i]->keywords.items[0]);
176-
help_text_buffer_append_str(out, column);
185+
help_text_buffer_append_help_link(out, topic);
186+
for (size_t padding = topic_length; padding < 20; padding++)
187+
help_text_buffer_append_str(out, " ");
177188
if ((i + 1) % 3 == 0)
178189
help_text_buffer_append_str(out, "\n");
179190
}
@@ -187,11 +198,15 @@ static void help_render_index_section(const HelpIndex *index,
187198
help_text_buffer_append_str(out, header);
188199
}
189200
for (i = 0; i < count; i++) {
190-
char line[256];
201+
const char *topic = entries[i]->keywords.items[0];
202+
size_t topic_length = strlen(topic);
191203

192-
snprintf(line, sizeof(line), "%-20s %s\n", entries[i]->keywords.items[0],
193-
entries[i]->description);
194-
help_text_buffer_append_str(out, line);
204+
help_text_buffer_append_help_link(out, topic);
205+
for (size_t padding = topic_length; padding < 20; padding++)
206+
help_text_buffer_append_str(out, " ");
207+
help_text_buffer_append_str(out, " ");
208+
help_text_buffer_append_str(out, entries[i]->description);
209+
help_text_buffer_append_str(out, "\n");
195210
}
196211
}
197212
free(entries);

tests/help_render.c

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
/* help_render.c -- Plain-text markdown rendering unit test */
22

3+
#include <stdlib.h>
34
#include <string.h>
45

56
#include "mux/help/help_index.h"
67
#include "mux/help/help_render.h"
78
#include "mux/help/help_types.h"
89
#include "mux/server/game.h"
910

10-
/*
11-
* help_render_markdown() never calls these (the render test never invokes
12-
* help_article_render_body or help_render_send), but the object file
13-
* references them; stub them out so this test can link help_render.c
14-
* without pulling in help_index.c and its server-wide dependencies.
15-
*/
11+
static const HelpArticle *test_articles;
12+
static size_t test_article_count;
13+
1614
char *help_index_read_body(const HelpIndex *index, const HelpArticle *article,
1715
size_t *out_length) {
1816
(void)index;
1917
(void)article;
20-
(void)out_length;
21-
return nullptr;
18+
char *body = malloc(1);
19+
20+
body[0] = '\0';
21+
if (out_length)
22+
*out_length = 0;
23+
return body;
2224
}
2325

2426
size_t help_index_article_count(const HelpIndex *index) {
2527
(void)index;
26-
return 0;
28+
return test_article_count;
2729
}
2830

2931
const HelpArticle *help_index_article_at(const HelpIndex *index,
3032
size_t article_index) {
3133
(void)index;
32-
(void)article_index;
33-
return nullptr;
34+
return &test_articles[article_index];
3435
}
3536

3637
void notify_checked(EvaluationContext *evaluation, DbRef target, DbRef sender,
@@ -53,6 +54,47 @@ static int help_render_test_expect(const char *markdown, const char *expected) {
5354
return ok;
5455
}
5556

57+
static int help_render_test_index_links(void) {
58+
char *index_tags[] = {"index-entry"};
59+
char *index_keywords[] = {"index"};
60+
char *topic_tags[] = {"index-entry"};
61+
char *topic_keywords[] = {"topic name"};
62+
HelpArticle articles[] = {
63+
{
64+
.keywords = {.items = index_keywords, .count = 1},
65+
.show_index_for_article_tags = {.items = index_tags, .count = 1},
66+
},
67+
{
68+
.description = "A linked help topic.",
69+
.keywords = {.items = topic_keywords, .count = 1},
70+
.article_tags = {.items = topic_tags, .count = 1},
71+
},
72+
};
73+
HelpTextBuffer buffer;
74+
int ok;
75+
76+
test_articles = articles;
77+
test_article_count = sizeof(articles) / sizeof(articles[0]);
78+
help_text_buffer_init(&buffer);
79+
help_article_render_body(nullptr, &articles[0], false, &buffer);
80+
ok = buffer.data != nullptr &&
81+
!strcmp(buffer.data,
82+
"TOPIC DESCRIPTION\n"
83+
"[send=\"help topic name\"]topic name[/] "
84+
"A linked help topic.\n");
85+
help_text_buffer_free(&buffer);
86+
articles[0].index_style = HELP_INDEX_STYLE_COLUMNAR;
87+
help_text_buffer_init(&buffer);
88+
help_article_render_body(nullptr, &articles[0], false, &buffer);
89+
ok = ok && buffer.data != nullptr &&
90+
!strcmp(buffer.data,
91+
"[send=\"help topic name\"]topic name[/] \n");
92+
help_text_buffer_free(&buffer);
93+
test_articles = nullptr;
94+
test_article_count = 0;
95+
return ok;
96+
}
97+
5698
int main(void) {
5799
if (!help_render_test_expect("# Header 1\n\nContent here\n\n## Header 2\n",
58100
"# Header 1\n\nContent here\n\n## Header 2"))
@@ -76,5 +118,7 @@ int main(void) {
76118
"```\n",
77119
"@name drone=[[fg=bright-cyan]Aegis[[/]\n"))
78120
return 6;
121+
if (!help_render_test_index_links())
122+
return 8;
79123
return 0;
80124
}

0 commit comments

Comments
 (0)