1414
1515import markdown_it
1616from cfbs .pretty import pretty_file
17+ from cfbs .utils import find
1718
1819from cfengine_cli .lint import lint_policy_file
1920from cfengine_cli .utils import UserError
@@ -270,7 +271,7 @@ def _process_markdown_code_blocks(
270271 code_block ["last_line" ],
271272 )
272273
273- if syntax_check and "novalidate" not in code_block [ " flags" ] :
274+ if syntax_check and "novalidate" not in flags :
274275 try :
275276 fn_check_syntax (
276277 origin_path ,
@@ -286,10 +287,10 @@ def _process_markdown_code_blocks(
286287 os .remove (snippet_path )
287288 raise e
288289
289- if output_check and "noexecute" not in code_block [ " flags" ] :
290+ if output_check and "noexecute" not in flags :
290291 fn_check_output ()
291292
292- if autoformat and "noautoformat" not in code_block [ " flags" ] :
293+ if autoformat and "noautoformat" not in flags :
293294 fn_autoformat (
294295 origin_path ,
295296 snippet_path ,
@@ -298,7 +299,7 @@ def _process_markdown_code_blocks(
298299 code_block ["last_line" ],
299300 )
300301
301- if replace and "noreplace" not in code_block [ " flags" ] :
302+ if replace and "noreplace" not in flags :
302303 offset = fn_replace (
303304 origin_path ,
304305 snippet_path ,
@@ -311,43 +312,81 @@ def _process_markdown_code_blocks(
311312 os .remove (snippet_path )
312313
313314
314- def _run_black ():
315- path = "."
316- assert os .path .isdir (path )
315+ def _run_black (path ):
316+ print (f"Formatting '{ path } ' with black..." )
317317 try :
318318 subprocess .run (
319319 ["black" , path ],
320320 capture_output = True ,
321321 text = True ,
322322 check = True ,
323- cwd = path ,
323+ cwd = "." ,
324324 )
325325 except :
326326 raise UserError (
327327 "Encountered an error running black\n Install: pipx install black"
328328 )
329329
330330
331- def _run_prettier ():
332- path = "."
333- assert os .path .isdir (path )
331+ def _run_prettier (path ):
332+ assert os .path .exists (path )
333+
334+ args = [path ]
335+ directory = "."
336+
337+ assert os .path .isdir (path ) or os .path .isfile (path )
338+ if os .path .isdir (path ):
339+ directory = path
340+ args = []
341+ if any (find (path , extension = ".markdown" )):
342+ args .append ("**.markdown" )
343+ if any (find (path , extension = ".md" )):
344+ args .append ("**.md" )
345+ if not args :
346+ return
334347 try :
348+ # Warning: Beware of shell expansion if you try to run this in your terminal.
349+ # Wrong: prettier --write **.markdown **.md
350+ # Right: prettier --write '**.markdown' '**.md'
335351 subprocess .run (
336- ["prettier" , "--write " , "**.markdown " , "**.md" ],
352+ ["prettier" , "--embedded-language-formatting " , "off " , "--write" , * args ],
337353 capture_output = True ,
338354 text = True ,
339355 check = True ,
340- cwd = path ,
356+ cwd = directory ,
341357 )
342358 except :
343359 raise UserError (
344360 "Encountered an error running prettier\n Install: npm install --global prettier"
345361 )
346362
347363
348- def update_docs () -> int :
364+ def _update_docs_single_arg (path ):
365+ if not os .path .exists (path ):
366+ raise UserError (f"The specified file/folder '{ path } ' does not exist" )
367+ if not os .path .isfile (path ) and not os .path .isdir (path ):
368+ raise UserError (f"Argument '{ path } ' is not a file or a folder" )
369+
370+ if os .path .isdir (path ) or path .endswith (".py" ):
371+ _run_black (path )
372+ if os .path .isdir (path ) or path .endswith ((".md" , ".markdown" )):
373+ _run_prettier (path )
374+ print (f"Formatting code blocks in '{ path } '..." )
375+ _process_markdown_code_blocks (
376+ path = path ,
377+ languages = ["json" ], # TODO: Add cfengine3 here
378+ extract = True ,
379+ syntax_check = False ,
380+ output_check = False ,
381+ autoformat = True ,
382+ replace = True ,
383+ cleanup = True ,
384+ )
385+
386+
387+ def update_docs (paths ) -> int :
349388 """
350- Iterate through entire docs repo (.) , autoformatting as much as possible:
389+ Iterate through entire docs repo, or specified subfolders / files , autoformatting as much as possible:
351390 - python code with black
352391 - markdown files with prettier
353392 - code blocks inside markdown files are formatted for the formats supported by prettier
@@ -356,28 +395,17 @@ def update_docs() -> int:
356395 Run by the command:
357396 cfengine dev format-docs
358397 """
359- print ("Formatting python files with black..." )
360- _run_black ()
361- print ("Formatting markdown files with prettier..." )
362- _run_prettier ()
363- print ("Formatting markdown code blocks according to our rules..." )
364- _process_markdown_code_blocks (
365- path = "." ,
366- languages = ["json" ], # TODO: Add cfengine3 here
367- extract = True ,
368- syntax_check = False ,
369- output_check = False ,
370- autoformat = True ,
371- replace = True ,
372- cleanup = True ,
373- )
398+ if not paths :
399+ _update_docs_single_arg ("." )
400+ return 0
401+ for path in paths :
402+ _update_docs_single_arg (path )
374403 return 0
375404
376405
377406def check_docs () -> int :
378407 """
379408 Run checks / tests on docs.
380- Currently only JSON syntax checking.
381409
382410 Run by the command:
383411 cfengine dev lint-docs"""
0 commit comments