|
| 1 | +import re |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | +from urllib.parse import urljoin |
| 5 | +from bs4 import BeautifulSoup, Tag |
| 6 | + |
| 7 | +from .crawlers.helpers import has_class |
| 8 | +from .config import config |
| 9 | + |
| 10 | + |
| 11 | +class AntoraDoc(): |
| 12 | + def crawl(self, doc_root: Path) -> dict: |
| 13 | + sections = {} |
| 14 | + doc_root = doc_root.resolve() |
| 15 | + |
| 16 | + for file_path in doc_root.rglob('*.html'): |
| 17 | + with open(file_path, 'r', encoding='utf-8', errors='ignore') as file: |
| 18 | + soup = BeautifulSoup(file.read(), 'html.parser') |
| 19 | + |
| 20 | + lvls = [] |
| 21 | + for link in soup.select('body nav.breadcrumbs ul li a'): |
| 22 | + lvls = lvls + [{'title': link.text, 'path': urljoin(str(file_path), link.get('href'))}] |
| 23 | + |
| 24 | + sect1 = soup.select_one('body article.doc') |
| 25 | + if sect1: |
| 26 | + self._extract_section_n(str(file_path), sections, sect1, lvls) |
| 27 | + |
| 28 | + return sections |
| 29 | + |
| 30 | + def _extract_section_n(self, file_path: str, sections: dict, sect: Tag, lvls: list = []): |
| 31 | + header = sect.select_one('h1, h2, h3, h4, h5, h6') |
| 32 | + |
| 33 | + if header.name == 'h1': |
| 34 | + path = file_path |
| 35 | + else: |
| 36 | + title = header.text |
| 37 | + path = file_path + '#' + header.get('id') |
| 38 | + lvls = lvls + [{'title': title, 'path': path}] |
| 39 | + |
| 40 | + if header.find_next_sibling() and has_class(header.find_next_sibling(), 'sectionbody'): |
| 41 | + siblings = header.find_next_sibling().find_all(recursive=False) |
| 42 | + else: |
| 43 | + siblings = header.next_siblings |
| 44 | + |
| 45 | + content = '' |
| 46 | + for sibling in siblings: |
| 47 | + if isinstance(sibling, Tag) and sibling.has_attr('class') and len([i for i in sibling.get('class') if i.startswith('sect')]) > 0: |
| 48 | + self._extract_section_n(file_path, sections, sibling, lvls) |
| 49 | + continue |
| 50 | + content += sibling.get_text() + ' ' |
| 51 | + |
| 52 | + sections[path] = {'content': content, 'lvls': lvls} |
| 53 | + |
| 54 | + |
| 55 | +def create_algolia_records(section_key: str, section_name: str, doc_root: Path, sections: dict): |
| 56 | + doc_root = doc_root.resolve() |
| 57 | + records = [] |
| 58 | + |
| 59 | + for _, section in sections.items(): |
| 60 | + for lvl in section['lvls']: |
| 61 | + lvl['path'] = lvl['path'].replace(str(doc_root) + '/', '') |
| 62 | + |
| 63 | + records.append({ |
| 64 | + 'type': 'content', |
| 65 | + 'section_key': section_key, |
| 66 | + 'section_name': section_name, |
| 67 | + 'content': re.sub(r'\s+', ' ', section['content']).strip(), |
| 68 | + 'weight': { |
| 69 | + 'pageRank': 0, |
| 70 | + 'level': 100 - len(section['lvls']) * 10, |
| 71 | + 'position': 0 |
| 72 | + }, |
| 73 | + 'path': section['lvls'][-1]['path'] if len(section['lvls']) > 0 else None, |
| 74 | + 'hierarchy': { |
| 75 | + 'lvl0': section['lvls'][0] if len(section['lvls']) > 0 else None, |
| 76 | + 'lvl1': section['lvls'][1] if len(section['lvls']) > 1 else None, |
| 77 | + 'lvl2': section['lvls'][2] if len(section['lvls']) > 2 else None, |
| 78 | + 'lvl3': section['lvls'][3] if len(section['lvls']) > 3 else None, |
| 79 | + 'lvl4': section['lvls'][4] if len(section['lvls']) > 4 else None, |
| 80 | + 'lvl5': section['lvls'][5] if len(section['lvls']) > 5 else None, |
| 81 | + 'lvl6': section['lvls'][6] if len(section['lvls']) > 6 else None |
| 82 | + }}) |
| 83 | + |
| 84 | + with open('./algolia_records/learn/' + section_key + '.json', 'w', encoding='utf-8') as outfile: |
| 85 | + json.dump(records, outfile, indent=4) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + crawler = AntoraDoc() |
| 90 | + |
| 91 | + for section in config['website-v2-docs']['sections']: |
| 92 | + sections = crawler.crawl(Path(config['website-v2-docs']['root']) / section['key']) |
| 93 | + create_algolia_records(section['key'], |
| 94 | + section['name'], |
| 95 | + Path(config['website-v2-docs']['root']), |
| 96 | + sections) |
0 commit comments