diff --git a/book.toml b/book.toml index 15214d764..a01a3d751 100644 --- a/book.toml +++ b/book.toml @@ -8,25 +8,15 @@ title = "HackTricks Cloud" create-missing = false extra-watch-dirs = ["translations"] -[preprocessor.alerts] -after = ["links"] - -[preprocessor.reading-time] - -[preprocessor.pagetoc] - [preprocessor.tabs] -[preprocessor.codename] - [preprocessor.hacktricks] command = "python3 ./hacktricks-preprocessor.py" env = "prod" [output.html] -additional-css = ["theme/pagetoc.css", "theme/tabs.css"] +additional-css = ["theme/tabs.css"] additional-js = [ - "theme/pagetoc.js", "theme/tabs.js", "theme/ht_searcher.js", "theme/sponsor.js", @@ -35,6 +25,7 @@ additional-js = [ no-section-label = true preferred-dark-theme = "hacktricks-dark" default-theme = "hacktricks-light" +hash-files = false [output.html.fold] enable = true # whether or not to enable section folding diff --git a/hacktricks-preprocessor.py b/hacktricks-preprocessor.py index f19dddbe1..25086220e 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,12 @@ 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): logger.debug(f"Chapter: {chapter['path']}") current_chapter = chapter # regex = r'{{[\s]*#ref[\s]*}}(?:\n)?([^\\\n]*)(?:\n)?{{[\s]*#endref[\s]*}}' diff --git a/hacktricks-preprocessor.py.bak b/hacktricks-preprocessor.py.bak new file mode 100644 index 000000000..f19dddbe1 --- /dev/null +++ b/hacktricks-preprocessor.py.bak @@ -0,0 +1,166 @@ +import json +import os +import sys +import re +import logging +from os import path +from urllib.request import urlopen, Request + +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) +handler = logging.FileHandler(filename='hacktricks-preprocessor.log', mode='w', encoding='utf-8') +handler.setLevel(logging.DEBUG) +logger.addHandler(handler) + +handler2 = logging.FileHandler(filename='hacktricks-preprocessor-error.log', mode='w', encoding='utf-8') +handler2.setLevel(logging.ERROR) +logger.addHandler(handler2) + + +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 + if isinstance(obj, list): + for k, v in enumerate(obj): + item = findtitle(search, v, key, (*path, k)) + if item is not None: + return item + if isinstance(obj, dict): + for k, v in obj.items(): + item = findtitle(search, v, key, (*path, k)) + if item is not None: + return item + + +def ref(matchobj): + logger.debug(f'Ref match: {matchobj.groups(0)[0].strip()}') + href = matchobj.groups(0)[0].strip() + title = href + if href.startswith("http://") or href.startswith("https://"): + if context['config']['preprocessor']['hacktricks']['env'] == 'dev': + pass + else: + try: + raw_html = str(urlopen(Request(href, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0'})).read()) + match = re.search('