コミットを比較

..

3 コミット

3個のファイルの変更29行の追加10行の削除

2
.gitignore vendored
ファイルの表示

@ -1 +1,3 @@
__pycache__/
*.json *.json
*.html

ファイルの表示

@ -14,7 +14,7 @@ class TableCell:
def to_html(self): def to_html(self):
if self.is_header: if self.is_header:
html = f"<th title={self.description}>{self.name}</th>" html = f"<th title='{self.description}'>{self.name}</th>"
elif not self.name: elif not self.name:
html = f'<td colspan="{self.colspan}" class="filler"></td>' html = f'<td colspan="{self.colspan}" class="filler"></td>'
else: else:

ファイルの表示

@ -2,12 +2,31 @@ from os import path, walk
from recipe import Recipe from recipe import Recipe
from sys import argv from sys import argv
def write_file_safely(filepath, content):
if path.exists(filepath):
response = input(f"File '{filepath}' exists. Overwrite? (y/N): ")
if not response or not response.lower().startswith("y"):
print(f"Skipping '{filepath}'")
return
with open(filepath, 'w') as file:
file.write(content)
if len(argv) < 2: if len(argv) < 2:
print(f"Usage: {argv[0]} <recipe directory>") print(f"Usage: {argv[0]} <recipe directory> [index_header.html]")
exit(1) exit(1)
recipe_dir = argv[1] recipe_dir = argv[1]
if len(argv) > 2:
with open(argv[2], 'r') as index_header_file:
index_header_html = index_header_file.read()
else:
index_header_html = ""
recipe_names = {} recipe_names = {}
for dirpath, dirnames, filenames in walk(recipe_dir): for dirpath, dirnames, filenames in walk(recipe_dir):
for filename in filenames: for filename in filenames:
@ -17,8 +36,7 @@ for dirpath, dirnames, filenames in walk(recipe_dir):
html = recipe.to_html() html = recipe.to_html()
html_filepath = path.join(dirpath, f"{filename.rsplit('.', maxsplit=1)[0]}.html") html_filepath = path.join(dirpath, f"{filename.rsplit('.', maxsplit=1)[0]}.html")
with open(html_filepath, 'w') as html_file: write_file_safely(html_filepath, html)
html_file.write(html)
recipe_names[recipe.name] = path.relpath(html_filepath, start=recipe_dir) recipe_names[recipe.name] = path.relpath(html_filepath, start=recipe_dir)
@ -32,20 +50,19 @@ index_html = f"""<!DOCTYPE html>
<body> <body>
<header> <header>
<h1>Recipes</h1> {index_header_html}
<p>These are recipes that I'd like to share.</p>
</header> </header>
<main> <section>
<h2>Recipes</h2>
<ul> <ul>
{"\n".join([f" <li><a href='./{filepath}'>{name}</a></li>" for name, filepath in recipe_names.items()])} {"\n".join([f" <li><a href='./{filepath}'>{name}</a></li>" for name, filepath in recipe_names.items()])}
</ul> </ul>
</main> </section>
</body> </body>
</html> </html>
""" """
with open(path.join(recipe_dir, "index.html"), 'w') as index_file: write_file_safely(path.join(recipe_dir, "index.html"), index_html)
index_file.write(index_html)