commit edbf7df6b153c9faeeb31be589b823672bafbea1
parent e76cddbbded4ce6cb5e6a6cc3f6ae1d59a5b09ab
Author: Chris Bracken <chris@bracken.jp>
Date: Fri, 10 May 2024 20:34:10 +0900
gitout_index: Add -m option to specify rel=me URL
Mastodon verification request either an <a> or <link> element with a
rel="me" attribute whose href attribute points back to a mastodon
profile.
This adds support for this to the HTML renderer. At present, no Gopher
implementation has been added.
Diffstat:
7 files changed, 44 insertions(+), 7 deletions(-)
diff --git a/gitout_index.c b/gitout_index.c
@@ -14,6 +14,7 @@
struct GitoutIndexOptions {
const char** repo_dirs;
size_t repo_dir_count;
+ const char* me_url;
IndexWriterType writer_type;
};
@@ -21,6 +22,7 @@ GitoutIndexOptions* gitout_index_options_create(int argc, const char* argv[]) {
GitoutIndexOptions options = {
.repo_dirs = NULL,
.repo_dir_count = 0,
+ .me_url = NULL,
.writer_type = kIndexWriterTypeHtml,
};
for (int i = 1; i < argc; i++) {
@@ -31,6 +33,12 @@ GitoutIndexOptions* gitout_index_options_create(int argc, const char* argv[]) {
err(1, "reallocarray");
}
options.repo_dirs[options.repo_dir_count++] = argv[i];
+ } else if (argv[i][1] == 'm') {
+ // Requires 'me' URL argument.
+ if (i + 1 >= argc) {
+ return NULL;
+ }
+ options.me_url = argv[++i];
} else if (argv[i][1] == 'H') {
options.writer_type = kIndexWriterTypeHtml;
} else if (argv[i][1] == 'G') {
@@ -67,6 +75,9 @@ void gitout_index_init(const GitoutIndexOptions* options) {
void gitout_index_run(const GitoutIndexOptions* options) {
IndexWriter* writer = indexwriter_create(options->writer_type);
+ if (options->me_url) {
+ indexwriter_set_me_url(writer, options->me_url);
+ }
indexwriter_begin(writer);
for (size_t i = 0; i < options->repo_dir_count; i++) {
RepoInfo* ri = repoinfo_create(options->repo_dirs[i]);
diff --git a/writer/html/index_writer.c b/writer/html/index_writer.c
@@ -27,6 +27,11 @@ void html_indexwriter_free(HtmlIndexWriter* writer) {
free(writer);
}
+void html_indexwriter_set_me_url(void* writer, const char* url) {
+ HtmlIndexWriter* html_writer = (HtmlIndexWriter*)writer;
+ html_repoindex_set_me_url(html_writer->index_, url);
+}
+
void html_indexwriter_begin(void* writer) {
HtmlIndexWriter* html_writer = (HtmlIndexWriter*)writer;
html_repoindex_begin(html_writer->index_);
diff --git a/writer/html/index_writer.h b/writer/html/index_writer.h
@@ -7,6 +7,7 @@ typedef struct HtmlIndexWriter HtmlIndexWriter;
HtmlIndexWriter* html_indexwriter_create();
void html_indexwriter_free(HtmlIndexWriter* writer);
+void html_indexwriter_set_me_url(void* writer, const char* url);
void html_indexwriter_begin(void* writer);
void html_indexwriter_add_repo(void* writer, RepoInfo* ri);
void html_indexwriter_end(void* writer);
diff --git a/writer/html/repo_index.c b/writer/html/repo_index.c
@@ -8,11 +8,13 @@
struct HtmlRepoIndex {
FILE* out;
+ const char* me_url;
};
HtmlRepoIndex* html_repoindex_create(FILE* out) {
HtmlRepoIndex* index = ecalloc(1, sizeof(HtmlRepoIndex));
index->out = out;
+ index->me_url = NULL;
return index;
}
@@ -25,6 +27,10 @@ void html_repoindex_free(HtmlRepoIndex* index) {
free(index);
}
+void html_repoindex_set_me_url(HtmlRepoIndex* index, const char* url) {
+ index->me_url = url;
+}
+
void html_repoindex_begin(HtmlRepoIndex* index) {
FILE* out = index->out;
fprintf(
@@ -38,7 +44,12 @@ void html_repoindex_begin(HtmlRepoIndex* index) {
"name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n"
"<title>Repositories</title>\n"
"<link rel=\"icon\" type=\"image/png\" href=\"favicon.png\" />\n"
- "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n"
+ "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n");
+ if (index->me_url) {
+ fprintf(out, "<link rel=\"me\" href=\"%s\" />\n", index->me_url);
+ }
+ fprintf(
+ out,
"</head>\n"
"<body>\n"
"<table>\n<tr><td>"
diff --git a/writer/html/repo_index.h b/writer/html/repo_index.h
@@ -9,6 +9,7 @@ typedef struct HtmlRepoIndex HtmlRepoIndex;
HtmlRepoIndex* html_repoindex_create(FILE* out);
void html_repoindex_free(HtmlRepoIndex* index);
+void html_repoindex_set_me_url(HtmlRepoIndex* index, const char* url);
void html_repoindex_begin(HtmlRepoIndex* index);
void html_repoindex_add_repo(HtmlRepoIndex* index, RepoInfo* ri);
void html_repoindex_end(HtmlRepoIndex* index);
diff --git a/writer/index_writer.c b/writer/index_writer.c
@@ -7,9 +7,9 @@
#include "writer/gopher/index_writer.h"
#include "writer/html/index_writer.h"
-typedef void (*IndexWriterBegin)(void* impl);
-typedef void (*IndexWriterAddRepo)(void* impl, RepoInfo* ri);
-typedef void (*IndexWriterEnd)(void* impl);
+typedef void (*IndexWriterVoidFunc)(void* impl);
+typedef void (*IndexWriterStringFunc)(void* impl, const char* url);
+typedef void (*IndexWriterRepoInfoFunc)(void* impl, RepoInfo* ri);
struct IndexWriter {
/* Writer implementation. */
@@ -17,9 +17,10 @@ struct IndexWriter {
void* impl;
/* Writer operations. */
- IndexWriterBegin begin;
- IndexWriterAddRepo add_repo;
- IndexWriterEnd end;
+ IndexWriterStringFunc set_me_url;
+ IndexWriterVoidFunc begin;
+ IndexWriterRepoInfoFunc add_repo;
+ IndexWriterVoidFunc end;
};
static IndexWriter* htmlindexwriter_create();
@@ -53,6 +54,10 @@ void indexwriter_free(IndexWriter* writer) {
errx(1, "unknown IndexWriterType %d", writer->type);
}
+void indexwriter_set_me_url(IndexWriter* writer, const char* url) {
+ writer->set_me_url(writer->impl, url);
+}
+
void indexwriter_begin(IndexWriter* writer) {
writer->begin(writer->impl);
}
@@ -72,6 +77,7 @@ IndexWriter* htmlindexwriter_create() {
HtmlIndexWriter* html_writer = html_indexwriter_create();
writer->type = kIndexWriterTypeHtml;
writer->impl = html_writer;
+ writer->set_me_url = html_indexwriter_set_me_url;
writer->begin = html_indexwriter_begin;
writer->add_repo = html_indexwriter_add_repo;
writer->end = html_indexwriter_end;
@@ -94,6 +100,7 @@ IndexWriter* gopherindexwriter_create() {
GopherIndexWriter* gopher_writer = gopher_indexwriter_create();
writer->type = kIndexWriterTypeGopher;
writer->impl = gopher_writer;
+ writer->set_me_url = NULL;
writer->begin = gopher_indexwriter_begin;
writer->add_repo = gopher_indexwriter_add_repo;
writer->end = gopher_indexwriter_end;
diff --git a/writer/index_writer.h b/writer/index_writer.h
@@ -12,6 +12,7 @@ typedef struct IndexWriter IndexWriter;
IndexWriter* indexwriter_create(IndexWriterType type);
void indexwriter_free(IndexWriter* writer);
+void indexwriter_set_me_url(IndexWriter* writer, const char* url);
void indexwriter_begin(IndexWriter* writer);
void indexwriter_add_repo(IndexWriter* writer, RepoInfo* ri);
void indexwriter_end(IndexWriter* writer);