Simplify mdoctor to use only pymarkdownlnt as it is now a dependency
- Remove markdownlint fallback check since pymarkdownlnt is now a runtime dependency - Simplify _get_linter_command() to just use pymarkdownlnt scan directly - Rename _parse_pymarkdownlnt_output to _parse_output - Remove json and shutil imports that are no longer needed Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
+5
-53
@@ -2,10 +2,7 @@
|
||||
MDoctor - A tool for diagnosing and managing the LLM wiki.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
@@ -25,21 +22,9 @@ class MDoctor:
|
||||
"""List all .md files in the given directory using the librarian."""
|
||||
return self.librarian.list_all_md_files(directory)
|
||||
|
||||
def _get_linter_command(self) -> list[str]:
|
||||
"""Get the available markdown linter command."""
|
||||
# Try markdownlint first (node-based, supports --json)
|
||||
if shutil.which("markdownlint"):
|
||||
return ["markdownlint", "--json"]
|
||||
# Fall back to pymarkdownlnt (Python-based, use scan subcommand)
|
||||
if shutil.which("pymarkdownlnt"):
|
||||
return ["pymarkdownlnt", "scan"]
|
||||
# No linter found
|
||||
return []
|
||||
|
||||
def _parse_pymarkdownlnt_output(self, output: str) -> list[str]:
|
||||
def _parse_output(self, output: str) -> list[str]:
|
||||
"""Parse pymarkdownlnt output into list of error strings."""
|
||||
violations = []
|
||||
# pymarkdownlnt output format: "FILE:LINE: RULE: message"
|
||||
for line in output.split('\n'):
|
||||
line = line.strip()
|
||||
if not line or ':' not in line:
|
||||
@@ -47,25 +32,9 @@ class MDoctor:
|
||||
violations.append(line)
|
||||
return violations
|
||||
|
||||
def _parse_markdownlint_output(self, output: str) -> list[str]:
|
||||
"""Parse markdownlint JSON output into list of error strings."""
|
||||
violations = []
|
||||
try:
|
||||
lint_errors = json.loads(output)
|
||||
for error in lint_errors:
|
||||
line = error.get("line", error.get("line-number", "?"))
|
||||
rule = error.get("rule", error.get("rule-id", "?"))
|
||||
rule_description = error.get("ruleDescription", "")
|
||||
message = error.get("message", "")
|
||||
violations.append(f"Line {line}: {rule} - {rule_description} ({message})")
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
if output:
|
||||
violations = [line.strip() for line in output.split('\n') if line.strip()]
|
||||
return violations
|
||||
|
||||
def check_markdown_lint(self, directory: str) -> dict[str, list[str]]:
|
||||
"""
|
||||
Run markdownlint on all .md files in the given directory.
|
||||
Run pymarkdownlnt on all .md files in the given directory.
|
||||
|
||||
Args:
|
||||
directory: Path to the directory to scan
|
||||
@@ -73,34 +42,19 @@ class MDoctor:
|
||||
Returns:
|
||||
Dictionary mapping file paths to lists of lint violations
|
||||
"""
|
||||
linter_cmd = self._get_linter_command()
|
||||
if not linter_cmd:
|
||||
print("Error: No markdown linter found. "
|
||||
"Please install 'markdownlint-cli' (npm install -g markdownlint-cli) "
|
||||
"or 'pymarkdownlnt' (pip install pymarkdownlnt).", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
is_markdownlint = linter_cmd[0] == "markdownlint"
|
||||
|
||||
md_files = self.list_all_md_files(directory)
|
||||
violations: dict[str, list[str]] = {}
|
||||
|
||||
for md_file in md_files:
|
||||
try:
|
||||
# Run linter on the file
|
||||
result = subprocess.run(
|
||||
linter_cmd + [md_file],
|
||||
["pymarkdownlnt", "scan", md_file],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
|
||||
# Parse output based on linter
|
||||
if is_markdownlint:
|
||||
errors = self._parse_markdownlint_output(result.stdout)
|
||||
else:
|
||||
# pymarkdownlnt outputs to stderr
|
||||
output = result.stderr or result.stdout
|
||||
errors = self._parse_pymarkdownlnt_output(output)
|
||||
output = result.stderr or result.stdout
|
||||
errors = self._parse_output(output)
|
||||
|
||||
if errors:
|
||||
violations[md_file] = errors
|
||||
@@ -132,14 +86,12 @@ def main():
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.subcommand is None:
|
||||
# Default behavior: list files
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
|
||||
mdoctor = MDoctor()
|
||||
|
||||
if args.subcommand == "list":
|
||||
# For backward compatibility, if no directory argument, use current dir
|
||||
if not hasattr(args, 'directory'):
|
||||
args.directory = "."
|
||||
if not os.path.isdir(args.directory):
|
||||
|
||||
Reference in New Issue
Block a user