Generate Table of Contents (TOC) for Markdown file (like README.md) automatically
Project description
Table of Contents
Intro
Toc4github is a tiny tool to automatically generate Table of Contents (TOC) for Markdown file, like README.md in Github.com. It might also be used for other markdown rendering system, but I never tested! :)
Install
$ pip3 install toc4github
Command Line Usage
You should insert a line of placeholder with content {toc}
in README.md.
The placeholder itself must be single line, and it most likely be at the beginning of markdown file. This is the start position of TOC section.
When the placeholder line is ready:
$ python3 -m toc4github [--dryrun|-d] [--title|-t] <path/to/README.md>
Now, the placeholder line is replaced and expanded by TOC generated, your markdown file is updated.
You can use --dryrun(-d)
to check the generated TOC first, this option
would only print out TOC, file is intact and nothing is changed.
--title(-t)
option is used if you need a title for TOC section.
The title is fixed with Table of Contents
, just like this README.md file.
Different languages can be mixed, but I only tested English and Chinese.
Here is an example:
$ python3 -m toc4github -dt text.txt
# Table of Contents
* [Hello](#Hello)
* [Past 过去](#Past-过去)
* [Present 现在](#Present-现在)
* [Item-1](#Item-1)
* [Item-2](#Item-2)
* [Item-3](#Item-3)
* [Item-4](#Item-4)
* [Future 未来](#Future-未来)
* [中英文can mix哦](#中英文can-mix哦)
API
Or, you can call make_toc
interface in your python code like:
>>> from toc4github import make_toc
>>>
>>> with open('text.txt') as f:
... lines = f.read()
...
>>> print(lines) # show original content
{toc}
# Hello
Hi, here is a test.
## Past 过去
Past is full of bloody experiences and cheerful memories.
## Present 现在
Present is the only thing you have.
### Item-1
TODO
### Item-2
### Item-3
### Item-4
## Future 未来
Future is created by your imagination.
## 中英文can mix哦
>>> print(make_toc(lines)) # show TOC
* [Hello](#Hello)
* [Past 过去](#Past-过去)
* [Present 现在](#Present-现在)
* [Item-1](#Item-1)
* [Item-2](#Item-2)
* [Item-3](#Item-3)
* [Item-4](#Item-4)
* [Future 未来](#Future-未来)
* [中英文can mix哦](#中英文can-mix哦)
>>>
Interface make_toc
only return the generated TOC, you should
insert it to your file anywhere you like by yourself. And this
is the only interface toc4github provides.
Showcase
In addition, All my repos' TOC are generated by toc4github. Have fun ... ^___^
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for toc4github-0.16-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a8c72ce8fcc30dc5d8cd4f215a94187b54c7aff225dbe277f98ffb79fe1f522 |
|
MD5 | dc649db020ccb95bb8f8136b378459ee |
|
BLAKE2b-256 | feb5285b11c4b133e1892860e9a7d59c1bd1daedb1bc9a454b905d581d2aa3ac |