Skip to main content

HeaderAnchors

HeaderAnchors is a module which converts heading titles into IDs just like it is done by specific backends.

Module exports three main functions:

  • to_id which converts a title into an ID by the rules of specific backend,
  • make_unique which adds a digit to make duplicate heading ID unique, according to the rules of specific backend.
  • is_flat which determines whether backend uses flatten preprocessor or not.

Introduction

All Foliant backends add anchors to each heading to make it possible to reference headings in URLs. The problem is that each backend has its own way to do that. For example, the heading My wife's birthday-party will get an ID my-wifes-birthday-party in Pandoc, header-my-wife’s-birthday-party in aglio and my-wife-39-s-birthday-party in slate.

Moreover, different backends have different ways to deal with duplicate IDs. Utils in this module help you cope with these problems.

Usage

To use functions from this module, first install it with command

pip3 install foliantcontrib.utils.header_anchors

Then import the main functions:

>>> from foliant.preprocessors.utils.header_anchors import to_id, make_unique, is_flat

to_id

Feed a header title to the to_id function to get the proper id for each backend:

>>> title = "My wife's birthday-party"
>>> to_id(title, 'pandoc')
'my-wifes-birthday-party'
>>> to_id(title, 'aglio')
'header-my-wife’s-birthday-party'
>>> to_id(title, 'slate')
'my-wife-39-s-birthday-party'

available backends:

param backend
'pandoc' Pandoc
'mdtopdf' MdToPdf
'aglio' Aglio
'mkdocs' MkDocs
'slate' Slate
'confluence' Confluence
'no_transform' no transformations, return string as is

If the name of the backend is not recognized, pandoc will be used as a fallback backend:

>>> to_id(title, 'nonexistent backend')
'my-wifes-birthday-party'

make_unique

If some headers in the document have the same title or their IDs match, each backend transforms the ID for it to remain unique. Pandoc adds subsequent numbers with a hyphen, MkDocs — numbers with an unerscore.

make_unique function handles the proper transformations for you. Feed it an id, backend name and number of occurrences of this title in the document to get the proper unique id:

>>> make_unique('my-title', 3, 'pandoc')
'my-title-2'
>>> make_unique('my-title', 3, 'mkdocs')
'my-title_2'
>>> make_unique('my-title', 3, 'slate')
'my-title-3'

If the name of the backend is not recognized, pandoc will be used as a fallback backend:

>>> make_unique('my-title', 3, 'nonexistent backend')
'my-title-2'

is_flat

is_flat function takes the backend name as parameter and returns True if backend uses flatten preprocessor to make a single file out of all chapters.

>>> is_flat('pandoc')
True
>>> is_flat('mkdocs')
False

IDGenerator class

IDGenerator is a class which helps generate unique anchors in seemless way. It records every call to generate an anchor from a title, and if the anchor repeats, it calls make_unique to make it unique.

Here's an example usage:

>>> from foliant.preprocessors.utils.header_anchors import IDGenerator
>>> idgen = IDGenerator('pandoc')
>>> idgen.generate('My title')
'my-title'
>>> idgen.generate('Another title!')
'another-title'
>>> idgen.generate('My title')
'my-title-1'

After generating id for My title for a second time generator had added a -1 to it. To reset the id count call the reset method:

>>> idgen.reset()
>>> idgen.generate('My title')
'my-title'

Metadata

Release files for foliantcontrib.utils.header-anchors 1.0.4

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.utils.header-anchors 1.0.4
File Size Uploaded
foliantcontrib.utils.header_anchors-1.0.4.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for foliantcontrib.utils.header-anchors 1.0.4
File Interpreter ABI Platform
foliantcontrib.utils.header_anchors-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / foliantcontrib.utils.header_anchors-1.0.4.tar.gz

Download URL foliantcontrib.utils.header_anchors-1.0.4.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d9011fe57aea9070f48fcfa10a62ce7cb4ba492f0d8148000292b93e0d4ae16f
BLAKE2b-256 checksum
How to use checksums
1f4bf49bb5f04976d1985e17a6bceee5c7a2f29d4ac2b5eb930bdfbf92dd3113
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / foliantcontrib.utils.header_anchors-1.0.4-py3-none-any.whl

Download URL foliantcontrib.utils.header_anchors-1.0.4-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d6780c6a24431a2f2549e4788c73db7581dd87707df155778c4c75b51dcecdc6
BLAKE2b-256 checksum
How to use checksums
dcf0ba5ffbc6ccadc218224711340240d6bc2aa80515b9469a4590a06939b8d7
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.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