-
Notifications
You must be signed in to change notification settings - Fork 18
improved gen_numbering and action #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,5 @@ __pycache__ | |
| results | ||
|
|
||
| .vscode | ||
| .devcontainer | ||
| .devcontainer | ||
| .venv | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,34 @@ | ||||||||||
| import csv | ||||||||||
| from pathlib import Path | ||||||||||
| import difflib | ||||||||||
| import io | ||||||||||
| import re | ||||||||||
| import sys | ||||||||||
| from pathlib import Path | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def write_if_changed(path: Path, content: str, *, original_content=None, encoding: str = "utf-8", label: str | None = None) -> bool: | ||||||||||
| if original_content is None: | ||||||||||
| if path.exists(): | ||||||||||
| original_content = path.read_text(encoding=encoding) | ||||||||||
| else: | ||||||||||
| original_content = "" | ||||||||||
| old_content = original_content | ||||||||||
| if old_content == content: | ||||||||||
| return False | ||||||||||
| display_label = str(label or path) | ||||||||||
| old_lines = old_content.splitlines(keepends=True) | ||||||||||
| new_lines = content.splitlines(keepends=True) | ||||||||||
| diff = difflib.unified_diff( | ||||||||||
| old_lines, | ||||||||||
| new_lines, | ||||||||||
| fromfile=f"a/{display_label}", | ||||||||||
| tofile=f"b/{display_label}", | ||||||||||
| ) | ||||||||||
| diff_output = "".join(diff) | ||||||||||
| if diff_output: | ||||||||||
| print(diff_output, end="" if diff_output.endswith("\n") else "\n") | ||||||||||
| path.write_text(content, encoding=encoding) | ||||||||||
| return True | ||||||||||
|
Comment on lines
+9
to
+31
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| def update_heading_numbers_and_generate_toc(directory: Path): | ||||||||||
|
|
@@ -15,6 +43,13 @@ def update_heading_numbers_and_generate_toc(directory: Path): | |||||||||
| all_learning_objectives = [] | ||||||||||
| Path().as_posix() | ||||||||||
| chapters = {} | ||||||||||
| modified_paths = set() | ||||||||||
|
|
||||||||||
| def rel_label(path: Path) -> str: | ||||||||||
| try: | ||||||||||
| return str(path.relative_to(directory)) | ||||||||||
| except ValueError: | ||||||||||
| return str(path) | ||||||||||
|
Comment on lines
+48
to
+52
|
||||||||||
|
|
||||||||||
| for file in sorted(directory.glob("website/docs/**/*.md")): | ||||||||||
| file_path = file.relative_to(Path('website/docs').resolve()) | ||||||||||
|
|
@@ -144,30 +179,39 @@ def update_heading_numbers_and_generate_toc(directory: Path): | |||||||||
| print(f"Duplicate anchor '{heading_anchor}' in '{file_path}' and '{heading_catalog[heading_anchor][0]}'") | ||||||||||
| heading_catalog[heading_anchor] = (file_path, *heading) | ||||||||||
|
|
||||||||||
| with file.open("w", encoding="utf-8") as md_file: | ||||||||||
| md_file.writelines(updated_lines) | ||||||||||
| original_content = "".join(lines) | ||||||||||
| updated_content = "".join(updated_lines) | ||||||||||
| if write_if_changed(file, updated_content, original_content=original_content, label=rel_label(file)): | ||||||||||
| modified_paths.add(file.resolve()) | ||||||||||
|
|
||||||||||
| all_learning_objectives.extend(learning_objectives) | ||||||||||
|
|
||||||||||
| sorted_lo = sorted(all_learning_objectives, key=lambda x: x[0]) | ||||||||||
|
|
||||||||||
| with (directory / "LOs.csv").open("w", encoding="utf-8") as csv_file: | ||||||||||
| writer = csv.writer(csv_file) | ||||||||||
| writer.writerow(["LO ID", "K Level", "Content", "Slide Number", "Done", "Notes"]) | ||||||||||
| writer.writerows([(f"LO-{lo_id}", f"({k_level})", lo_content, "", "", "") for lo_id, k_level, lo_content in sorted_lo]) | ||||||||||
| lo_csv_buffer = io.StringIO() | ||||||||||
| writer = csv.writer(lo_csv_buffer) | ||||||||||
| writer.writerow(["LO ID", "K Level", "Content", "Slide Number", "Done", "Notes"]) | ||||||||||
| writer.writerows([(f"LO-{lo_id}", f"({k_level})", lo_content, "", "", "") for lo_id, k_level, lo_content in sorted_lo]) | ||||||||||
| lo_csv_content = lo_csv_buffer.getvalue().replace("\r\n", "\n") | ||||||||||
| lo_csv_buffer.close() | ||||||||||
|
||||||||||
| lo_csv_buffer.close() |
Copilot
AI
Dec 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name fix_count is misleading. This variable represents the number of modified files, not the number of fixes. Since the script reformats/regenerates files even when they're correct, a non-zero count doesn't necessarily indicate that something was broken. Consider renaming to modified_count or changed_files_count for clarity.
| fix_count = update_heading_numbers_and_generate_toc(Path.cwd()) | |
| sys.exit(1 if fix_count > 0 else 0) | |
| modified_count = update_heading_numbers_and_generate_toc(Path.cwd()) | |
| sys.exit(1 if modified_count > 0 else 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
working-directoryis set to.which is already the default. This line can be removed as it's redundant.