/* * blogc: A blog compiler. * Copyright (C) 2014-2020 Rafael G. Martins * * This program can be distributed under the terms of the BSD License. * See the file LICENSE. */ #include #include "../common/utils.h" #include "toctree.h" bc_slist_t* blogc_toctree_append(bc_slist_t *headers, size_t level, const char *slug, const char *text) { if (level == 0) return headers; blogc_toctree_header_t *t = bc_malloc(sizeof(blogc_toctree_header_t)); t->level = level; t->slug = bc_strdup(slug); t->text = bc_strdup(text); return bc_slist_append(headers, t); } char* blogc_toctree_render(bc_slist_t *headers, int maxdepth, const char *endl) { if (headers == NULL || maxdepth == 0) return NULL; // find lower level size_t lower_level = 0; for (bc_slist_t *l = headers; l != NULL; l = l->next) { size_t lv = ((blogc_toctree_header_t*) l->data)->level; if (lower_level == 0 || lower_level > lv) { lower_level = lv; } } if (lower_level == 0) return NULL; // render bc_string_t *rv = bc_string_new(); bc_string_append_printf(rv, "%s", spacing, "", endl == NULL ? "\n" : endl); current_level--; } while (current_level < t->level) { bc_string_append_printf(rv, "%*s%s", spacing, "", endl == NULL ? "\n" : endl); current_level--; } return bc_string_free(rv, false); } static void free_header(blogc_toctree_header_t *h) { free(h->slug); free(h->text); free(h); } void blogc_toctree_free(bc_slist_t *l) { bc_slist_free_full(l, (bc_free_func_t) free_header); }