|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import subprocess |
| 4 | +from bs4 import BeautifulSoup |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +CHAPTER_ID = { |
| 8 | + 1 : 'ch_data_model', |
| 9 | + 2 : 'ch_sequences', |
| 10 | + 3 : 'ch_dicts_sets', |
| 11 | + 4 : 'ch_str_bytes', |
| 12 | + 5 : 'ch_dataclass', |
| 13 | + 6 : 'ch_refs_mut_mem', |
| 14 | + 7 : 'ch_func_objects', |
| 15 | + 8 : 'ch_type_hints_def', |
| 16 | + 9 : 'ch_closure_decorator', |
| 17 | + 10 : 'ch_design_patterns', |
| 18 | + 11 : 'ch_pythonic_obj', |
| 19 | + 12 : 'ch_seq_methods', |
| 20 | + 13 : 'ch_ifaces_prot_abc', |
| 21 | + 14 : 'ch_inheritance', |
| 22 | + 15 : 'ch_more_types', |
| 23 | + 16 : 'ch_op_overload', |
| 24 | + 17 : 'ch_generators', |
| 25 | + 18 : 'ch_with_match', |
| 26 | + 19 : 'ch_concurrency_models', |
| 27 | + 20 : 'ch_executors', |
| 28 | + 21 : 'ch_async', |
| 29 | + 22 : 'ch_dynamic_attrs', |
| 30 | + 23 : 'ch_descriptors', |
| 31 | + 24 : 'ch_class_metaprog', |
| 32 | +} |
| 33 | + |
| 34 | +CHAPTER_NUMBER = {i:n for (n, i) in CHAPTER_ID.items()} |
| 35 | + |
| 36 | + |
| 37 | +def find_git_root(): |
| 38 | + path = Path(__file__).resolve() |
| 39 | + while path != path.parent: |
| 40 | + if (path / '.git').is_dir(): |
| 41 | + return path |
| 42 | + path = path.parent |
| 43 | + raise LookupError(f'no .git dir found in {path} or parents') |
| 44 | + |
| 45 | + |
| 46 | +INVALID_MSG = 'asciidoctor: INFO: possible invalid reference: ' |
| 47 | + |
| 48 | + |
| 49 | +def list_invalid_xrefs() -> list[str]: |
| 50 | + adoc = find_git_root() / 'vol1/vol1.adoc' |
| 51 | + cmd = f'''asciidoctor -v {adoc} -o lixo''' |
| 52 | + result = subprocess.run(cmd, shell=True, stderr=subprocess.PIPE, text=True) |
| 53 | + seen = set() |
| 54 | + xrefs = [] |
| 55 | + for line in result.stderr.splitlines(): |
| 56 | + assert line.startswith(INVALID_MSG), '? msg: ' + line |
| 57 | + xref = line[len(INVALID_MSG):].strip() |
| 58 | + if xref not in seen: |
| 59 | + xrefs.append(xref) |
| 60 | + seen.add(xref) |
| 61 | + |
| 62 | + return xrefs |
| 63 | + |
| 64 | + |
| 65 | +def get_section_title(ident: str, root: BeautifulSoup) -> str: |
| 66 | + element = root.find(id=ident) |
| 67 | + if element: |
| 68 | + return element.get_text(strip=True) |
| 69 | + raise LookupError(f'element {ident!r} not found') |
| 70 | + |
| 71 | + |
| 72 | +def section_numbers(sec_title: str): |
| 73 | + parts = sec_title.split('.') |
| 74 | + numbers = [] |
| 75 | + for part in parts: |
| 76 | + try: |
| 77 | + numbers.append(int(part)) |
| 78 | + except ValueError: |
| 79 | + break |
| 80 | + assert len(numbers) > 0, 'no number prefix: ' + repr(sec_title) |
| 81 | + return numbers |
| 82 | + |
| 83 | +BASE_URL = 'https://pythonfluente.com/2/' |
| 84 | + |
| 85 | +PART_VOL = { |
| 86 | + 'data_structures_part': 1, |
| 87 | + # 'function_objects_part': 2, # may be vol 2 or 3 |
| 88 | + 'classes_protocols_part': 2, |
| 89 | + 'control_flow_part': 3, |
| 90 | + 'metaprog_part': 3, |
| 91 | +} |
| 92 | + |
| 93 | +PART_TITLE = { |
| 94 | + 'data_structures_part': 'I—Estruturas de dados', |
| 95 | + 'function_objects_part': 'II—Funções como objetos', |
| 96 | + 'classes_protocols_part': 'III—Classes e protocolos', |
| 97 | + 'control_flow_part': 'IV—Controle de fluxo', |
| 98 | + 'metaprog_part': 'V—Metaprogramação', |
| 99 | +} |
| 100 | + |
| 101 | +def replace_xrefs_to_vols(): |
| 102 | + html_path = find_git_root() / 'online/index.html' |
| 103 | + with open(html_path) as fp: |
| 104 | + html = fp.read() |
| 105 | + root = BeautifulSoup(html, 'html.parser') |
| 106 | + for xref in list_invalid_xrefs(): |
| 107 | + if xref.startswith('ch_'): |
| 108 | + chapter = CHAPTER_NUMBER[xref] |
| 109 | + text = f'Capítulo {chapter}' |
| 110 | + volume = (chapter - 1) // 8 + 1 |
| 111 | + elif xref.endswith('_part'): |
| 112 | + volume = PART_VOL[xref] |
| 113 | + text = PART_TITLE[xref] |
| 114 | + elif xref.endswith('_sec'): |
| 115 | + title = get_section_title(xref, root) |
| 116 | + numbers = section_numbers(title) |
| 117 | + number_str = '.'.join(str(n) for n in numbers) |
| 118 | + chapter = numbers[0] |
| 119 | + volume = (chapter - 1) // 8 + 1 |
| 120 | + text = f'Seção {number_str}' |
| 121 | + else: |
| 122 | + raise ValueError(f'unexpected xref: {xref!r}') |
| 123 | + link = BASE_URL + '#' + xref |
| 124 | + print(f'<<{xref}>>', f'{text} [vol.{volume}, {link}]') |
| 125 | + |
| 126 | +if __name__ == '__main__': |
| 127 | + replace_xrefs_to_vols() |
0 commit comments