-
Notifications
You must be signed in to change notification settings - Fork 0
/
anchorlinks.py
69 lines (55 loc) · 2.09 KB
/
anchorlinks.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
# -*- coding: utf8 -*-
import logging
import bs4
from pelican import signals
from pelican.generators import ArticlesGenerator, PagesGenerator
IGNORE_VAR_NAME = "ANCHORLINKS_IGNORE"
DEFAULT_IGNORE = ["footnote-ref", "toclink"]
def process_content(article):
"""
Pelican callback
"""
# TODO: This is too slow
if article._content is None:
logging.warning(f"{article.title} is empty!")
return
dirty = False
settings = article.settings
ignore_tags = settings.get(IGNORE_VAR_NAME, DEFAULT_IGNORE)
soup_doc = bs4.BeautifulSoup(article._content, 'html.parser')
for anchor in soup_doc.findAll("a", href=True):
if anchor['href'].startswith("#"):
tag_class = anchor.get('class', [])
if not any(c in tag_class for c in ignore_tags):
anchor['class'] = tag_class + ['anchorlink']
dirty = True
if dirty:
article._content = str(soup_doc)
return
def add_deps(generators):
# Process the articles and pages
for generator in generators:
if isinstance(generator, ArticlesGenerator):
for article in generator.articles:
process_content(article)
for article in generator.drafts:
process_content(article)
for article in generator.translations:
process_content(article)
for article in generator.drafts_translations:
process_content(article)
elif isinstance(generator, PagesGenerator):
for page in generator.pages:
process_content(page)
for page in generator.hidden_pages:
process_content(page)
for page in generator.draft_pages:
process_content(page)
for page in generator.translations:
process_content(page)
for page in generator.hidden_translations:
process_content(page)
for page in generator.draft_translations:
process_content(page)
def register():
signals.all_generators_finalized.connect(add_deps)