|
16 | 16 | {%- endmacro render_book_page_toc -%} |
17 | 17 |
|
18 | 18 | {# Recursively render a book's chapters table of contents #} |
19 | | -{%- macro render_book_outline(section, current_path, indents) -%} |
| 19 | +{%- macro render_book_outline(parent, current_path, index, indents) -%} |
20 | 20 | {#- Setup -#} |
21 | | - {%- set items = [] -%} |
22 | | - {%- if section.pages -%} |
23 | | - {%- set_global items = items | concat(with = section.pages) -%} |
| 21 | + {%- set chapters = parent.pages | default(value = []) -%} |
| 22 | + {%- if index == 0 -%} |
| 23 | + {%- set_global chapters = [parent] -%} |
| 24 | + {%- else -%} |
| 25 | + {%- for subsection_path in parent.subsections -%} |
| 26 | + {%- set_global chapters = chapters | concat(with = get_section(path = subsection_path)) -%} |
| 27 | + {%- endfor -%} |
24 | 28 | {%- endif -%} |
25 | | - {%- if section.subsections -%} |
26 | | - {%- for subsection_path in section.subsections -%} |
27 | | - {%- set subsection = get_section(path = subsection_path) -%} |
28 | | - {%- set_global items = items | concat(with = subsection) -%} |
29 | | - {%- endfor -%} |
| 29 | + {%- if index > 0 -%} |
| 30 | + {%- set_global chapters = chapters | sort(attribute = "extra.order") -%} |
30 | 31 | {%- endif -%} |
31 | | - {%- set items = items | sort(attribute = "extra.order") -%} |
32 | 32 | {#- End of setup -#} |
33 | 33 |
|
34 | 34 | {%- set tabs = "" -%} |
35 | 35 | {%- for i in range(end = indents) -%} |
36 | 36 | {%- set_global tabs = tabs ~ " " -%} |
37 | 37 | {%- endfor -%} |
38 | 38 |
|
39 | | - {%- if items | length > 0 %} |
| 39 | + {%- if chapters | length > 0 %} |
40 | 40 | {{ tabs }}<ul> |
41 | | - {%- for item in items %} |
42 | | - {{ tabs }}<li {%- if current_path == item.path %} class="active"{%- endif -%}><a href="{{ item.path | safe }}" title="{{ item.title | safe }}">{{ item.title }}</a></li> |
43 | | - {%- if item.pages or item.subsections -%} |
44 | | - {{ self::render_book_outline(section = item, current_path = current_path, indents = indents + 1) }} |
| 41 | + {%- for chapter in chapters %} |
| 42 | + {%- set children = chapter.pages or chapter.subsections | default(value = []) -%} |
| 43 | + {%- set_global classes = [] -%} |
| 44 | + {%- if index == 0 -%} |
| 45 | + {%- set_global classes = classes | concat(with = "title") -%} |
| 46 | + {%- endif -%} |
| 47 | + {%- if index == 1 -%} |
| 48 | + {%- set_global classes = classes | concat(with = "chapter") -%} |
| 49 | + {%- endif -%} |
| 50 | + {%- if current_path == chapter.path -%} |
| 51 | + {%- set_global classes = classes | concat(with = "active") -%} |
| 52 | + {%- endif %} |
| 53 | + {{ tabs }}<li {%- if classes | length > 0 %} class="{{ classes | join(sep = " ") }}"{% endif %}> |
| 54 | + {{ tabs }}<label>{% if children and not index == 0 %}<input type="checkbox" {%- if current_path is starting_with(chapter.path) %} checked{% endif %} />{% endif %}</label> |
| 55 | + {{ tabs }}<a href="{{ chapter.path | safe }}" title="{{ chapter.title | safe }}">{{ chapter.title }}</a> |
| 56 | + {{ tabs }}</li> |
| 57 | + {%- if children -%} |
| 58 | + {{ self::render_book_outline(parent = chapter, current_path = current_path, index = index + 1, indents = indents + 1) }} |
45 | 59 | {%- endif %} |
46 | 60 | {%- endfor %} |
47 | 61 | {{ tabs }}</ul> |
|
0 commit comments