{% extends "base.html" %}
{% import "macros/book-outline.html" as book_outline %}
{%- block head -%}{%- set page = page | default(value = section) -%}
{%- set title = page.title -%}
{%- set meta_article_type = true -%}
{%- set meta_description = page.extra.summary | default(value = page.content | striptags | safe | linebreaksbr | replace(from = " ", to = " ") | replace(from = " ", to = " ") | trim | truncate(length = 200)) -%}
{%- set css = ["/template/book.css", "/layout/reading-material.css", "/component/code-snippet.css"] -%}
{%- set js = ["/js/template/book.js"] -%}
{%- endblock head -%}
{%- block content -%}{%- set page = page | default(value = section) -%}
{#- Search this page-or-section's ancestor tree for a section that identifies itself as a book, and save it to a `book` variable -#}
{%- for ancestor_path in page.ancestors | concat(with = page.relative_path) -%}
{#- Get the ancestor section from this ancestor path string -#}
{%- if ancestor_path is ending_with("/_index.md") -%}
{%- set potential_book = get_section(path = ancestor_path) -%}
{%- endif -%}
{#- Check if the ancestor section is the root of a book, and if so, set it to a variable accessible outside the loop -#}
{%- if potential_book.extra.book -%}
{%- set_global book = get_section(path = potential_book.path ~ "_index.md" | trim_start_matches(pat = "/")) -%}
{%- endif -%}
{%- endfor -%}
{#- Map this book's chapter path strings to an array of sections -#}
{%- set chapters = [] -%}
{%- for chapter_path in book.subsections -%}
{%- set_global chapters = chapters | concat(with = get_section(path = chapter_path)) -%}
{%- endfor -%}
{%- set chapters = chapters | sort(attribute = "extra.order") -%}
{#- A flat list of all pages in the ToC -#}
{%- set flattened_outline = book_outline::flatten_book_outline(section = book) -%}
{%- set flat_pages_list = book.path ~ ",,,,," ~ book.title ~ ";;;;;" ~ flattened_outline | split(pat = ";;;;;") -%}
{%- set flat_index_of_this = 0 -%}
{%- set flat_pages_path = [] -%}
{%- set flat_pages_title = [] -%}
{%- for item_str in flat_pages_list -%}
{%- if item_str | trim | length > 0 -%}
{%- set parts = item_str | split(pat = ",,,,,") -%}
{%- if current_path == parts | first -%}
{%- set_global flat_index_of_this = loop.index0 -%}
{%- endif -%}
{%- set_global flat_pages_path = flat_pages_path | concat(with = parts | first) -%}
{%- set_global flat_pages_title = flat_pages_title | concat(with = parts | last) -%}
{%- endif -%}
{%- endfor -%}