|
17 | 17 | logger.addHandler(handler2) |
18 | 18 |
|
19 | 19 |
|
20 | | -def findtitle(search ,obj, key, path=(),): |
| 20 | +def findtitle(search, obj, key, path=()): |
21 | 21 | # logger.debug(f"Looking for {search} in {path}") |
22 | 22 | if isinstance(obj, dict) and key in obj and obj[key] == search: |
23 | 23 | return obj, path |
@@ -54,26 +54,42 @@ def ref(matchobj): |
54 | 54 | if href.endswith("/"): |
55 | 55 | href = href+"README.md" # Fix if ref points to a folder |
56 | 56 | if "#" in href: |
57 | | - chapter, _path = findtitle(href.split("#")[0], book, "source_path") |
58 | | - title = " ".join(href.split("#")[1].split("-")).title() |
59 | | - logger.debug(f'Ref has # using title: {title}') |
| 57 | + result = findtitle(href.split("#")[0], book, "source_path") |
| 58 | + if result is not None: |
| 59 | + chapter, _path = result |
| 60 | + title = " ".join(href.split("#")[1].split("-")).title() |
| 61 | + logger.debug(f'Ref has # using title: {title}') |
| 62 | + else: |
| 63 | + raise Exception(f"Chapter not found for path: {href.split('#')[0]}") |
60 | 64 | else: |
61 | | - chapter, _path = findtitle(href, book, "source_path") |
62 | | - logger.debug(f'Recursive title search result: {chapter["name"]}') |
63 | | - title = chapter['name'] |
| 65 | + result = findtitle(href, book, "source_path") |
| 66 | + if result is not None: |
| 67 | + chapter, _path = result |
| 68 | + logger.debug(f'Recursive title search result: {chapter["name"]}') |
| 69 | + title = chapter['name'] |
| 70 | + else: |
| 71 | + raise Exception(f"Chapter not found for path: {href}") |
64 | 72 | except Exception as e: |
65 | 73 | dir = path.dirname(current_chapter['source_path']) |
66 | 74 | rel_path = path.normpath(path.join(dir,href)) |
67 | 75 | try: |
68 | 76 | logger.debug(f'Not found chapter title from: {href} -- trying with relative path {rel_path}') |
69 | 77 | if "#" in href: |
70 | | - chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path") |
71 | | - title = " ".join(href.split("#")[1].split("-")).title() |
72 | | - logger.debug(f'Ref has # using title: {title}') |
| 78 | + result = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path") |
| 79 | + if result is not None: |
| 80 | + chapter, _path = result |
| 81 | + title = " ".join(href.split("#")[1].split("-")).title() |
| 82 | + logger.debug(f'Ref has # using title: {title}') |
| 83 | + else: |
| 84 | + raise Exception(f"Chapter not found for relative path: {path.normpath(path.join(dir,href.split('#')[0]))}") |
73 | 85 | else: |
74 | | - chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path") |
75 | | - title = chapter["name"] |
76 | | - logger.debug(f'Recursive title search result: {chapter["name"]}') |
| 86 | + result = findtitle(path.normpath(path.join(dir,href)), book, "source_path") |
| 87 | + if result is not None: |
| 88 | + chapter, _path = result |
| 89 | + title = chapter["name"] |
| 90 | + logger.debug(f'Recursive title search result: {chapter["name"]}') |
| 91 | + else: |
| 92 | + raise Exception(f"Chapter not found for relative path: {path.normpath(path.join(dir,href))}") |
77 | 93 | except Exception as e: |
78 | 94 | logger.debug(e) |
79 | 95 | logger.error(f'Error getting chapter title: {rel_path}') |
|
0 commit comments