forked from OWASP/CheatSheetSeries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update_CheatSheets_Index.py
80 lines (74 loc) · 2.79 KB
/
Update_CheatSheets_Index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Python3 script to generate the index markdown page that
reference all cheat sheets grouped by the first letter.
The index markdown page is located on the root folder
and is named "Index.md".
"""
import os
from collections import OrderedDict
# Define utility functions
def extract_languages_snippet_provided(cheatsheet):
languages = []
markers = ["javascript", "java", "csharp", "c", "cpp", "html", "xml", "python",
"ruby", "php", "json", "sql", "bash", "shell", "coldfusion", "perl",
"vbnet"]
with open("../cheatsheets/" + cheatsheet, encoding="utf8") as cs_file:
cs_content = cs_file.read().lower().replace(" ","")
for marker in markers:
if "```" + marker + "\n" in cs_content:
languages.append(marker.capitalize())
return languages
# Define templates
cs_md_link_template = "[%s](cheatsheets/%s)."
language_md_link_template = "![%s](assets/Index_%s.png)"
header_template = "# %s\n\n"
top_menu_template = "[%s](Index.md#%s)"
cs_count_template = "**%s** cheat sheets available."
# Scan all CS files
index = {}
cs_count = 0
cheatsheets = [f.name for f in os.scandir("../cheatsheets") if f.is_file()]
for cheatsheet in cheatsheets:
letter = cheatsheet[0].upper()
if letter not in index:
index[letter] = [cheatsheet]
else:
index[letter].append(cheatsheet)
cs_count += 1
index = OrderedDict(sorted(index.items()))
# Generate the index file
with open("../Index.md", "w") as index_file:
index_count = len(index)
index_file.write(cs_count_template % cs_count)
index_file.write("\n\n*Icons beside the cheat sheet name indicate in which language(s) code snippet(s) are provided.*")
index_file.write("\n\n")
# Generate the top menu
for letter in index:
index_file.write(top_menu_template % (letter, letter.lower()))
index_file.write(" ")
index_file.write("\n\n")
# Generate letter sections
j = 0
for letter in index:
cs_count = len(index[letter])
index_file.write(header_template % letter)
i = 0
for cs_file in index[letter]:
cs_name = cs_file.replace("_", " ").replace(".md", "").strip()
index_file.write(cs_md_link_template % (cs_name, cs_file))
languages = extract_languages_snippet_provided(cs_file)
if len(languages) > 0:
index_file.write(" ")
for language in languages:
index_file.write(language_md_link_template % (language, language))
index_file.write(" ")
i += 1
index_file.write("\n")
if i != cs_count:
index_file.write("\n")
j += 1
if j != index_count:
index_file.write("\n")
print("Index updated.")