auto toc help
This commit is contained in:
@@ -4,9 +4,24 @@ import argparse
|
|||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
def get_opts():
|
def get_opts():
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="TOC for markdown.")
|
parser = argparse.ArgumentParser(
|
||||||
|
description="TOC for markdown.",
|
||||||
|
epilog="""Automatic TOC guide:
|
||||||
|
Add these hidden comments in the markdown:
|
||||||
|
[TOCSTART]: #
|
||||||
|
[TOCEND]: #
|
||||||
|
|
||||||
|
Then, use this command to generate TOC: (make a backup first...)
|
||||||
|
sed -i -n \\
|
||||||
|
-e '1,/^\[TOCSTART\]: #/p;/^\[TOCEND\]: #/,$p' \\
|
||||||
|
-e '/^\[TOCSTART\]: #/r '<( md-toc document.md ) \\
|
||||||
|
document.md
|
||||||
|
""",
|
||||||
|
formatter_class=argparse.RawTextHelpFormatter,
|
||||||
|
)
|
||||||
parser.add_argument("-n", help="Number TOC", action="store_true")
|
parser.add_argument("-n", help="Number TOC", action="store_true")
|
||||||
parser.add_argument("-l", help="TOC as links", action="store_true")
|
parser.add_argument("-l", help="TOC as links", action="store_true")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
@@ -30,12 +45,12 @@ def main():
|
|||||||
bullet = "-"
|
bullet = "-"
|
||||||
counters = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
counters = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||||
lastlev = 0
|
lastlev = 0
|
||||||
re_anchor = re.compile('[^0-9a-z-]')
|
re_anchor = re.compile("[^0-9a-z-]")
|
||||||
for row in fp:
|
for row in fp:
|
||||||
if row.startswith("#"):
|
if row.startswith("#"):
|
||||||
headers, title = row.strip().split(" ", 1)
|
headers, title = row.strip().split(" ", 1)
|
||||||
if opts.l:
|
if opts.l:
|
||||||
anchor = re_anchor.sub('' , title.lower().replace(" ", "-"))
|
anchor = re_anchor.sub("", title.lower().replace(" ", "-"))
|
||||||
title = "[{}](#{})".format(title, anchor)
|
title = "[{}](#{})".format(title, anchor)
|
||||||
pad = headers.count("#") - 1
|
pad = headers.count("#") - 1
|
||||||
if pad < lastlev:
|
if pad < lastlev:
|
||||||
|
|||||||
Reference in New Issue
Block a user