Class: Ace::Docs::Molecules::DocumentLoader
- Inherits:
-
Object
- Object
- Ace::Docs::Molecules::DocumentLoader
- Defined in:
- lib/ace/docs/molecules/document_loader.rb
Overview
Loads document files with frontmatter parsing
Class Method Summary collapse
-
.load_directory(directory, recursive: true) ⇒ Array<Document>
Load all documents from a directory.
-
.load_file(path) ⇒ Document?
Load a single document from file.
-
.load_files(paths) ⇒ Array<Document>
Load multiple documents from paths.
-
.load_glob(pattern, base_dir: Dir.pwd) ⇒ Array<Document>
Load documents matching a glob pattern.
-
.load_or_error(path) ⇒ Document
Load document or return error document.
-
.managed_document?(path) ⇒ Boolean
Check if a file has ace-docs frontmatter.
Class Method Details
.load_directory(directory, recursive: true) ⇒ Array<Document>
Load all documents from a directory
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
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..include?("No frontmatter found") return load_frontmatter_free_document(path, content) end warn "Error loading document #{path}: #{e.}" nil end |
.load_files(paths) ⇒ Array<Document>
Load multiple documents from paths
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
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
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
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 |