Skip to main content

AltStructure Extension

AltStructure is a config extension for Foliant to generate alternative chapter structure based on metadata.

It adds an alt_structure preprocessor and resolves !alt_structure YAML tags in the project config.

Installation

$ pip install foliantcontrib.alt_structure

Configuration

Config extension

Options for AltStructure are specified in the alt_structure section at the root of Foliant config file:

alt_structure:
    structure:
        - "topic/entity"
        - "additional"
    sep: '/'
    add_unmatched_to_root: false
    registry:
        topic:
            auth: Аутентификация и авторизация
            weather: Погода
        entity:
            test_case: Тест кейсы
            spec: Спецификации

structure : (required) List of structure strings (or lists), each of them representing a branch in chapters structure.

sep : separator for structure strings (ignored for structure lists). Deafault: /

add_unmatched_to_root : if true, all chapters that weren't matched to structure in metadata will be added to root of the chapter tree. If false — they will be ignored. Default: false

registry : A dictionary which defines aliases for chapter tree categories.

Preprocessor

Preprocessor has just one option:

preprocessors:
    - alt_structure:
        create_subfolders: true

create_subfolders : If true, preprocessor will create a full copy of the working_dir and add it to the beginning of all filepaths in the generated structure. If false — preprocessor doesn't do anything. Default: true

Usage

Step 1

Add !alt_structure tag to your chapters in the place where you expect new structure to be generated. It accepts one argument: list of chapters, which will be scanned.

chapters:
    - basic:  # <-- this is _chapter tree category_
        - auth/auth.md
        - index.md
        - auth/test_auth.md
        - auth/test_auth_aux.md
        - weather.md
        - glossary.md
        - auth/spec_auth.md
        - test_weather.md
    - Alternative: !alt_structure
        - auth/auth.md
        - index.md
        - auth/test_auth.md
        - auth/test_auth_aux.md
        - weather.md
        - glossary.md
        - auth/spec_auth.md
        - test_weather.md

AltStructure extension introduces a lot of new notions so to make sure we are on the same page, let's agree on some terms. Chapter tree category is a mapping with single key which you add to your chapter list to create hierarchy. basic: and Alternative: are categories in this example.

You can also utilize YAML anchors and aliases, but in this case, because of language limitation you need to supply alias inside a list. Let's use it to get the same result as the above, but in a more compact way:

chapters:
    - basic: &basic
        - auth/auth.md
        - index.md
        - auth/test_auth.md
        - auth/test_auth_aux.md
        - weather.md
        - glossary.md
        - auth/spec_auth.md
        - test_weather.md
    - Alternative: !alt_structure [*basic]

Step 2

Next you need to define the structure in structure parameter of extension config. It is defined by list of node types, or a single string with node types, divided by separators. For example:

alt_structure:
    structure:
        - ['topic', 'entity']  # list with two node types
        - 'additional/glossaries'  # single string with two node types and separator /

These names of the node types are arbitrary, you can use any words you like except root and subfolder.

Step 3

Open your source md-files and edit their main meta sections. Main meta section is a section, defined before the first heading in the document (check metadata documentation for details). Add a mapping with nodes for this chapter under the key structure.

file auth_spec.md

---
structure:
    topic: auth  # <-- node type: node name
    entity: spec
---

# Specification for authorization

Here topic and entity are node types, which are part of our structure (step 2). auth and spec are node names. After applying !alt_structure tag nodes will be converted into chapter tree categories. Node type defines the level of the category and node name defines the caption of the category.

We've added two nodes to the structure field of chapter metadata: topic: auth and entity: spec. In the structure that we've defined on step 2 the topic goes first and entity — second. So after applying the tag, this chapter will appear in config like this:

- auth:
    - spec:
        - auth_spec.md

If we'd stated only topic key in metadata, then it would look like this:

- auth:
    - auth_spec.md

Step 4

Now let's fill up registry. We used spec and auth in our metadata for node names, but these words don't look pretty in the documents. Registry allows us to set verbose captions for node names in config:

alt_structure:
    structure:
        - ['topic', 'entity']
        - 'additional/glossaries'
    registry:
        topic:
            auth: Authentication and Authorization
        entity:
            spec: Specifications

With such registry now our new structure will look like this:

- Authentication and Authorization:
    - Specifications:
        - auth_spec.md

Special node types

In the step 2 of the user guide above we've mentioned that you may choose any node names in the structure except root and subfolder. These are special note types and here's how you can use them.

root

For example, if our structure looks like this:

alt_structure:
    structure:
        - ['topic', 'entity']

and our chapter's metadata looks like this:

---
structure:
    foo: bar
---

The node foo: bar is not part of the structure, so applying the !alt_structure tag it will just be ignored (unless add_unmatched_to_root is set to true in config). But what if you want to add it to the root of your chapter tree?

To do that — add the root node to your metadata:

---
structure:
    foo: bar
    root: true  # the value of the key `root` is ignored, we use `true` for clarity
---

Please note that if you add root: true to meta, the chapter will be sent to root of your tree only if it wasn't matched by your structure definition.

So in the following example it will still appear under node auth and root: true will be ignored:

---
structure:
    topic: auth
    root: true  # ignored
---

subfolder

By defining subfolder node in chapter's metadata you can manually add another chapter tree category to any chapter.

For example:

file auth_spec.md

---
structure:
    topic: auth
    entity: spec
    subfolder: Main specifications
---

After applying tag the new structure will look like this:

- auth:
    - spec:
        - Main specifications:
            - auth_spec.md

Using preprocessor

By default the !alt_structure tag only affects the chapters section of your foliant.yml. This may lead to situation when the same file is mentioned several times in the chapters section. While most backends are fine with that — they will just publish the file two times, MkDocs does not handle this situation well.

That's where you will need to add the preprocessor alt_structure to your preprocessors list. Preprocessor creates a subfolder in the working_dir and copies the entier working_dir contents into it. Then it inserts the subfolder name into the beginning of all chapters paths in the alternative structure.

Important: It is recommended to add this preprocessor to the end of the preprocessors list.

preprocessors:
    ...
    alt_structure:
        create_subfolders: true

Note, that the parameter create_subfolders is not necessary, it is true by default. But we recommend to state it anyway for clarity.

After applying the tag, your new structure will now look like this:

- Authentication and Authorization:
    - Specifications:
        - alt1/auth_spec.md

The contents of the working_dir were copied into a subdir alt1, and new structure refers to the files in this subdir.

Release files for foliantcontrib.alt-structure 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for foliantcontrib.alt-structure 0.1.2
File Size Uploaded
foliantcontrib.alt_structure-0.1.2.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for foliantcontrib.alt-structure 0.1.2
File Interpreter ABI Platform
foliantcontrib.alt_structure-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size:19.9 kB

Release files / foliantcontrib.alt_structure-0.1.2.tar.gz

Download URL foliantcontrib.alt_structure-0.1.2.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e683364cda1ae1427092484505ee11bb62a77cfedeec7b1a04d8bb8afbb64f96
BLAKE2b-256 checksum
How to use checksums
5acd713a7c736936df9b23674f173b34568b207a283064481e35adcfcf409e17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6

Release files / foliantcontrib.alt_structure-0.1.2-py3-none-any.whl

Download URL foliantcontrib.alt_structure-0.1.2-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e6ed9a97ebf73d34d60a06f799e2abe2944a385b774fa924aa2539be18a848c
BLAKE2b-256 checksum
How to use checksums
d8d1ad2a2bbaa4e30f7e080cd2a8b739961918da58ef4bf4798528d9ecc3df2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6

Release history Release notifications | RSS feed

0.2.1

2 release files

0.2.0

2 release files

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page