1
1
import datetime
2
2
import json
3
- import os
4
3
import re
5
4
import subprocess
6
5
from pathlib import Path
7
6
8
7
import click
9
8
from dateutil import parser as dateparser
10
9
from dateutil .parser import ParserError
10
+ from render_engine import Collection
11
11
from rich .console import Console
12
12
13
13
from render_engine_cli .event import ServerEventHandler
14
14
from render_engine_cli .utils import (
15
15
create_collection_entry ,
16
16
display_filtered_templates ,
17
17
get_available_themes ,
18
+ get_editor ,
18
19
get_site ,
19
20
get_site_content_paths ,
20
21
remove_output_folder ,
@@ -145,7 +146,7 @@ def build(module_site: str, clean: bool):
145
146
is_flag = True ,
146
147
default = False ,
147
148
)
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 )
149
150
def serve (module_site : str , clean : bool , reload : bool , port : int ):
150
151
"""
151
152
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):
217
218
help = "Title for the new entry." ,
218
219
default = None ,
219
220
)
220
- @click .option ("-s" , "--slug" , type = click .STRING , help = "Slug for the new page." , callback = validate_file_name_or_slug )
221
221
@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." ,
223
234
)
224
235
@click .option (
225
236
"-a" ,
@@ -228,7 +239,15 @@ def serve(module_site: str, clean: bool, reload: bool, port: int):
228
239
type = click .STRING ,
229
240
help = "key value attrs to include in your entry use the format `--args key=value` or `--args key:value`" ,
230
241
)
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
+ )
232
251
@click .option (
233
252
"-f" ,
234
253
"--filename" ,
@@ -245,7 +264,7 @@ def new_entry(
245
264
slug : str ,
246
265
include_date : bool ,
247
266
args : list [str ],
248
- editor : bool ,
267
+ editor : str ,
249
268
filename : str ,
250
269
):
251
270
"""Creates a new collection entry based on the parser. Entries are added to the Collections content_path"""
@@ -272,12 +291,20 @@ def new_entry(
272
291
273
292
module , site_name = split_module_site (module_site )
274
293
site = get_site (module , site_name )
294
+ _collection : Collection
275
295
if not (
276
296
_collection := next (
277
297
coll for coll in site .route_list .values () if type (coll ).__name__ .lower () == collection .lower ()
278
298
)
279
299
):
280
300
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
281
308
if content and content_file :
282
309
raise TypeError ("Both content and content_file provided. At most one may be provided." )
283
310
if content_file :
@@ -287,11 +314,10 @@ def new_entry(
287
314
# If we had a title earlier this is where we replace the default that is added by the template handler with
288
315
# the one supplied by the user.
289
316
entry = re .sub (r"title: Untitled Entry" , f"title: { title } " , entry )
290
- filepath = Path (_collection .content_path ).joinpath (filename )
291
317
filepath .write_text (entry )
292
318
Console ().print (f'New { collection } entry created at "{ filepath } "' )
293
319
294
- if editor and ( editor := os . getenv ( "EDITOR" , None )) :
320
+ if editor :
295
321
subprocess .run ([editor , filepath ])
296
322
297
323
0 commit comments