11import datetime
22import json
3- import os
43import re
54import subprocess
65from pathlib import Path
76
87import click
98from dateutil import parser as dateparser
109from dateutil .parser import ParserError
10+ from render_engine import Collection
1111from rich .console import Console
1212
1313from render_engine_cli .event import ServerEventHandler
1414from render_engine_cli .utils import (
1515 create_collection_entry ,
1616 display_filtered_templates ,
1717 get_available_themes ,
18+ get_editor ,
1819 get_site ,
1920 get_site_content_paths ,
2021 remove_output_folder ,
@@ -145,7 +146,7 @@ def build(module_site: str, clean: bool):
145146 is_flag = True ,
146147 default = False ,
147148)
148- @click .option ("-p" , "--port" , type = click .IntRange (0 , 65534 ), help = "Port to serve on" , default = 8000 )
149+ @click .option ("-p" , "--port" , type = click .IntRange (0 , 65534 ), help = "Port to serve on" , default = 8000.0 )
149150def serve (module_site : str , clean : bool , reload : bool , port : int ):
150151 """
151152 Create an HTTP server to serve the site at `localhost`.
@@ -217,9 +218,19 @@ def serve(module_site: str, clean: bool, reload: bool, port: int):
217218 help = "Title for the new entry." ,
218219 default = None ,
219220)
220- @click .option ("-s" , "--slug" , type = click .STRING , help = "Slug for the new page." , callback = validate_file_name_or_slug )
221221@click .option (
222- "-d" , "--include-date" , is_flag = True , default = False , help = "Include today's date in the metadata for your entry."
222+ "-s" ,
223+ "--slug" ,
224+ type = click .STRING ,
225+ help = "Slug for the new page." ,
226+ callback = validate_file_name_or_slug ,
227+ )
228+ @click .option (
229+ "-d" ,
230+ "--include-date" ,
231+ is_flag = True ,
232+ default = False ,
233+ help = "Include today's date in the metadata for your entry." ,
223234)
224235@click .option (
225236 "-a" ,
@@ -228,7 +239,15 @@ def serve(module_site: str, clean: bool, reload: bool, port: int):
228239 type = click .STRING ,
229240 help = "key value attrs to include in your entry use the format `--args key=value` or `--args key:value`" ,
230241)
231- @click .option ("--editor/--no-editor" , default = True , help = "Load the system editor after the file is created." )
242+ @click .option (
243+ "-e" ,
244+ "--editor" ,
245+ default = "default" ,
246+ type = click .STRING ,
247+ callback = get_editor ,
248+ help = "Select the editor to use. If not set the default editor (as set by the EDITOR environment variable) "
249+ "will be used. If 'none' is set no editor will be launched." ,
250+ )
232251@click .option (
233252 "-f" ,
234253 "--filename" ,
@@ -245,7 +264,7 @@ def new_entry(
245264 slug : str ,
246265 include_date : bool ,
247266 args : list [str ],
248- editor : bool ,
267+ editor : str ,
249268 filename : str ,
250269):
251270 """Creates a new collection entry based on the parser. Entries are added to the Collections content_path"""
@@ -272,12 +291,20 @@ def new_entry(
272291
273292 module , site_name = split_module_site (module_site )
274293 site = get_site (module , site_name )
294+ _collection : Collection
275295 if not (
276296 _collection := next (
277297 coll for coll in site .route_list .values () if type (coll ).__name__ .lower () == collection .lower ()
278298 )
279299 ):
280300 raise click .exceptions .BadParameter (f"Unknown collection: { collection } " )
301+ filepath = Path (_collection .content_path ).joinpath (filename )
302+ if filepath .exists ():
303+ if not click .confirm (
304+ f"File { filename } exists are { _collection .content_path } - do you wish to overwrite that file?"
305+ ):
306+ click .secho ("Aborting new entry." , fg = "yellow" )
307+ return
281308 if content and content_file :
282309 raise TypeError ("Both content and content_file provided. At most one may be provided." )
283310 if content_file :
@@ -287,11 +314,10 @@ def new_entry(
287314 # If we had a title earlier this is where we replace the default that is added by the template handler with
288315 # the one supplied by the user.
289316 entry = re .sub (r"title: Untitled Entry" , f"title: { title } " , entry )
290- filepath = Path (_collection .content_path ).joinpath (filename )
291317 filepath .write_text (entry )
292318 Console ().print (f'New { collection } entry created at "{ filepath } "' )
293319
294- if editor and ( editor := os . getenv ( "EDITOR" , None )) :
320+ if editor :
295321 subprocess .run ([editor , filepath ])
296322
297323
0 commit comments