From b1d2dc549bf27abfeabd4f87afa46307de8387b9 Mon Sep 17 00:00:00 2001 From: carlospolop Date: Mon, 24 Nov 2025 23:42:56 +0100 Subject: [PATCH] Sync hacktricks-preprocessor.py with master (mdbook 0.5.x fix) --- hacktricks-preprocessor.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/hacktricks-preprocessor.py b/hacktricks-preprocessor.py index f19dddbe1..ee68ea548 100644 --- a/hacktricks-preprocessor.py +++ b/hacktricks-preprocessor.py @@ -53,11 +53,17 @@ 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") + result = findtitle(href.split("#")[0], book, "source_path") + if result is None or result[0] is None: + raise Exception(f"Chapter not found") + chapter, _path = result title = " ".join(href.split("#")[1].split("-")).title() logger.debug(f'Ref has # using title: {title}') else: - chapter, _path = findtitle(href, book, "source_path") + result = findtitle(href, book, "source_path") + if result is None or result[0] is None: + raise Exception(f"Chapter not found") + chapter, _path = result logger.debug(f'Recursive title search result: {chapter["name"]}') title = chapter['name'] except Exception as e: @@ -65,11 +71,17 @@ def ref(matchobj): dir = path.dirname(current_chapter['source_path']) logger.debug(f'Error getting chapter title: {href} trying with relative path {path.normpath(path.join(dir,href))}') if "#" in href: - chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path") + result = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path") + if result is None or result[0] is None: + raise Exception(f"Chapter not found") + chapter, _path = result title = " ".join(href.split("#")[1].split("-")).title() logger.debug(f'Ref has # using title: {title}') else: - chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path") + result = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path") + if result is None or result[0] is None: + raise Exception(f"Chapter not found") + chapter, _path = result title = chapter["name"] logger.debug(f'Recursive title search result: {chapter["name"]}') except Exception as e: @@ -147,8 +159,14 @@ if __name__ == '__main__': context, book = json.load(sys.stdin) logger.debug(f"Context: {context}") + logger.debug(f"Book keys: {book.keys()}") - for chapter in iterate_chapters(book['sections']): + # Handle both old (sections) and new (items) mdbook API + book_items = book.get('sections') or book.get('items', []) + + for chapter in iterate_chapters(book_items): + if chapter is None: + continue logger.debug(f"Chapter: {chapter['path']}") current_chapter = chapter # regex = r'{{[\s]*#ref[\s]*}}(?:\n)?([^\\\n]*)(?:\n)?{{[\s]*#endref[\s]*}}'