/
customAnchors.py
70 lines (62 loc) · 2.14 KB
/
customAnchors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from mkdocs.structure.toc import AnchorLink
from html import unescape, escape
from html.parser import HTMLParser
import re
class CustomHeadings(HTMLParser):
_targetTag = 'strong'
_linkDepth = 2
_linkCounter = 0
_isHeading = False
_id = ''
_content = ''
targetLink = None
def handle_starttag(self, tag: str, attrs: list):
if re.match(r"h[2-6]", tag):
if 'id' in dict(attrs):
self._linkCounter += 1
if self._isHeading or tag != self._targetTag:
return
print(self.get_starttag_text())
for (attr, val) in attrs:
if attr == 'id':
self._linkCounter += 1
self._isHeading = True
self._id = val
def handle_data(self, data: str):
if not self._isHeading:
return
self._content += data
def handle_endtag(self, tag: str):
if not self._isHeading or tag != self._targetTag:
return
print('Add link for heading...')
print(' > ' + self._content)
print(f' --> targetLink != None: {not self.targetLink is None}')
if not self.targetLink is None:
self.targetLink.children.insert(
self._linkCounter - 1,
AnchorLink(self._content, self._id, self._linkDepth)
)
self._isHeading = False
self._content = ''
def on_page_content(html, page, **kwargs):
if page.file.src_uri != 'index.md':
return
print('\n=====< PAGE >=====')
print('src: ' + page.file.src_uri)
print('title: ' + page.title)
print('-----< PAGE.toc >-----')
for link in page.toc:
parser = CustomHeadings()
parser.targetLink = link
parser.feed(html)
# dump_links(link)
print('\n======= // =======\n')
def dump_links(link, depth=0):
padding = ' ' * 2 * (depth + 1)
link.title = escape(unescape(link.title).title())
print(f'{padding}- [{link.level}] {link.title} => {link.url}')
if len(link.children) > 0:
print(f'{padding} -> {len(link.children)} children')
for child in link.children:
dump_links(child, depth+1)