Skip to content

Commit

Permalink
Link the generated index pages
Browse files Browse the repository at this point in the history
  • Loading branch information
TahaHachana committed Jun 1, 2024
1 parent e2ff146 commit 3bf4f16
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 17 deletions.
27 changes: 27 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>

<body>
<div class="d-flex flex-column min-vh-100 min-vw-100">
<div class="d-flex flex-grow-1 justify-content-center align-items-center">
<div class="d-grid gap-2 col-4 mx-auto">

<a class="btn btn-outline-primary" href="/python/" role="button">python</a>

</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.min.js"
integrity="sha384-0pUGZvbkm6XF6gxjEnlmuGrJXVbNuzT9qBBavbLwCsOGabYfZo0T0to5eqruptLy"
crossorigin="anonymous"></script>
</body>

</html>
2 changes: 1 addition & 1 deletion main.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/home/taha/.pyenv/versions/3.12.2/bin/python3 src/main.py
#python3 server.py --dir .
python3 server.py --dir .
27 changes: 27 additions & 0 deletions python/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>

<body>
<div class="d-flex flex-column min-vh-100 min-vw-100">
<div class="d-flex flex-grow-1 justify-content-center align-items-center">
<div class="d-grid gap-2 col-4 mx-auto">

<a class="btn btn-outline-primary" href="/variables/" role="button">variables</a>

</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.min.js"
integrity="sha384-0pUGZvbkm6XF6gxjEnlmuGrJXVbNuzT9qBBavbLwCsOGabYfZo0T0to5eqruptLy"
crossorigin="anonymous"></script>
</body>

</html>
25 changes: 25 additions & 0 deletions python/variables/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>

<body>
<div class="d-flex flex-column min-vh-100 min-vw-100">
<div class="d-flex flex-grow-1 justify-content-center align-items-center">
<div class="d-grid gap-2 col-4 mx-auto">

</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.min.js"
integrity="sha384-0pUGZvbkm6XF6gxjEnlmuGrJXVbNuzT9qBBavbLwCsOGabYfZo0T0to5eqruptLy"
crossorigin="anonymous"></script>
</body>

</html>
62 changes: 48 additions & 14 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import shutil

from html_template import html_output
from html_template import template

CONTENT_DIR = "./content"

Expand All @@ -20,30 +20,64 @@

# print(sys.executable)

def list_folders_in_dir(dir):

def rec_list_folders_in_dir(dir):
folders = []
for folder in os.listdir(dir):
folder_path = os.path.join(dir, folder)
if os.path.isdir(folder_path):
print(folder_path)
list_folders_in_dir(folder_path)
folders.append(folder_path)
folders.extend(rec_list_folders_in_dir(folder_path))
return folders


def create_index_file(path):
def move_up_one_dir(path):
new_path = path.replace(CONTENT_DIR, ".")
if new_path != ".":
shutil.rmtree(new_path) #, ignore_errors=True)
print(f"deleting {new_path}")
# shutil.rmtree(new_path) #, ignore_errors=True)
# if not os.path.exists(new_path):
os.makedirs(new_path)
os.makedirs(new_path, exist_ok=True)
print(f"Created directory: {new_path}")
index_file_path = os.path.join(new_path, 'index.html')
with open(index_file_path, 'w') as index_file:
index_file.write("<html><body><h1>Index</h1></body></html>")
return new_path

folders_to_exclude = ["content", ".git", "src"]

def not_excluded_folder(folder):
return folder not in folders_to_exclude

def write_index_html(new_path):
folder_names = [
name
for name in os.listdir(new_path)
if os.path.isdir(os.path.join(new_path, name))
]
folder_names = list(filter(not_excluded_folder, folder_names))
links = [
{"href": f"/{folder}/", "text": folder} for folder in folder_names
]
context = {
"title": "My Title",
"links": links,
}
html_output = template.render(context)
index_file_path = os.path.join(new_path, "index.html")
with open(index_file_path, "w") as index_file:
index_file.write(html_output)
print(f"Created index.html in: {new_path}")


def main():
folders = list_folders_in_dir(CONTENT_DIR)
create_index_file(CONTENT_DIR)
folders = rec_list_folders_in_dir(CONTENT_DIR)
print(f"folders: {folders}")
# move_up_one_dir(CONTENT_DIR)
write_index_html(".")
new_paths = []
for folder in folders:
create_index_file(folder)
new_paths.append(move_up_one_dir(folder))
for new_path in new_paths:
write_index_html(new_path)


if __name__ == "__main__":
main()
main()
5 changes: 3 additions & 2 deletions src/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
<div class="d-flex flex-column min-vh-100 min-vw-100">
<div class="d-flex flex-grow-1 justify-content-center align-items-center">
<div class="d-grid gap-2 col-4 mx-auto">
<button class="btn btn-outline-primary" type="button">Python</button>
<button class="btn btn-outline-primary" type="button">Bash</button>
{% for link in links %}
<a class="btn btn-outline-primary" href="{{ link.href }}" role="button">{{ link.text }}</a>
{% endfor %}
</div>
</div>
</div>
Expand Down

0 comments on commit 3bf4f16

Please sign in to comment.