Skip to content

Commit a92f4c8

Browse files
committed
fix preprocessor
1 parent 8f2203f commit a92f4c8

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

hacktricks-preprocessor.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
logger.addHandler(handler2)
1818

1919

20-
def findtitle(search ,obj, key, path=(),):
20+
def findtitle(search, obj, key, path=()):
2121
# logger.debug(f"Looking for {search} in {path}")
2222
if isinstance(obj, dict) and key in obj and obj[key] == search:
2323
return obj, path
@@ -54,26 +54,42 @@ def ref(matchobj):
5454
if href.endswith("/"):
5555
href = href+"README.md" # Fix if ref points to a folder
5656
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]}")
6064
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}")
6472
except Exception as e:
6573
dir = path.dirname(current_chapter['source_path'])
6674
rel_path = path.normpath(path.join(dir,href))
6775
try:
6876
logger.debug(f'Not found chapter title from: {href} -- trying with relative path {rel_path}')
6977
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]))}")
7385
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))}")
7793
except Exception as e:
7894
logger.debug(e)
7995
logger.error(f'Error getting chapter title: {rel_path}')

0 commit comments

Comments
 (0)