def test_gfm_spec(entry): """Test mdformat-gfm against the GFM spec. Test that: 1. Markdown AST is the same before and after 1 pass of formatting 2. Markdown after 1st pass and 2nd pass of formatting are equal """ md_original = entry["md"] md_new = mdformat.text(md_original, extensions={"gfm"}) md_2nd_pass = mdformat.text(md_new, extensions={"gfm"}) assert is_md_equal(md_original, md_new, extensions={"gfm"}) assert md_new == md_2nd_pass
def test_commonmark_spec(wrap, number, entry): """mdformat.text() against the Commonmark spec. Test that: 1. Markdown AST is the same before and after 1 pass of formatting 2. Markdown after 1st pass and 2nd pass of formatting are equal """ options = {"wrap": wrap, "number": number} md_original = entry["md"] md_new = mdformat.text(md_original, options=options) md_2nd_pass = mdformat.text(md_new, options=options) assert is_md_equal(md_original, md_new, options=options) assert md_new == md_2nd_pass
def run(cli_args: Sequence[str]) -> int: # noqa: C901 parser = argparse.ArgumentParser( description="CommonMark compliant Markdown formatter") parser.add_argument("paths", nargs="*", help="Files to format") parser.add_argument("--check", action="store_true") args = parser.parse_args(cli_args) if not args.paths: sys.stderr.write("No files have been passed in. Doing nothing.\n") return 0 # Convert paths given as args to pathlib.Path objects. # Check that all paths are either files, directories or stdin. # Resolve directory paths to a list of file paths (ending with ".md"). file_paths: List[Optional[Path]] = [ ] # Path to file or None for stdin/stdout for path_str in args.paths: if path_str == "-": file_paths.append(None) continue path_obj = Path(path_str) try: path_exists = path_obj.exists() except OSError: # Catch "OSError: [WinError 123]" on Windows path_exists = False if not path_exists: sys.stderr.write(f'Error: File "{path_str}" does not exist.\n') return 1 if path_obj.is_dir(): for p in path_obj.glob("**/*.md"): file_paths.append(p) else: file_paths.append(path_obj) format_errors_found = False for path in file_paths: if path: path_str = str(path) original_str = path.read_text(encoding="utf-8") else: path_str = "-" original_str = sys.stdin.read() formatted_str = mdformat.text(original_str) if args.check: if formatted_str != original_str: format_errors_found = True sys.stderr.write( f'Error: File "{path_str}" is not formatted.\n') else: if not is_md_equal(original_str, formatted_str): sys.stderr.write( f'Error: Could not format "{path_str}"\n' "\n" "The formatted Markdown renders to different HTML than the input Markdown.\n" # noqa: E501 "This is likely a bug in mdformat. Please create an issue report here:\n" # noqa: E501 "https://github.com/executablebooks/mdformat/issues\n") return 1 if path: path.write_text(formatted_str, encoding="utf-8") else: sys.stdout.write(formatted_str) if format_errors_found: return 1 return 0
def run(cli_args: Sequence[str]) -> int: # noqa: C901 # Enable all parser plugins enabled_parserplugins = mdformat.plugins.PARSER_EXTENSIONS # Enable code formatting for all languages that have a plugin installed enabled_codeformatters = mdformat.plugins.CODEFORMATTERS changes_ast = any( getattr(plugin, "CHANGES_AST", False) for plugin in enabled_parserplugins.values()) arg_parser = make_arg_parser(enabled_parserplugins.values()) args = arg_parser.parse_args(cli_args) if not args.paths: print_paragraphs(["No files have been passed in. Doing nothing."]) return 0 try: file_paths = resolve_file_paths(args.paths) except InvalidPath as e: arg_parser.error(f'File "{e.path}" does not exist.') # Convert args to a mapping options: Mapping[str, Any] = vars(args) format_errors_found = False renderer_warning_printer = RendererWarningPrinter() for path in file_paths: if path: path_str = str(path) original_str = path.read_text(encoding="utf-8") else: path_str = "-" original_str = sys.stdin.read() with log_handler_applied(mdformat.renderer.LOGGER, renderer_warning_printer): formatted_str = mdformat.text( original_str, options=options, extensions=enabled_parserplugins, codeformatters=enabled_codeformatters, ) if args.check: if formatted_str != original_str: format_errors_found = True print_error(f'File "{path_str}" is not formatted.') else: if not changes_ast and not is_md_equal( original_str, formatted_str, options=options, extensions=enabled_parserplugins, codeformatters=enabled_codeformatters, ): print_error( f'Could not format "{path_str}".', paragraphs=[ "The formatted Markdown renders to different HTML than the input Markdown. " # noqa: E501 "This is likely a bug in mdformat. " "Please create an issue report here, including the input Markdown: " # noqa: E501 "https://github.com/executablebooks/mdformat/issues", ], ) return 1 if path: path.write_text(formatted_str, encoding="utf-8") else: sys.stdout.write(formatted_str) if format_errors_found: return 1 return 0