{% for section_type in document_dictionary.keys() -%}
{{ '' if loop.first else '\n' -}}
{% if '#' in document_dictionary[section_type] -%}
{% for comment in document_dictionary[section_type]['#'] -%}
{{ comment }}
{% endfor -%}
{% endif -%}
{% if section_type != ('', 'global') -%}
{{ section_type[0] + section_type[1] }} {{ section_type[2] }}
{% endif -%}
{% for directive in document_dictionary[section_type].keys() if directive[1] == 'include' -%}
{% for comment in  document_dictionary[section_type][directive][:-1] -%}
{{ comment }}
{% endfor -%}
{{ directive[0] + directive[1] }}    {{ directive[2] }}
{% endfor -%}
{% for directive in document_dictionary[section_type].keys() -%}
{% if directive != '#' and directive[1] != 'include' -%}
{% set directive_value = document_dictionary[section_type][directive] -%}
{% if directive[1] == 'access to' -%}
{% set access_values = directive_value.keys()|list -%}
{% if '#' in directive_value -%}
{% for comment in  directive_value['#'] -%}
{{ comment }}
{% endfor -%}
{% set access_values = access_values[1:] -%}
{% endif -%}
{% if access_values|length == 1 -%}
{{ directive[0] + directive[1] }} {{ directive[2] }} by {{ access_values[0][1] }} {{ directive_value[access_values[0]][0] }}
{% else -%}
{{ directive[0] + directive[1] }} {{ directive[2] }}
{% for value in access_values|reverse -%}
{{ '    by ' + value[0] + value[1] }} {{ directive_value[value][0] }}
{% endfor -%}
{% endif -%}
{% elif directive[1] == 'syncrepl' -%}
{% set syncrepl_values = directive_value.keys()|list -%}
{% if '#' in directive_value -%}
{% for comment in  directive_value['#'] -%}
{{ comment }}
{% endfor -%}
{% set syncrepl_values = syncrepl_values[1:] -%}
{% endif -%}
{{ directive[0] + directive[1] }} {{ directive[2] }}
{% for value in syncrepl_values -%}
{{ '    ' + value[0] + value[1] }}={{ directive_value[value][0] }}
{% endfor -%}
{% else -%}
{% for comment in  directive_value[:-1] -%}
{{ comment }}
{% endfor -%}
{{ directive[0] + directive[1:]|join('    ') }}{{ '    ' + directive_value[-1] if directive_value[-1] else '' }}
{% endif -%}
{% endif -%}
{% endfor -%}
{% endfor -%}
