tools: fix CDoc ordering of types

Fixes CDoc ordering the sidebar and main page types in alphabetical
order. Only the sidebar should be sorted and the main page should
be rendered the way its header file was parsed, top to bottom.

Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
Ronald Caesar
2026-01-17 02:06:36 -04:00
parent 4e41957c21
commit 3cf17ac088
2 changed files with 35 additions and 16 deletions

View File

@@ -90,7 +90,7 @@ BAL_ALIGNED(64) typedef struct
///
/// # Errors
///
/// Returns [`BAL_ERROR_INVALID_ARGUMENT` if the pointers are `NULL`.
/// Returns [`BAL_ERROR_INVALID_ARGUMENT`] if the pointers are `NULL`.
///
/// Returns [`BAL_ERROR_ALLOCATION_FAILED`] if the allocator cannot fulfill the
/// request.

View File

@@ -589,24 +589,47 @@ void write_common_head(FILE* f, const char* title) {
fprintf(f, "</style></head><body>");
}
int compare_item_ptrs(const void* a, const void* b) {
const DocItem* da = *(const DocItem**)a;
const DocItem* db = *(const DocItem**)b;
if (!da->name && !db->name) return 0;
if (!da->name) return 1;
if (!db->name) return -1;
return strcmp(da->name, db->name);
}
void render_sidebar_section(FILE* f, FileContext* ctx, ItemKind kind, const char* title) {
int found = 0;
// Check if any items of this kind exist
size_t count = 0;
// Count items of this kind
for(size_t i=0; i<ctx->count; i++) {
if (ctx->items[i].kind == kind) {
found = 1;
break;
count++;
}
}
// If found, print the header and the links
if (found) {
fprintf(f, "<h3>%s</h3>", title);
for(size_t i=0; i<ctx->count; i++) {
if (ctx->items[i].kind == kind) {
fprintf(f, "<a href='#%s'>%s</a>", ctx->items[i].anchor_id, ctx->items[i].name);
}
if (count == 0) return;
// Create a temporary array of pointers
DocItem** ptrs = malloc(sizeof(DocItem*) * count);
size_t idx = 0;
for(size_t i=0; i<ctx->count; i++) {
if (ctx->items[i].kind == kind) {
ptrs[idx++] = &ctx->items[i];
}
}
// Sort the pointers alphabetically for the sidebar
qsort(ptrs, count, sizeof(DocItem*), compare_item_ptrs);
// Render sorted links
fprintf(f, "<h3>%s</h3>", title);
for(size_t i=0; i<count; i++) {
fprintf(f, "<a href='#%s'>%s</a>", ptrs[i]->anchor_id, ptrs[i]->name);
}
free(ptrs);
}
void generate_file_html(ProjectContext* proj, FileContext* ctx, const char* out_dir) {
@@ -860,10 +883,6 @@ int main(int argc, char** argv) {
clang_disposeTranslationUnit(unit);
}
for (size_t i = 0; i < proj.count; i++) {
qsort(proj.files[i].items, proj.files[i].count, sizeof(DocItem), compare_items);
}
printf("Generating HTML in '%s'...\n", out_dir);
#ifdef _WIN32