From a92f4c8447f61443e2332373ec8d800710bc71fb Mon Sep 17 00:00:00 2001 From: carlospolop Date: Mon, 29 Sep 2025 11:33:02 +0200 Subject: [PATCH] fix preprocessor --- hacktricks-preprocessor.py | 42 ++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/hacktricks-preprocessor.py b/hacktricks-preprocessor.py index 0165a854a..c525f665d 100644 --- a/hacktricks-preprocessor.py +++ b/hacktricks-preprocessor.py @@ -17,7 +17,7 @@ handler2.setLevel(logging.ERROR) logger.addHandler(handler2) -def findtitle(search ,obj, key, path=(),): +def findtitle(search, obj, key, path=()): # logger.debug(f"Looking for {search} in {path}") if isinstance(obj, dict) and key in obj and obj[key] == search: return obj, path @@ -54,26 +54,42 @@ def ref(matchobj): if href.endswith("/"): href = href+"README.md" # Fix if ref points to a folder if "#" in href: - chapter, _path = findtitle(href.split("#")[0], book, "source_path") - title = " ".join(href.split("#")[1].split("-")).title() - logger.debug(f'Ref has # using title: {title}') + result = findtitle(href.split("#")[0], book, "source_path") + if result is not None: + chapter, _path = result + title = " ".join(href.split("#")[1].split("-")).title() + logger.debug(f'Ref has # using title: {title}') + else: + raise Exception(f"Chapter not found for path: {href.split('#')[0]}") else: - chapter, _path = findtitle(href, book, "source_path") - logger.debug(f'Recursive title search result: {chapter["name"]}') - title = chapter['name'] + result = findtitle(href, book, "source_path") + if result is not None: + chapter, _path = result + logger.debug(f'Recursive title search result: {chapter["name"]}') + title = chapter['name'] + else: + raise Exception(f"Chapter not found for path: {href}") except Exception as e: dir = path.dirname(current_chapter['source_path']) rel_path = path.normpath(path.join(dir,href)) try: logger.debug(f'Not found chapter title from: {href} -- trying with relative path {rel_path}') if "#" in href: - chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path") - title = " ".join(href.split("#")[1].split("-")).title() - logger.debug(f'Ref has # using title: {title}') + result = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path") + if result is not None: + chapter, _path = result + title = " ".join(href.split("#")[1].split("-")).title() + logger.debug(f'Ref has # using title: {title}') + else: + raise Exception(f"Chapter not found for relative path: {path.normpath(path.join(dir,href.split('#')[0]))}") else: - chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path") - title = chapter["name"] - logger.debug(f'Recursive title search result: {chapter["name"]}') + result = findtitle(path.normpath(path.join(dir,href)), book, "source_path") + if result is not None: + chapter, _path = result + title = chapter["name"] + logger.debug(f'Recursive title search result: {chapter["name"]}') + else: + raise Exception(f"Chapter not found for relative path: {path.normpath(path.join(dir,href))}") except Exception as e: logger.debug(e) logger.error(f'Error getting chapter title: {rel_path}')