Skip to content

Commit b510992

Browse files
committed
f
1 parent b054fe5 commit b510992

File tree

4 files changed

+203
-30
lines changed

4 files changed

+203
-30
lines changed

book.toml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,15 @@ title = "HackTricks Cloud"
88
create-missing = false
99
extra-watch-dirs = ["translations"]
1010

11-
[preprocessor.alerts]
12-
after = ["links"]
13-
14-
[preprocessor.reading-time]
15-
16-
[preprocessor.pagetoc]
17-
1811
[preprocessor.tabs]
1912

20-
[preprocessor.codename]
21-
2213
[preprocessor.hacktricks]
2314
command = "python3 ./hacktricks-preprocessor.py"
2415
env = "prod"
2516

2617
[output.html]
27-
additional-css = ["theme/pagetoc.css", "theme/tabs.css"]
18+
additional-css = ["theme/tabs.css"]
2819
additional-js = [
29-
"theme/pagetoc.js",
3020
"theme/tabs.js",
3121
"theme/ht_searcher.js",
3222
"theme/sponsor.js",
@@ -35,6 +25,7 @@ additional-js = [
3525
no-section-label = true
3626
preferred-dark-theme = "hacktricks-dark"
3727
default-theme = "hacktricks-light"
28+
hash-files = false
3829

3930
[output.html.fold]
4031
enable = true # whether or not to enable section folding

hacktricks-preprocessor.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,35 @@ def ref(matchobj):
5353
if href.endswith("/"):
5454
href = href+"README.md" # Fix if ref points to a folder
5555
if "#" in href:
56-
chapter, _path = findtitle(href.split("#")[0], book, "source_path")
56+
result = findtitle(href.split("#")[0], book, "source_path")
57+
if result is None or result[0] is None:
58+
raise Exception(f"Chapter not found")
59+
chapter, _path = result
5760
title = " ".join(href.split("#")[1].split("-")).title()
5861
logger.debug(f'Ref has # using title: {title}')
5962
else:
60-
chapter, _path = findtitle(href, book, "source_path")
63+
result = findtitle(href, book, "source_path")
64+
if result is None or result[0] is None:
65+
raise Exception(f"Chapter not found")
66+
chapter, _path = result
6167
logger.debug(f'Recursive title search result: {chapter["name"]}')
6268
title = chapter['name']
6369
except Exception as e:
6470
try:
6571
dir = path.dirname(current_chapter['source_path'])
6672
logger.debug(f'Error getting chapter title: {href} trying with relative path {path.normpath(path.join(dir,href))}')
6773
if "#" in href:
68-
chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path")
74+
result = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path")
75+
if result is None or result[0] is None:
76+
raise Exception(f"Chapter not found")
77+
chapter, _path = result
6978
title = " ".join(href.split("#")[1].split("-")).title()
7079
logger.debug(f'Ref has # using title: {title}')
7180
else:
72-
chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path")
81+
result = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path")
82+
if result is None or result[0] is None:
83+
raise Exception(f"Chapter not found")
84+
chapter, _path = result
7385
title = chapter["name"]
7486
logger.debug(f'Recursive title search result: {chapter["name"]}')
7587
except Exception as e:
@@ -147,8 +159,12 @@ def iterate_chapters(sections):
147159
context, book = json.load(sys.stdin)
148160

149161
logger.debug(f"Context: {context}")
162+
logger.debug(f"Book keys: {book.keys()}")
150163

151-
for chapter in iterate_chapters(book['sections']):
164+
# Handle both old (sections) and new (items) mdbook API
165+
book_items = book.get('sections') or book.get('items', [])
166+
167+
for chapter in iterate_chapters(book_items):
152168
logger.debug(f"Chapter: {chapter['path']}")
153169
current_chapter = chapter
154170
# regex = r'{{[\s]*#ref[\s]*}}(?:\n)?([^\\\n]*)(?:\n)?{{[\s]*#endref[\s]*}}'

hacktricks-preprocessor.py.bak

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import json
2+
import os
3+
import sys
4+
import re
5+
import logging
6+
from os import path
7+
from urllib.request import urlopen, Request
8+
9+
logger = logging.getLogger(__name__)
10+
logger.setLevel(logging.DEBUG)
11+
handler = logging.FileHandler(filename='hacktricks-preprocessor.log', mode='w', encoding='utf-8')
12+
handler.setLevel(logging.DEBUG)
13+
logger.addHandler(handler)
14+
15+
handler2 = logging.FileHandler(filename='hacktricks-preprocessor-error.log', mode='w', encoding='utf-8')
16+
handler2.setLevel(logging.ERROR)
17+
logger.addHandler(handler2)
18+
19+
20+
def findtitle(search ,obj, key, path=(),):
21+
# logger.debug(f"Looking for {search} in {path}")
22+
if isinstance(obj, dict) and key in obj and obj[key] == search:
23+
return obj, path
24+
if isinstance(obj, list):
25+
for k, v in enumerate(obj):
26+
item = findtitle(search, v, key, (*path, k))
27+
if item is not None:
28+
return item
29+
if isinstance(obj, dict):
30+
for k, v in obj.items():
31+
item = findtitle(search, v, key, (*path, k))
32+
if item is not None:
33+
return item
34+
35+
36+
def ref(matchobj):
37+
logger.debug(f'Ref match: {matchobj.groups(0)[0].strip()}')
38+
href = matchobj.groups(0)[0].strip()
39+
title = href
40+
if href.startswith("http://") or href.startswith("https://"):
41+
if context['config']['preprocessor']['hacktricks']['env'] == 'dev':
42+
pass
43+
else:
44+
try:
45+
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())
46+
match = re.search('<title>(.*?)</title>', raw_html)
47+
title = match.group(1) if match else href
48+
except Exception as e:
49+
logger.error(f'Error opening URL {href}: {e}')
50+
pass #Dont stop on broken link
51+
else:
52+
try:
53+
if href.endswith("/"):
54+
href = href+"README.md" # Fix if ref points to a folder
55+
if "#" in href:
56+
chapter, _path = findtitle(href.split("#")[0], book, "source_path")
57+
title = " ".join(href.split("#")[1].split("-")).title()
58+
logger.debug(f'Ref has # using title: {title}')
59+
else:
60+
chapter, _path = findtitle(href, book, "source_path")
61+
logger.debug(f'Recursive title search result: {chapter["name"]}')
62+
title = chapter['name']
63+
except Exception as e:
64+
try:
65+
dir = path.dirname(current_chapter['source_path'])
66+
logger.debug(f'Error getting chapter title: {href} trying with relative path {path.normpath(path.join(dir,href))}')
67+
if "#" in href:
68+
chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path")
69+
title = " ".join(href.split("#")[1].split("-")).title()
70+
logger.debug(f'Ref has # using title: {title}')
71+
else:
72+
chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path")
73+
title = chapter["name"]
74+
logger.debug(f'Recursive title search result: {chapter["name"]}')
75+
except Exception as e:
76+
logger.error(f"Error: {e}")
77+
logger.error(f'Error getting chapter title: {path.normpath(path.join(dir,href))}')
78+
sys.exit(1)
79+
80+
if href.endswith("/README.md"):
81+
href = href.replace("/README.md", "/index.html")
82+
83+
template = f"""<a class="content_ref" href="{href}"><span class="content_ref_label">{title}</span></a>"""
84+
85+
# translate_table = str.maketrans({"\"":"\\\"","\n":"\\n"})
86+
# translated_text = template.translate(translate_table)
87+
result = template
88+
89+
return result
90+
91+
92+
def files(matchobj):
93+
logger.debug(f'Files match: {matchobj.groups(0)[0].strip()}')
94+
href = matchobj.groups(0)[0].strip()
95+
title = ""
96+
97+
try:
98+
for root, dirs, files in os.walk(os.getcwd()+'/src/files'):
99+
logger.debug(root)
100+
logger.debug(files)
101+
if href in files:
102+
title = href
103+
logger.debug(f'File search result: {os.path.join(root, href)}')
104+
105+
except Exception as e:
106+
logger.error(f"Error: {e}")
107+
logger.error(f'Error searching file: {href}')
108+
sys.exit(1)
109+
110+
if title=="":
111+
logger.error(f'Error searching file: {href}')
112+
sys.exit(1)
113+
114+
template = f"""<a class="content_ref" href="/files/{href}"><span class="content_ref_label">{title}</span></a>"""
115+
116+
result = template
117+
118+
return result
119+
120+
121+
def add_read_time(content):
122+
regex = r'(<\/style>\n# .*(?=\n))'
123+
new_content = re.sub(regex, lambda x: x.group(0) + "\n\nReading time: {{ #reading_time }}", content)
124+
return new_content
125+
126+
127+
def iterate_chapters(sections):
128+
if isinstance(sections, dict) and "PartTitle" in sections: # Not a chapter section
129+
return
130+
elif isinstance(sections, dict) and "Chapter" in sections: # Is a chapter return it and look into sub items
131+
# logger.debug(f"Chapter {sections['Chapter']}")
132+
yield sections['Chapter']
133+
yield from iterate_chapters(sections['Chapter']["sub_items"])
134+
elif isinstance(sections, list): # Iterate through list when in sections and in sub_items
135+
for k, v in enumerate(sections):
136+
yield from iterate_chapters(v)
137+
138+
139+
if __name__ == '__main__':
140+
global context, book, current_chapter
141+
if len(sys.argv) > 1: # we check if we received any argument
142+
if sys.argv[1] == "supports":
143+
# then we are good to return an exit status code of 0, since the other argument will just be the renderer's name
144+
sys.exit(0)
145+
logger.debug('Started hacktricks preprocessor')
146+
# load both the context and the book representations from stdin
147+
context, book = json.load(sys.stdin)
148+
149+
logger.debug(f"Context: {context}")
150+
151+
for chapter in iterate_chapters(book['sections']):
152+
logger.debug(f"Chapter: {chapter['path']}")
153+
current_chapter = chapter
154+
# regex = r'{{[\s]*#ref[\s]*}}(?:\n)?([^\\\n]*)(?:\n)?{{[\s]*#endref[\s]*}}'
155+
regex = r'{{[\s]*#ref[\s]*}}(?:\n)?([^\\\n#]*(?:#(.*))?)(?:\n)?{{[\s]*#endref[\s]*}}'
156+
new_content = re.sub(regex, ref, chapter['content'])
157+
regex = r'{{[\s]*#file[\s]*}}(?:\n)?([^\\\n]*)(?:\n)?{{[\s]*#endfile[\s]*}}'
158+
new_content = re.sub(regex, files, new_content)
159+
new_content = add_read_time(new_content)
160+
chapter['content'] = new_content
161+
162+
content = json.dumps(book)
163+
logger.debug(content)
164+
165+
166+
print(content)

theme/index.hbs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,33 +255,33 @@
255255

256256
<nav class="nav-wrapper" aria-label="Page navigation">
257257
<!-- Mobile navigation buttons -->
258-
{{#previous}}
259-
<a rel="prev" href="{{ path_to_root }}{{link}}" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
258+
{{#if previous}}
259+
<a rel="prev" href="{{ path_to_root }}{{previous.path}}" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
260260
<i class="fa fa-angle-left"></i>
261261
</a>
262-
{{/previous}}
262+
{{/if}}
263263

264-
{{#next}}
265-
<a rel="next prefetch" href="{{ path_to_root }}{{link}}" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
264+
{{#if next}}
265+
<a rel="next prefetch" href="{{ path_to_root }}{{next.path}}" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
266266
<i class="fa fa-angle-right"></i>
267267
</a>
268-
{{/next}}
268+
{{/if}}
269269

270270
<div style="clear: both"></div>
271271
</nav>
272272

273273
<nav class="nav-wide-wrapper" aria-label="Page navigation">
274-
{{#previous}}
275-
<a rel="prev" href="{{ path_to_root }}{{link}}" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
276-
<i class="fa fa-angle-left"></i><span style="font-size: medium; align-self: center; margin-left: 10px;">{{title}}</span>
274+
{{#if previous}}
275+
<a rel="prev" href="{{ path_to_root }}{{previous.path}}" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
276+
<i class="fa fa-angle-left"></i><span style="font-size: medium; align-self: center; margin-left: 10px;">{{previous.name}}</span>
277277
</a>
278-
{{/previous}}
278+
{{/if}}
279279

280-
{{#next}}
281-
<a rel="next prefetch" href="{{ path_to_root }}{{link}}" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
282-
<span style="font-size: medium; align-self: center; margin-right: 10px;">{{title}}</span><i class="fa fa-angle-right"></i>
280+
{{#if next}}
281+
<a rel="next prefetch" href="{{ path_to_root }}{{next.path}}" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
282+
<span style="font-size: medium; align-self: center; margin-right: 10px;">{{next.name}}</span><i class="fa fa-angle-right"></i>
283283
</a>
284-
{{/next}}
284+
{{/if}}
285285
</nav>
286286
</div>
287287

0 commit comments

Comments
 (0)