Skip to content

Commit

Permalink
Merge pull request #152 from canonical/wd-2017
Browse files Browse the repository at this point in the history
Generate toc based on heading anchor tags
  • Loading branch information
petesfrench authored Feb 21, 2023
2 parents 9bbc1f4 + a4f5431 commit f4e1be8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions canonicalwebteam/discourse/parsers/docs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Standard library
import os
import re
from urllib.parse import urlparse

# Packages
Expand Down Expand Up @@ -120,11 +121,13 @@ def parse_topic(self, topic, docs_version=""):

self._replace_lightbox(soup)
sections = self._get_sections(soup)
headings_map = self._generate_headings_map(soup)

return {
"title": topic["title"],
"body_html": str(soup),
"sections": sections,
"headings_map": headings_map,
"updated": humanize.naturaltime(
updated_datetime.replace(tzinfo=None)
),
Expand Down Expand Up @@ -709,3 +712,35 @@ def _replace_tutorials(self, tutorial_tables, tutorial_data):
table["soup_table"].replace_with(
BeautifulSoup(card, features="html.parser")
)

def _generate_headings_map(self, soup):
headings_map = []
current_list = headings_map
previous_tag = None

headings = soup.find_all(["h2", "h3"])

for heading in headings:
current_tag = re.findall(r"\d", heading.name)[0]
if previous_tag and previous_tag < current_tag:
current_list = []
elif previous_tag and previous_tag > current_tag:
current_list = headings_map

if heading.a and heading.a.has_attr("name"):
current_list.append(
(
{
"heading_level": current_tag,
"heading_text": re.sub("\n", "", heading.text),
"heading_slug": heading.a["name"],
}
)
)

if previous_tag and previous_tag <= current_tag:
headings_map[-1]["children"] = current_list

previous_tag = current_tag

return headings_map
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="canonicalwebteam.discourse",
version="5.0.4",
version="5.1.4",
author="Canonical webteam",
author_email="webteam@canonical.com",
url=(
Expand Down

0 comments on commit f4e1be8

Please sign in to comment.