To write the Contents element (C) in the index.

§1. This is a hierarchical contents page.

void ContentsElement::render(OUTPUT_STREAM, index_session *session) {
    localisation_dictionary *LD = Indexing::get_localisation(session);
    inter_tree *I = Indexing::get_tree(session);
    tree_inventory *inv = Indexing::get_inventory(session);
    InterNodeList::array_sort(inv->extension_nodes, MakeSynopticModuleStage::category_order);
    InterNodeList::array_sort(inv->heading_nodes, MakeSynopticModuleStage::module_order);

    Write a sort of half-title page1.1;
    Index the headings1.2;
    Index the extensions1.3;
}

§1.1. Write a sort of half-title page1.1 =

    HTML_OPEN("p");
    WRITE("<b>");
    inter_package *pack = InterPackage::from_URL(I, I"/main/completion/bibliographic");
    text_stream *title = Metadata::optional_textual(pack, I"^title");
    text_stream *author = Metadata::optional_textual(pack, I"^author");
    if (Str::len(title) > 0) {
        if (Str::len(author) > 0)
            Localisation::roman_tt(OUT, LD, I"Index.Elements.C.Titling", title, author);
        else
            Localisation::roman_t(OUT, LD, I"Index.Elements.C.AnonymousTitling", title);
    }
    WRITE("</b>");
    HTML_CLOSE("p");

§1.2. Index the headings1.2 =

    HTML_OPEN("p");
    Localisation::roman(OUT, LD, I"Index.Elements.C.ContentsHeading");
    HTML_CLOSE("p");
    int min_positive_level = 10, entries_written = 0;
    inter_package *heading_pack;
    LOOP_OVER_INVENTORY_PACKAGES(heading_pack, i, inv->heading_nodes)
        if (Metadata::read_numeric(heading_pack, I"^indexable")) {
            int L = (int) Metadata::read_numeric(heading_pack, I"^level");
            if ((L > 0) && (L < min_positive_level)) min_positive_level = L;
        }
    LOOP_OVER_INVENTORY_PACKAGES(heading_pack, i, inv->heading_nodes)
        if (Metadata::read_numeric(heading_pack, I"^indexable")) {
            Index this entry in the contents1.2.1;
            entries_written++;
        }

    if (entries_written == 0) {
        HTML_OPEN("p");
        WRITE("(");
        Localisation::roman(OUT, LD, I"Index.Elements.C.NoContents");
        WRITE(")");
        HTML_CLOSE("p");
        WRITE("\n");
    }

§1.2.1. Index this entry in the contents1.2.1 =

    int L = (int) Metadata::read_numeric(heading_pack, I"^level");
     indent to correct tab position
    HTML_OPEN_WITH("ul", "class=\"leaders\""); WRITE("\n");
    int ind_used = (int) Metadata::read_numeric(heading_pack, I"^indentation");
    if (L == 0) ind_used = 1;
    HTML_OPEN_WITH("li", "class=\"leaded indent%d\"", ind_used);
    HTML::begin_span(OUT, NULL);
    WRITE("%S", Metadata::required_textual(heading_pack, I"^text"));
    HTML::end_span(OUT);
    HTML::begin_span(OUT, NULL);
    if (L > min_positive_level) HTML::begin_span(OUT, I"indexgrey");
    ContentsElement::word_count(OUT, heading_pack, LD);
    if (L > min_positive_level) HTML::end_span(OUT);
     place a link to the relevant line of the primary source text
    IndexUtilities::link_package(OUT, heading_pack);
    HTML::end_span(OUT);
    HTML_CLOSE("li");
    HTML_CLOSE("ul");
    WRITE("\n");
    text_stream *summary = Metadata::optional_textual(heading_pack, I"^summary");
    if (Str::len(summary) > 0) {
        HTML::open_indented_p(OUT, ind_used+1, "hanging");
        HTML::begin_span(OUT, I"indexgrey");
        WRITE("<i>%S</i>", summary);
        HTML::end_span(OUT);
        HTML_CLOSE("p");
    }

§1.3. Index the extensions1.3 =

    HTML_OPEN("p");
    Localisation::roman(OUT, LD, I"Index.Elements.C.ExtensionsHeading");
    HTML_CLOSE("p");
    ContentsElement::index_extensions_included_by(OUT, inv, NULL, FALSE, LD);
    inter_package *ext_pack;
    LOOP_OVER_INVENTORY_PACKAGES(ext_pack, i, inv->extension_nodes) {
        inter_symbol *by_id =
            Metadata::optional_symbol(ext_pack, I"^included_by");
        if (by_id) ContentsElement::index_extensions_included_by(OUT, inv, by_id,
            NOT_APPLICABLE, LD);
    }
    ContentsElement::index_extensions_included_by(OUT, inv, NULL, TRUE, LD);

§2. This is called recursively to show how extensions have included each other:

void ContentsElement::index_extensions_included_by(OUTPUT_STREAM, tree_inventory *inv,
    inter_symbol *owner_id, int auto_included, localisation_dictionary *LD) {
    int show_head = TRUE;
    inter_package *pack;
    LOOP_OVER_INVENTORY_PACKAGES(pack, i, inv->extension_nodes) {
        inter_symbol *by_id = Metadata::optional_symbol(pack, I"^included_by");
        if (by_id == owner_id) {
            if ((auto_included != NOT_APPLICABLE) &&
                ((int) Metadata::read_optional_numeric(pack, I"^auto_included") != auto_included))
                continue;
            if (show_head) {
                HTML::open_indented_p(OUT, 2, "hanging");
                HTML::begin_span(OUT, I"indexgrey");
                if (auto_included == TRUE)
                    Localisation::roman(OUT, LD, I"Index.Elements.C.IncludedAutomatically");
                else if (auto_included == FALSE)
                    Localisation::roman(OUT, LD, I"Index.Elements.C.IncludedFromSource");
                else {
                    inter_package *owner_pack = InterPackage::container(owner_id->definition);
                    Localisation::roman_t(OUT, LD, I"Index.Elements.C.IncludedBy",
                        Metadata::optional_textual(owner_pack, I"^title"));
                }
                HTML::end_span(OUT);
                HTML_CLOSE("p");
                show_head = FALSE;
            }
            Index this extension2.1;
        }
    }
}

§2.1. Index this extension2.1 =

    inter_symbol *by_id = Metadata::optional_symbol(pack, I"^included_by");
    HTML_OPEN_WITH("ul", "class=\"leaders\"");
    HTML_OPEN_WITH("li", "class=\"leaded indent2\"");
    HTML::begin_span(OUT, NULL);
    WRITE("%S ", Metadata::required_textual(pack, I"^title"));
    if (Metadata::read_optional_numeric(pack, I"^standard") == 0) {
        IndexUtilities::link_package(OUT, pack); WRITE("&nbsp;&nbsp;");
    }

    if (auto_included != TRUE) WRITE("by %S ", Metadata::required_textual(pack, I"^author"));
    text_stream *v = Metadata::required_textual(pack, I"^version");
    if (Str::len(v) > 0) {
        HTML::begin_span(OUT, I"smaller");
        Localisation::roman_t(OUT, LD, I"Index.Elements.C.Version", v);
        HTML::end_span(OUT);
        WRITE(" ");
    }
    text_stream *ec = Metadata::optional_textual(pack, I"^extra_credit");
    if (Str::len(ec) > 0) {
        HTML::begin_span(OUT, I"smaller");
        WRITE("(%S) ", ec);
        HTML::end_span(OUT);
    }
    HTML::end_span(OUT);
    HTML::begin_span(OUT, NULL);
    ContentsElement::word_count(OUT, pack, LD);
    if (by_id == NULL) {
        int at = (int) Metadata::read_optional_numeric(pack, I"^included_at");
        if (at > 0) IndexUtilities::link(OUT, at);
    }
    HTML::end_span(OUT);
    HTML_CLOSE("li");
    HTML_CLOSE("ul");
    WRITE("\n");

§3.

void ContentsElement::word_count(OUTPUT_STREAM, inter_package *pack,
    localisation_dictionary *LD) {
    Localisation::roman_i(OUT, LD, I"Index.Elements.C.Words",
        (int) Metadata::read_numeric(pack, I"^word_count"));
}