|
2 | 2 | import os |
3 | 3 |
|
4 | 4 |
|
5 | | -class CollectionMarkdownGenerator: |
6 | | - def __init__(self, json_path, output_dir): |
7 | | - self.json_path = json_path |
8 | | - self.output_dir = output_dir |
9 | | - |
10 | | - def load_collections(self): |
11 | | - try: |
12 | | - with open(self.json_path, 'r') as file: |
13 | | - collections = json.load(file) |
14 | | - return collections |
15 | | - except FileNotFoundError: |
16 | | - print(f"Error: The file '{self.json_path}' was not found.") |
17 | | - exit(1) |
18 | | - except json.JSONDecodeError: |
19 | | - print(f"Error: The file '{self.json_path}' is not a valid JSON file.") |
20 | | - exit(1) |
21 | | - |
22 | | - @staticmethod |
23 | | - def create_markdown_content(collection): |
24 | | - return f"""--- |
25 | | -layout: {collection['layout']} |
26 | | -title: {collection['title']} |
27 | | -description: {collection['description']} |
28 | | -img: {collection['img']} |
29 | | -importance: {collection['importance']} |
30 | | -category: {collection['category']} |
31 | | -category_book: {collection['category_book']} |
32 | | -related_publications: {str(collection['related_publications']).lower()} |
33 | | -horizontal: {str(collection['horizontal']).lower()} |
34 | | ---- |
35 | | -
|
36 | | -{{% include books_display.liquid %}} |
37 | | -""" |
38 | | - |
39 | | - def ensure_output_dir_exists(self): |
40 | | - if not os.path.exists(self.output_dir): |
41 | | - os.makedirs(self.output_dir) |
42 | | - |
43 | | - @staticmethod |
44 | | - def write_markdown_file(filename, content): |
45 | | - with open(filename, 'w') as md_file: |
46 | | - md_file.write(content) |
47 | | - print(f"Created {filename}") |
48 | | - |
49 | | - def generate_markdown_files(self): |
50 | | - self.ensure_output_dir_exists() |
51 | | - collections = self.load_collections() |
52 | | - |
53 | | - for collection in collections: |
54 | | - filename = os.path.join(self.output_dir, collection['filename']) |
55 | | - if not os.path.exists(filename): |
56 | | - content = self.create_markdown_content(collection) |
57 | | - self.write_markdown_file(filename, content) |
58 | | - else: |
59 | | - print(f"Skipped {filename} (already exists)") |
| 5 | +def get_collections_json_path(): |
| 6 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 7 | + resources_dir = os.path.join(script_dir, 'resources') |
| 8 | + return os.path.join(resources_dir, 'collections.json') |
| 9 | + |
| 10 | + |
| 11 | +def file_exists(file_path): |
| 12 | + return os.path.exists(file_path) |
| 13 | + |
| 14 | + |
| 15 | +def load_json(file_path): |
| 16 | + try: |
| 17 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 18 | + return json.load(file) |
| 19 | + except FileNotFoundError: |
| 20 | + print(f"Error: The file '{file_path}' was not found.") |
| 21 | + exit(1) |
| 22 | + except json.JSONDecodeError: |
| 23 | + print(f"Error: The file '{file_path}' is not a valid JSON file.") |
| 24 | + exit(1) |
| 25 | + |
| 26 | + |
| 27 | +required_fields = { |
| 28 | + "layout": str, |
| 29 | + "title": str, |
| 30 | + "description": str, |
| 31 | + "img": str, |
| 32 | + "importance": int, |
| 33 | + "category": str, |
| 34 | + "category_book": str, |
| 35 | + "related_publications": bool, |
| 36 | + "horizontal": bool, |
| 37 | + "filename": str |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +def validate_json_structure(data): |
| 42 | + for collection in data: |
| 43 | + for field, field_type in required_fields.items(): |
| 44 | + if field not in collection: |
| 45 | + return False, f"Field {field} is missing in collection {collection}" |
| 46 | + if not isinstance(collection[field], field_type): |
| 47 | + return False, f"Field {field} in collection {collection} should be of type {field_type.__name__}" |
| 48 | + return True, "All checks passed" |
| 49 | + |
| 50 | + |
| 51 | +def ensure_output_dir_exists(output_dir): |
| 52 | + if not os.path.exists(output_dir): |
| 53 | + os.makedirs(output_dir) |
| 54 | + |
| 55 | + |
| 56 | +def create_collection_md_content(collection): |
| 57 | + content = ["---"] |
| 58 | + for field, value in collection.items(): |
| 59 | + if field != "filename": # Exclude the filename field |
| 60 | + if isinstance(value, bool): |
| 61 | + value = str(value).lower() # Convert boolean to lowercase string |
| 62 | + content.append(f"{field}: {value}") |
| 63 | + content.append("---") |
| 64 | + content.append("") |
| 65 | + content.append("{% include books_display.liquid %}") |
| 66 | + content.append("") # Add an extra blank line at the end |
| 67 | + return "\n".join(content) |
| 68 | + |
| 69 | + |
| 70 | +def write_markdown_file(filename, content): |
| 71 | + with open(filename, 'w', encoding='utf-8') as md_file: |
| 72 | + md_file.write(content) |
| 73 | + print(f"Created {filename}") |
| 74 | + |
| 75 | + |
| 76 | +def generate_collection_markdown_files(collections, output_dir): |
| 77 | + ensure_output_dir_exists(output_dir) |
| 78 | + created_files = [] |
| 79 | + for collection in collections: |
| 80 | + file_name = f"{collection['filename']}.md" |
| 81 | + file_path = os.path.join(output_dir, file_name) |
| 82 | + if not os.path.exists(file_path): |
| 83 | + content = create_collection_md_content(collection) |
| 84 | + write_markdown_file(file_path, content) |
| 85 | + created_files.append(file_path) |
| 86 | + else: |
| 87 | + print(f"Skipped {file_path} (already exists)") |
| 88 | + return created_files |
60 | 89 |
|
61 | 90 |
|
62 | 91 | def main(): |
63 | | - # Set the base path to the root of your project |
64 | | - base_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
| 92 | + collections_json_path = get_collections_json_path() |
| 93 | + collections_output_dir = os.path.join('..', '_collections') |
65 | 94 |
|
66 | | - # Use the base path to construct the absolute path for the JSON file |
67 | | - json_path = os.path.join(base_path, 'assets', 'json', 'collections.json') |
68 | | - output_dir = os.path.join(base_path, '_collections') |
| 95 | + print(f"Using JSON file for collections: {collections_json_path}") |
| 96 | + print(f"Output directory for collections: {collections_output_dir}") |
69 | 97 |
|
70 | | - print(f"Using JSON file: {json_path}") |
71 | | - print(f"Output directory: {output_dir}") |
72 | | - |
73 | | - generator = CollectionMarkdownGenerator(json_path, output_dir) |
74 | | - generator.generate_markdown_files() |
| 98 | + collections_data = load_json(collections_json_path) |
| 99 | + generate_collection_markdown_files(collections_data, collections_output_dir) |
75 | 100 |
|
76 | 101 |
|
77 | 102 | if __name__ == "__main__": |
|
0 commit comments