Skip to content

Commit 42cfc51

Browse files
authored
Merge pull request #22 from olehermanse/improvements
dev format-docs: Added the option of specifying folders / files and fixed issue with skipped JSON code blocks
2 parents 75f946f + 5da68b7 commit 42cfc51

File tree

3 files changed

+66
-36
lines changed

3 files changed

+66
-36
lines changed

src/cfengine_cli/dev.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ def print_dependency_tables(args) -> int:
5050
return 1
5151

5252

53-
def format_docs() -> int:
53+
def format_docs(files) -> int:
5454
answer = _expect_repo("documentation")
5555
if answer:
56-
return update_docs()
56+
return update_docs(files)
5757
return 1
5858

5959

@@ -80,7 +80,7 @@ def dispatch_dev_subcommand(subcommand, args) -> int:
8080
if subcommand == "print-dependency-tables":
8181
return print_dependency_tables(args)
8282
if subcommand == "format-docs":
83-
return format_docs()
83+
return format_docs(args.files)
8484
if subcommand == "lint-docs":
8585
return lint_docs()
8686
if subcommand == "generate-release-information":

src/cfengine_cli/docs.py

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import markdown_it
1616
from cfbs.pretty import pretty_file
17+
from cfbs.utils import find
1718

1819
from cfengine_cli.lint import lint_policy_file
1920
from 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\nInstall: 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\nInstall: 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

377406
def 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"""

src/cfengine_cli/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ def _get_arg_parser():
8585
nargs="+",
8686
help="Versions to compare (minimum 1 required)",
8787
)
88-
dev_subparsers.add_parser("format-docs")
89-
dev_subparsers.add_parser("lint-docs")
88+
parser = dev_subparsers.add_parser("format-docs")
89+
parser.add_argument("files", nargs="*")
90+
parser = dev_subparsers.add_parser("lint-docs")
91+
parser.add_argument("files", nargs="*")
9092
parser = dev_subparsers.add_parser("generate-release-information")
9193

9294
parser.add_argument(

0 commit comments

Comments
 (0)