Class: Ace::Lint::Molecules::MarkdownSurgicalFixer

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/lint/molecules/markdown_surgical_fixer.rb

Overview

Applies low-risk, line-level markdown fixes without re-serializing the document.

Constant Summary collapse

SMART_QUOTE_REPLACEMENTS =
{
  "\u201C" => '"',
  "\u201D" => '"',
  "\u2018" => "'",
  "\u2019" => "'"
}.freeze

Class Method Summary collapse

Class Method Details

.fix_content(content) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ace/lint/molecules/markdown_surgical_fixer.rb', line 47

def fix_content(content)
  extraction = Atoms::FrontmatterExtractor.extract(content)
  if extraction[:has_frontmatter]
    body = extraction[:body].to_s
    # FrontmatterExtractor returns body as the exact suffix after frontmatter delimiters.
    frontmatter_prefix = content[0...(content.length - body.length)]
  else
    body = content
    frontmatter_prefix = ""
  end

  fixed_body = apply_body_fixes(body)
  formatted_content = frontmatter_prefix + fixed_body

  {
    success: true,
    formatted: formatted_content != content,
    formatted_content: formatted_content,
    errors: [],
    warnings: []
  }
rescue => e
  {
    success: false,
    formatted: false,
    formatted_content: content,
    errors: ["Error fixing markdown content: #{e.message}"],
    warnings: []
  }
end

.fix_file(file_path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ace/lint/molecules/markdown_surgical_fixer.rb', line 19

def fix_file(file_path)
  content = File.binread(file_path).dup.force_encoding(Encoding::UTF_8)
  unless content.valid_encoding?
    return {
      success: true,
      formatted: false,
      errors: [],
      warnings: ["Skipped non-UTF8 file: #{file_path}"]
    }
  end

  result = fix_content(content)
  return {success: false, formatted: false, errors: result[:errors], warnings: result[:warnings]} unless result[:success]

  File.write(file_path, result[:formatted_content]) if result[:formatted]

  {
    success: true,
    formatted: result[:formatted],
    errors: [],
    warnings: result[:warnings]
  }
rescue Errno::ENOENT
  {success: false, formatted: false, errors: ["File not found: #{file_path}"], warnings: []}
rescue => e
  {success: false, formatted: false, errors: ["Error fixing file: #{e.message}"], warnings: []}
end