Skip to main content

Utils for chapters

Project description

Chapters utils

This module contains utils which make work with Foliant chapter lists easier.

Installation

To use functions and classes from this module, install it with command

pip3 install foliantcontrib.utils.chapters

Usage

Right now this module offers only one useful class, called Chapters. To start using it, import it:

>>> from foliant.contrib.chapters import Chapters

Let's assume we have the following list of chapters specified in foliant.yml:

chapters:
    - introduction.md
    - Overview:
        - The Problem: problem.md
        - Requirements: req.md
        - Quick Start:
            - qs/installation.md
            - qs/first_steps.md
            - qs/advanced_usage.md
    - Specifications:
        - specs/core.md
        - specs/classes.md

If we want to interact wit this list of chapters, we will probably need path to Markdown-files in the proper order. That's exactly what the Chapters class offers. Let's translate this chapter list into Python and give it to the Chapters class:

>>> chapters_list = ['introduction.md',{'Overview': [{'The Problem': 'problem.md'},{'Requirements': 'req.md'},{'Quick Start': ['qs/installation.md','qs/first_steps.md','qs/advanced_usage.md']}]},{'Specifications': ['specs/core.md', 'specs/classes.md']}]
>>> chapters = Chapters(chapters_list)

Optionally you can specify paths to your working and src dirs, those will be needed for the proper work of some methods:

>>> chapters = Chapters(chapters_list, working_dir='__folianttmp__', src_dir='src')

But the recommended much easier option for initializing the Chapter class is by using from_config method:

chapters = Chapters.from_config(config)

where config is your parsed foliant config dictionary.

The flat property

The flat property of the Chapters class contains the list of chapter filenames in the correct order, with all the original hierarchy neatly flattened.

>>> for chapter in chapters.flat:
...     print(chapter)
introduction.md
problem.md
req.md
qs/installation.md
qs/first_steps.md
qs/advanced_usage.md
specs/core.md
specs/classes.md

The paths method

Usually when we work with chapters, we need not just ther names, as they stated in foliant.yml, but the paths to the actual files.

This is the work of the paths method, which accepts one argument: root of the markdown-files directory (usually src or __folianttmp__).

This method returns a generator, which yields PosixPath objects to each chapter in the proper order

>>> for path in chapters.paths('src'):
...     print(repr(path))
PosixPath('src/introduction.md')
PosixPath('src/problem.md')
PosixPath('src/req.md')
PosixPath('src/qs/installation.md')
PosixPath('src/qs/first_steps.md')
PosixPath('src/qs/advanced_usage.md')
PosixPath('src/specs/core.md')
PosixPath('src/specs/classes.md')

The get_chapter_title method

Method tries to find the chapter by its path in the chapter list and returns its MkDocs-style title, if it was defined in the chapter list.

Let's return to the example chapter list from the Usage section:

chapters:
    - introduction.md
    - Overview:
        - The Problem: problem.md
        - Requirements: req.md
        - Quick Start:
            - qs/installation.md
            - qs/first_steps.md
            - qs/advanced_usage.md
    - Specifications:
        - specs/core.md
        - specs/classes.md

If we try to find the title of the req.md chapter:

>>> chapters.get_chapter_title('req.md')
'Requirements'

We get the title 'Requirements', which was defined in the config. On the other hand, if we try to get the title of a chapter qs/first_steps.md:

>>> chapters.get_chapter_title('qs/first_steps.md')
''

We will get an empty string, because the title for this chapter was not defined in the config.

Alternative usage

You can also use the Chapters object as if it was list:

>>> chapters[0]
'introduction.md'
>>> 'req.md' in chapters
True
>>> for chapter in chapters:
...     print(chapter)
...     break
introduction.md

Original chapters list is available in the chapters property:

>>> chapters.chapters
['introduction.md', {'Overview': [{'The Problem': 'problem.md'}, {'Requirements': 'req.md'}, {'Quick Start': ['qs/installation.md', 'qs/first_steps.md', 'qs/advanced_usage.md']}]}, {'Specifications': ['specs/core.md', 'specs/classes.md']}]

You can redefine your chapters on the fly:

>>> chapters.chapters = ['one.md', {'two': 'three.md'}]
>>> for chapter in chapters:
...     print(chapter)
one.md
three.md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

foliantcontrib.utils.chapters-1.0.4.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file foliantcontrib.utils.chapters-1.0.4.tar.gz.

File metadata

  • Download URL: foliantcontrib.utils.chapters-1.0.4.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.6

File hashes

Hashes for foliantcontrib.utils.chapters-1.0.4.tar.gz
Algorithm Hash digest
SHA256 9ae39b607403109b7dd96555b9114493c5cdeb119e8fac8c47b4b87918249a22
MD5 c940b504ad29be515355a3687f591b45
BLAKE2b-256 edae4b4b97473f8383a41d914f0070712960ddd47fc960d69f3b8bbbdbdd9278

See more details on using hashes here.

File details

Details for the file foliantcontrib.utils.chapters-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: foliantcontrib.utils.chapters-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.6

File hashes

Hashes for foliantcontrib.utils.chapters-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 568ec90123abf84a263dc94d65b27526f4d34ea0be030d9bc58eb9f41170de28
MD5 23fdf0b7b4da69324dd331e3ada0744d
BLAKE2b-256 ed11eb86946177ca1de82ed6ea33dc36e6a4088a3d4e3c3e0b578764a6a07d5a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page