From 2b9a05110e3c096d6caabe68a929a9815ed69a27 Mon Sep 17 00:00:00 2001 From: Congon4tor Date: Thu, 2 Jan 2025 21:18:12 +0100 Subject: [PATCH] Add language selector --- book.toml | 1 + hacktricks-preprocessor.py | 18 +++++---- theme/book.js | 56 +++++++++++++++++++++++++++ theme/css/chrome.css | 53 ++++++++++++++++++++++++-- theme/index.hbs | 78 +++++++++++++++++++++++++------------- theme/sponsor.js | 2 - 6 files changed, 169 insertions(+), 39 deletions(-) diff --git a/book.toml b/book.toml index e3330d5e6b..f20e2d2a29 100644 --- a/book.toml +++ b/book.toml @@ -22,6 +22,7 @@ after = ["links"] [preprocessor.hacktricks] command = "python3 ./hacktricks-preprocessor.py" +env = "prod" [output.html] additional-css = ["theme/pagetoc.css", "theme/tabs.css"] diff --git a/hacktricks-preprocessor.py b/hacktricks-preprocessor.py index 37f5491011..8e67ab77b8 100644 --- a/hacktricks-preprocessor.py +++ b/hacktricks-preprocessor.py @@ -30,14 +30,16 @@ def ref(matchobj): href = matchobj.groups(0)[0].strip() title = href if href.startswith("http://") or href.startswith("https://"): - # pass - 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('(.*?)', raw_html) - title = match.group(1) if match else href - except Exception as e: - logger.debug(f'Error opening URL {href}: {e}') - pass #nDont stop on broken link + 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('(.*?)', raw_html) + title = match.group(1) if match else href + except Exception as e: + logger.debug(f'Error opening URL {href}: {e}') + pass #nDont stop on broken link else: try: if href.endswith("/"): diff --git a/theme/book.js b/theme/book.js index 1c8d772870..b11674b5a5 100644 --- a/theme/book.js +++ b/theme/book.js @@ -590,6 +590,62 @@ function playground_text(playground, hidden = true) { }); })(); + + +(function menubarLanguage() { + var menubarLanguageToggleButton = document.getElementById('menubar-languages-toggle'); + var menubarLanguagePopup = document.getElementById('menubar-languages-popup'); + var languageButtons = menubarLanguagePopup.querySelectorAll('.menu-bar-link'); + + function showLanguage() { + menubarLanguagePopup.style.display = 'flex'; + menubarLanguageToggleButton.setAttribute('aria-expanded', true); + } + + function hideLanguage() { + menubarLanguagePopup.style.display = 'none'; + menubarLanguageToggleButton.setAttribute('aria-expanded', false); + menubarLanguageToggleButton.focus(); + } + + menubarLanguageToggleButton.addEventListener('click', function () { + if (menubarLanguagePopup.style.display === 'flex') { + hideLanguage(); + } else { + showLanguage(); + } + }); + + menubarLanguagePopup.addEventListener('focusout', function(e) { + // e.relatedTarget is null in Safari and Firefox on macOS (see workaround below) + if (!!e.relatedTarget && !menubarLanguageToggleButton.contains(e.relatedTarget) && !menubarLanguagePopup.contains(e.relatedTarget)) { + hideLanguage(); + } + }); + + // Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang/mdBook/issues/628 + document.addEventListener('click', function(e) { + if (menubarLanguagePopup.style.display === 'block' && !menubarLanguageToggleButton.contains(e.target) && !menubarLanguagePopup.contains(e.target)) { + hideLanguage(); + } + }); + + languageButtons.forEach((btn) => { + btn.addEventListener('click', function(e) { + const regex = /(?:(?:\/)+(?[a-z]{2}(?=\/|$)))?(?(?:\/)*.*)?/g + var match = regex.exec(window.location.pathname) + + var path = match.groups.path + console.log(`Path: ${path} ${typeof path}`) + + const lang = match.groups.lang + console.log(`Lang: ${lang}`) + + window.location = `/${e.target.id}${path}${window.location.hash}` + }); + }) +})(); + (function chapterNavigation() { document.addEventListener('keydown', function (e) { if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; } diff --git a/theme/css/chrome.css b/theme/css/chrome.css index 8cbda7c693..17e5411639 100644 --- a/theme/css/chrome.css +++ b/theme/css/chrome.css @@ -83,6 +83,13 @@ body.sidebar-visible #menu-bar { } } +.right-buttons .icons { + display: flex; + flex-direction: row; + flex-wrap: wrap; + column-gap: 0.5rem; +} + .icon-button { border: none; background: var(--bg); @@ -138,11 +145,13 @@ body.sidebar-visible #menu-bar { } /* Collapse Menu Popup */ - +#menubar-collapse-toggle { + position: relative; +} #menubar-collapse-popup { position: absolute; - right: 30px; - top: var(--menu-bar-height); + right: 0px; + top: 35px; z-index: 105; border-radius: 5px; font-size: 14px; @@ -172,6 +181,44 @@ body.sidebar-visible #menu-bar { background-color: var(--theme-hover); } +/* Languages Menu Popup */ +#menubar-languages-toggle { + position: relative; +} + +#menubar-languages-popup { + position: absolute; + right: 0px; + top: 35px; + z-index: 105; + border-radius: 5px; + font-size: 14px; + color: var(--fg); + background: var(--bg); + border: 1px solid var(--table-border-color); + margin: 0; + padding: 0px; + display: none; + flex-direction: column; + max-height: 300px; + width: 150px; + overflow: scroll; +} +#menubar-languages-popup .menu-bar-link { + border: 0; + margin: 0; + padding: 8px 20px; + line-height: 25px; + white-space: nowrap; + text-align: start; + cursor: pointer; + color: inherit; + background: inherit; + font-size: inherit; +} +#menubar-languages-popup .menu-bar-link:hover { + background-color: var(--theme-hover); +} .left-buttons { display: flex; diff --git a/theme/index.hbs b/theme/index.hbs index 049bc3ea72..419bb6e7aa 100644 --- a/theme/index.hbs +++ b/theme/index.hbs @@ -144,34 +144,60 @@