On Sun, 30 Apr 2023 at 07:51, Mitch Berkson <[email protected]> wrote:
>
> Here's a way I did it. Probably there is a better way.
>
> Add a page to each directory (I used the file name "list_title.md") with the
> metadata:
> list_title = "Heading for the navigation links for this directory"
> do_not_list = "True"
>
> In conf.py, define a context variable with the name that was used for the
> page file with the metadata:
> GLOBAL_CONTEXT = {
> ...
> 'LIST_TITLE_FILENAME': 'list_title.md',
> }
>
> In base_header.tmpl, add the macros:
> {# Return the title to use for the navigation links for the directory in
> which #}
> {# link is found by looking for the metadata "list_title" in
> LIST_TITLE_FILENAME file #}
> {% macro html_navigation_links_title(link) %}
> {% set filename = link.split('/')[-2] + ".md" %}
> {% set file_path = link[:-1] + ".md"%}
> {% if filename == LIST_TITLE_FILENAME %}
> {% set full_file_path = os.path.realpath("pages/" + file_path) %}
> {% set f = open(full_file_path, 'r') %}
> {% set file_contents = f.read() %}
> {% set metadata = toml.loads(file_contents.split('+++')[1]) %}
> <h1>{{ metadata['list_title'] }}</h1>
> {% endif %}
> {% endmacro %}
>
> {# Return listing for the link if the value of link's do_not_list metadata
> field is not True #}
> {% macro list_link(link, text) %}
> {% set filename = link.split('/')[-2] + ".md" %}
> {% set file_path = link[:-1] + ".md"%}
> {% set full_file_path = os.path.realpath("pages/" + file_path) %}
> {% set f = open(full_file_path, 'r') %}
> {% set file_contents = f.read() %}
> {% set metadata = toml.loads(file_contents.split('+++')[1]) %}
> {% if metadata['do_not_list'] != "True" %}
> <li><a href="{{ link }}">{{ text|e }}</a></li>
> {% endif %}
> {% endmacro %}
>
> In list.tmpl, modify the for loop to use the macros:
> <ul class="postlist">
> {% for text, link, count in items %}
> {{ header.html_navigation_links_title(link) }}
> {{ header.list_link(link, text) }}
> {% if count %}
> ({{ count }})
> {% endif %}
> {% endfor %}
> </ul>
1. You should be able to just set the `title` variable.
2. Reading the files manually seems like an ugly hack. The list.tmpl
template does not get the posts directly (although changing that
wouldn't hurt). But without the posts variable, you could use some
prefix for titles and then filter them out by that prefix. Something
like:
{% for text, link, count in items %}
{% if text.startswith("DIRECTORY-TITLE:") %}
{% set title = text.split("DIRECTORY-TITLE:")[1] %}
{% endif %}
{% endfor %}
And then do the same check for display (but negated). I'm not sure if
this is fully doable from within Jinja2 templates; you might need to
expose some small helper functions in GLOBAL_CONTEXT.
--
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16
--
You received this message because you are subscribed to the Google Groups
"nikola-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nikola-discuss/CAMw%2Bj7LxvqqTqyCWm3XkyRWnkF1MnAMRLqQQCq%3DO1GECitAJCg%40mail.gmail.com.