3131_INDEX_TEMPLATE_SHORTCODE_SNIPPET = '\n {{< image src="img01" >}}'
3232
3333
34+ def get_latest_year () -> str :
35+ """
36+ Get the latest year from the content directory.
37+
38+ Returns:
39+ str: The latest year found in the content directory, or the current year if none found
40+ """
41+ years = []
42+ for path in _TARGET_BASE_PATH .iterdir ():
43+ if path .is_dir () and path .name .isdigit ():
44+ years .append (path .name )
45+
46+ if not years :
47+ return str (datetime .now ().year )
48+
49+ return max (years )
50+
51+
52+ def get_next_id (year : str ) -> str :
53+ """
54+ Get the next available ID for a given year.
55+
56+ Args:
57+ year (str): The year to check for existing IDs
58+
59+ Returns:
60+ str: The next available ID formatted as a 4-digit string
61+ """
62+ year_path = _TARGET_BASE_PATH .joinpath (year )
63+ if not year_path .exists ():
64+ return "0000"
65+
66+ ids = []
67+ for path in year_path .iterdir ():
68+ if path .is_dir () and path .name .isdigit ():
69+ ids .append (int (path .name ))
70+
71+ if not ids :
72+ return "0000"
73+
74+ return f"{ max (ids ) + 1 :04d} "
75+
76+
3477def parse_arguments ():
3578 """
3679 Parse command line arguments for the script.
@@ -46,13 +89,17 @@ def parse_arguments():
4689 parser = argparse .ArgumentParser (description = "Parse arguments for template, name, and year." )
4790 subparsers = parser .add_subparsers (dest = 'command' , required = True , help = "Subcommands" )
4891
92+ # Get default values for create command
93+ default_year = get_latest_year ()
94+ default_id = get_next_id (default_year )
95+
4996 remove_parser = subparsers .add_parser ('remove' , help = 'Removes a content page' )
5097 remove_parser .add_argument ('--year' , type = str , required = True , help = 'The year' )
5198 remove_parser .add_argument ('--id' , type = str , required = True , help = 'The content id' )
5299
53100 create_parser = subparsers .add_parser ('create' , help = 'Creates a content page' )
54- create_parser .add_argument ('--year' , type = str , required = True , help = 'The year' )
55- create_parser .add_argument ('--id' , type = str , required = True , help = 'The content id' )
101+ create_parser .add_argument ('--year' , type = str , default = default_year , help = f 'The year (default: { default_year } ) ' )
102+ create_parser .add_argument ('--id' , type = str , default = default_id , help = f 'The content id (default: { default_id } ) ' )
56103 create_parser_group = create_parser .add_mutually_exclusive_group (required = True )
57104 create_parser_group .add_argument ('--template' , action = 'store_true' )
58105 create_parser_group .add_argument ('--input' , type = str )
@@ -87,6 +134,7 @@ def create_base_paths(year: str, index: str):
87134
88135 os .mkdir (content_path )
89136 os .mkdir (image_path )
137+ print (f'Using year: { year } and id: { index } ' )
90138 print (f'Created { content_path } ' )
91139
92140
@@ -164,6 +212,7 @@ def copy_images(year: str, content_id: str, input_path: Path):
164212
165213 The first image found is also copied as a thumbnail.
166214 All images are renamed according to the pattern: year-contentId-counter.jpg
215+ Searches recursively through all subdirectories.
167216
168217 Args:
169218 year (str): The year for the content
@@ -177,8 +226,17 @@ def copy_images(year: str, content_id: str, input_path: Path):
177226 image_path = content_path .joinpath ('img' )
178227 counter = 1
179228
180- # Iterate through all files in the directory
181- for file_path in sorted (input_path .iterdir ()):
229+ # Get all files recursively
230+ all_files = []
231+ for root , _ , files in os .walk (input_path ):
232+ for file in files :
233+ all_files .append (Path (root ) / file )
234+
235+ # Sort files to ensure consistent ordering
236+ all_files .sort ()
237+
238+ # Process all image files
239+ for file_path in all_files :
182240 if file_path .is_file ():
183241 mime_type , _ = mimetypes .guess_type (file_path )
184242 if mime_type and mime_type == 'image/jpeg' :
@@ -191,6 +249,7 @@ def copy_images(year: str, content_id: str, input_path: Path):
191249 shutil .copy (file_path , new_image )
192250 print (f'Copy { file_path .name } to { new_image } ' )
193251 counter = counter + 1
252+
194253 if counter == 1 :
195254 raise FileNotFoundError (f'No JPG images found in { input_path } ' )
196255
@@ -236,7 +295,7 @@ def find_markdown_content(input_path: Path) -> str:
236295 """
237296 for file_path in input_path .iterdir ():
238297 if file_path .is_file () and file_path .suffix == '.md' :
239- print (f'Use markdown file: { file_path . name } ' )
298+ print (f'Use markdown file: { file_path } ' )
240299 with open (file_path , 'r' ) as md_file :
241300 content = md_file .read ()
242301 return content
0 commit comments