Skip to content

Instantly share code, notes, and snippets.

@ryancdotorg
Created November 7, 2022 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryancdotorg/735fc5db75638fd21fe1f8f2f09c8af7 to your computer and use it in GitHub Desktop.
Save ryancdotorg/735fc5db75638fd21fe1f8f2f09c8af7 to your computer and use it in GitHub Desktop.
All you really need to know is that this is cursed.
{% block followups %}
{% set dir = page.components | slice(end=-1) | join(sep="/") %}
{% set parent = page.ancestors | last %}
{% set section = get_section(path=parent) %}
{% set followups = [] %}
{% set a = page.extra.followup_groups | default(value=[]) | sort %}
{% set a_sz = a | length %}
{# Add explicitly listed pages to follow-up list. #}
{% for stem in page.extra.followups | default(value=[]) %}
{% set filename = dir ~ "/" ~ stem ~ ".md" %}
{% set other = get_page(path=filename) %}
{# We need to use `set_global` because of scoping rules. #}
{% set_global followups = followups | concat(with=other) %}
{% endfor %}
{% if a_sz %}
{# Search through other faq pages for those in our follow-up groups. #}
{# Yes, this is O(n^2), but that's fine with small coefficients! #}
{% for other in section.pages %}
{% if other != page and other not in followups %}
{% set b = other.extra.groups | default(value=[]) | sort %}
{% set b_sz = b | length %}
{% set_global i = 0 %}{% set_global j = 0 %}
{# No `while` loop support, so do some stupid shit. #}
{% for _ in range(end=999) %}
{# Break out of the loop at the end of either array. #}
{% if i >= a_sz or j >= b_sz %}
{% break %}
{% elif a[i] == b[j] %}
{% set_global followups = followups | concat(with=other) %}
{% break %}
{% else %}
{# Advance one of the lists, depending on comparison. #}
{# This is just `if a[i] < b[j]` with extra steps. #}
{% set compare = [a[i], b[j]] | sort | first %}
{% if compare == a[i] %}
{% set_global i = i + 1 %}
{% else %}
{% set_global j = j + 1 %}
{% endif %}
{% endif %}{# state handling #}
{% endfor %}{# range #}
{% endif %}{# eligible? #}
{% endfor %}{# other #}
{% endif %}{# a_sz #}
{# ACTUAL CONTENT GOES HERE! #}
{% if followups | length %}
<h3>Follow-up questions:</h3>
<ul>
{% for other in followups %}
<li><a href="{{ other.permalink | safe }}">{{ other.title | safe }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endblock followups %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment