Class: Ace::Docs::Molecules::DocumentLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/docs/molecules/document_loader.rb

Overview

Loads document files with frontmatter parsing

Class Method Summary collapse

Class Method Details

.load_directory(directory, recursive: true) ⇒ Array<Document>

Load all documents from a directory

Parameters:

  • directory (String)

    Directory path

  • recursive (Boolean) (defaults to: true)

    Whether to search recursively

Returns:

  • (Array<Document>)

    Array of loaded documents



53
54
55
56
57
58
59
60
# File 'lib/ace/docs/molecules/document_loader.rb', line 53

def self.load_directory(directory, recursive: true)
  return [] unless File.directory?(directory)

  pattern = recursive ? "**/*.md" : "*.md"
  md_files = Dir.glob(File.join(directory, pattern))

  load_files(md_files)
end

.load_file(path) ⇒ Document?

Load a single document from file

Parameters:

  • path (String)

    Path to the markdown file

Returns:

  • (Document, nil)

    Document object or nil if loading fails



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ace/docs/molecules/document_loader.rb', line 17

def self.load_file(path)
  return nil unless File.exist?(path)
  return nil unless path.end_with?(".md")

  content = File.read(path)
  doc = Ace::Support::Markdown::Models::MarkdownDocument.parse(content, file_path: path)

  if doc.frontmatter.empty?
    return load_frontmatter_free_document(path, content)
  end

  Models::Document.new(
    path: path,
    frontmatter: doc.frontmatter,
    content: doc.raw_body
  )
rescue => e
  if e.message.include?("No frontmatter found")
    return load_frontmatter_free_document(path, content)
  end

  warn "Error loading document #{path}: #{e.message}"
  nil
end

.load_files(paths) ⇒ Array<Document>

Load multiple documents from paths

Parameters:

  • paths (Array<String>)

    Array of file paths

Returns:

  • (Array<Document>)

    Array of loaded documents



45
46
47
# File 'lib/ace/docs/molecules/document_loader.rb', line 45

def self.load_files(paths)
  paths.map { |path| load_file(path) }.compact
end

.load_glob(pattern, base_dir: Dir.pwd) ⇒ Array<Document>

Load documents matching a glob pattern

Parameters:

  • pattern (String)

    Glob pattern

  • base_dir (String) (defaults to: Dir.pwd)

    Base directory for the pattern

Returns:

  • (Array<Document>)

    Array of loaded documents



66
67
68
69
# File 'lib/ace/docs/molecules/document_loader.rb', line 66

def self.load_glob(pattern, base_dir: Dir.pwd)
  md_files = Dir.glob(File.join(base_dir, pattern))
  load_files(md_files)
end

.load_or_error(path) ⇒ Document

Load document or return error document

Parameters:

  • path (String)

    File path

Returns:

  • (Document)

    Document object (may be empty with error metadata)



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ace/docs/molecules/document_loader.rb', line 118

def self.load_or_error(path)
  doc = load_file(path)
  return doc if doc

  # Return error document
  Models::Document.new(
    path: path,
    frontmatter: {
      "error" => "Failed to load document"
    }
  )
end

.managed_document?(path) ⇒ Boolean

Check if a file has ace-docs frontmatter

Parameters:

  • path (String)

    File path

Returns:

  • (Boolean)

    true if file has valid ace-docs frontmatter



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ace/docs/molecules/document_loader.rb', line 74

def self.managed_document?(path)
  return false unless File.exist?(path)
  return false unless path.end_with?(".md")

  content = File.read(path)
  doc = Ace::Support::Markdown::Models::MarkdownDocument.parse(content)

  if doc.frontmatter.empty?
    return frontmatter_free?(path)
  end

  # Check if has doc-type field (ace-docs requirement)
  !doc.frontmatter.empty? && doc.frontmatter["doc-type"]
rescue
  frontmatter_free?(path)
end