|
| 1 | +from pathlib import Path |
| 2 | +from urllib.parse import urljoin |
| 3 | + |
| 4 | +from bs4 import BeautifulSoup, Tag |
| 5 | + |
| 6 | +from .crawler import Crawler |
| 7 | +from .helpers import has_class |
| 8 | + |
| 9 | + |
| 10 | +class BoostJson(Crawler): |
| 11 | + def crawl(self, library_key: str) -> dict: |
| 12 | + assert library_key == 'json' |
| 13 | + |
| 14 | + index_path = self._boost_root / 'libs' / library_key / 'doc/html' |
| 15 | + |
| 16 | + sections = {} |
| 17 | + for file_path in Path(index_path).parent.rglob('*.html'): |
| 18 | + with open(file_path, 'r', encoding='utf-8', errors='ignore') as file: |
| 19 | + soup = BeautifulSoup(file.read(), 'html.parser') |
| 20 | + if 'ref' in file_path.parts : |
| 21 | + self._extract_reference(str(file_path), sections, soup.select_one('body div.boostlook')) |
| 22 | + else : |
| 23 | + for sect1 in soup.select('body div[id="content"] > .sect1'): |
| 24 | + self._extract_section_n(str(file_path), sections, sect1) |
| 25 | + |
| 26 | + return sections |
| 27 | + |
| 28 | + def _extract_reference(self, index_path: str, sections: dict, boostlook: Tag): |
| 29 | + lvls = [] |
| 30 | + for link in boostlook.select('nav[id="breadcrumbs"] ul li:not(:first-child) > a'): |
| 31 | + lvls = lvls + [{'title': link.text.split("::")[-1], 'path': urljoin(index_path, link.get('href'))}] |
| 32 | + |
| 33 | + header = boostlook.select_one('h1, h2, h3, h4, h5, h6') |
| 34 | + path = lvls[-1]['path'] |
| 35 | + |
| 36 | + if header.find_next_sibling() and has_class(header.find_next_sibling(), 'sectionbody'): |
| 37 | + siblings = header.find_next_sibling().find_all(recursive=False) |
| 38 | + else: |
| 39 | + siblings = header.next_siblings |
| 40 | + |
| 41 | + content = '' |
| 42 | + for sibling in siblings: |
| 43 | + if isinstance(sibling, Tag) and sibling.has_attr('class') and len([i for i in sibling.get('class') if i.startswith('sect')]) > 0: |
| 44 | + self._extract_section_n(index_path, sections, sibling, lvls) |
| 45 | + continue |
| 46 | + content += sibling.get_text() + ' ' |
| 47 | + |
| 48 | + sections[path] = {'content': content, 'lvls': lvls} |
| 49 | + |
| 50 | + |
| 51 | + def _extract_section_n(self, index_path: str, sections: dict, sect: Tag, lvls: list = []): |
| 52 | + header = sect.select_one('h1, h2, h3, h4, h5, h6') |
| 53 | + title = header.text |
| 54 | + path = index_path + '#' + header.get('id') |
| 55 | + lvls = lvls + [{'title': title, 'path': path}] |
| 56 | + |
| 57 | + if header.find_next_sibling() and has_class(header.find_next_sibling(), 'sectionbody'): |
| 58 | + siblings = header.find_next_sibling().find_all(recursive=False) |
| 59 | + else: |
| 60 | + siblings = header.next_siblings |
| 61 | + |
| 62 | + content = '' |
| 63 | + for sibling in siblings: |
| 64 | + if isinstance(sibling, Tag) and sibling.has_attr('class') and len([i for i in sibling.get('class') if i.startswith('sect')]) > 0: |
| 65 | + self._extract_section_n(index_path, sections, sibling, lvls) |
| 66 | + continue |
| 67 | + content += sibling.get_text() + ' ' |
| 68 | + |
| 69 | + sections[path] = {'content': content, 'lvls': lvls} |
0 commit comments